From e48242bb69ba9a0148f43a2c3c342f1e1f608574 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 16 Sep 2025 10:09:06 +0200 Subject: [PATCH] cmd/evm/internal/t8ntool: use ApplyTransaction instead of ApplyMessage (#32618) ApplyTransaction calls the hooks and builds the receipt, so some duplicated code can be removed from t8ntool. Test cases have been changed to add the `blockNumber` and `blockHash` in receipts, since they were previously not filled in. --- cmd/evm/internal/t8ntool/execution.go | 58 +++------------------------ cmd/evm/testdata/1/exp.json | 3 +- cmd/evm/testdata/13/exp2.json | 6 ++- cmd/evm/testdata/23/exp.json | 3 +- cmd/evm/testdata/24/exp.json | 6 ++- cmd/evm/testdata/25/exp.json | 3 +- cmd/evm/testdata/28/exp.json | 5 ++- cmd/evm/testdata/29/exp.json | 3 +- cmd/evm/testdata/3/exp.json | 3 +- cmd/evm/testdata/30/exp.json | 6 ++- cmd/evm/testdata/33/exp.json | 3 +- 11 files changed, 33 insertions(+), 66 deletions(-) diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index 7ea6e578cc..f595a9bf4d 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -32,7 +32,6 @@ import ( "github.com/ethereum/go-ethereum/core/tracing" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" @@ -152,7 +151,6 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, gasUsed = uint64(0) blobGasUsed = uint64(0) receipts = make(types.Receipts, 0) - txIndex = 0 ) gaspool.AddGas(pre.Env.GasLimit) vmContext := vm.BlockContext{ @@ -250,24 +248,17 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, continue } } - statedb.SetTxContext(tx.Hash(), txIndex) + statedb.SetTxContext(tx.Hash(), len(receipts)) var ( snapshot = statedb.Snapshot() prevGas = gaspool.Gas() ) - if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxStart != nil { - evm.Config.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From) - } - // (ret []byte, usedGas uint64, failed bool, err error) - msgResult, err := core.ApplyMessage(evm, msg, gaspool) + receipt, err := core.ApplyTransactionWithEVM(msg, gaspool, statedb, vmContext.BlockNumber, blockHash, pre.Env.Timestamp, tx, &gasUsed, evm) if err != nil { statedb.RevertToSnapshot(snapshot) log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err) rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) gaspool.SetGas(prevGas) - if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxEnd != nil { - evm.Config.Tracer.OnTxEnd(nil, err) - } continue } includedTxs = append(includedTxs, tx) @@ -275,50 +266,11 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, return nil, nil, nil, NewError(ErrorMissingBlockhash, hashError) } blobGasUsed += txBlobGas - gasUsed += msgResult.UsedGas - - // Receipt: - { - var root []byte - if chainConfig.IsByzantium(vmContext.BlockNumber) { - statedb.Finalise(true) - } else { - root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes() - } - - // Create a new receipt for the transaction, storing the intermediate root and - // gas used by the tx. - receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed} - if msgResult.Failed() { - receipt.Status = types.ReceiptStatusFailed - } else { - receipt.Status = types.ReceiptStatusSuccessful - } - receipt.TxHash = tx.Hash() - receipt.GasUsed = msgResult.UsedGas - - // If the transaction created a contract, store the creation address in the receipt. - if msg.To == nil { - receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce()) - } - - // Set the receipt logs and create the bloom filter. - receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash, vmContext.Time) - receipt.Bloom = types.CreateBloom(receipt) - - // These three are non-consensus fields: - //receipt.BlockHash - //receipt.BlockNumber - receipt.TransactionIndex = uint(txIndex) - receipts = append(receipts, receipt) - if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxEnd != nil { - evm.Config.Tracer.OnTxEnd(receipt, nil) - } - } - - txIndex++ + receipts = append(receipts, receipt) } + statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)) + // Add mining reward? (-1 means rewards are disabled) if miningReward >= 0 { // Add mining reward. The mining reward may be `0`, which only makes a difference in the cases diff --git a/cmd/evm/testdata/1/exp.json b/cmd/evm/testdata/1/exp.json index 50662f35ea..6537c9517d 100644 --- a/cmd/evm/testdata/1/exp.json +++ b/cmd/evm/testdata/1/exp.json @@ -29,7 +29,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x5208", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" } ], diff --git a/cmd/evm/testdata/13/exp2.json b/cmd/evm/testdata/13/exp2.json index 6415a4f1f4..f716289cf7 100644 --- a/cmd/evm/testdata/13/exp2.json +++ b/cmd/evm/testdata/13/exp2.json @@ -17,7 +17,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x84d0", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" }, { @@ -31,7 +32,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x84d0", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x1" } ], diff --git a/cmd/evm/testdata/23/exp.json b/cmd/evm/testdata/23/exp.json index 7f36165e35..2d9cd492db 100644 --- a/cmd/evm/testdata/23/exp.json +++ b/cmd/evm/testdata/23/exp.json @@ -16,7 +16,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x520b", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x5", "transactionIndex": "0x0" } ], diff --git a/cmd/evm/testdata/24/exp.json b/cmd/evm/testdata/24/exp.json index 8f380c662b..0dd552e112 100644 --- a/cmd/evm/testdata/24/exp.json +++ b/cmd/evm/testdata/24/exp.json @@ -32,7 +32,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0xa861", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" }, { @@ -45,7 +46,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x5aa5", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x1" } ], diff --git a/cmd/evm/testdata/25/exp.json b/cmd/evm/testdata/25/exp.json index a674633762..3dac46aa60 100644 --- a/cmd/evm/testdata/25/exp.json +++ b/cmd/evm/testdata/25/exp.json @@ -28,7 +28,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x5208", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" } ], diff --git a/cmd/evm/testdata/28/exp.json b/cmd/evm/testdata/28/exp.json index b86c2d8def..15b29bc0ac 100644 --- a/cmd/evm/testdata/28/exp.json +++ b/cmd/evm/testdata/28/exp.json @@ -33,7 +33,10 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0xa865", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blobGasUsed": "0x20000", + "blobGasPrice": "0x1", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" } ], diff --git a/cmd/evm/testdata/29/exp.json b/cmd/evm/testdata/29/exp.json index 7fbdc18283..69c8661aa8 100644 --- a/cmd/evm/testdata/29/exp.json +++ b/cmd/evm/testdata/29/exp.json @@ -31,7 +31,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x5208", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" } ], diff --git a/cmd/evm/testdata/3/exp.json b/cmd/evm/testdata/3/exp.json index 831c078591..807cdccfb4 100644 --- a/cmd/evm/testdata/3/exp.json +++ b/cmd/evm/testdata/3/exp.json @@ -29,7 +29,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x521f", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x5", "transactionIndex": "0x0" } ], diff --git a/cmd/evm/testdata/30/exp.json b/cmd/evm/testdata/30/exp.json index a206c3bbdf..9861f5a071 100644 --- a/cmd/evm/testdata/30/exp.json +++ b/cmd/evm/testdata/30/exp.json @@ -30,7 +30,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x5208", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" }, { @@ -44,7 +45,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x5208", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x1" } ], diff --git a/cmd/evm/testdata/33/exp.json b/cmd/evm/testdata/33/exp.json index ae82ef3efa..b40ca9fee2 100644 --- a/cmd/evm/testdata/33/exp.json +++ b/cmd/evm/testdata/33/exp.json @@ -48,7 +48,8 @@ "contractAddress": "0x0000000000000000000000000000000000000000", "gasUsed": "0x15fa9", "effectiveGasPrice": null, - "blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000", + "blockNumber": "0x1", "transactionIndex": "0x0" } ],