diff --git a/app/ante/eth.go b/app/ante/eth.go index 82402e2da5..c09205e131 100644 --- a/app/ante/eth.go +++ b/app/ante/eth.go @@ -303,10 +303,7 @@ func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate BaseFee: baseFee, } - stateDB, err := statedb.New(ctx, ctd.evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))) - if err != nil { - return ctx, errorsmod.Wrap(err, "failed to create new stateDB") - } + stateDB := statedb.New(ctx, ctd.evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))) evm := ctd.evmKeeper.NewEVM(ctx, coreMsg, cfg, evmtypes.NewNoOpTracer(), stateDB) // check that caller has enough balance to cover asset transfer for **topmost** call diff --git a/app/ante/utils_test.go b/app/ante/utils_test.go index a9d19f5c32..9a23c4874e 100644 --- a/app/ante/utils_test.go +++ b/app/ante/utils_test.go @@ -76,9 +76,7 @@ type AnteTestSuite struct { const TestGasLimit uint64 = 100000 func (suite *AnteTestSuite) StateDB() *statedb.StateDB { - db, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash()))) - suite.Require().NoError(err) - return db + return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash()))) } func (suite *AnteTestSuite) SetupTest() { diff --git a/go.mod b/go.mod index 1e030c763b..e6f4fca1cc 100644 --- a/go.mod +++ b/go.mod @@ -248,10 +248,10 @@ require ( replace ( cosmossdk.io/core => cosmossdk.io/core v0.11.0 - cosmossdk.io/store => /Users/dudong2/workspace/github.com/b-harvest/cosmos-sdk2/store + cosmossdk.io/store => github.com/b-harvest/cosmos-sdk/store v0.0.0-20241015155842-364149db166b // use cosmos keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.1.7-0.20210622111912-ef00f8ac3d76 - github.com/cosmos/cosmos-sdk => /Users/dudong2/workspace/github.com/b-harvest/cosmos-sdk + github.com/cosmos/cosmos-sdk => github.com/b-harvest/cosmos-sdk v0.0.0-20241017100038-2b7fae21f59d github.com/ethereum/go-ethereum => github.com/b-harvest/go-ethereum v0.0.0-20240925105515-5fa8b7772734 // Fix upstream GHSA-h395-qcrw-5vmq vulnerability. // TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409 diff --git a/go.sum b/go.sum index ed35529117..d7c8387b27 100644 --- a/go.sum +++ b/go.sum @@ -265,6 +265,10 @@ github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX github.com/aws/aws-sdk-go v1.44.224 h1:09CiaaF35nRmxrzWZ2uRq5v6Ghg/d2RiPjZnSgtt+RQ= github.com/aws/aws-sdk-go v1.44.224/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= +github.com/b-harvest/cosmos-sdk v0.0.0-20241017100038-2b7fae21f59d h1:XG9XWbLOi/IhelbffMmNEicMSARLAgmsFVjTpM4U7VI= +github.com/b-harvest/cosmos-sdk v0.0.0-20241017100038-2b7fae21f59d/go.mod h1:i72MYwrjOF82mlXRK91O2h2MYHl3EG0+zjDeKJLZ6/g= +github.com/b-harvest/cosmos-sdk/store v0.0.0-20241015155842-364149db166b h1:V8FGjyQqNZ4qTWGEFJQsInCWORqevhSs9oAENzuLEEE= +github.com/b-harvest/cosmos-sdk/store v0.0.0-20241015155842-364149db166b/go.mod h1:oZfW/4Fc/zYqu3JmQcQdUJ3fqu5vnYTn3LZFFy8P8ng= github.com/b-harvest/go-ethereum v0.0.0-20240925105515-5fa8b7772734 h1:7uYMhLMyZgyEuulnqAVaD5wWua66nbY3kS62fFQaoeE= github.com/b-harvest/go-ethereum v0.0.0-20240925105515-5fa8b7772734/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= diff --git a/tests/importer/importer_test.go b/tests/importer/importer_test.go index 1b0d7da7e8..285300b265 100644 --- a/tests/importer/importer_test.go +++ b/tests/importer/importer_test.go @@ -142,8 +142,7 @@ func (suite *ImporterTestSuite) TestImportBlocks() { suite.Require().NoError(err) ctx := suite.app.NewContextLegacy(false, tmheader) ctx = ctx.WithBlockHeight(tmheader.Height) - vmdb, err := statedb.New(ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))) - suite.Require().NoError(err) + vmdb := statedb.New(ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))) if chainConfig.DAOForkSupport && chainConfig.DAOForkBlock != nil && chainConfig.DAOForkBlock.Cmp(block.Number()) == 0 { applyDAOHardFork(vmdb) diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index 21a74b7eaf..adc080ba76 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -185,9 +185,7 @@ func (suite *EvmTestSuite) SignTx(tx *types.MsgEthereumTx) { } func (suite *EvmTestSuite) StateDB() *statedb.StateDB { - db, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash()))) - suite.Require().NoError(err) - return db + return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash()))) } func TestEvmTestSuite(t *testing.T) { diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 6468e6d1a1..c36559733b 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -387,8 +387,7 @@ func (suite *KeeperTestSuite) TestQueryTxLogs() { suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { suite.SetupTest() // reset - vmdb, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(common.BytesToHash(suite.ctx.HeaderHash()), txHash, txIndex, logIndex)) - suite.Require().NoError(err) + vmdb := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig(common.BytesToHash(suite.ctx.HeaderHash()), txHash, txIndex, logIndex)) tc.malleate(vmdb) suite.Require().NoError(vmdb.Commit()) diff --git a/x/evm/keeper/hooks_test.go b/x/evm/keeper/hooks_test.go index 6c3dfb876e..d5afb55a11 100644 --- a/x/evm/keeper/hooks_test.go +++ b/x/evm/keeper/hooks_test.go @@ -66,13 +66,12 @@ func (suite *KeeperTestSuite) TestEvmHooks() { k := suite.app.EvmKeeper ctx := suite.ctx txHash := common.BigToHash(big.NewInt(1)) - vmdb, err := statedb.New(ctx, k, statedb.NewTxConfig( + vmdb := statedb.New(ctx, k, statedb.NewTxConfig( common.BytesToHash(ctx.HeaderHash()), txHash, 0, 0, )) - suite.Require().NoError(err) vmdb.AddLog(ðtypes.Log{ Topics: []common.Hash{}, diff --git a/x/evm/keeper/keeper_test.go b/x/evm/keeper/keeper_test.go index 02c6ca7ba6..50c7cc6f1e 100644 --- a/x/evm/keeper/keeper_test.go +++ b/x/evm/keeper/keeper_test.go @@ -32,7 +32,6 @@ import ( "github.com/evmos/ethermint/crypto/ethsecp256k1" "github.com/evmos/ethermint/server/config" "github.com/evmos/ethermint/tests" - "github.com/evmos/ethermint/testutil" ethermint "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm/statedb" "github.com/evmos/ethermint/x/evm/types" @@ -255,9 +254,7 @@ func (suite *KeeperTestSuite) Commit() { } func (suite *KeeperTestSuite) StateDB() *statedb.StateDB { - db, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash()))) - suite.Require().NoError(err) - return db + return statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(suite.ctx.HeaderHash()))) } // DeployTestContract deploy a test erc20 contract and returns the contract address @@ -534,58 +531,3 @@ func (suite *KeeperTestSuite) TestGetAccountOrEmpty() { }) } } - -func (suite *KeeperTestSuite) TestRevertByPrecompileSnapshot() { - db := suite.StateDB() - - // snapshot id for journal - rev := db.Snapshot() - - ctx, err := db.GetCacheContext() - suite.NoError(err) - - snapshotMultiStore, err := db.MultiStoreSnapshot() - suite.NoError(err) - snapshotEvents := suite.ctx.EventManager().Events() - db.AddPrecompileSnapshot(snapshotMultiStore, snapshotEvents) - - // manipulate statedb(evm) - evmAddr, priv := tests.NewAddrKey() - key1 := common.BigToHash(big.NewInt(1)) - value1 := common.BigToHash(big.NewInt(2)) - key2 := common.BigToHash(big.NewInt(3)) - value2 := common.BigToHash(big.NewInt(4)) - db.SetState(evmAddr, key1, value1) - db.SetState(evmAddr, key2, value2) - - suite.Equal(value1, db.GetState(evmAddr, key1)) - suite.Equal(value2, db.GetState(evmAddr, key2)) - - // manipulate bank keeper(sdk) - addr1 := sdk.AccAddress(priv.PubKey().Address().Bytes()) - denom := "testdenom" - testutil.FundAccount(suite.app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewCoin(denom, sdkmath.NewInt(1000000)))) - addr2 := sdk.AccAddress(tests.GenerateAddress().Bytes()) - suite.app.BankKeeper.SendCoins(ctx, addr1, addr2, sdk.NewCoins(sdk.NewCoin(denom, sdkmath.NewInt(1000)))) - - suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(999000)), suite.app.BankKeeper.GetBalance(ctx, addr1, denom)) - suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(1000)), suite.app.BankKeeper.GetBalance(ctx, addr2, denom)) - - // revert to snapshot - db.RevertToSnapshot(rev) - - suite.Equal(common.Hash{}, db.GetState(evmAddr, key1)) - suite.Equal(common.Hash{}, db.GetState(evmAddr, key2)) - - suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(999000)), suite.app.BankKeeper.GetBalance(ctx, addr1, denom)) - suite.Equal(sdk.NewCoin(denom, sdkmath.NewInt(1000)), suite.app.BankKeeper.GetBalance(ctx, addr2, denom)) - - // commit changes(revert sdk state) - db.Commit() - - suite.Equal(common.Hash{}, db.GetState(evmAddr, key1)) - suite.Equal(common.Hash{}, db.GetState(evmAddr, key2)) - - suite.Equal(sdk.NewCoin(denom, sdkmath.ZeroInt()), suite.app.BankKeeper.GetBalance(suite.ctx, addr1, denom)) - suite.Equal(sdk.NewCoin(denom, sdkmath.ZeroInt()), suite.app.BankKeeper.GetBalance(suite.ctx, addr2, denom)) -} diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index e9c1facd64..c54aedf4d4 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -349,10 +349,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, return nil, errorsmod.Wrap(types.ErrCallDisabled, "failed to call contract") } - stateDB, err := statedb.New(ctx, k, txConfig) - if err != nil { - return nil, errorsmod.Wrap(err, "failed to create new stateDB") - } + stateDB := statedb.New(ctx, k, txConfig) evm := k.NewEVM(ctx, msg, cfg, tracer, stateDB) leftoverGas := msg.Gas() diff --git a/x/evm/keeper/statedb_test.go b/x/evm/keeper/statedb_test.go index afa2350490..ffcede1708 100644 --- a/x/evm/keeper/statedb_test.go +++ b/x/evm/keeper/statedb_test.go @@ -715,12 +715,11 @@ func (suite *KeeperTestSuite) TestAddLog() { for _, tc := range testCases { suite.Run(tc.name, func() { suite.SetupTest() - vmdb, err := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig( + vmdb := statedb.New(suite.ctx, suite.app.EvmKeeper, statedb.NewTxConfig( common.BytesToHash(suite.ctx.HeaderHash()), tc.hash, 0, 0, )) - suite.Require().NoError(err) tc.malleate(vmdb) vmdb.AddLog(tc.log) diff --git a/x/evm/statedb/journal.go b/x/evm/statedb/journal.go index 9fa922e437..3ed2e753c5 100644 --- a/x/evm/statedb/journal.go +++ b/x/evm/statedb/journal.go @@ -20,9 +20,7 @@ import ( "bytes" "sort" - storetypes "cosmossdk.io/store/types" - - sdk "github.com/cosmos/cosmos-sdk/types" + "cosmossdk.io/store/cachemulti" "github.com/ethereum/go-ethereum/common" ) @@ -137,14 +135,9 @@ type ( slot *common.Hash } - precompileChange struct { - ms storetypes.MultiStore - es sdk.Events - } - nativeChange struct { - ms storetypes.MultiStore - es int + snapshot cachemulti.Store + events int // native events index } ) @@ -240,28 +233,9 @@ func (ch accessListAddSlotChange) Dirtied() *common.Address { return nil } -func (ch precompileChange) Revert(s *StateDB) { - s.cacheCtx = s.cacheCtx.WithMultiStore(ch.ms) - - // Necessary to revert the sdk state - s.writeCache = func() { - s.cacheCtx.EventManager().EmitEvents(ch.es) - ch.ms.CacheMultiStore().Write() - } -} - -func (ch precompileChange) Dirtied() *common.Address { - return nil -} - func (ch nativeChange) Revert(s *StateDB) { - s.cacheCtx = s.cacheCtx.WithMultiStore(ch.ms) - - // Necessary to revert the sdk state - s.writeCache = func() { - s.nativeEvents = s.nativeEvents[:len(s.nativeEvents)-ch.es] - ch.ms.CacheMultiStore().Write() - } + s.cacheMS.Restore(ch.snapshot) + s.nativeEvents = s.nativeEvents[:len(s.nativeEvents)-ch.events] } func (ch nativeChange) Dirtied() *common.Address { diff --git a/x/evm/statedb/state_object.go b/x/evm/statedb/state_object.go index 584f0d3402..46cdb8f206 100644 --- a/x/evm/statedb/state_object.go +++ b/x/evm/statedb/state_object.go @@ -119,7 +119,7 @@ func (s *stateObject) Code() []byte { if bytes.Equal(s.CodeHash(), emptyCodeHash) { return nil } - code := s.db.keeper.GetCode(s.db.ctx, common.BytesToHash(s.CodeHash())) + code := s.db.keeper.GetCode(s.db.cacheCtx, common.BytesToHash(s.CodeHash())) s.code = code return code } @@ -176,7 +176,7 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash { return value } // If no live objects are available, load it from keeper - value := s.db.keeper.GetState(s.db.ctx, s.Address(), key) + value := s.db.keeper.GetState(s.db.cacheCtx, s.Address(), key) s.originStorage[key] = value return value } diff --git a/x/evm/statedb/statedb.go b/x/evm/statedb/statedb.go index c5fd452d04..cdf1105c54 100644 --- a/x/evm/statedb/statedb.go +++ b/x/evm/statedb/statedb.go @@ -16,7 +16,6 @@ package statedb import ( - "errors" "fmt" "math/big" "sort" @@ -24,7 +23,6 @@ import ( errorsmod "cosmossdk.io/errors" sdkmath "cosmossdk.io/math" "cosmossdk.io/store/cachemulti" - storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -34,6 +32,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" ) +const StateDBContextKey = "statedb" + type EventConverter = func(sdk.Event) (*ethtypes.Log, error) // revision is the identifier of a version of state. @@ -56,6 +56,7 @@ type StateDB struct { ctx sdk.Context cacheCtx sdk.Context + cacheMS cachemulti.Store writeCache func() // Journal of state modifications. This is the backbone of @@ -86,7 +87,7 @@ type StateDB struct { } // New creates a new state from a given trie. -func New(ctx sdk.Context, keeper Keeper, txConfig TxConfig) (*StateDB, error) { +func New(ctx sdk.Context, keeper Keeper, txConfig TxConfig) *StateDB { db := &StateDB{ keeper: keeper, ctx: ctx, @@ -95,13 +96,23 @@ func New(ctx sdk.Context, keeper Keeper, txConfig TxConfig) (*StateDB, error) { accessList: newAccessList(), txConfig: txConfig, + } - evmDenom: keeper.GetParams(ctx).EvmDenom, + if parentCacheMS, ok := ctx.MultiStore().(cachemulti.Store); ok { + db.cacheMS = parentCacheMS.Clone() + db.writeCache = func() { parentCacheMS.Restore(db.cacheMS) } + } else { + // in unit test, it could be run with a uncached multistore + if db.cacheMS, ok = ctx.MultiStore().CacheWrap().(cachemulti.Store); !ok { + panic("expect the CacheWrap result to be cachemulti.Store") + } + db.writeCache = db.cacheMS.Write } - // init cache context - _, err := db.GetCacheContext() + db.cacheCtx = ctx.WithValue(StateDBContextKey, db).WithMultiStore(db.cacheMS) + + db.evmDenom = keeper.GetParams(ctx).EvmDenom - return db, err + return db } func (s *StateDB) NativeEvents() sdk.Events { @@ -249,7 +260,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject { return obj } // If no live objects are available, load it from keeper - account := s.keeper.GetAccount(s.ctx, addr) + account := s.keeper.GetAccount(s.cacheCtx, addr) if account == nil { return nil } @@ -322,29 +333,15 @@ func (s *StateDB) setStateObject(object *stateObject) { s.stateObjects[object.Address()] = object } -func (s *StateDB) GetCacheContext() (sdk.Context, error) { - if s.writeCache == nil { - if s.ctx.MultiStore() == nil { - return s.ctx, errors.New("ctx has no multi store") - } - s.cacheCtx, s.writeCache = s.ctx.CacheContext() - } - - return s.cacheCtx, nil +func (s *StateDB) GetCacheContext() sdk.Context { + return s.cacheCtx } -func (s *StateDB) MultiStoreSnapshot() (cachemulti.Store, error) { - ctx, err := s.GetCacheContext() - if err != nil { // means s.ctx.MultiStore() == nil - return cachemulti.Store{}, err - } - - cms := ctx.MultiStore().(cachemulti.Store) - snapshot := cms.Clone() - return snapshot, nil +func (s *StateDB) MultiStoreSnapshot() cachemulti.Store { + return s.cacheMS.Clone() } -func (s *StateDB) ProcessSDKEvents(contract common.Address, events sdk.Events, converter EventConverter) { +func (s *StateDB) emitNativeEvents(contract common.Address, events sdk.Events, converter EventConverter) { if converter == nil { return } @@ -369,38 +366,22 @@ func (s *StateDB) ProcessSDKEvents(contract common.Address, events sdk.Events, c } } -// If revert is occurred, the snapshot of journal is overwritten. -func (s *StateDB) AddPrecompileSnapshot(snapshot storetypes.MultiStore, events sdk.Events) { - s.journal.append(precompileChange{snapshot, events}) -} - // ExecuteNativeAction executes native action in isolate, // the writes will be revert when either the native action itself fail // or the wrapping message call reverted. func (s *StateDB) ExecuteNativeAction(contract common.Address, converter EventConverter, action func(ctx sdk.Context) error) error { - snapshot, err := s.MultiStoreSnapshot() - if err != nil { - return err - } - - tmpCacheCtx, tmpWriteCache := s.cacheCtx.CacheContext() + snapshot := s.MultiStoreSnapshot() eventManager := sdk.NewEventManager() - if err := action(tmpCacheCtx.WithEventManager(eventManager)); err != nil { + + if err := action(s.cacheCtx.WithEventManager(eventManager)); err != nil { + s.cacheMS.Restore(snapshot) return err } - tmpWriteCache() - - // if err := action(s.cacheCtx.WithEventManager(eventManager)); err != nil { - // // TODO(dudong2): need to revert to snapshot? - // s.cacheCtx.MultiStore().(cachemulti.Store).Restore(snapshot) - // // s.revertNativeStateToSnapshot(snapshot) - // return err - // } events := eventManager.Events() - s.ProcessSDKEvents(contract, events, converter) + s.emitNativeEvents(contract, events, converter) s.nativeEvents = s.nativeEvents.AppendEvents(events) - s.journal.append(nativeChange{ms: snapshot, es: len(events)}) + s.journal.append(nativeChange{snapshot: snapshot, events: len(events)}) return nil } @@ -611,9 +592,9 @@ func (s *StateDB) Commit() error { return s.err } - // write all store updates from precompile - if s.writeCache != nil { - s.writeCache() + s.writeCache() + if len(s.nativeEvents) > 0 { + s.ctx.EventManager().EmitEvents(s.nativeEvents) } for _, addr := range s.journal.sortedDirties() { diff --git a/x/evm/statedb/statedb_test.go b/x/evm/statedb/statedb_test.go index faf6d6e456..a946c30146 100644 --- a/x/evm/statedb/statedb_test.go +++ b/x/evm/statedb/statedb_test.go @@ -81,8 +81,7 @@ func (suite *StateDBTestSuite) TestAccount() { suite.Require().Equal(statedb.NewEmptyAccount(), acct) suite.Require().False(acct.IsContract()) - db, err := statedb.New(ctx, keeper, txConfig) - suite.Require().NoError(err) + db = statedb.New(ctx, keeper, txConfig) suite.Require().Equal(true, db.Exist(address)) suite.Require().Equal(true, db.Empty(address)) suite.Require().Equal(big.NewInt(0), db.GetBalance(address)) @@ -109,8 +108,7 @@ func (suite *StateDBTestSuite) TestAccount() { suite.Require().NotEmpty(keeper.GetCode(ctx, codeHash)) // suicide - db, err := statedb.New(ctx, keeper, txConfig) - suite.Require().NoError(err) + db = statedb.New(ctx, keeper, txConfig) suite.Require().False(db.HasSuicided(address)) suite.Require().True(db.Suicide(address)) @@ -127,8 +125,7 @@ func (suite *StateDBTestSuite) TestAccount() { ctx, keeper = newTestKeeper(suite.T(), cms) // not accessible from StateDB anymore - db, err = statedb.New(ctx, keeper, txConfig) - suite.Require().NoError(err) + db = statedb.New(ctx, keeper, txConfig) suite.Require().False(db.Exist(address)) // and cleared in keeper too @@ -140,8 +137,7 @@ func (suite *StateDBTestSuite) TestAccount() { for _, tc := range testCases { suite.Run(tc.name, func() { raw, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, txConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, txConfig) tc.malleate(db, raw) }) } @@ -149,8 +145,7 @@ func (suite *StateDBTestSuite) TestAccount() { func (suite *StateDBTestSuite) TestAccountOverride() { _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) // test balance carry over when overwritten amount := big.NewInt(1) @@ -178,8 +173,7 @@ func (suite *StateDBTestSuite) TestDBError() { } for _, tc := range testCases { _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) tc.malleate(db) suite.Require().Error(db.Commit()) } @@ -217,8 +211,7 @@ func (suite *StateDBTestSuite) TestBalance() { for _, tc := range testCases { suite.Run(tc.name, func() { raw, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) tc.malleate(db) // check dirty state @@ -272,8 +265,7 @@ func (suite *StateDBTestSuite) TestState() { for _, tc := range testCases { suite.Run(tc.name, func() { raw, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) tc.malleate(db) suite.Require().NoError(db.Commit()) @@ -284,8 +276,7 @@ func (suite *StateDBTestSuite) TestState() { } // check ForEachStorage - db, err = statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db = statedb.New(ctx, keeper, emptyTxConfig) collected := CollectContractStorage(db) if len(tc.expStates) > 0 { suite.Require().Equal(tc.expStates, collected) @@ -318,8 +309,7 @@ func (suite *StateDBTestSuite) TestCode() { for _, tc := range testCases { suite.Run(tc.name, func() { raw, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) tc.malleate(db) // check dirty state @@ -331,8 +321,7 @@ func (suite *StateDBTestSuite) TestCode() { // check the committed state ctx, keeper = newTestKeeper(suite.T(), raw) - db, err = statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db = statedb.New(ctx, keeper, emptyTxConfig) suite.Require().Equal(tc.expCode, db.GetCode(address)) suite.Require().Equal(len(tc.expCode), db.GetCodeSize(address)) suite.Require().Equal(tc.expCodeHash, db.GetCodeHash(address)) @@ -390,8 +379,7 @@ func (suite *StateDBTestSuite) TestRevertSnapshot() { { // do some arbitrary changes to the storage - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) db.SetNonce(address, 1) db.AddBalance(address, big.NewInt(100)) db.SetCode(address, []byte("hello world")) @@ -404,8 +392,7 @@ func (suite *StateDBTestSuite) TestRevertSnapshot() { ctx, keeper = newTestKeeper(suite.T(), raw) // run test - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) rev := db.Snapshot() tc.malleate(db) db.RevertToSnapshot(rev) @@ -429,8 +416,7 @@ func (suite *StateDBTestSuite) TestNestedSnapshot() { value2 := common.BigToHash(big.NewInt(2)) _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) rev1 := db.Snapshot() db.SetState(address, key, value1) @@ -448,8 +434,7 @@ func (suite *StateDBTestSuite) TestNestedSnapshot() { func (suite *StateDBTestSuite) TestInvalidSnapshotId() { _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) suite.Require().Panics(func() { db.RevertToSnapshot(1) }) @@ -501,8 +486,7 @@ func (suite *StateDBTestSuite) TestAccessList() { for _, tc := range testCases { _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) tc.malleate(db) } } @@ -516,8 +500,7 @@ func (suite *StateDBTestSuite) TestLog() { 1, 1, ) _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, txConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, txConfig) data := []byte("hello world") db.AddLog(ðtypes.Log{ Address: address, @@ -570,8 +553,7 @@ func (suite *StateDBTestSuite) TestRefund() { } for _, tc := range testCases { _, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) if !tc.expPanic { tc.malleate(db) suite.Require().Equal(tc.expRefund, db.GetRefund()) @@ -590,8 +572,7 @@ func (suite *StateDBTestSuite) TestIterateStorage() { value2 := common.BigToHash(big.NewInt(4)) raw, ctx, keeper := setupTestEnv(suite.T()) - db, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + db := statedb.New(ctx, keeper, emptyTxConfig) db.SetState(address, key1, value1) db.SetState(address, key2, value2) @@ -640,8 +621,7 @@ func (suite *StateDBTestSuite) TestNativeAction() { return converter(event) } - stateDB, err := statedb.New(ctx, keeper, emptyTxConfig) - suite.Require().NoError(err) + stateDB := statedb.New(ctx, keeper, emptyTxConfig) contract := common.BigToAddress(big.NewInt(101)) stateDB.ExecuteNativeAction(contract, eventConverter, func(ctx sdk.Context) error { @@ -715,6 +695,7 @@ func (suite *StateDBTestSuite) TestNativeAction() { TxHash: emptyTxConfig.TxHash, Data: []byte("success2"), }}, stateDB.Logs()) + // test query stateDB.ExecuteNativeAction(contract, nil, func(ctx sdk.Context) error { store := ctx.KVStore(storeKey) @@ -726,34 +707,34 @@ func (suite *StateDBTestSuite) TestNativeAction() { stateDB.RevertToSnapshot(rev1) - // // check events - // suite.Require().Equal(sdk.Events{{Type: "success1"}}, stateDB.NativeEvents()) - // suite.Require().Equal([]*ethtypes.Log{{ - // Address: contract, - // BlockHash: emptyTxConfig.BlockHash, - // TxHash: emptyTxConfig.TxHash, - // Data: []byte("success1"), - // }}, stateDB.Logs()) - - // _ = stateDB.Snapshot() - // stateDB.ExecuteNativeAction(contract, eventConverter, func(ctx sdk.Context) error { - // store := ctx.KVStore(storeKey) - // store.Set([]byte("success3"), []byte("value")) - // ctx.EventManager().EmitEvent(sdk.NewEvent("success3")) - // return nil - // }) - - // // check events - // suite.Require().Equal(sdk.Events{{Type: "success1"}, {Type: "success3"}}, stateDB.NativeEvents()) - - // // test query - // stateDB.ExecuteNativeAction(contract, eventConverter, func(ctx sdk.Context) error { - // store := ctx.KVStore(storeKey) - // suite.Require().Equal([]byte("value"), store.Get([]byte("success1"))) - // suite.Require().Nil(store.Get([]byte("success2"))) - // suite.Require().Equal([]byte("value"), store.Get([]byte("success3"))) - // return nil - // }) + // check events + suite.Require().Equal(sdk.Events{{Type: "success1"}}, stateDB.NativeEvents()) + suite.Require().Equal([]*ethtypes.Log{{ + Address: contract, + BlockHash: emptyTxConfig.BlockHash, + TxHash: emptyTxConfig.TxHash, + Data: []byte("success1"), + }}, stateDB.Logs()) + + _ = stateDB.Snapshot() + stateDB.ExecuteNativeAction(contract, eventConverter, func(ctx sdk.Context) error { + store := ctx.KVStore(storeKey) + store.Set([]byte("success3"), []byte("value")) + ctx.EventManager().EmitEvent(sdk.NewEvent("success3")) + return nil + }) + + // check events + suite.Require().Equal(sdk.Events{{Type: "success1"}, {Type: "success3"}}, stateDB.NativeEvents()) + + // test query + stateDB.ExecuteNativeAction(contract, eventConverter, func(ctx sdk.Context) error { + store := ctx.KVStore(storeKey) + suite.Require().Equal([]byte("value"), store.Get([]byte("success1"))) + suite.Require().Nil(store.Get([]byte("success2"))) + suite.Require().Equal([]byte("value"), store.Get([]byte("success3"))) + return nil + }) suite.Require().NoError(stateDB.Commit()) @@ -761,13 +742,12 @@ func (suite *StateDBTestSuite) TestNativeAction() { store := ctx.KVStore(storeKey) suite.Require().Equal([]byte("value"), store.Get([]byte("success1"))) suite.Require().Nil(store.Get([]byte("success2"))) - // suite.Require().Equal([]byte("value"), store.Get([]byte("success3"))) + suite.Require().Equal([]byte("value"), store.Get([]byte("success3"))) suite.Require().Nil(store.Get([]byte("failure1"))) suite.Require().Nil(store.Get([]byte("failure2"))) // check events - // suite.Require().Equal(sdk.Events{{Type: "success1"}, {Type: "success3"}}, ctx.EventManager().Events()) - suite.Require().Equal(sdk.Events{{Type: "success1"}}, ctx.EventManager().Events()) + suite.Require().Equal(sdk.Events{{Type: "success1"}, {Type: "success3"}}, ctx.EventManager().Events()) } func CollectContractStorage(db vm.StateDB) statedb.Storage {