From a969cd0da4eccfdd43558ad9f5ecadb90273934c Mon Sep 17 00:00:00 2001 From: Leo <89185836+djm07073@users.noreply.github.com> Date: Mon, 2 Dec 2024 17:27:59 +0900 Subject: [PATCH] chore: add default block context function to prevent duplicated logic * fix: dummy block ctx without context func * fix: handle empty hash * revert: contract compiled * fix: build default block context function --- x/evm/keeper/context.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/x/evm/keeper/context.go b/x/evm/keeper/context.go index d1eb3f86..700e06f9 100644 --- a/x/evm/keeper/context.go +++ b/x/evm/keeper/context.go @@ -40,23 +40,33 @@ func (k Keeper) computeGasLimit(sdkCtx sdk.Context) uint64 { return gasLimit } -func (k Keeper) buildBlockContext(ctx context.Context, evm *vm.EVM, fee types.Fee) (vm.BlockContext, error) { +func (k Keeper) buildDefaultBlockContext(ctx context.Context) (vm.BlockContext, error) { sdkCtx := sdk.UnwrapSDKContext(ctx) headerHash := sdkCtx.HeaderHash() if len(headerHash) == 0 { headerHash = make([]byte, 32) } + return vm.BlockContext{ + BlockNumber: big.NewInt(sdkCtx.BlockHeight()), + Time: uint64(sdkCtx.BlockTime().Unix()), + Random: (*common.Hash)(headerHash), + }, nil +} + +func (k Keeper) buildBlockContext(ctx context.Context, defaultBlockCtx vm.BlockContext, evm *vm.EVM, fee types.Fee) (vm.BlockContext, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) baseFee, err := k.baseFee(ctx, fee) if err != nil { return vm.BlockContext{}, err } return vm.BlockContext{ + BlockNumber: defaultBlockCtx.BlockNumber, + Time: defaultBlockCtx.Time, + Random: defaultBlockCtx.Random, BaseFee: baseFee, GasLimit: k.computeGasLimit(sdkCtx), - BlockNumber: big.NewInt(sdkCtx.BlockHeight()), - Time: uint64(sdkCtx.BlockTime().Unix()), CanTransfer: func(sd vm.StateDB, a common.Address, i *uint256.Int) bool { if i == nil || i.IsZero() { return true @@ -116,8 +126,6 @@ func (k Keeper) buildBlockContext(ctx context.Context, evm *vm.EVM, fee types.Fe return common.BytesToHash(bz) }, - // put header hash to bypass isMerge check in evm - Random: (*common.Hash)(headerHash), // unused fields Coinbase: common.Address{}, Difficulty: big.NewInt(0), @@ -163,11 +171,12 @@ func (k Keeper) CreateEVM(ctx context.Context, caller common.Address, tracer *tr chainConfig := types.DefaultChainConfig(ctx) vmConfig := vm.Config{Tracer: tracer, ExtraEips: extraEIPs, NumRetainBlockHashes: ¶ms.NumRetainBlockHashes} - // use dummy block context for chain rules in EVM creation - dummyBlockContext, err := k.buildBlockContext(ctx, nil, fee) + // use default block context for chain rules in EVM creation + defaultBlockContext, err := k.buildDefaultBlockContext(ctx) if err != nil { return ctx, nil, err } + txContext, err := k.buildTxContext(ctx, caller, fee) if err != nil { return ctx, nil, err @@ -175,14 +184,14 @@ func (k Keeper) CreateEVM(ctx context.Context, caller common.Address, tracer *tr // NOTE: need to check if the EVM is correctly initialized with empty context and stateDB evm := vm.NewEVM( - dummyBlockContext, + defaultBlockContext, txContext, nil, chainConfig, vmConfig, ) // customize EVM contexts and stateDB and precompiles - evm.Context, err = k.buildBlockContext(ctx, evm, fee) + evm.Context, err = k.buildBlockContext(ctx, defaultBlockContext, evm, fee) if err != nil { return ctx, nil, err }