Skip to content

Commit 1e1fe25

Browse files
committed
remove change in context for tx and msg index
1 parent 4d6f399 commit 1e1fe25

File tree

8 files changed

+23
-56
lines changed

8 files changed

+23
-56
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
6060
* x/distribution can now utilize an externally managed community pool. NOTE: this will make the message handlers for FundCommunityPool and CommunityPoolSpend error, as well as the query handler for CommunityPool.
6161
* (client) [#18101](https://github.com/cosmos/cosmos-sdk/pull/18101) Add a `keyring-default-keyname` in `client.toml` for specifying a default key name, and skip the need to use the `--from` flag when signing transactions.
6262
* (x/gov) [#24355](https://github.com/cosmos/cosmos-sdk/pull/24355) Allow users to set a custom CalculateVoteResultsAndVotingPower function to be used in govkeeper.Tally.
63-
* (baseapp) [#24458](https://github.com/cosmos/cosmos-sdk/pull/24458) Add `TxExecutor` baseapp option, add `TxIndex`/`TxCount`/`MsgIndex`/`BlockGasUsed` fields to `Context, to support parallel execution, introduce incarnation cache for performance optimisation
63+
* (baseapp) [#24458](https://github.com/cosmos/cosmos-sdk/pull/24458) Add `TxExecutor` to support parallel execution and introduce incarnation cache for performance optimisation
6464

6565
### Improvements
6666

baseapp/abci.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,7 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request
791791

792792
// Reset the gas meter so that the AnteHandlers aren't required to
793793
gasMeter = app.getBlockGasMeter(app.finalizeBlockState.Context())
794-
app.finalizeBlockState.SetContext(
795-
app.finalizeBlockState.Context().
796-
WithBlockGasMeter(gasMeter).
797-
WithTxCount(len(req.Txs)),
798-
)
794+
app.finalizeBlockState.SetContext(app.finalizeBlockState.Context().WithBlockGasMeter(gasMeter))
799795

800796
// Iterate over all raw transactions in the proposal and attempt to execute
801797
// them, gathering the execution results.
@@ -839,17 +835,17 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request
839835
func (app *BaseApp) executeTxsWithExecutor(ctx context.Context, txs [][]byte) ([]*abci.ExecTxResult, error) {
840836
if app.txExecutor != nil {
841837
return app.txExecutor(ctx, txs, app.finalizeBlockState.ms, func(i int, memTx sdk.Tx, ms storetypes.MultiStore, incarnationCache map[string]any) *abci.ExecTxResult {
842-
return app.deliverTxWithMultiStore(txs[i], i, ms, incarnationCache)
838+
return app.deliverTxWithMultiStore(txs[i], ms, incarnationCache)
843839
})
844840
}
845841

846842
// Fallback to the default execution logic
847843
txResults := make([]*abci.ExecTxResult, 0, len(txs))
848-
for i, rawTx := range txs {
844+
for _, rawTx := range txs {
849845
var response *abci.ExecTxResult
850846

851847
if _, err := app.txDecoder(rawTx); err == nil {
852-
response = app.deliverTx(rawTx, i)
848+
response = app.deliverTx(rawTx)
853849
} else {
854850
// In the case where a transaction included in a block proposal is malformed,
855851
// we still want to return a default response to comet. This is because comet

baseapp/baseapp.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ func (app *BaseApp) getBlockGasMeter(ctx sdk.Context) storetypes.GasMeter {
677677
}
678678

679679
// retrieve the context for the tx w/ txBytes and other memoized values.
680-
func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte, txIndex int) sdk.Context {
680+
func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte) sdk.Context {
681681
app.mu.Lock()
682682
defer app.mu.Unlock()
683683

@@ -687,8 +687,7 @@ func (app *BaseApp) getContextForTx(mode execMode, txBytes []byte, txIndex int)
687687
}
688688
ctx := modeState.Context().
689689
WithTxBytes(txBytes).
690-
WithGasMeter(storetypes.NewInfiniteGasMeter()).
691-
WithTxIndex(txIndex)
690+
WithGasMeter(storetypes.NewInfiniteGasMeter())
692691
// WithVoteInfos(app.voteInfos) // TODO: identify if this is needed
693692

694693
ctx = ctx.WithIsSigverifyTx(app.sigverifyTx)
@@ -773,11 +772,11 @@ func (app *BaseApp) beginBlock(_ *abci.RequestFinalizeBlock) (sdk.BeginBlock, er
773772
return resp, nil
774773
}
775774

776-
func (app *BaseApp) deliverTx(tx []byte, txIndex int) *abci.ExecTxResult {
777-
return app.deliverTxWithMultiStore(tx, txIndex, nil, nil)
775+
func (app *BaseApp) deliverTx(tx []byte) *abci.ExecTxResult {
776+
return app.deliverTxWithMultiStore(tx, nil, nil)
778777
}
779778

780-
func (app *BaseApp) deliverTxWithMultiStore(tx []byte, txIndex int, txMultiStore storetypes.MultiStore, incarnationCache map[string]any) *abci.ExecTxResult {
779+
func (app *BaseApp) deliverTxWithMultiStore(tx []byte, txMultiStore storetypes.MultiStore, incarnationCache map[string]any) *abci.ExecTxResult {
781780
gInfo := sdk.GasInfo{}
782781
resultStr := "successful"
783782

@@ -790,7 +789,7 @@ func (app *BaseApp) deliverTxWithMultiStore(tx []byte, txIndex int, txMultiStore
790789
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
791790
}()
792791

793-
gInfo, result, anteEvents, err := app.runTxWithMultiStore(execModeFinalize, tx, nil, txIndex, txMultiStore, incarnationCache)
792+
gInfo, result, anteEvents, err := app.runTxWithMultiStore(execModeFinalize, tx, nil, txMultiStore, incarnationCache)
794793
if err != nil {
795794
resultStr = "failed"
796795
resp = sdkerrors.ResponseExecTxResultWithEvents(
@@ -850,17 +849,19 @@ func (app *BaseApp) endBlock(_ context.Context) (sdk.EndBlock, error) {
850849
// both txbytes and the decoded tx are passed to runTx to avoid the state machine encoding the tx and decoding the transaction twice
851850
// passing the decoded tx to runTX is optional, it will be decoded if the tx is nil
852851
func (app *BaseApp) runTx(mode execMode, txBytes []byte, tx sdk.Tx) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, err error) {
853-
return app.runTxWithMultiStore(mode, txBytes, tx, -1, nil, nil)
852+
return app.runTxWithMultiStore(mode, txBytes, tx, nil, nil)
854853
}
855854

856-
func (app *BaseApp) runTxWithMultiStore(mode execMode, txBytes []byte, tx sdk.Tx, txIndex int, txMultiStore storetypes.MultiStore, incarnationCache map[string]any) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, err error) {
855+
func (app *BaseApp) runTxWithMultiStore(mode execMode, txBytes []byte, tx sdk.Tx, txMultiStore storetypes.MultiStore, incarnationCache map[string]any) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, err error) {
857856
// NOTE: GasWanted should be returned by the AnteHandler. GasUsed is
858857
// determined by the GasMeter. We need access to the context to get the gas
859858
// meter, so we initialize upfront.
860859
var gasWanted uint64
861860

862-
ctx := app.getContextForTx(mode, txBytes, txIndex)
863-
ctx = ctx.WithIncarnationCache(incarnationCache)
861+
ctx := app.getContextForTx(mode, txBytes)
862+
if incarnationCache != nil {
863+
ctx = ctx.WithIncarnationCache(incarnationCache)
864+
}
864865
if txMultiStore != nil {
865866
ctx = ctx.WithMultiStore(txMultiStore)
866867
}
@@ -1056,8 +1057,6 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, msgsV2 []protov2.Me
10561057
break
10571058
}
10581059

1059-
ctx = ctx.WithMsgIndex(i)
1060-
10611060
handler := app.msgServiceRouter.Handler(msg)
10621061
if handler == nil {
10631062
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "no message handler found for %T", msg)

baseapp/genesis.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var _ genesis.TxHandler = (*BaseApp)(nil)
1313
// ExecuteGenesisTx implements genesis.GenesisState from
1414
// cosmossdk.io/core/genesis to set initial state in genesis
1515
func (ba *BaseApp) ExecuteGenesisTx(tx []byte) error {
16-
res := ba.deliverTx(tx, -1)
16+
res := ba.deliverTx(tx)
1717

1818
if res.Code != types.CodeTypeOK {
1919
return errors.New(res.Log)

baseapp/test_helpers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ func (app *BaseApp) NewUncachedContext(isCheckTx bool, header cmtproto.Header) s
7777
}
7878

7979
func (app *BaseApp) GetContextForFinalizeBlock(txBytes []byte) sdk.Context {
80-
return app.getContextForTx(execModeFinalize, txBytes, -1)
80+
return app.getContextForTx(execModeFinalize, txBytes)
8181
}
8282

8383
func (app *BaseApp) GetContextForCheckTx(txBytes []byte) sdk.Context {
84-
return app.getContextForTx(execModeCheck, txBytes, -1)
84+
return app.getContextForTx(execModeCheck, txBytes)
8585
}

baseapp/txexecutor.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
sdk "github.com/cosmos/cosmos-sdk/types"
1111
)
1212

13+
// TxExecutor function type for implementing custom execution logic, such as block-stm
1314
type TxExecutor func(
1415
ctx context.Context,
1516
block [][]byte,

types/context.go

-30
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ type Context struct {
6666
headerInfo header.Info
6767

6868
// For block-stm
69-
txIndex int // the index of the current tx in the block, -1 means not in finalize block context
70-
msgIndex int // the index of the current msg in the tx, -1 means not in finalize block context
71-
txCount int // the total number of transactions in current block
72-
blockGasUsed uint64 // sum the gas used by all the transactions in the current block, only accessible by end blocker
7369
incarnationCache map[string]any // incarnationCache is shared between multiple incarnations of the same transaction, it must only cache stateless computation results that only depends on tx body and block level information that don't change during block execution, like the result of tx signature verification.
7470
}
7571

@@ -99,10 +95,6 @@ func (c Context) TransientKVGasConfig() storetypes.GasConfig { return c.trans
9995
func (c Context) StreamingManager() storetypes.StreamingManager { return c.streamingManager }
10096
func (c Context) CometInfo() comet.BlockInfo { return c.cometInfo }
10197
func (c Context) HeaderInfo() header.Info { return c.headerInfo }
102-
func (c Context) TxIndex() int { return c.txIndex }
103-
func (c Context) MsgIndex() int { return c.msgIndex }
104-
func (c Context) TxCount() int { return c.txCount }
105-
func (c Context) BlockGasUsed() uint64 { return c.blockGasUsed }
10698
func (c Context) IncarnationCache() map[string]any { return c.incarnationCache }
10799

108100
// BlockHeader returns the header by value.
@@ -150,8 +142,6 @@ func NewContext(ms storetypes.MultiStore, header cmtproto.Header, isCheckTx bool
150142
eventManager: NewEventManager(),
151143
kvGasConfig: storetypes.KVGasConfig(),
152144
transientKVGasConfig: storetypes.TransientGasConfig(),
153-
txIndex: -1,
154-
msgIndex: -1,
155145
}
156146
}
157147

@@ -331,26 +321,6 @@ func (c Context) WithHeaderInfo(headerInfo header.Info) Context {
331321
return c
332322
}
333323

334-
func (c Context) WithTxIndex(txIndex int) Context {
335-
c.txIndex = txIndex
336-
return c
337-
}
338-
339-
func (c Context) WithTxCount(txCount int) Context {
340-
c.txCount = txCount
341-
return c
342-
}
343-
344-
func (c Context) WithMsgIndex(msgIndex int) Context {
345-
c.msgIndex = msgIndex
346-
return c
347-
}
348-
349-
func (c Context) WithBlockGasUsed(gasUsed uint64) Context {
350-
c.blockGasUsed = gasUsed
351-
return c
352-
}
353-
354324
// TODO: remove???
355325
func (c Context) IsZero() bool {
356326
return c.ms == nil

x/auth/ante/sigverify.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var (
3232
simSecp256k1Pubkey = &secp256k1.PubKey{Key: key}
3333
simSecp256k1Sig [64]byte
3434

35-
// SigVerificationResultCacheKey key for incarnation cache
35+
// SigVerificationResultCacheKey default key for incarnation cache
3636
SigVerificationResultCacheKey = "ante:SigVerificationResult"
3737
)
3838

@@ -261,6 +261,7 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul
261261
}
262262
} else {
263263
err = svd.verify(ctx, tx, simulate)
264+
// if enabled, use incarnation cache to store the result to improve performance for block-stm
264265
ctx.SetIncarnationCache(SigVerificationResultCacheKey, err)
265266
}
266267
if err != nil {

0 commit comments

Comments
 (0)