Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: dummy block ctx without context func #118

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 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,26 +172,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
Loading