-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server/v2/cometbft): optimistic execution #22560
Changes from 10 commits
0b80553
4807332
907bcd1
a58e20d
082ddec
266d53f
857f14c
f78dbe0
5718170
6791357
05e8ee7
bc9250b
f24f35e
e91f209
c836cab
d6cf77e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||||||||||||||||
"context" | ||||||||||||||||||||||||||||||||||||||||||||||
"cosmossdk.io/server/v2/cometbft/oe" | ||||||||||||||||||||||||||||||||||||||||||||||
"crypto/sha256" | ||||||||||||||||||||||||||||||||||||||||||||||
"errors" | ||||||||||||||||||||||||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -77,6 +78,11 @@ | |||||||||||||||||||||||||||||||||||||||||||||
extendVote handlers.ExtendVoteHandler | ||||||||||||||||||||||||||||||||||||||||||||||
checkTxHandler handlers.CheckTxHandler[T] | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// optimisticExec contains the context required for Optimistic Execution, | ||||||||||||||||||||||||||||||||||||||||||||||
// including the goroutine handling.This is experimental and must be enabled | ||||||||||||||||||||||||||||||||||||||||||||||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
// by developers. | ||||||||||||||||||||||||||||||||||||||||||||||
optimisticExec *oe.OptimisticExecution[T] | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
addrPeerFilter types.PeerFilter // filter peers by address and port | ||||||||||||||||||||||||||||||||||||||||||||||
idPeerFilter types.PeerFilter // filter peers by node ID | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -385,6 +391,14 @@ | |||||||||||||||||||||||||||||||||||||||||||||
return nil, errors.New("no prepare proposal function was set") | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Abort any running OE so it cannot overlap with `PrepareProposal`. This could happen if optimistic | ||||||||||||||||||||||||||||||||||||||||||||||
// `internalFinalizeBlock` from previous round takes a long time, but consensus has moved on to next round. | ||||||||||||||||||||||||||||||||||||||||||||||
// Overlap is undesirable, since `internalFinalizeBlock` and `PrepareProoposal` could share access to | ||||||||||||||||||||||||||||||||||||||||||||||
// in-memory structs depending on application implementation. | ||||||||||||||||||||||||||||||||||||||||||||||
// No-op if OE is not enabled. | ||||||||||||||||||||||||||||||||||||||||||||||
// Similar call to Abort() is done in `ProcessProposal`. | ||||||||||||||||||||||||||||||||||||||||||||||
c.optimisticExec.Abort() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+394
to
+401
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling for Abort operation The -c.optimisticExec.Abort()
+if err := c.optimisticExec.Abort(); err != nil {
+ c.logger.Error("failed to abort optimistic execution", "err", err)
+ // Continue execution as the abort error shouldn't block proposal preparation
+}
|
||||||||||||||||||||||||||||||||||||||||||||||
ciCtx := contextWithCometInfo(ctx, comet.Info{ | ||||||||||||||||||||||||||||||||||||||||||||||
Evidence: toCoreEvidence(req.Misbehavior), | ||||||||||||||||||||||||||||||||||||||||||||||
ValidatorsHash: req.NextValidatorsHash, | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -421,6 +435,17 @@ | |||||||||||||||||||||||||||||||||||||||||||||
return nil, errors.New("no process proposal function was set") | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Since the application can get access to FinalizeBlock state and write to it, | ||||||||||||||||||||||||||||||||||||||||||||||
// we must be sure to reset it in case ProcessProposal timeouts and is called | ||||||||||||||||||||||||||||||||||||||||||||||
// again in a subsequent round. However, we only want to do this after we've | ||||||||||||||||||||||||||||||||||||||||||||||
// processed the first block, as we want to avoid overwriting the finalizeState | ||||||||||||||||||||||||||||||||||||||||||||||
// after state changes during InitChain. | ||||||||||||||||||||||||||||||||||||||||||||||
if req.Height > int64(c.initialHeight) { | ||||||||||||||||||||||||||||||||||||||||||||||
// abort any running OE | ||||||||||||||||||||||||||||||||||||||||||||||
c.optimisticExec.Abort() | ||||||||||||||||||||||||||||||||||||||||||||||
//c.setState(execModeFinalize, header) | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove comment |
||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential nil pointer dereference in In Apply this diff to add a nil check before calling if req.Height > int64(c.initialHeight) {
// abort any running OE
- c.optimisticExec.Abort()
+ if c.optimisticExec != nil {
+ c.optimisticExec.Abort()
+ }
//c.setState(execModeFinalize, header)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
ciCtx := contextWithCometInfo(ctx, comet.Info{ | ||||||||||||||||||||||||||||||||||||||||||||||
Evidence: toCoreEvidence(req.Misbehavior), | ||||||||||||||||||||||||||||||||||||||||||||||
ValidatorsHash: req.NextValidatorsHash, | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -436,6 +461,17 @@ | |||||||||||||||||||||||||||||||||||||||||||||
}, nil | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Only execute optimistic execution if the proposal is accepted, OE is | ||||||||||||||||||||||||||||||||||||||||||||||
// enabled and the block height is greater than the initial height. During | ||||||||||||||||||||||||||||||||||||||||||||||
// the first block we'll be carrying state from InitChain, so it would be | ||||||||||||||||||||||||||||||||||||||||||||||
// impossible for us to easily revert. | ||||||||||||||||||||||||||||||||||||||||||||||
// After the first block has been processed, the next blocks will get executed | ||||||||||||||||||||||||||||||||||||||||||||||
// optimistically, so that when the ABCI client calls `FinalizeBlock` the app | ||||||||||||||||||||||||||||||||||||||||||||||
// can have a response ready. | ||||||||||||||||||||||||||||||||||||||||||||||
if c.optimisticExec.Enabled() && req.Height > int64(c.initialHeight) { | ||||||||||||||||||||||||||||||||||||||||||||||
c.optimisticExec.Execute(req) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential nil pointer dereference when checking In Apply this diff to add a nil check: if c.optimisticExec != nil && c.optimisticExec.Enabled() && req.Height > int64(c.initialHeight) {
c.optimisticExec.Execute(req)
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
return &abciproto.ProcessProposalResponse{ | ||||||||||||||||||||||||||||||||||||||||||||||
Status: abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT, | ||||||||||||||||||||||||||||||||||||||||||||||
}, nil | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -447,46 +483,33 @@ | |||||||||||||||||||||||||||||||||||||||||||||
ctx context.Context, | ||||||||||||||||||||||||||||||||||||||||||||||
req *abciproto.FinalizeBlockRequest, | ||||||||||||||||||||||||||||||||||||||||||||||
) (*abciproto.FinalizeBlockResponse, error) { | ||||||||||||||||||||||||||||||||||||||||||||||
if err := c.validateFinalizeBlockHeight(req); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if err := c.checkHalt(req.Height, req.Time); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// TODO(tip): can we expect some txs to not decode? if so, what we do in this case? this does not seem to be the case, | ||||||||||||||||||||||||||||||||||||||||||||||
// considering that prepare and process always decode txs, assuming they're the ones providing txs we should never | ||||||||||||||||||||||||||||||||||||||||||||||
// have a tx that fails decoding. | ||||||||||||||||||||||||||||||||||||||||||||||
decodedTxs, err := decodeTxs(req.Txs, c.txCodec) | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
cid, err := c.store.LastCommitID() | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
blockReq := &server.BlockRequest[T]{ | ||||||||||||||||||||||||||||||||||||||||||||||
Height: uint64(req.Height), | ||||||||||||||||||||||||||||||||||||||||||||||
Time: req.Time, | ||||||||||||||||||||||||||||||||||||||||||||||
Hash: req.Hash, | ||||||||||||||||||||||||||||||||||||||||||||||
AppHash: cid.Hash, | ||||||||||||||||||||||||||||||||||||||||||||||
ChainId: c.chainID, | ||||||||||||||||||||||||||||||||||||||||||||||
Txs: decodedTxs, | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
ciCtx := contextWithCometInfo(ctx, comet.Info{ | ||||||||||||||||||||||||||||||||||||||||||||||
Evidence: toCoreEvidence(req.Misbehavior), | ||||||||||||||||||||||||||||||||||||||||||||||
ValidatorsHash: req.NextValidatorsHash, | ||||||||||||||||||||||||||||||||||||||||||||||
ProposerAddress: req.ProposerAddress, | ||||||||||||||||||||||||||||||||||||||||||||||
LastCommit: toCoreCommitInfo(req.DecidedLastCommit), | ||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||
var ( | ||||||||||||||||||||||||||||||||||||||||||||||
resp *server.BlockResponse | ||||||||||||||||||||||||||||||||||||||||||||||
newState store.WriterMap | ||||||||||||||||||||||||||||||||||||||||||||||
decodedTxs []T | ||||||||||||||||||||||||||||||||||||||||||||||
err error | ||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if c.optimisticExec.Initialized() { | ||||||||||||||||||||||||||||||||||||||||||||||
// check if the hash we got is the same as the one we are executing | ||||||||||||||||||||||||||||||||||||||||||||||
aborted := c.optimisticExec.AbortIfNeeded(req.Hash) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Wait for the OE to finish, regardless of whether it was aborted or not | ||||||||||||||||||||||||||||||||||||||||||||||
res, err := c.optimisticExec.WaitResult() | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if aborted { | ||||||||||||||||||||||||||||||||||||||||||||||
resp, newState, decodedTxs, err = c.internalFinalizeBlock(ctx, req) | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||
resp = res.Resp | ||||||||||||||||||||||||||||||||||||||||||||||
newState = res.StateChanges | ||||||||||||||||||||||||||||||||||||||||||||||
decodedTxs = res.DecodedTxs | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
resp, newState, err := c.app.DeliverBlock(ciCtx, blockReq) | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
// if it was aborted, we need to reset the state | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove comment, as we are always resetting |
||||||||||||||||||||||||||||||||||||||||||||||
c.optimisticExec.Reset() | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding timeout for WaitResult The -res, err := c.optimisticExec.WaitResult()
+ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
+defer cancel()
+res, err := c.optimisticExec.WaitResult(ctx)
+if err == context.DeadlineExceeded {
+ c.logger.Error("optimistic execution timed out")
+ // Fall back to normal execution
+ resp, newState, decodedTxs, err = c.internalFinalizeBlock(ctx, req)
+ if err != nil {
+ return nil, err
+ }
+}
|
||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// after we get the changeset we can produce the commit hash, | ||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -531,6 +554,52 @@ | |||||||||||||||||||||||||||||||||||||||||||||
return finalizeBlockResponse(resp, cp, appHash, c.indexedEvents, c.cfg.AppTomlConfig.Trace) | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
func (c *consensus[T]) internalFinalizeBlock( | ||||||||||||||||||||||||||||||||||||||||||||||
ctx context.Context, | ||||||||||||||||||||||||||||||||||||||||||||||
req *abciproto.FinalizeBlockRequest, | ||||||||||||||||||||||||||||||||||||||||||||||
) (*server.BlockResponse, store.WriterMap, []T, error) { | ||||||||||||||||||||||||||||||||||||||||||||||
if err := c.validateFinalizeBlockHeight(req); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, nil, nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if err := c.checkHalt(req.Height, req.Time); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, nil, nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// TODO(tip): can we expect some txs to not decode? if so, what we do in this case? this does not seem to be the case, | ||||||||||||||||||||||||||||||||||||||||||||||
// considering that prepare and process always decode txs, assuming they're the ones providing txs we should never | ||||||||||||||||||||||||||||||||||||||||||||||
// have a tx that fails decoding. | ||||||||||||||||||||||||||||||||||||||||||||||
decodedTxs, err := decodeTxs(req.Txs, c.txCodec) | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, nil, nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
cid, err := c.store.LastCommitID() | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, nil, nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
blockReq := &server.BlockRequest[T]{ | ||||||||||||||||||||||||||||||||||||||||||||||
Height: uint64(req.Height), | ||||||||||||||||||||||||||||||||||||||||||||||
Time: req.Time, | ||||||||||||||||||||||||||||||||||||||||||||||
Hash: req.Hash, | ||||||||||||||||||||||||||||||||||||||||||||||
AppHash: cid.Hash, | ||||||||||||||||||||||||||||||||||||||||||||||
ChainId: c.chainID, | ||||||||||||||||||||||||||||||||||||||||||||||
Txs: decodedTxs, | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
ciCtx := contextWithCometInfo(ctx, comet.Info{ | ||||||||||||||||||||||||||||||||||||||||||||||
Evidence: toCoreEvidence(req.Misbehavior), | ||||||||||||||||||||||||||||||||||||||||||||||
ValidatorsHash: req.NextValidatorsHash, | ||||||||||||||||||||||||||||||||||||||||||||||
ProposerAddress: req.ProposerAddress, | ||||||||||||||||||||||||||||||||||||||||||||||
LastCommit: toCoreCommitInfo(req.DecidedLastCommit), | ||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
resp, stateChanges, err := c.app.DeliverBlock(ciCtx, blockReq) | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
return resp, stateChanges, decodedTxs, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// Commit implements types.Application. | ||||||||||||||||||||||||||||||||||||||||||||||
// It is called by cometbft to notify the application that a block was committed. | ||||||||||||||||||||||||||||||||||||||||||||||
func (c *consensus[T]) Commit(ctx context.Context, _ *abciproto.CommitRequest) (*abciproto.CommitResponse, error) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,12 +4,14 @@ import ( | |||||||||||||||||||||||||||
"context" | ||||||||||||||||||||||||||||
"crypto/sha256" | ||||||||||||||||||||||||||||
"encoding/json" | ||||||||||||||||||||||||||||
"errors" | ||||||||||||||||||||||||||||
"io" | ||||||||||||||||||||||||||||
"strings" | ||||||||||||||||||||||||||||
"sync" | ||||||||||||||||||||||||||||
"testing" | ||||||||||||||||||||||||||||
"time" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
"cosmossdk.io/server/v2/cometbft/oe" | ||||||||||||||||||||||||||||
abciproto "github.com/cometbft/cometbft/api/cometbft/abci/v1" | ||||||||||||||||||||||||||||
v1 "github.com/cometbft/cometbft/api/cometbft/types/v1" | ||||||||||||||||||||||||||||
"github.com/cosmos/gogoproto/proto" | ||||||||||||||||||||||||||||
|
@@ -724,3 +726,76 @@ func assertStoreLatestVersion(t *testing.T, store types.Store, target uint64) { | |||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||
require.Equal(t, target, commitInfo.Version) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
func TestOptimisticExecution(t *testing.T) { | ||||||||||||||||||||||||||||
c := setUpConsensus(t, 100_000, mempool.NoOpMempool[mock.Tx]{}) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// Set up handlers | ||||||||||||||||||||||||||||
c.processProposalHandler = DefaultServerOptions[mock.Tx]().ProcessProposalHandler | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// mock optimistic execution | ||||||||||||||||||||||||||||
calledTimes := 0 | ||||||||||||||||||||||||||||
optimisticMockFunc := func(_ context.Context, _ *abciproto.FinalizeBlockRequest) (*abciproto.FinalizeBlockResponse, error) { | ||||||||||||||||||||||||||||
calledTimes++ | ||||||||||||||||||||||||||||
return nil, errors.New("test error") | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
c.optimisticExec = oe.NewOptimisticExecution(log.NewNopLogger(), optimisticMockFunc) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
_, err := c.InitChain(context.Background(), &abciproto.InitChainRequest{ | ||||||||||||||||||||||||||||
Time: time.Now(), | ||||||||||||||||||||||||||||
ChainId: "test", | ||||||||||||||||||||||||||||
InitialHeight: 1, | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
_, err = c.FinalizeBlock(context.Background(), &abciproto.FinalizeBlockRequest{ | ||||||||||||||||||||||||||||
Time: time.Now(), | ||||||||||||||||||||||||||||
Height: 1, | ||||||||||||||||||||||||||||
Txs: [][]byte{mockTx.Bytes()}, | ||||||||||||||||||||||||||||
Hash: emptyHash[:], | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
theHash := sha256.Sum256([]byte("test")) | ||||||||||||||||||||||||||||
ppReq := &abciproto.ProcessProposalRequest{ | ||||||||||||||||||||||||||||
Height: 2, | ||||||||||||||||||||||||||||
Hash: theHash[:], | ||||||||||||||||||||||||||||
Time: time.Now(), | ||||||||||||||||||||||||||||
Txs: [][]byte{mockTx.Bytes()}, | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// Start optimistic execution | ||||||||||||||||||||||||||||
resp, err := c.ProcessProposal(context.Background(), ppReq) | ||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||
require.Equal(t, resp.Status, abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// Initialize FinalizeBlock with correct hash - should use optimistic result | ||||||||||||||||||||||||||||
theHash = sha256.Sum256([]byte("test")) | ||||||||||||||||||||||||||||
fbReq := &abciproto.FinalizeBlockRequest{ | ||||||||||||||||||||||||||||
Height: 2, | ||||||||||||||||||||||||||||
Hash: theHash[:], | ||||||||||||||||||||||||||||
Time: ppReq.Time, | ||||||||||||||||||||||||||||
Txs: ppReq.Txs, | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
fbResp, err := c.FinalizeBlock(context.Background(), fbReq) | ||||||||||||||||||||||||||||
require.Error(t, err) | ||||||||||||||||||||||||||||
require.ErrorContains(t, err, "test error") // from optimisticMockFunc | ||||||||||||||||||||||||||||
require.Equal(t, 1, calledTimes) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Comment on lines
+788
to
+792
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix ineffectual assignment The - fbResp, err := c.FinalizeBlock(context.Background(), fbReq)
+ _, err = c.FinalizeBlock(context.Background(), fbReq) 📝 Committable suggestion
Suggested change
🧰 Tools🪛 golangci-lint (1.62.2)780-780: ineffectual assignment to fbResp (ineffassign) |
||||||||||||||||||||||||||||
resp, err = c.ProcessProposal(context.Background(), ppReq) | ||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||
require.Equal(t, resp.Status, abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
theWrongHash := sha256.Sum256([]byte("wrong_hash")) | ||||||||||||||||||||||||||||
fbReq.Hash = theWrongHash[:] | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// Initialize FinalizeBlock with wrong hash - should abort optimistic execution | ||||||||||||||||||||||||||||
// Because is aborted, the result comes from the normal execution | ||||||||||||||||||||||||||||
fbResp, err = c.FinalizeBlock(context.Background(), fbReq) | ||||||||||||||||||||||||||||
require.NotNil(t, fbResp) | ||||||||||||||||||||||||||||
require.NoError(t, err) | ||||||||||||||||||||||||||||
require.Equal(t, 2, calledTimes) | ||||||||||||||||||||||||||||
Comment on lines
+800
to
+805
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add assertions for optimistic execution state The test should verify the optimistic execution state immediately after the wrong hash is processed, before the final assertion. // Initialize FinalizeBlock with wrong hash - should abort optimistic execution
// Because is aborted, the result comes from the normal execution
fbResp, err = c.FinalizeBlock(context.Background(), fbReq)
require.NotNil(t, fbResp)
require.NoError(t, err)
require.Equal(t, 2, calledTimes)
+require.False(t, c.optimisticExec.Initialized(), "optimistic execution should be reset immediately after wrong hash") 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// Verify optimistic execution was reset | ||||||||||||||||||||||||||||
require.False(t, c.optimisticExec.Initialized()) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add test cases for concurrent optimistic executions The test suite should include scenarios testing concurrent optimistic executions to ensure thread safety. Consider adding a test case that simulates multiple goroutines attempting optimistic execution simultaneously: func TestConcurrentOptimisticExecution(t *testing.T) {
c := setUpConsensus(t, 100_000, mempool.NoOpMempool[mock.Tx]{})
c.processProposalHandler = DefaultServerOptions[mock.Tx]().ProcessProposalHandler
var wg sync.WaitGroup
numGoroutines := 10
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(height int64) {
defer wg.Done()
theHash := sha256.Sum256([]byte(fmt.Sprintf("test-%d", height)))
ppReq := &abciproto.ProcessProposalRequest{
Height: height,
Hash: theHash[:],
Time: time.Now(),
Txs: [][]byte{mockTx.Bytes()},
}
resp, err := c.ProcessProposal(context.Background(), ppReq)
require.NoError(t, err)
require.Equal(t, resp.Status, abciproto.PROCESS_PROPOSAL_STATUS_ACCEPT)
}(int64(i + 2))
}
wg.Wait()
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will need to move lower, otherwise linting will complain