From 00226035ae4f644b192d78d7507438db1fd8790f Mon Sep 17 00:00:00 2001 From: chengzhinei Date: Sun, 25 Jun 2023 16:00:39 +0800 Subject: [PATCH] fix dds wasm (#183) * fix dds wasm * simulate not set WasmStoreCode * change isDeliver to IsDeliverWithSerial * change isDeliver to IsDeliverWithSerial * change isDeliver to IsDeliverWithSerial * change isDeliver to IsDeliverWithSerial --------- Co-authored-by: KamiD <44460798+KamiD@users.noreply.github.com> --- app/innertx_test.go | 2 +- libs/cosmos-sdk/baseapp/baseapp.go | 2 +- libs/cosmos-sdk/types/context.go | 72 ++++++++++++------------ libs/cosmos-sdk/x/auth/keeper/account.go | 7 ++- libs/tendermint/state/execution_dds.go | 4 +- libs/tendermint/types/deltas.go | 1 + x/evm/handler.go | 2 +- x/evm/handler_test.go | 10 ++-- x/evm/keeper/params_test.go | 4 +- x/evm/types/state_transition.go | 2 +- x/evm/types/statedb_protect_test.go | 2 +- x/evm/types/statedb_test.go | 2 +- x/evm/watcher/watcher_test.go | 2 +- x/feesplit/keeper/fee_splits.go | 6 +- x/params/types/upgrade_cache_test.go | 2 +- x/wasm/handler.go | 5 +- 16 files changed, 65 insertions(+), 60 deletions(-) diff --git a/app/innertx_test.go b/app/innertx_test.go index 1d0b89b9d..ce00967e9 100644 --- a/app/innertx_test.go +++ b/app/innertx_test.go @@ -61,7 +61,7 @@ func (suite *InnerTxTestSuite) SetupTest() { suite.app = Setup(checkTx) suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: chain_id, Time: time.Now().UTC()}) - suite.ctx.SetDeliver() + suite.ctx.SetDeliverSerial() suite.stateDB = evm_types.CreateEmptyCommitStateDB(suite.app.EvmKeeper.GenerateCSDBParams(), suite.ctx) suite.codec = codec.New() diff --git a/libs/cosmos-sdk/baseapp/baseapp.go b/libs/cosmos-sdk/baseapp/baseapp.go index 03418d100..464fd164c 100644 --- a/libs/cosmos-sdk/baseapp/baseapp.go +++ b/libs/cosmos-sdk/baseapp/baseapp.go @@ -699,7 +699,7 @@ func (app *BaseApp) getContextForTx(mode runTxMode, txBytes []byte) sdk.Context } if mode == runTxModeDeliver { - ctx.SetDeliver() + ctx.SetDeliverSerial() } ctx.SetFeeSplitInfo(&sdk.FeeSplitInfo{}) diff --git a/libs/cosmos-sdk/types/context.go b/libs/cosmos-sdk/types/context.go index 27802a4f9..11d8b29b0 100644 --- a/libs/cosmos-sdk/types/context.go +++ b/libs/cosmos-sdk/types/context.go @@ -24,36 +24,36 @@ but please do not over-use it. We try to keep all data structured and standard additions here would be better just to add to the Context struct */ type Context struct { - ctx context.Context - ms MultiStore - header *abci.Header - chainID string - from string - txBytes []byte - logger log.Logger - voteInfo []abci.VoteInfo - gasMeter GasMeter - blockGasMeter GasMeter - isDeliver bool - checkTx bool - recheckTx bool // if recheckTx == true, then checkTx must also be true - wrappedCheckTx bool // if wrappedCheckTx == true, then checkTx must also be true - traceTx bool // traceTx is set true for trace tx and its predesessors , traceTx was set in app.beginBlockForTrace() - traceTxLog bool // traceTxLog is used to create trace logger for evm , traceTxLog is set to true when only tracing target tx (its predesessors will set false), traceTxLog is set before runtx - traceTxConfigBytes []byte // traceTxConfigBytes is used to save traceTxConfig, passed from api to x/evm - minGasPrice DecCoins - consParams *abci.ConsensusParams - eventManager *EventManager - accountNonce uint64 - trc *trace.Tracer - accountCache *AccountCache - paraMsg *ParaMsg + ctx context.Context + ms MultiStore + header *abci.Header + chainID string + from string + txBytes []byte + logger log.Logger + voteInfo []abci.VoteInfo + gasMeter GasMeter + blockGasMeter GasMeter + isDeliverWithSerial bool + checkTx bool + recheckTx bool // if recheckTx == true, then checkTx must also be true + wrappedCheckTx bool // if wrappedCheckTx == true, then checkTx must also be true + traceTx bool // traceTx is set true for trace tx and its predesessors , traceTx was set in app.beginBlockForTrace() + traceTxLog bool // traceTxLog is used to create trace logger for evm , traceTxLog is set to true when only tracing target tx (its predesessors will set false), traceTxLog is set before runtx + traceTxConfigBytes []byte // traceTxConfigBytes is used to save traceTxConfig, passed from api to x/evm + minGasPrice DecCoins + consParams *abci.ConsensusParams + eventManager *EventManager + accountNonce uint64 + trc *trace.Tracer + accountCache *AccountCache + paraMsg *ParaMsg // txCount uint32 - overridesBytes []byte // overridesBytes is used to save overrides info, passed from ethCall to x/evm - watcher *TxWatcher - feesplitInfo *FeeSplitInfo - statedb vm.StateDB - outOfGas bool + overridesBytes []byte // overridesBytes is used to save overrides info, passed from ethCall to x/evm + watcher *TxWatcher + feesplitInfo *FeeSplitInfo + statedb vm.StateDB + outOfGas bool wasmSimulateCache map[string][]byte } @@ -83,12 +83,12 @@ func (c *Context) VoteInfos() []abci.VoteInfo { return c.voteInfo } func (c *Context) GasMeter() GasMeter { return c.gasMeter } func (c *Context) BlockGasMeter() GasMeter { return c.blockGasMeter } -func (c *Context) IsDeliver() bool { - return c.isDeliver +func (c *Context) IsDeliverWithSerial() bool { + return c.isDeliverWithSerial } func (c *Context) UseParamCache() bool { - return c.isDeliver || (c.paraMsg != nil && !c.paraMsg.HaveCosmosTxInBlock) || c.checkTx + return c.isDeliverWithSerial || (c.paraMsg != nil && !c.paraMsg.HaveCosmosTxInBlock) || c.checkTx } func (c *Context) IsCheckTx() bool { return c.checkTx } @@ -208,8 +208,8 @@ func NewContext(ms MultiStore, header abci.Header, isCheckTx bool, logger log.Lo } } -func (c *Context) SetDeliver() *Context { - c.isDeliver = true +func (c *Context) SetDeliverSerial() *Context { + c.isDeliverWithSerial = true return c } @@ -318,8 +318,8 @@ func (c *Context) SetIsCheckTx(isCheckTx bool) *Context { return c } -func (c *Context) SetIsDeliverTx(isDeliverTx bool) *Context { - c.isDeliver = isDeliverTx +func (c *Context) SetIsDeliverTxSerial(isDeliverWithSerial bool) *Context { + c.isDeliverWithSerial = isDeliverWithSerial return c } diff --git a/libs/cosmos-sdk/x/auth/keeper/account.go b/libs/cosmos-sdk/x/auth/keeper/account.go index d9b003edb..34f8e21f3 100644 --- a/libs/cosmos-sdk/x/auth/keeper/account.go +++ b/libs/cosmos-sdk/x/auth/keeper/account.go @@ -77,7 +77,7 @@ func (ak AccountKeeper) SetAccount(ctx sdk.Context, acc exported.Account) { storeAccKey := types.AddressStoreKey(addr) store.Set(storeAccKey, bz) - if ctx.IsDeliver() { + if ctx.IsDeliverWithSerial() { mpt.GAccToPrefetchChannel <- [][]byte{storeAccKey} } @@ -119,7 +119,7 @@ func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc exported.Account) { storeAccKey := types.AddressStoreKey(addr) store.Delete(storeAccKey) - if ctx.IsDeliver() { + if ctx.IsDeliverWithSerial() { mpt.GAccToPrefetchChannel <- [][]byte{storeAccKey} } } @@ -141,7 +141,8 @@ func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account exporte } // IterateAccounts iterates over all the stored accounts and performs a callback function -// TODO by yxq: deprecated +// +// TODO by yxq: deprecated func (ak AccountKeeper) MigrateAccounts(ctx sdk.Context, cb func(account exported.Account, key, value []byte) (stop bool)) { store := ctx.KVStore(ak.mptKey) diff --git a/libs/tendermint/state/execution_dds.go b/libs/tendermint/state/execution_dds.go index 60562c8bf..a7943ef1c 100644 --- a/libs/tendermint/state/execution_dds.go +++ b/libs/tendermint/state/execution_dds.go @@ -194,14 +194,14 @@ func (dc *DeltaContext) postApplyBlock(height int64, deltaInfo *DeltaInfo, } // delta producer - if dc.uploadDelta { + if dc.uploadDelta && !types.WasmStoreCode { trace.GetElapsedInfo().AddInfo(trace.Delta, fmt.Sprintf("ratio<%.2f>", dc.hitRatio())) wdFunc := evmWatchDataManager.CreateWatchDataGenerator() wasmWdFunc := wasmWatchDataManager.CreateWatchDataGenerator() go dc.uploadData(height, abciResponses, deltaMap, wdFunc, wasmWdFunc) - } + types.WasmStoreCode = false } func (dc *DeltaContext) uploadData(height int64, abciResponses *ABCIResponses, deltaMap interface{}, wdFunc, wasmWdFunc func() ([]byte, error)) { diff --git a/libs/tendermint/types/deltas.go b/libs/tendermint/types/deltas.go index 3ffe4e283..f684b347c 100644 --- a/libs/tendermint/types/deltas.go +++ b/libs/tendermint/types/deltas.go @@ -44,6 +44,7 @@ var ( FastQuery = false DownloadDelta = false UploadDelta = false + WasmStoreCode = false ) type TreeDelta struct { diff --git a/x/evm/handler.go b/x/evm/handler.go index 64781d157..4cde21681 100644 --- a/x/evm/handler.go +++ b/x/evm/handler.go @@ -16,7 +16,7 @@ func NewHandler(k *Keeper) sdk.Handler { return func(ctx sdk.Context, msg sdk.Msg) (result *sdk.Result, err error) { ctx.SetEventManager(sdk.NewEventManager()) - if ctx.IsDeliver() { + if ctx.IsDeliverWithSerial() { k.UpdatedAccount = k.UpdatedAccount[:0] } diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 0bc21bc63..b0e811527 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -51,7 +51,7 @@ func (suite *EvmTestSuite) SetupTest() { suite.app = app.Setup(checkTx) suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: chain_id, Time: time.Now().UTC()}) - suite.ctx.SetDeliver() + suite.ctx.SetDeliverSerial() suite.stateDB = types.CreateEmptyCommitStateDB(suite.app.EvmKeeper.GenerateCSDBParams(), suite.ctx) suite.handler = evm.NewHandler(suite.app.EvmKeeper) suite.querier = keeper.NewQuerier(*suite.app.EvmKeeper) @@ -614,14 +614,14 @@ func (suite *EvmTestSuite) TestSimulateConflict() { suite.Require().NoError(err) suite.ctx.SetGasMeter(sdk.NewInfiniteGasMeter()) - suite.ctx.SetIsCheckTx(true).SetIsDeliverTx(false) + suite.ctx.SetIsCheckTx(true).SetIsDeliverTxSerial(false) cacheCtx, _ := suite.ctx.CacheContext() result, err := suite.handler(cacheCtx, tx) suite.Require().NotNil(result) suite.Require().Nil(err) - suite.ctx.SetIsCheckTx(false).SetIsDeliverTx(true) + suite.ctx.SetIsCheckTx(false).SetIsDeliverTxSerial(true) result, err = suite.handler(suite.ctx, tx) suite.Require().NotNil(result) suite.Require().Nil(err) @@ -801,7 +801,7 @@ func (suite *EvmContractBlockedListTestSuite) SetupTest() { suite.app = app.Setup(checkTx) suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: "ethermint-3", Time: time.Now().UTC()}) - suite.ctx.SetDeliver() + suite.ctx.SetDeliverSerial() suite.stateDB = types.CreateEmptyCommitStateDB(suite.app.EvmKeeper.GenerateCSDBParams(), suite.ctx) suite.handler = evm.NewHandler(suite.app.EvmKeeper) @@ -1043,7 +1043,7 @@ func (suite *EvmContractBlockedListTestSuite) TestEvmParamsAndContractMethodBloc for _, tc := range testCases { suite.Run(tc.msg, func() { suite.SetupTest() - suite.ctx.SetIsDeliverTx(true).SetIsCheckTx(false) + suite.ctx.SetIsDeliverTxSerial(true).SetIsCheckTx(false) // set contract code suite.stateDB.CreateAccount(callEthAcc) diff --git a/x/evm/keeper/params_test.go b/x/evm/keeper/params_test.go index 3f3e48715..1c3f067d1 100644 --- a/x/evm/keeper/params_test.go +++ b/x/evm/keeper/params_test.go @@ -10,9 +10,9 @@ func (suite *KeeperTestSuite) TestParams() { suite.app.EvmKeeper.SetParams(suite.ctx, params) newParams := suite.app.EvmKeeper.GetParams(suite.ctx) suite.Require().Equal(newParams, params) - newParams = suite.app.EvmKeeper.GetParams(*suite.ctx.SetDeliver()) + newParams = suite.app.EvmKeeper.GetParams(*suite.ctx.SetDeliverSerial()) suite.Require().Equal(newParams, params) types.GetEvmParamsCache().UpdateParams(params, false) - newParams = suite.app.EvmKeeper.GetParams(*suite.ctx.SetDeliver()) + newParams = suite.app.EvmKeeper.GetParams(*suite.ctx.SetDeliverSerial()) suite.Require().Equal(newParams, params) } diff --git a/x/evm/types/state_transition.go b/x/evm/types/state_transition.go index 596769e74..932c0474e 100644 --- a/x/evm/types/state_transition.go +++ b/x/evm/types/state_transition.go @@ -381,7 +381,7 @@ func (st StateTransition) TransitionDb(ctx sdk.Context, config ChainConfig) (exe } if !st.Simulate { - if ctx.IsDeliver() || ctx.ParaMsg() != nil { + if ctx.IsDeliverWithSerial() || ctx.ParaMsg() != nil { csdb.Commit(true) } } diff --git a/x/evm/types/statedb_protect_test.go b/x/evm/types/statedb_protect_test.go index 24beb2dc9..b00891e09 100644 --- a/x/evm/types/statedb_protect_test.go +++ b/x/evm/types/statedb_protect_test.go @@ -44,7 +44,7 @@ func (suite *StateDB_ProtectTestSuite) SetupTest() { suite.app = app.Setup(checkTx) suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: "ethermint-1"}) - suite.ctx.SetDeliver() + suite.ctx.SetDeliverSerial() suite.stateDB = types.CreateEmptyCommitStateDB(suite.app.EvmKeeper.GenerateCSDBParams(), suite.ctx) privkey, err := ethsecp256k1.GenerateKey() diff --git a/x/evm/types/statedb_test.go b/x/evm/types/statedb_test.go index 0fb9e9540..83900a1ba 100644 --- a/x/evm/types/statedb_test.go +++ b/x/evm/types/statedb_test.go @@ -40,7 +40,7 @@ func (suite *StateDBTestSuite) SetupTest() { suite.app = app.Setup(checkTx) suite.ctx = suite.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: "ethermint-1"}) - suite.ctx.SetDeliver() + suite.ctx.SetDeliverSerial() suite.stateDB = types.CreateEmptyCommitStateDB(suite.app.EvmKeeper.GenerateCSDBParams(), suite.ctx) privkey, err := ethsecp256k1.GenerateKey() diff --git a/x/evm/watcher/watcher_test.go b/x/evm/watcher/watcher_test.go index 795500676..d8efa60da 100644 --- a/x/evm/watcher/watcher_test.go +++ b/x/evm/watcher/watcher_test.go @@ -69,7 +69,7 @@ func setupTest() *WatcherTestSt { w.app = app.Setup(checkTx) w.ctx = w.app.BaseApp.NewContext(checkTx, abci.Header{Height: 1, ChainID: chain_id, Time: time.Now().UTC()}) - w.ctx.SetDeliver() + w.ctx.SetDeliverSerial() w.handler = evm.NewHandler(w.app.EvmKeeper) ethermint.SetChainId(chain_id) diff --git a/x/feesplit/keeper/fee_splits.go b/x/feesplit/keeper/fee_splits.go index 96c64bdb1..4ee472916 100644 --- a/x/feesplit/keeper/fee_splits.go +++ b/x/feesplit/keeper/fee_splits.go @@ -94,7 +94,7 @@ func (k Keeper) SetFeeSplit(ctx sdk.Context, feeSplit types.FeeSplit) { store.Set(key.Bytes(), bz) // update cache - if ctx.IsDeliver() || ctx.ParaMsg() != nil { + if ctx.IsDeliverWithSerial() || ctx.ParaMsg() != nil { types.GetParamsCache().UpdateFeeSplit(feeSplit.ContractAddress, feeSplit, ctx.IsCheckTx()) } } @@ -106,7 +106,7 @@ func (k Keeper) DeleteFeeSplit(ctx sdk.Context, feeSplit types.FeeSplit) { store.Delete(key.Bytes()) // update cache - if ctx.IsDeliver() || ctx.ParaMsg() != nil { + if ctx.IsDeliverWithSerial() || ctx.ParaMsg() != nil { types.GetParamsCache().DeleteFeeSplit(feeSplit.ContractAddress, ctx.IsCheckTx()) } } @@ -196,7 +196,7 @@ func (k Keeper) SetContractShare( store.Set(contract.Bytes(), share.Bytes()) // update cache - if ctx.IsDeliver() || ctx.ParaMsg() != nil { + if ctx.IsDeliverWithSerial() || ctx.ParaMsg() != nil { types.GetParamsCache().UpdateShare(contract, share, ctx.IsCheckTx()) } } diff --git a/x/params/types/upgrade_cache_test.go b/x/params/types/upgrade_cache_test.go index 63c196f04..1cfc0a21b 100644 --- a/x/params/types/upgrade_cache_test.go +++ b/x/params/types/upgrade_cache_test.go @@ -43,7 +43,7 @@ func (suite *UpgradeKeeperSuite) SetupTest() { func (suite *UpgradeKeeperSuite) Context(height int64) sdk.Context { ctx := sdk.NewContext(suite.ms, abci.Header{Height: height}, false, suite.logger) - ctx.SetDeliver() + ctx.SetDeliverSerial() return ctx } diff --git a/x/wasm/handler.go b/x/wasm/handler.go index 21ffe7bcd..890f712f7 100644 --- a/x/wasm/handler.go +++ b/x/wasm/handler.go @@ -35,7 +35,7 @@ func NewHandler(k types.ContractOpsKeeper) sdk.Handler { // update watcher defer func() { // update watchDB when delivering tx - if ctx.IsDeliver() || ctx.ParaMsg() != nil { + if ctx.IsDeliverWithSerial() || ctx.ParaMsg() != nil { watcher.Save(err) } @@ -46,6 +46,9 @@ func NewHandler(k types.ContractOpsKeeper) sdk.Handler { switch msg := msg.(type) { case *MsgStoreCode: //nolint:typecheck + if !ctx.IsCheckTx() { + types2.WasmStoreCode = true + } res, err = msgServer.StoreCode(sdk.WrapSDKContext(ctx), msg) case *MsgInstantiateContract: res, err = msgServer.InstantiateContract(sdk.WrapSDKContext(ctx), msg)