Skip to content

Commit e48242b

Browse files
authored
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.
1 parent b05fe4a commit e48242b

File tree

11 files changed

+33
-66
lines changed

11 files changed

+33
-66
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/ethereum/go-ethereum/core/tracing"
3333
"github.com/ethereum/go-ethereum/core/types"
3434
"github.com/ethereum/go-ethereum/core/vm"
35-
"github.com/ethereum/go-ethereum/crypto"
3635
"github.com/ethereum/go-ethereum/ethdb"
3736
"github.com/ethereum/go-ethereum/log"
3837
"github.com/ethereum/go-ethereum/params"
@@ -152,7 +151,6 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
152151
gasUsed = uint64(0)
153152
blobGasUsed = uint64(0)
154153
receipts = make(types.Receipts, 0)
155-
txIndex = 0
156154
)
157155
gaspool.AddGas(pre.Env.GasLimit)
158156
vmContext := vm.BlockContext{
@@ -250,75 +248,29 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
250248
continue
251249
}
252250
}
253-
statedb.SetTxContext(tx.Hash(), txIndex)
251+
statedb.SetTxContext(tx.Hash(), len(receipts))
254252
var (
255253
snapshot = statedb.Snapshot()
256254
prevGas = gaspool.Gas()
257255
)
258-
if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxStart != nil {
259-
evm.Config.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
260-
}
261-
// (ret []byte, usedGas uint64, failed bool, err error)
262-
msgResult, err := core.ApplyMessage(evm, msg, gaspool)
256+
receipt, err := core.ApplyTransactionWithEVM(msg, gaspool, statedb, vmContext.BlockNumber, blockHash, pre.Env.Timestamp, tx, &gasUsed, evm)
263257
if err != nil {
264258
statedb.RevertToSnapshot(snapshot)
265259
log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err)
266260
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
267261
gaspool.SetGas(prevGas)
268-
if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxEnd != nil {
269-
evm.Config.Tracer.OnTxEnd(nil, err)
270-
}
271262
continue
272263
}
273264
includedTxs = append(includedTxs, tx)
274265
if hashError != nil {
275266
return nil, nil, nil, NewError(ErrorMissingBlockhash, hashError)
276267
}
277268
blobGasUsed += txBlobGas
278-
gasUsed += msgResult.UsedGas
279-
280-
// Receipt:
281-
{
282-
var root []byte
283-
if chainConfig.IsByzantium(vmContext.BlockNumber) {
284-
statedb.Finalise(true)
285-
} else {
286-
root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes()
287-
}
288-
289-
// Create a new receipt for the transaction, storing the intermediate root and
290-
// gas used by the tx.
291-
receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed}
292-
if msgResult.Failed() {
293-
receipt.Status = types.ReceiptStatusFailed
294-
} else {
295-
receipt.Status = types.ReceiptStatusSuccessful
296-
}
297-
receipt.TxHash = tx.Hash()
298-
receipt.GasUsed = msgResult.UsedGas
299-
300-
// If the transaction created a contract, store the creation address in the receipt.
301-
if msg.To == nil {
302-
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
303-
}
304-
305-
// Set the receipt logs and create the bloom filter.
306-
receipt.Logs = statedb.GetLogs(tx.Hash(), vmContext.BlockNumber.Uint64(), blockHash, vmContext.Time)
307-
receipt.Bloom = types.CreateBloom(receipt)
308-
309-
// These three are non-consensus fields:
310-
//receipt.BlockHash
311-
//receipt.BlockNumber
312-
receipt.TransactionIndex = uint(txIndex)
313-
receipts = append(receipts, receipt)
314-
if evm.Config.Tracer != nil && evm.Config.Tracer.OnTxEnd != nil {
315-
evm.Config.Tracer.OnTxEnd(receipt, nil)
316-
}
317-
}
318-
319-
txIndex++
269+
receipts = append(receipts, receipt)
320270
}
271+
321272
statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber))
273+
322274
// Add mining reward? (-1 means rewards are disabled)
323275
if miningReward >= 0 {
324276
// Add mining reward. The mining reward may be `0`, which only makes a difference in the cases

cmd/evm/testdata/1/exp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"contractAddress": "0x0000000000000000000000000000000000000000",
3030
"gasUsed": "0x5208",
3131
"effectiveGasPrice": null,
32-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
32+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
33+
"blockNumber": "0x1",
3334
"transactionIndex": "0x0"
3435
}
3536
],

