Skip to content

Commit 266d53f

Browse files
author
Randy Grok
committed
create unit test
1 parent 082ddec commit 266d53f

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

server/v2/cometbft/abci_test.go

+66-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cometbft
22

33
import (
44
"context"
5+
"cosmossdk.io/server/v2/cometbft/oe"
56
"crypto/sha256"
67
"encoding/json"
78
"io"
@@ -55,10 +56,10 @@ func getQueryRouterBuilder[T any, PT interface {
5556
*T
5657
proto.Message
5758
},
58-
U any, UT interface {
59-
*U
60-
proto.Message
61-
}](
59+
U any, UT interface {
60+
*U
61+
proto.Message
62+
}](
6263
t *testing.T,
6364
handler func(ctx context.Context, msg PT) (UT, error),
6465
) *stf.MsgRouterBuilder {
@@ -85,10 +86,10 @@ func getMsgRouterBuilder[T any, PT interface {
8586
*T
8687
transaction.Msg
8788
},
88-
U any, UT interface {
89-
*U
90-
transaction.Msg
91-
}](
89+
U any, UT interface {
90+
*U
91+
transaction.Msg
92+
}](
9293
t *testing.T,
9394
handler func(ctx context.Context, msg PT) (UT, error),
9495
) *stf.MsgRouterBuilder {
@@ -715,3 +716,60 @@ func assertStoreLatestVersion(t *testing.T, store types.Store, target uint64) {
715716
require.NoError(t, err)
716717
require.Equal(t, target, commitInfo.Version)
717718
}
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

Comments
 (0)