all: rename Gas to IntrinsicGas in message, fixup uses of Message

This commit is contained in:
lightclient 2025-03-12 14:35:51 -06:00
parent f1ded86873
commit c0ca97f96a
No known key found for this signature in database
GPG key ID: 657913021EF45A6A
10 changed files with 39 additions and 20 deletions

View file

@ -213,7 +213,8 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
if beaconRoot := pre.Env.ParentBeaconBlockRoot; beaconRoot != nil {
core.ProcessBeaconBlockRoot(*beaconRoot, evm)
}
if pre.Env.BlockHashes != nil && chainConfig.IsPrague(new(big.Int).SetUint64(pre.Env.Number), pre.Env.Timestamp) {
rules := evm.Rules()
if pre.Env.BlockHashes != nil && rules.IsPrague {
var (
prevNumber = pre.Env.Number - 1
prevHash = pre.Env.BlockHashes[math.HexOrDecimal64(prevNumber)]
@ -233,8 +234,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
rejectedTxs = append(rejectedTxs, &rejectedTx{i, errMsg})
continue
}
rules := evm.ChainConfig().Rules(evm.Context.BlockNumber, evm.Context.Difficulty.BitLen() == 0, evm.Context.Time)
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee, &rules)
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee, rules)
if err != nil {
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})

View file

