Skip to content

Commit

Permalink
chore: add default block context function to prevent duplicated logic
Browse files Browse the repository at this point in the history
* fix: dummy block ctx without context func

* fix: handle empty hash

* revert: contract compiled

* fix: build default block context function
  • Loading branch information
djm07073 authored Dec 2, 2024
1 parent 43d4784 commit a969cd0
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions x/evm/keeper/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -163,26 +171,27 @@ 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
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
}

// 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 a969cd0

Please sign in to comment.