diff --git a/consensus/tests/engine_v1_tests/helper.go b/consensus/tests/engine_v1_tests/helper.go index d89d95610b..1f887437bd 100644 --- a/consensus/tests/engine_v1_tests/helper.go +++ b/consensus/tests/engine_v1_tests/helper.go @@ -373,7 +373,7 @@ func createBlockFromHeader(bc *core.BlockChain, customHeader *types.Header, txs statedb.SetTxContext(tx.Hash(), i) blockContext := core.NewEVMBlockContext(&header, bc, &header.Coinbase) evm := vm.NewEVM(blockContext, statedb, nil, bc.Config(), vm.Config{}) - receipt, _, _, err := core.ApplyTransaction(bc.Config(), nil, evm, gp, statedb, &header, tx, gasUsed) + receipt, _, _, err := core.ApplyTransaction(nil, evm, gp, statedb, &header, tx, gasUsed) if err != nil { return nil, fmt.Errorf("%v when applying transaction", err) } diff --git a/consensus/tests/engine_v2_tests/helper.go b/consensus/tests/engine_v2_tests/helper.go index 30c3b7cfd1..afa2da801c 100644 --- a/consensus/tests/engine_v2_tests/helper.go +++ b/consensus/tests/engine_v2_tests/helper.go @@ -903,7 +903,7 @@ func createBlockFromHeader(bc *core.BlockChain, customHeader *types.Header, txs statedb.SetTxContext(tx.Hash(), i) blockContext := core.NewEVMBlockContext(&header, bc, &header.Coinbase) evm := vm.NewEVM(blockContext, statedb, nil, bc.Config(), vm.Config{}) - receipt, _, _, err := core.ApplyTransaction(bc.Config(), nil, evm, gp, statedb, &header, tx, gasUsed) + receipt, _, _, err := core.ApplyTransaction(nil, evm, gp, statedb, &header, tx, gasUsed) if err != nil { return nil, fmt.Errorf("%v when applying transaction", err) } diff --git a/core/chain_makers.go b/core/chain_makers.go index 64871dc948..723af74f15 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -89,7 +89,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti b.statedb.SetTxContext(tx.Hash(), len(b.txs)) blockContext := NewEVMBlockContext(b.header, bc, &b.header.Coinbase) evm := vm.NewEVM(blockContext, b.statedb, nil, b.config, vmConfig) - receipt, gas, tokenFeeUsed, err := ApplyTransaction(b.config, feeCapacity, evm, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed) + receipt, gas, tokenFeeUsed, err := ApplyTransaction(feeCapacity, evm, b.gasPool, b.statedb, b.header, tx, &b.header.GasUsed) if err != nil { panic(err) } @@ -238,7 +238,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse // EIP-2935 blockContext := NewEVMBlockContext(b.header, chainReader, &b.header.Coinbase) evm := vm.NewEVM(blockContext, statedb, nil, config, vm.Config{}) - ProcessParentBlockHash(b.header.ParentHash, evm, statedb) + ProcessParentBlockHash(b.header.ParentHash, evm) } // Execute any user modifications to the block diff --git a/core/state_processor.go b/core/state_processor.go index 45d1033003..4d04d56792 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -103,7 +103,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra coinbaseOwner := getCoinbaseOwner(p.bc, statedb, header, nil) if p.config.IsPrague(block.Number()) { - ProcessParentBlockHash(block.ParentHash(), evm, tracingStateDB) + ProcessParentBlockHash(block.ParentHash(), evm) } // Iterate over and process the individual transactions @@ -146,7 +146,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, tra } statedb.SetTxContext(tx.Hash(), i) - receipt, gas, tokenFeeUsed, err := ApplyTransactionWithEVM(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, evm, balanceFee, coinbaseOwner) + receipt, gas, tokenFeeUsed, err := ApplyTransactionWithEVM(msg, gp, statedb, blockNumber, blockHash, tx, usedGas, evm, balanceFee, coinbaseOwner) if err != nil { return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err) } @@ -210,7 +210,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated coinbaseOwner := getCoinbaseOwner(p.bc, statedb, header, nil) if p.config.IsPrague(block.Number()) { - ProcessParentBlockHash(block.ParentHash(), evm, tracingStateDB) + ProcessParentBlockHash(block.ParentHash(), evm) } // Iterate over and process the individual transactions @@ -253,7 +253,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated } statedb.SetTxContext(tx.Hash(), i) - receipt, gas, tokenFeeUsed, err := ApplyTransactionWithEVM(msg, p.config, gp, statedb, blockNumber, blockHash, tx, usedGas, evm, balanceFee, coinbaseOwner) + receipt, gas, tokenFeeUsed, err := ApplyTransactionWithEVM(msg, gp, statedb, blockNumber, blockHash, tx, usedGas, evm, balanceFee, coinbaseOwner) if err != nil { return nil, nil, 0, err } @@ -279,7 +279,7 @@ func (p *StateProcessor) ProcessBlockNoValidator(cBlock *CalculatedBlock, stated // ApplyTransactionWithEVM attempts to apply a transaction to the given state database // and uses the input parameters for its environment similar to ApplyTransaction. However, // this method takes an already created EVM instance as input. -func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, balanceFee *big.Int, coinbaseOwner common.Address) (receipt *types.Receipt, gasUsed uint64, tokenFeeUsed bool, err error) { +func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM, balanceFee *big.Int, coinbaseOwner common.Address) (receipt *types.Receipt, gasUsed uint64, tokenFeeUsed bool, err error) { if hooks := evm.Config.Tracer; hooks != nil { if hooks.OnTxStart != nil { hooks.OnTxStart(evm.GetVMContext(), tx, msg.From) @@ -290,6 +290,7 @@ func ApplyTransactionWithEVM(msg *Message, config *params.ChainConfig, gp *GasPo } to := tx.To() + config := evm.ChainConfig() if to != nil { if *to == common.BlockSignersBinary && config.IsTIPSigning(blockNumber) { return ApplySignTransaction(msg, config, statedb, blockNumber, blockHash, tx, usedGas, evm) @@ -520,7 +521,7 @@ func getCoinbaseOwner(bc *BlockChain, statedb *state.StateDB, header *types.Head // and uses the input parameters for its environment. It returns the receipt // for the transaction, gas used and an error if the transaction failed, // indicating the block was invalid. -func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*big.Int, evm *vm.EVM, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, bool, error) { +func ApplyTransaction(tokensFee map[common.Address]*big.Int, evm *vm.EVM, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64) (*types.Receipt, uint64, bool, error) { var balanceFee *big.Int if tx.To() != nil { if value, ok := tokensFee[*tx.To()]; ok { @@ -528,13 +529,13 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]* } } - signer := types.MakeSigner(config, header.Number) + signer := types.MakeSigner(evm.ChainConfig(), header.Number) msg, err := TransactionToMessage(tx, signer, balanceFee, header.Number, header.BaseFee) if err != nil { return nil, 0, false, err } coinbaseOwner := statedb.GetOwner(evm.Context.Coinbase) - return ApplyTransactionWithEVM(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, evm, balanceFee, coinbaseOwner) + return ApplyTransactionWithEVM(msg, gp, statedb, header.Number, header.Hash(), tx, usedGas, evm, balanceFee, coinbaseOwner) } func ApplySignTransaction(msg *Message, config *params.ChainConfig, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (receipt *types.Receipt, gasUsed uint64, tokenFeeUsed bool, err error) { @@ -645,9 +646,9 @@ func InitSignerInTransactions(config *params.ChainConfig, header *types.Header, // ProcessParentBlockHash writes the parent hash to the EIP-2935 history contract // and enforces the expected code, with a one-time Prague backfill if missing. -func ProcessParentBlockHash(prevHash common.Hash, vmenv *vm.EVM, statedb vm.StateDB) { +func ProcessParentBlockHash(prevHash common.Hash, evm *vm.EVM) { // Verify history contract code matches the expected bytecode - code := statedb.GetCode(params.HistoryStorageAddress) + code := evm.StateDB.GetCode(params.HistoryStorageAddress) if len(code) > 0 && !bytes.Equal(code, params.HistoryStorageCode) { log.Error("History storage code mismatch", "have", crypto.Keccak256Hash(code), @@ -656,11 +657,11 @@ func ProcessParentBlockHash(prevHash common.Hash, vmenv *vm.EVM, statedb vm.Stat panic("history storage code mismatch") } - blockNumber := vmenv.Context.BlockNumber - if blockNumber == nil || !vmenv.ChainConfig().IsPrague(blockNumber) { + blockNumber := evm.Context.BlockNumber + if blockNumber == nil || !evm.ChainConfig().IsPrague(blockNumber) { return } - forkBlock := vmenv.ChainConfig().PragueBlock + forkBlock := evm.ChainConfig().PragueBlock if forkBlock == nil { forkBlock = common.PragueBlock } @@ -670,13 +671,13 @@ func ProcessParentBlockHash(prevHash common.Hash, vmenv *vm.EVM, statedb vm.Stat // Only deploy and backfill if the contract is missing at/after Prague activation. if len(code) == 0 { - if !statedb.Exist(params.HistoryStorageAddress) { - statedb.CreateAccount(params.HistoryStorageAddress) + if !evm.StateDB.Exist(params.HistoryStorageAddress) { + evm.StateDB.CreateAccount(params.HistoryStorageAddress) } - if statedb.GetNonce(params.HistoryStorageAddress) == 0 { - statedb.SetNonce(params.HistoryStorageAddress, 1) + if evm.StateDB.GetNonce(params.HistoryStorageAddress) == 0 { + evm.StateDB.SetNonce(params.HistoryStorageAddress, 1) } - statedb.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode) + evm.StateDB.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode) if blockNumber.Sign() > 0 { end := blockNumber.Uint64() - 1 @@ -691,17 +692,17 @@ func ProcessParentBlockHash(prevHash common.Hash, vmenv *vm.EVM, statedb vm.Stat } } for n := start; n <= end; n++ { - hash := vmenv.Context.GetHash(n) + hash := evm.Context.GetHash(n) if hash == (common.Hash{}) { log.Debug("History backfill missing hash", "number", n) continue } - statedb.SetState(params.HistoryStorageAddress, historyStorageKey(n), hash) + evm.StateDB.SetState(params.HistoryStorageAddress, historyStorageKey(n), hash) } } } - if tracer := vmenv.Config.Tracer; tracer != nil { + if tracer := evm.Config.Tracer; tracer != nil { if tracer.OnSystemCallStart != nil { tracer.OnSystemCallStart() } @@ -719,13 +720,13 @@ func ProcessParentBlockHash(prevHash common.Hash, vmenv *vm.EVM, statedb vm.Stat To: ¶ms.HistoryStorageAddress, Data: prevHash.Bytes(), } - vmenv.SetTxContext(NewEVMTxContext(msg)) - statedb.AddAddressToAccessList(params.HistoryStorageAddress) - _, _, err := vmenv.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560) + evm.SetTxContext(NewEVMTxContext(msg)) + evm.StateDB.AddAddressToAccessList(params.HistoryStorageAddress) + _, _, err := evm.Call(msg.From, *msg.To, msg.Data, 30_000_000, common.U2560) if err != nil { panic(err) } - statedb.Finalise(true) + evm.StateDB.Finalise(true) } func historyStorageKey(number uint64) common.Hash { diff --git a/core/state_processor_test.go b/core/state_processor_test.go index 7aff0b1f31..5bbc9718a7 100644 --- a/core/state_processor_test.go +++ b/core/state_processor_test.go @@ -471,7 +471,7 @@ func TestApplyTransactionWithEVMTracer(t *testing.T) { // Apply transaction var usedGas uint64 - _, _, _, err = ApplyTransactionWithEVM(msg, config, gasPool, statedb, blockNumber, blockHash, signedTx, &usedGas, evm, big.NewInt(0), common.Address{}) + _, _, _, err = ApplyTransactionWithEVM(msg, gasPool, statedb, blockNumber, blockHash, signedTx, &usedGas, evm, big.NewInt(0), common.Address{}) // NOTE: Some special transactions (like BlockSignersBinary or XDCXAddrBinary) // may fail in test environment due to missing configuration or state, but // the tracer should still be called at the beginning of ApplyTransactionWithEVM. @@ -559,7 +559,7 @@ func TestApplyTransactionWithEVMStateChangeHooks(t *testing.T) { gasPool := new(GasPool).AddGas(1000000) var usedGas uint64 - _, _, _, err = ApplyTransactionWithEVM(msg, config, gasPool, statedb, big.NewInt(1), genesis.Hash(), signedTx, &usedGas, evmenv, nil, common.Address{}) + _, _, _, err = ApplyTransactionWithEVM(msg, gasPool, statedb, big.NewInt(1), genesis.Hash(), signedTx, &usedGas, evmenv, nil, common.Address{}) if err != nil { t.Fatalf("ApplyTransactionWithEVM failed: %v", err) } @@ -584,11 +584,11 @@ func TestProcessParentBlockHash(t *testing.T) { vmContext := NewEVMBlockContext(header, nil, &coinbase) evm := vm.NewEVM(vmContext, statedb, nil, chainConfig, vm.Config{}) - ProcessParentBlockHash(header.ParentHash, evm, statedb) + ProcessParentBlockHash(header.ParentHash, evm) vmContext = NewEVMBlockContext(parent, nil, &coinbase) evm = vm.NewEVM(vmContext, statedb, nil, chainConfig, vm.Config{}) - ProcessParentBlockHash(parent.ParentHash, evm, statedb) + ProcessParentBlockHash(parent.ParentHash, evm) // make sure that the state is correct if have := getParentBlockHash(statedb, 1); have != hashA { @@ -624,7 +624,7 @@ func TestProcessParentBlockHashPragueGuard(t *testing.T) { Random: &random, } evm := vm.NewEVM(blockContext, statedb, nil, &config, vm.Config{}) - ProcessParentBlockHash(common.Hash{0x01}, evm, statedb) + ProcessParentBlockHash(common.Hash{0x01}, evm) if code := statedb.GetCode(params.HistoryStorageAddress); len(code) != 0 { t.Fatalf("unexpected history contract code predeploy: %x", code) @@ -662,7 +662,7 @@ func TestProcessParentBlockHashBackfillMissingHistory(t *testing.T) { Random: &random, } evm := vm.NewEVM(blockContext, statedb, nil, &config, vm.Config{}) - ProcessParentBlockHash(common.Hash{0x01}, evm, statedb) + ProcessParentBlockHash(common.Hash{0x01}, evm) if have := getParentBlockHash(statedb, 1); have != available[1] { t.Fatalf("expected hash at slot 1, have %v", have) @@ -701,7 +701,7 @@ func TestProcessParentBlockHashCodeMismatchPanics(t *testing.T) { t.Fatal("expected panic on history storage code mismatch") } }() - ProcessParentBlockHash(common.Hash{0x01}, evm, statedb) + ProcessParentBlockHash(common.Hash{0x01}, evm) } func getParentBlockHash(statedb *state.StateDB, number uint64) common.Hash { diff --git a/eth/state_accessor.go b/eth/state_accessor.go index c453c23a53..8ac2f1f80a 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -210,7 +210,7 @@ func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, evm := vm.NewEVM(context, statedb, nil, eth.blockchain.Config(), vm.Config{}) // If prague hardfork, insert parent block hash in the state as per EIP-2935. if eth.blockchain.Config().IsPrague(block.Number()) { - core.ProcessParentBlockHash(block.ParentHash(), evm, statedb) + core.ProcessParentBlockHash(block.ParentHash(), evm) } if txIndex == 0 && len(block.Transactions()) == 0 { return nil, vm.BlockContext{}, statedb, release, nil diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 41b30df579..ab72896bb6 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -383,7 +383,7 @@ func (api *API) traceChain(start, end *types.Block, config *TraceConfig, closed if api.backend.ChainConfig().IsPrague(next.Number()) { context := core.NewEVMBlockContext(next.Header(), api.chainContext(ctx), nil) evm := vm.NewEVM(context, statedb, nil, api.backend.ChainConfig(), vm.Config{}) - core.ProcessParentBlockHash(next.ParentHash(), evm, statedb) + core.ProcessParentBlockHash(next.ParentHash(), evm) } // Clean out any pending release functions of trace state. Note this // step must be done after constructing tracing state, because the @@ -521,7 +521,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config ) evm := vm.NewEVM(vmctx, statedb, nil, chainConfig, vm.Config{}) if chainConfig.IsPrague(block.Number()) { - core.ProcessParentBlockHash(block.ParentHash(), evm, statedb) + core.ProcessParentBlockHash(block.ParentHash(), evm) } feeCapacity := statedb.GetTRC21FeeCapacityFromState() for i, tx := range block.Transactions() { @@ -590,7 +590,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) evm := vm.NewEVM(blockCtx, statedb, nil, api.backend.ChainConfig(), vm.Config{}) if api.backend.ChainConfig().IsPrague(block.Number()) { - core.ProcessParentBlockHash(block.ParentHash(), evm, statedb) + core.ProcessParentBlockHash(block.ParentHash(), evm) } // JS tracers have high overhead. In this case run a parallel @@ -695,7 +695,7 @@ func (api *API) traceBlockParallel(ctx context.Context, block *types.Block, stat blockCtx := core.NewEVMBlockContext(block.Header(), api.chainContext(ctx), nil) evm := vm.NewEVM(blockCtx, statedb, nil, api.backend.ChainConfig(), vm.Config{}) if api.backend.ChainConfig().IsPrague(block.Number()) { - core.ProcessParentBlockHash(block.ParentHash(), evm, statedb) + core.ProcessParentBlockHash(block.ParentHash(), evm) } txloop: @@ -930,7 +930,7 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor // Call SetTxContext to clear out the statedb access list statedb.SetTxContext(txctx.TxHash, txctx.TxIndex) - _, _, _, err = core.ApplyTransactionWithEVM(message, api.backend.ChainConfig(), new(core.GasPool).AddGas(message.GasLimit), statedb, vmctx.BlockNumber, txctx.BlockHash, tx, &usedGas, evm, balance, common.Address{}) + _, _, _, err = core.ApplyTransactionWithEVM(message, new(core.GasPool).AddGas(message.GasLimit), statedb, vmctx.BlockNumber, txctx.BlockHash, tx, &usedGas, evm, balance, common.Address{}) if err != nil { return nil, fmt.Errorf("tracing failed: %w", err) } diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go index 267487db9d..0653ac872c 100644 --- a/internal/ethapi/simulate.go +++ b/internal/ethapi/simulate.go @@ -247,7 +247,7 @@ func repairLogs(calls []simCallResult, hash common.Hash) { } } -func (sim *simulator) sanitizeCall(call *TransactionArgs, state *state.StateDB, header *types.Header, blockContext vm.BlockContext, gasUsed *uint64) error { +func (sim *simulator) sanitizeCall(call *TransactionArgs, state vm.StateDB, header *types.Header, blockContext vm.BlockContext, gasUsed *uint64) error { if call.Nonce == nil { nonce := state.GetNonce(call.from()) call.Nonce = (*hexutil.Uint64)(&nonce) diff --git a/miner/worker.go b/miner/worker.go index cf2f2604ec..fa10c58dea 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -790,7 +790,7 @@ func (w *worker) commitNewWork() { work.state.DeleteAddress(common.BlockSignersBinary) } if w.config.IsPrague(header.Number) { - core.ProcessParentBlockHash(header.ParentHash, work.evm, work.state) + core.ProcessParentBlockHash(header.ParentHash, work.evm) } // won't grasp txs at checkpoint var ( @@ -1225,7 +1225,7 @@ func (w *Work) commitTransactions(mux *event.TypeMux, balanceFee map[common.Addr func (w *Work) commitTransaction(balanceFee map[common.Address]*big.Int, tx *types.Transaction, gp *core.GasPool) ([]*types.Log, bool, uint64, error) { snap := w.state.Snapshot() - receipt, gas, tokenFeeUsed, err := core.ApplyTransaction(w.config, balanceFee, w.evm, gp, w.state, w.header, tx, &w.header.GasUsed) + receipt, gas, tokenFeeUsed, err := core.ApplyTransaction(balanceFee, w.evm, gp, w.state, w.header, tx, &w.header.GasUsed) if err != nil { w.state.RevertToSnapshot(snap) return nil, false, 0, err