@ -93,7 +93,7 @@ type Message struct {
GasPrice *big.Int
GasFeeCap *big.Int
GasTipCap *big.Int
Gas uint64
IntrinsicGas uint64
Data []byte
AccessList types.AccessList
BlobGasFeeCap *big.Int
@ -139,7 +139,7 @@ func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.In
if err != nil {
return nil, err
}
msg.Gas = gas
msg.IntrinsicGas = gas
// Recover sender.
msg.From, err = types.Sender(s, tx)
@ -378,7 +378,7 @@ func (st *stateTransition) execute() (*ExecutionResult, error) {
floorDataGas uint64
)
gas := msg.Gas
gas := msg.IntrinsicGas
// Check clauses 4-5, subtract intrinsic gas if everything is correct
if st.gasRemaining < gas {
return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas)

View file

@ -592,7 +592,7 @@ func (evm *EVM) resolveCodeHash(addr common.Address) common.Hash {
func (evm *EVM) ChainConfig() *params.ChainConfig { return evm.chainConfig }
func (evm *EVM) Rules() *params.Rules {
rules := evm.ChainConfig().Rules(evm.Context.BlockNumber, evm.Context.Difficulty.BitLen() == 0, evm.Context.Time)
rules := evm.ChainConfig().Rules(evm.Context.BlockNumber, evm.Context.Random != nil, evm.Context.Time)
return &rules
}

View file

@ -969,7 +969,8 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
return nil, err
}
var (
msg = args.ToMessage(vmctx.BaseFee, true, true)
rules = api.backend.ChainConfig().Rules(block.Number(), block.Difficulty().BitLen() == 0, block.Time())
msg = args.ToMessage(&rules, vmctx.BaseFee, true, true)
tx = args.ToTransaction(types.LegacyTxType)
traceConfig *TraceConfig
)

View file

@ -174,11 +174,12 @@ func (b *testBackend) StateAtTransaction(ctx context.Context, block *types.Block
signer := types.MakeSigner(b.chainConfig, block.Number(), block.Time())
context := core.NewEVMBlockContext(block.Header(), b.chain, nil)
evm := vm.NewEVM(context, statedb, b.chainConfig, vm.Config{})
rules := evm.Rules()
for idx, tx := range block.Transactions() {
if idx == txIndex {
return tx, context, statedb, release, nil
}
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee(), rules)
if _, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil {
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err)
}

View file

@ -693,7 +693,8 @@ func applyMessage(ctx context.Context, b Backend, args TransactionArgs, state *s
if err := args.CallDefaults(gp.Gas(), blockContext.BaseFee, b.ChainConfig().ChainID); err != nil {
return nil, err
}
msg := args.ToMessage(header.BaseFee, skipChecks, skipChecks)
rules := b.ChainConfig().Rules(header.Number, header.Difficulty.BitLen() == 0, header.Time)
msg := args.ToMessage(&rules, header.BaseFee, skipChecks, skipChecks)
// Lower the basefee to 0 to avoid breaking EVM
// invariants (basefee < feecap).
if msg.GasPrice.Sign() == 0 {
@ -837,7 +838,8 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil {
return 0, err
}
call := args.ToMessage(header.BaseFee, true, true)
rules := b.ChainConfig().Rules(header.Number, header.Difficulty.BitLen() == 0, header.Time)
call := args.ToMessage(&rules, header.BaseFee, true, true)
// Run the gas estimation and wrap any revertals into a custom return
estimate, revert, err := gasestimator.Estimate(ctx, call, opts, gasCap)
@ -1180,15 +1182,15 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
// Copy the original db so we don't modify it
statedb := db.Copy()
// Set the accesslist to the last al
args.AccessList = &accessList
msg := args.ToMessage(header.BaseFee, true, true)
// Apply the transaction with the access list tracer
tracer := logger.NewAccessListTracer(accessList, args.from(), to, precompiles)
config := vm.Config{Tracer: tracer.Hooks(), NoBaseFee: true}
evm := b.GetEVM(ctx, statedb, header, &config, nil)
// Set the accesslist to the last al
args.AccessList = &accessList
msg := args.ToMessage(evm.Rules(), header.BaseFee, true, true)
// Lower the basefee to 0 to avoid breaking EVM
// invariants (basefee < feecap).
if msg.GasPrice.Sign() == 0 {

View file

@ -201,12 +201,13 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
tracingStateDB = state.NewHookedState(sim.state, hooks)
}
evm := vm.NewEVM(blockContext, tracingStateDB, sim.chainConfig, *vmConfig)
rules := evm.Rules()
// It is possible to override precompiles with EVM bytecode, or
// move them to another address.
if precompiles != nil {
evm.SetPrecompiles(precompiles)
}
if sim.chainConfig.IsPrague(header.Number, header.Time) || sim.chainConfig.IsVerkle(header.Number, header.Time) {
if rules.IsPrague || rules.IsVerkle {
core.ProcessParentBlockHash(header.ParentHash, evm)
}
var allLogs []*types.Log
@ -225,7 +226,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header,
tracer.reset(txHash, uint(i))
sim.state.SetTxContext(txHash, i)
// EoA check is always skipped, even in validation mode.
msg := call.ToMessage(header.BaseFee, !sim.validate, true)
msg := call.ToMessage(rules, header.BaseFee, !sim.validate, true)
result, err := applyMessageWithEVM(ctx, evm, msg, timeout, sim.gp)
if err != nil {
txErr := txValidationError(err)

View file

@ -414,7 +414,7 @@ func (args *TransactionArgs) CallDefaults(globalGasCap uint64, baseFee *big.Int,
// core evm. This method is used in calls and traces that do not require a real
// live transaction.
// Assumes that fields are not nil, i.e. setDefaults or CallDefaults has been called.
func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck, skipEoACheck bool) *core.Message {
func (args *TransactionArgs) ToMessage(rules *params.Rules, baseFee *big.Int, skipNonceCheck, skipEoACheck bool) *core.Message {
var (
gasPrice *big.Int
gasFeeCap *big.Int
@ -447,11 +447,25 @@ func (args *TransactionArgs) ToMessage(baseFee *big.Int, skipNonceCheck, skipEoA
if args.AccessList != nil {
accessList = *args.AccessList
}
// Compute the intrinsic gas for stTransaction. Since the test file doesn't
// specify the transaction type we need to infer. This is hacky but we can use
// dynamic fee tx to represent all txs (with respect to instrinsic gas calc at
// least) types except the set code tx type.
var txdata types.TxData
if args.To == nil {
txdata = &types.DynamicFeeTx{To: args.To, Data: args.data(), AccessList: accessList}
} else {
txdata = &types.SetCodeTx{To: *args.To, Data: args.data(), AccessList: accessList, AuthList: args.AuthorizationList}
}
intrinsicGas := types.IntrinsicGas(txdata, rules)
return &core.Message{
From: args.from(),
To: args.To,
Value: (*big.Int)(args.Value),
Nonce: uint64(*args.Nonce),
IntrinsicGas: intrinsicGas,
GasLimit: uint64(*args.Gas),
GasPrice: gasPrice,
GasFeeCap: gasFeeCap,

View file

@ -271,7 +271,7 @@ func runBenchmark(b *testing.B, t *StateTest) {
}
}
post := t.json.Post[subtest.Fork][subtest.Index]
msg, err := t.json.Tx.toMessage(post, baseFee)
msg, err := t.json.Tx.toMessage(post, baseFee, &rules)
if err != nil {
b.Error(err)
return

View file

@ -481,7 +481,7 @@ func (tx *stTransaction) toMessage(ps stPostState, baseFee *big.Int, rules *para
To: to,
Nonce: tx.Nonce,
Value: value,
Gas: gas,
IntrinsicGas: gas,
GasLimit: gasLimit,
GasPrice: gasPrice,
GasFeeCap: tx.MaxFeePerGas,