From 3b0fa638329553b112eb47db8eb38eeca3652916 Mon Sep 17 00:00:00 2001 From: yihuang Date: Tue, 30 Nov 2021 19:36:10 +0800 Subject: [PATCH] evm: clear access list for each transaction (#802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Clear access list for each transaction Closes: #801 * changelog Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 6 +++++- x/evm/handler_test.go | 4 ++-- x/evm/keeper/grpc_query_test.go | 2 +- x/evm/keeper/state_transition.go | 2 ++ x/evm/keeper/statedb.go | 10 ++++++++++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bed7702c6d..36e680f008 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,11 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Unreleased -## Improvements +### State Machine Breaking + +- (evm) [tharsis#802](https://github.com/tharsis/ethermint/pull/802) Clear access list for each transaction + +### Improvements * (app) [tharsis#794](https://github.com/tharsis/ethermint/pull/794) Setup in-place store migrators. * (ci) [tharsis#784](https://github.com/tharsis/ethermint/pull/784) Enable automatic backport of PRs. diff --git a/x/evm/handler_test.go b/x/evm/handler_test.go index a7d6264bb9..1a62aeeac7 100644 --- a/x/evm/handler_test.go +++ b/x/evm/handler_test.go @@ -596,9 +596,9 @@ func (suite *EvmTestSuite) TestGasRefundWhenReverted() { after := k.GetBalance(suite.from) - suite.Require().Equal(uint64(21861), res.GasUsed) + suite.Require().Equal(uint64(23861), res.GasUsed) // check gas refund works - suite.Require().Equal(big.NewInt(21861), new(big.Int).Sub(before, after)) + suite.Require().Equal(big.NewInt(23861), new(big.Int).Sub(before, after)) } // DummyHook implements EvmHooks interface diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index a1fd045c50..6ee5fceabb 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -689,7 +689,7 @@ func (suite *KeeperTestSuite) TestTraceTx() { predecessors = append(predecessors, firstTx) }, expPass: true, - traceResponse: []byte{0x7b, 0x22, 0x67, 0x61, 0x73, 0x22, 0x3a, 0x33, 0x30, 0x38, 0x32, 0x38, 0x2c, 0x22, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x22, 0x3a, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x22, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x3a, 0x22, 0x22, 0x2c, 0x22, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x4c, 0x6f, 0x67, 0x73, 0x22, 0x3a, 0x5b, 0x7b, 0x22, 0x70, 0x63, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x6f, 0x70, 0x22, 0x3a, 0x22, 0x50, 0x55, 0x53, 0x48, 0x31, 0x22, 0x2c, 0x22, 0x67, 0x61, 0x73, 0x22, 0x3a, 0x39, 0x31, 0x39, 0x36, 0x2c, 0x22, 0x67, 0x61, 0x73, 0x43, 0x6f, 0x73, 0x74, 0x22, 0x3a, 0x33, 0x2c, 0x22, 0x64, 0x65, 0x70, 0x74, 0x68, 0x22, 0x3a, 0x31, 0x2c, 0x22, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0x2c, 0x7b, 0x22, 0x70, 0x63, 0x22, 0x3a, 0x32, 0x2c, 0x22, 0x6f, 0x70, 0x22, 0x3a, 0x22, 0x50, 0x55, 0x53, 0x48, 0x31, 0x22, 0x2c, 0x22, 0x67, 0x61, 0x73}, + traceResponse: []byte{0x7b, 0x22, 0x67, 0x61, 0x73, 0x22, 0x3a, 0x33, 0x34, 0x38, 0x32, 0x38, 0x2c, 0x22, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x22, 0x3a, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x2c, 0x22, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x3a, 0x22, 0x22, 0x2c, 0x22, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x4c, 0x6f, 0x67, 0x73, 0x22, 0x3a, 0x5b, 0x7b, 0x22, 0x70, 0x63, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x6f, 0x70, 0x22, 0x3a, 0x22, 0x50, 0x55, 0x53, 0x48, 0x31, 0x22, 0x2c, 0x22, 0x67, 0x61, 0x73, 0x22, 0x3a, 0x31, 0x33, 0x31, 0x39, 0x36, 0x2c, 0x22, 0x67, 0x61, 0x73, 0x43, 0x6f, 0x73, 0x74, 0x22, 0x3a, 0x33, 0x2c, 0x22, 0x64, 0x65, 0x70, 0x74, 0x68, 0x22, 0x3a, 0x31, 0x2c, 0x22, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x22, 0x3a, 0x5b, 0x5d, 0x7d, 0x2c, 0x7b, 0x22, 0x70, 0x63, 0x22, 0x3a, 0x32, 0x2c, 0x22, 0x6f, 0x70, 0x22, 0x3a, 0x22, 0x50, 0x55, 0x53, 0x48, 0x31, 0x22, 0x2c, 0x22, 0x67, 0x61}, dynamicTxFee: false, }, } diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 02c64224d7..f2938c0095 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -334,6 +334,8 @@ func (k *Keeper) ApplyMessageWithConfig(msg core.Message, tracer vm.Tracer, comm } leftoverGas := msg.Gas() - intrinsicGas + // Clear access list before executing the contract + k.ClearAccessList() // access list preparaion is moved from ante handler to here, because it's needed when `ApplyMessage` is called // under contexts where ante handlers are not run, for example `eth_call` and `eth_estimateGas`. if rules := cfg.ChainConfig.Rules(big.NewInt(k.Ctx().BlockHeight())); rules.IsBerlin { diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index 10fb78c732..ac1cef6650 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -729,6 +729,16 @@ func (k *Keeper) AddSlotToAccessList(addr common.Address, slot common.Hash) { ts.Set(key, []byte{0x1}) } +// ClearAccessList clear current access list +func (k *Keeper) ClearAccessList() { + ctx := k.Ctx() + ts := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientAccessListSlot) + itr := ts.Iterator(nil, nil) + for ; itr.Valid(); itr.Next() { + ts.Delete(itr.Key()) + } +} + // ---------------------------------------------------------------------------- // Snapshotting // ----------------------------------------------------------------------------