Skip to content

Commit

Permalink
fix: use statedb context at get_hash (#112)
Browse files Browse the repository at this point in the history
* rename go-ethereum version to actual version

* use statedb context at get_hash
  • Loading branch information
beer-1 authored Nov 19, 2024
1 parent b441435 commit d4298cf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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{}
}
Expand Down
5 changes: 5 additions & 0 deletions x/evm/precompiles/cosmos/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions x/evm/precompiles/erc20_registry/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions x/evm/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
//
Expand Down
1 change: 1 addition & 0 deletions x/evm/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type IERC721Keeper interface {
type StateDB interface {
vm.StateDB
ContextOfSnapshot(i int) sdk.Context
Context() sdk.Context
}

type GRPCRouter interface {
Expand Down

0 comments on commit d4298cf

Please sign in to comment.