Skip to content

Commit

Permalink
fix: build default block context function
Browse files Browse the repository at this point in the history
  • Loading branch information
djm07073 committed Dec 2, 2024
1 parent 5cf5fc6 commit a8587a1
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,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
Expand Down Expand Up @@ -117,8 +127,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),
Expand Down Expand Up @@ -164,16 +172,10 @@ func (k Keeper) CreateEVM(ctx context.Context, caller common.Address, tracer *tr
chainConfig := types.DefaultChainConfig(ctx)
vmConfig := vm.Config{Tracer: tracer, ExtraEips: extraEIPs, NumRetainBlockHashes: &params.NumRetainBlockHashes}

// use dummy block context for chain rules in EVM creation
sdkCtx := sdk.UnwrapSDKContext(ctx)
headerHash := sdkCtx.HeaderHash()
if len(headerHash) == 0 {
headerHash = make([]byte, 32)
}
dummyBlockContext := vm.BlockContext{
BlockNumber: big.NewInt(sdkCtx.BlockHeight()),
Random: (*common.Hash)(headerHash),
Time: uint64(sdkCtx.BlockTime().Unix()),
// 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)
Expand All @@ -183,14 +185,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
}
Expand Down

0 comments on commit a8587a1

Please sign in to comment.