From d4298cf5d67395edece611d583ef16985c987fea Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:12:20 +0900 Subject: [PATCH] fix: use statedb context at get_hash (#112) * rename go-ethereum version to actual version * use statedb context at get_hash --- go.mod | 2 +- integration-tests/go.mod | 2 +- x/evm/keeper/context.go | 13 +++++-------- x/evm/precompiles/cosmos/common_test.go | 5 +++++ x/evm/precompiles/erc20_registry/common_test.go | 5 +++++ x/evm/state/statedb.go | 16 ++++++++-------- x/evm/types/expected_keeper.go | 1 + 7 files changed, 26 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 8995c74..4853541 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0 github.com/cosmos/ibc-go/modules/capability v1.0.1 github.com/cosmos/ibc-go/v8 v8.5.0 - github.com/ethereum/go-ethereum v1.14.9 + github.com/ethereum/go-ethereum v1.14.11 github.com/golang/mock v1.6.0 github.com/golang/protobuf v1.5.4 github.com/gorilla/mux v1.8.1 diff --git a/integration-tests/go.mod b/integration-tests/go.mod index ce5fbb6..3f0a0ad 100644 --- a/integration-tests/go.mod +++ b/integration-tests/go.mod @@ -9,7 +9,7 @@ require ( github.com/cometbft/cometbft v0.38.12 github.com/cosmos/cosmos-sdk v0.50.10 github.com/cosmos/ibc-go/v8 v8.5.0 - github.com/ethereum/go-ethereum v1.14.9 + github.com/ethereum/go-ethereum v1.14.11 github.com/initia-labs/initia v0.6.1 github.com/initia-labs/minievm v0.5.2 github.com/stretchr/testify v1.9.0 diff --git a/x/evm/keeper/context.go b/x/evm/keeper/context.go index 0243ed4..18eefb0 100644 --- a/x/evm/keeper/context.go +++ b/x/evm/keeper/context.go @@ -24,7 +24,7 @@ import ( "github.com/initia-labs/minievm/x/evm/types" ) -func (k Keeper) NewStateDB(ctx context.Context, evm callableEVM, fee types.Fee) (*evmstate.StateDB, error) { +func (k Keeper) NewStateDB(ctx context.Context, evm *vm.EVM, fee types.Fee) (*evmstate.StateDB, error) { return evmstate.NewStateDB( // delegate gas meter to the EVM sdk.UnwrapSDKContext(ctx).WithGasMeter(storetypes.NewInfiniteGasMeter()), k.Logger(ctx), @@ -44,12 +44,7 @@ func (k Keeper) computeGasLimit(sdkCtx sdk.Context) uint64 { return gasLimit } -type callableEVM interface { - Call(vm.ContractRef, common.Address, []byte, uint64, *uint256.Int) ([]byte, uint64, error) - StaticCall(vm.ContractRef, common.Address, []byte, uint64) ([]byte, uint64, error) -} - -func (k Keeper) buildBlockContext(ctx context.Context, evm callableEVM, fee types.Fee) (vm.BlockContext, error) { +func (k Keeper) buildBlockContext(ctx context.Context, evm *vm.EVM, fee types.Fee) (vm.BlockContext, error) { sdkCtx := sdk.UnwrapSDKContext(ctx) headerHash := sdkCtx.HeaderHash() if len(headerHash) == 0 { @@ -116,7 +111,9 @@ func (k Keeper) buildBlockContext(ctx context.Context, evm callableEVM, fee type } }, GetHash: func(n uint64) common.Hash { - bz, err := k.EVMBlockHashes.Get(sdkCtx, n) + // use snapshot context to get block hash + ctx := evm.StateDB.(types.StateDB).Context() + bz, err := k.EVMBlockHashes.Get(ctx, n) if err != nil { return common.Hash{} } diff --git a/x/evm/precompiles/cosmos/common_test.go b/x/evm/precompiles/cosmos/common_test.go index f52d9e2..1849cff 100644 --- a/x/evm/precompiles/cosmos/common_test.go +++ b/x/evm/precompiles/cosmos/common_test.go @@ -79,6 +79,11 @@ func (m *MockStateDB) ContextOfSnapshot(i int) sdk.Context { return m.snaps[i].Context() } +// Context implements types.StateDB. +func (m *MockStateDB) Context() sdk.Context { + return m.ctx +} + //////////////////////// MOCKED METHODS //////////////////////// // AddAddressToAccessList implements types.StateDB. diff --git a/x/evm/precompiles/erc20_registry/common_test.go b/x/evm/precompiles/erc20_registry/common_test.go index dba0ab6..8a8f09a 100644 --- a/x/evm/precompiles/erc20_registry/common_test.go +++ b/x/evm/precompiles/erc20_registry/common_test.go @@ -73,6 +73,11 @@ func (m *MockStateDB) ContextOfSnapshot(i int) sdk.Context { return m.snaps[i].Context() } +// Context implements types.StateDB. +func (m *MockStateDB) Context() sdk.Context { + return m.ctx +} + //////////////////////// MOCKED METHODS //////////////////////// // AddAddressToAccessList implements types.StateDB. diff --git a/x/evm/state/statedb.go b/x/evm/state/statedb.go index bf92b6c..1c6fd49 100644 --- a/x/evm/state/statedb.go +++ b/x/evm/state/statedb.go @@ -25,11 +25,6 @@ import ( evmtypes "github.com/initia-labs/minievm/x/evm/types" ) -type callableEVM interface { - Call(vm.ContractRef, common.Address, []byte, uint64, *uint256.Int) ([]byte, uint64, error) - StaticCall(vm.ContractRef, common.Address, []byte, uint64) ([]byte, uint64, error) -} - var _ vm.StateDB = &StateDB{} type StateDB struct { @@ -48,7 +43,7 @@ type StateDB struct { transientRefund collections.Map[uint64, uint64] execIndex uint64 - evm callableEVM + evm *vm.EVM erc20ABI *abi.ABI feeContractAddr common.Address // feeDenom contract address @@ -75,7 +70,7 @@ func NewStateDB( transientRefund collections.Map[uint64, uint64], execIndex *atomic.Uint64, // erc20 params - evm callableEVM, + evm *vm.EVM, erc20ABI *abi.ABI, feeContractAddr common.Address, ) (*StateDB, error) { @@ -130,7 +125,6 @@ func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int, _ tracing s.logger.Warn("failed to mint token", "error", err) panic(err) } - } // SubBalance burn coins from the account with addr @@ -585,6 +579,7 @@ func (s *StateDB) RevertToSnapshot(i int) { s.snaps = s.snaps[:i] } +// ContextOfSnapshot returns the context of the snapshot with the given id func (s *StateDB) ContextOfSnapshot(i int) sdk.Context { if i == -1 { return s.initialCtx @@ -593,6 +588,11 @@ func (s *StateDB) ContextOfSnapshot(i int) sdk.Context { return s.snaps[i].ctx } +// Context returns the current context +func (s *StateDB) Context() sdk.Context { + return s.ctx +} + // Prepare handles the preparatory steps for executing a state transition with. // This method must be invoked before state transition. // diff --git a/x/evm/types/expected_keeper.go b/x/evm/types/expected_keeper.go index cc53e25..be205f6 100644 --- a/x/evm/types/expected_keeper.go +++ b/x/evm/types/expected_keeper.go @@ -88,6 +88,7 @@ type IERC721Keeper interface { type StateDB interface { vm.StateDB ContextOfSnapshot(i int) sdk.Context + Context() sdk.Context } type GRPCRouter interface {