Skip to content

Commit 857f14c

Browse files
author
Randy Grok
committed
include unit test for optimistic exec
1 parent 266d53f commit 857f14c

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

server/v2/cometbft/abci_test.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"cosmossdk.io/server/v2/cometbft/oe"
66
"crypto/sha256"
77
"encoding/json"
8+
"errors"
89
"io"
910
"strings"
1011
"testing"
@@ -719,7 +720,17 @@ func assertStoreLatestVersion(t *testing.T, store types.Store, target uint64) {
719720

720721
func TestOptimisticExecution(t *testing.T) {
721722
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))
723734

724735
_, err := c.InitChain(context.Background(), &abciproto.InitChainRequest{
725736
Time: time.Now(),
@@ -736,12 +747,10 @@ func TestOptimisticExecution(t *testing.T) {
736747
})
737748
require.NoError(t, err)
738749

739-
// Set up handlers
740-
c.processProposalHandler = DefaultServerOptions[mock.Tx]().ProcessProposalHandler
741-
750+
theHash := sha256.Sum256([]byte("test"))
742751
ppReq := &abciproto.ProcessProposalRequest{
743752
Height: 2,
744-
Hash: []byte("test"),
753+
Hash: theHash[:],
745754
Time: time.Now(),
746755
Txs: [][]byte{mockTx.Bytes()},
747756
}
@@ -752,23 +761,31 @@ func TestOptimisticExecution(t *testing.T) {
752761
require.Equal(t, resp.Status, abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT)
753762

754763
// Initialize FinalizeBlock with correct hash - should use optimistic result
755-
theHash := sha256.Sum256([]byte("test"))
764+
theHash = sha256.Sum256([]byte("test"))
756765
fbReq := &abciproto.FinalizeBlockRequest{
757766
Height: 2,
758767
Hash: theHash[:],
759768
Time: ppReq.Time,
760769
Txs: ppReq.Txs,
761770
}
762771
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)
763777
require.NoError(t, err)
778+
require.Equal(t, resp.Status, abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT)
764779

765-
// Initialize FinalizeBlock with wrong hash - should abort optimistic execution
766780
theWrongHash := sha256.Sum256([]byte("wrong_hash"))
767781
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
769785
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)
772789

773790
// Verify optimistic execution was reset
774791
require.False(t, c.optimisticExec.Initialized())

0 commit comments

Comments
 (0)