@@ -2,6 +2,7 @@ package cometbft
2
2
3
3
import (
4
4
"context"
5
+ "cosmossdk.io/server/v2/cometbft/oe"
5
6
"crypto/sha256"
6
7
"encoding/json"
7
8
"io"
@@ -55,10 +56,10 @@ func getQueryRouterBuilder[T any, PT interface {
55
56
* T
56
57
proto.Message
57
58
},
58
- U any , UT interface {
59
- * U
60
- proto.Message
61
- }](
59
+ U any , UT interface {
60
+ * U
61
+ proto.Message
62
+ }](
62
63
t * testing.T ,
63
64
handler func (ctx context.Context , msg PT ) (UT , error ),
64
65
) * stf.MsgRouterBuilder {
@@ -85,10 +86,10 @@ func getMsgRouterBuilder[T any, PT interface {
85
86
* T
86
87
transaction.Msg
87
88
},
88
- U any , UT interface {
89
- * U
90
- transaction.Msg
91
- }](
89
+ U any , UT interface {
90
+ * U
91
+ transaction.Msg
92
+ }](
92
93
t * testing.T ,
93
94
handler func (ctx context.Context , msg PT ) (UT , error ),
94
95
) * stf.MsgRouterBuilder {
@@ -715,3 +716,60 @@ func assertStoreLatestVersion(t *testing.T, store types.Store, target uint64) {
715
716
require .NoError (t , err )
716
717
require .Equal (t , target , commitInfo .Version )
717
718
}
719
+
720
+ func TestOptimisticExecution (t * testing.T ) {
721
+ c := setUpConsensus (t , 100_000 , mempool.NoOpMempool [mock.Tx ]{})
722
+ c .SetOptimisticExecution (oe .NewOptimisticExecution (log .NewNopLogger (), c .internalFinalizeBlock ))
723
+
724
+ _ , err := c .InitChain (context .Background (), & abciproto.InitChainRequest {
725
+ Time : time .Now (),
726
+ ChainId : "test" ,
727
+ InitialHeight : 1 ,
728
+ })
729
+ require .NoError (t , err )
730
+
731
+ _ , err = c .FinalizeBlock (context .Background (), & abciproto.FinalizeBlockRequest {
732
+ Time : time .Now (),
733
+ Height : 1 ,
734
+ Txs : [][]byte {mockTx .Bytes ()},
735
+ Hash : emptyHash [:],
736
+ })
737
+ require .NoError (t , err )
738
+
739
+ // Set up handlers
740
+ c .processProposalHandler = DefaultServerOptions [mock.Tx ]().ProcessProposalHandler
741
+
742
+ ppReq := & abciproto.ProcessProposalRequest {
743
+ Height : 2 ,
744
+ Hash : []byte ("test" ),
745
+ Time : time .Now (),
746
+ Txs : [][]byte {mockTx .Bytes ()},
747
+ }
748
+
749
+ // Start optimistic execution
750
+ resp , err := c .ProcessProposal (context .Background (), ppReq )
751
+ require .NoError (t , err )
752
+ require .Equal (t , resp .Status , abciproto .PROCESS_PROPOSAL_STATUS_ACCEPT )
753
+
754
+ // Initialize FinalizeBlock with correct hash - should use optimistic result
755
+ theHash := sha256 .Sum256 ([]byte ("test" ))
756
+ fbReq := & abciproto.FinalizeBlockRequest {
757
+ Height : 2 ,
758
+ Hash : theHash [:],
759
+ Time : ppReq .Time ,
760
+ Txs : ppReq .Txs ,
761
+ }
762
+ fbResp , err := c .FinalizeBlock (context .Background (), fbReq )
763
+ require .NoError (t , err )
764
+
765
+ // Initialize FinalizeBlock with wrong hash - should abort optimistic execution
766
+ theWrongHash := sha256 .Sum256 ([]byte ("wrong_hash" ))
767
+ fbReq .Hash = theWrongHash [:]
768
+ fbReq .Height = 3
769
+ fbResp , err = c .FinalizeBlock (context .Background (), fbReq )
770
+ require .Nil (t , fbResp )
771
+ require .Error (t , err )
772
+
773
+ // Verify optimistic execution was reset
774
+ require .False (t , c .optimisticExec .Initialized ())
775
+ }
0 commit comments