cmd/evm/testdata/13/exp2.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"contractAddress": "0x0000000000000000000000000000000000000000",
1818
"gasUsed": "0x84d0",
1919
"effectiveGasPrice": null,
20-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
20+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
21+
"blockNumber": "0x1",
2122
"transactionIndex": "0x0"
2223
},
2324
{
@@ -31,7 +32,8 @@
3132
"contractAddress": "0x0000000000000000000000000000000000000000",
3233
"gasUsed": "0x84d0",
3334
"effectiveGasPrice": null,
34-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
35+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
36+
"blockNumber": "0x1",
3537
"transactionIndex": "0x1"
3638
}
3739
],

cmd/evm/testdata/23/exp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"contractAddress": "0x0000000000000000000000000000000000000000",
1717
"gasUsed": "0x520b",
1818
"effectiveGasPrice": null,
19-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
19+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
20+
"blockNumber": "0x5",
2021
"transactionIndex": "0x0"
2122
}
2223
],

cmd/evm/testdata/24/exp.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"contractAddress": "0x0000000000000000000000000000000000000000",
3333
"gasUsed": "0xa861",
3434
"effectiveGasPrice": null,
35-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
35+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
36+
"blockNumber": "0x1",
3637
"transactionIndex": "0x0"
3738
},
3839
{
@@ -45,7 +46,8 @@
4546
"contractAddress": "0x0000000000000000000000000000000000000000",
4647
"gasUsed": "0x5aa5",
4748
"effectiveGasPrice": null,
48-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
49+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
50+
"blockNumber": "0x1",
4951
"transactionIndex": "0x1"
5052
}
5153
],

cmd/evm/testdata/25/exp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"contractAddress": "0x0000000000000000000000000000000000000000",
2929
"gasUsed": "0x5208",
3030
"effectiveGasPrice": null,
31-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
31+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
32+
"blockNumber": "0x1",
3233
"transactionIndex": "0x0"
3334
}
3435
],

cmd/evm/testdata/28/exp.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
"contractAddress": "0x0000000000000000000000000000000000000000",
3434
"gasUsed": "0xa865",
3535
"effectiveGasPrice": null,
36-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
36+
"blobGasUsed": "0x20000",
37+
"blobGasPrice": "0x1",
38+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
39+
"blockNumber": "0x1",
3740
"transactionIndex": "0x0"
3841
}
3942
],

cmd/evm/testdata/29/exp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"contractAddress": "0x0000000000000000000000000000000000000000",
3232
"gasUsed": "0x5208",
3333
"effectiveGasPrice": null,
34-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
34+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
35+
"blockNumber": "0x1",
3536
"transactionIndex": "0x0"
3637
}
3738
],

cmd/evm/testdata/3/exp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"contractAddress": "0x0000000000000000000000000000000000000000",
3030
"gasUsed": "0x521f",
3131
"effectiveGasPrice": null,
32-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
32+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
33+
"blockNumber": "0x5",
3334
"transactionIndex": "0x0"
3435
}
3536
],

cmd/evm/testdata/30/exp.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"contractAddress": "0x0000000000000000000000000000000000000000",
3131
"gasUsed": "0x5208",
3232
"effectiveGasPrice": null,
33-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
33+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
34+
"blockNumber": "0x1",
3435
"transactionIndex": "0x0"
3536
},
3637
{
@@ -44,7 +45,8 @@
4445
"contractAddress": "0x0000000000000000000000000000000000000000",
4546
"gasUsed": "0x5208",
4647
"effectiveGasPrice": null,
47-
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
48+
"blockHash": "0x1337000000000000000000000000000000000000000000000000000000000000",
49+
"blockNumber": "0x1",
4850
"transactionIndex": "0x1"
4951
}
5052
],

0 commit comments

Comments
 (0)