Skip to content

Commit

Permalink
Problem: incarnation cache not enabled (#498)
Browse files Browse the repository at this point in the history
* Problem: incarnation cache not enabled

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

* cache eth sig verification result

* fix concurrent incarnations

* update deps

---------

Signed-off-by: yihuang <[email protected]>
  • Loading branch information
yihuang authored Jul 15, 2024
1 parent 45ac081 commit 9c959a2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

* (evm) [#414](https://github.com/crypto-org-chain/ethermint/pull/414) Integrate go-block-stm for parallel tx execution.
* (block-stm) [#498](https://github.com/crypto-org-chain/ethermint/pull/498) Enable incarnation cache for block-stm executor.

### State Machine Breaking

Expand Down
12 changes: 11 additions & 1 deletion app/ante/handler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
evmtypes "github.com/evmos/ethermint/x/evm/types"
)

const EthSigVerificationResultCacheKey = "ante:EthSigVerificationResult"

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper, EVM Keeper and Fee Market Keeper.
type HandlerOptions struct {
Expand Down Expand Up @@ -111,7 +113,15 @@ func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler {
return ctx, err
}

if err := VerifyEthSig(tx, ethSigner); err != nil {
if v, ok := ctx.GetIncarnationCache(EthSigVerificationResultCacheKey); ok {
if v != nil {
err = v.(error)
}
} else {
err = VerifyEthSig(tx, ethSigner)
ctx.SetIncarnationCache(EthSigVerificationResultCacheKey, err)
}
if err != nil {
return ctx, err
}

Expand Down
27 changes: 23 additions & 4 deletions app/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"
"io"
"sync/atomic"

"cosmossdk.io/store/cachemulti"
storetypes "cosmossdk.io/store/types"
Expand All @@ -17,11 +18,11 @@ import (
func DefaultTxExecutor(_ context.Context,
blockSize int,
ms storetypes.MultiStore,
deliverTxWithMultiStore func(int, storetypes.MultiStore) *abci.ExecTxResult,
deliverTxWithMultiStore func(int, storetypes.MultiStore, map[string]any) *abci.ExecTxResult,
) ([]*abci.ExecTxResult, error) {
results := make([]*abci.ExecTxResult, blockSize)
for i := 0; i < blockSize; i++ {
results[i] = deliverTxWithMultiStore(i, ms)
results[i] = deliverTxWithMultiStore(i, ms, nil)
}
return evmtypes.PatchTxResponses(results), nil
}
Expand All @@ -35,21 +36,39 @@ func STMTxExecutor(stores []storetypes.StoreKey, workers int) baseapp.TxExecutor
ctx context.Context,
blockSize int,
ms storetypes.MultiStore,
deliverTxWithMultiStore func(int, storetypes.MultiStore) *abci.ExecTxResult,
deliverTxWithMultiStore func(int, storetypes.MultiStore, map[string]any) *abci.ExecTxResult,
) ([]*abci.ExecTxResult, error) {
if blockSize == 0 {
return nil, nil
}
results := make([]*abci.ExecTxResult, blockSize)
incarnationCache := make([]atomic.Pointer[map[string]any], blockSize)
for i := 0; i < blockSize; i++ {
m := make(map[string]any)
incarnationCache[i].Store(&m)
}
if err := blockstm.ExecuteBlock(
ctx,
blockSize,
index,
stmMultiStoreWrapper{ms},
workers,
func(txn blockstm.TxnIndex, ms blockstm.MultiStore) {
result := deliverTxWithMultiStore(int(txn), msWrapper{ms})
var cache map[string]any

// only one of the concurrent incarnations gets the cache if there are any, otherwise execute without
// cache, concurrent incarnations should be rare.
v := incarnationCache[txn].Swap(nil)
if v != nil {
cache = *v
}

result := deliverTxWithMultiStore(int(txn), msWrapper{ms}, cache)
results[txn] = result

if v != nil {
incarnationCache[txn].Store(v)
}
},
); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ replace (
cosmossdk.io/client/v2 => github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240626040048-36295f051595
cosmossdk.io/store => github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240626040048-36295f051595
cosmossdk.io/x/tx => github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240626040048-36295f051595
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240626040048-36295f051595
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.50.6-0.20240715031529-5a1594f17924
)

replace (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c h1:MOgfS4+FBB8cMkDE2j2VBVsbY+HCkPIu0YsJ/9bbGeQ=
github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240626040048-36295f051595 h1:qiUv1Y+OE8ZgB5mfXFti8R9bjq8zmfuoe2m2g3Iq41c=
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240626040048-36295f051595/go.mod h1:bIUzWfqXnCF2WTFb2uN+FjzMIG3BsOk+P2QmvMtm4ic=
github.com/crypto-org-chain/cosmos-sdk v0.50.6-0.20240715031529-5a1594f17924 h1:rTiEYiXC8AxKeKsOTz4QODkX9fvMAnlAj8R2gOACoxU=
github.com/crypto-org-chain/cosmos-sdk v0.50.6-0.20240715031529-5a1594f17924/go.mod h1:bIUzWfqXnCF2WTFb2uN+FjzMIG3BsOk+P2QmvMtm4ic=
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240626040048-36295f051595 h1:xfrVLBZgV2DXjQXTSlaWHcG/s6Fdeme8tczaN4TTERw=
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240626040048-36295f051595/go.mod h1:W5sR4asmVDUhJpEmuXTUBkk/yEefKlXTjVWcNciVSR0=
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240626040048-36295f051595 h1:gHBOTNAuuGeD9HXGkTE04x3zee+00bXcVd8Jb3WG0nY=
Expand Down
4 changes: 2 additions & 2 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ schema = 3
version = "v1.0.0-beta.5"
hash = "sha256-Fy/PbsOsd6iq0Njy3DVWK6HqWsogI+MkE8QslHGWyVg="
[mod."github.com/cosmos/cosmos-sdk"]
version = "v0.0.0-20240626040048-36295f051595"
hash = "sha256-rwGLcCHeeq74zLoqZ/yCSB6WdHmdwFiz/xRw1SIiBYQ="
version = "v0.50.6-0.20240715031529-5a1594f17924"
hash = "sha256-1oX4RDHti4eGTkHIdqUZfCYUXpw5Ao7oBZDfZclF1Mk="
replaced = "github.com/crypto-org-chain/cosmos-sdk"
[mod."github.com/cosmos/go-bip39"]
version = "v1.0.0"
Expand Down

0 comments on commit 9c959a2

Please sign in to comment.