5
5
"cosmossdk.io/server/v2/cometbft/oe"
6
6
"crypto/sha256"
7
7
"encoding/json"
8
+ "errors"
8
9
"io"
9
10
"strings"
10
11
"testing"
@@ -719,7 +720,17 @@ func assertStoreLatestVersion(t *testing.T, store types.Store, target uint64) {
719
720
720
721
func TestOptimisticExecution (t * testing.T ) {
721
722
c := setUpConsensus (t , 100_000 , mempool.NoOpMempool [mock.Tx ]{})
722
- c .SetOptimisticExecution (oe .NewOptimisticExecution (log .NewNopLogger (), c .internalFinalizeBlock ))
723
+ // Set up handlers
724
+ c .processProposalHandler = DefaultServerOptions [mock.Tx ]().ProcessProposalHandler
725
+
726
+ // mock optimistic execution
727
+ calledTimes := 0
728
+ optimisticMockFunc := func (_ context.Context , _ * abciproto.FinalizeBlockRequest ) (* abciproto.FinalizeBlockResponse , error ) {
729
+ calledTimes ++
730
+ return nil , errors .New ("test error" )
731
+ }
732
+
733
+ c .SetOptimisticExecution (oe .NewOptimisticExecution (log .NewNopLogger (), optimisticMockFunc ))
723
734
724
735
_ , err := c .InitChain (context .Background (), & abciproto.InitChainRequest {
725
736
Time : time .Now (),
@@ -736,12 +747,10 @@ func TestOptimisticExecution(t *testing.T) {
736
747
})
737
748
require .NoError (t , err )
738
749
739
- // Set up handlers
740
- c .processProposalHandler = DefaultServerOptions [mock.Tx ]().ProcessProposalHandler
741
-
750
+ theHash := sha256 .Sum256 ([]byte ("test" ))
742
751
ppReq := & abciproto.ProcessProposalRequest {
743
752
Height : 2 ,
744
- Hash : [] byte ( "test" ) ,
753
+ Hash : theHash [:] ,
745
754
Time : time .Now (),
746
755
Txs : [][]byte {mockTx .Bytes ()},
747
756
}
@@ -752,23 +761,31 @@ func TestOptimisticExecution(t *testing.T) {
752
761
require .Equal (t , resp .Status , abciproto .PROCESS_PROPOSAL_STATUS_ACCEPT )
753
762
754
763
// Initialize FinalizeBlock with correct hash - should use optimistic result
755
- theHash : = sha256 .Sum256 ([]byte ("test" ))
764
+ theHash = sha256 .Sum256 ([]byte ("test" ))
756
765
fbReq := & abciproto.FinalizeBlockRequest {
757
766
Height : 2 ,
758
767
Hash : theHash [:],
759
768
Time : ppReq .Time ,
760
769
Txs : ppReq .Txs ,
761
770
}
762
771
fbResp , err := c .FinalizeBlock (context .Background (), fbReq )
772
+ require .Error (t , err )
773
+ require .ErrorContains (t , err , "test error" ) // from optimisticMockFunc
774
+ require .Equal (t , 1 , calledTimes )
775
+
776
+ resp , err = c .ProcessProposal (context .Background (), ppReq )
763
777
require .NoError (t , err )
778
+ require .Equal (t , resp .Status , abciproto .PROCESS_PROPOSAL_STATUS_ACCEPT )
764
779
765
- // Initialize FinalizeBlock with wrong hash - should abort optimistic execution
766
780
theWrongHash := sha256 .Sum256 ([]byte ("wrong_hash" ))
767
781
fbReq .Hash = theWrongHash [:]
768
- fbReq .Height = 3
782
+
783
+ // Initialize FinalizeBlock with wrong hash - should abort optimistic execution
784
+ // Because is aborted, the result comes from the normal execution
769
785
fbResp , err = c .FinalizeBlock (context .Background (), fbReq )
770
- require .Nil (t , fbResp )
771
- require .Error (t , err )
786
+ require .NotNil (t , fbResp )
787
+ require .NoError (t , err )
788
+ require .Equal (t , 2 , calledTimes )
772
789
773
790
// Verify optimistic execution was reset
774
791
require .False (t , c .optimisticExec .Initialized ())
0 commit comments