mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-24 08:49:29 +00:00
eth/tracers: fix standardTraceBlockToFile (#31763)
Fixes methods debug_standardTraceBlockToFile and debug_standardTraceBadBlockToFile which were outputting empty files. --------- Co-authored-by: maskpp <maskpp266@gmail.com> Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
This commit is contained in:
parent
b135da2eac
commit
7705d13ed4
2 changed files with 150 additions and 30 deletions
|
|
@ -778,6 +778,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
|
||||||
// Note: This copies the config, to not screw up the main config
|
// Note: This copies the config, to not screw up the main config
|
||||||
chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
|
chainConfig, canon = overrideConfig(chainConfig, config.Overrides)
|
||||||
}
|
}
|
||||||
|
|
||||||
evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
|
evm := vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{})
|
||||||
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
|
if beaconRoot := block.BeaconRoot(); beaconRoot != nil {
|
||||||
core.ProcessBeaconBlockRoot(*beaconRoot, evm)
|
core.ProcessBeaconBlockRoot(*beaconRoot, evm)
|
||||||
|
|
@ -787,42 +788,45 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
|
||||||
}
|
}
|
||||||
for i, tx := range block.Transactions() {
|
for i, tx := range block.Transactions() {
|
||||||
// Prepare the transaction for un-traced execution
|
// Prepare the transaction for un-traced execution
|
||||||
var (
|
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
|
||||||
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee())
|
if txHash != (common.Hash{}) && tx.Hash() != txHash {
|
||||||
vmConf vm.Config
|
// Process the tx to update state, but don't trace it.
|
||||||
dump *os.File
|
_, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
|
||||||
writer *bufio.Writer
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
// If the transaction needs tracing, swap out the configs
|
|
||||||
if tx.Hash() == txHash || txHash == (common.Hash{}) {
|
|
||||||
// Generate a unique temporary file to dump it into
|
|
||||||
prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
|
|
||||||
if !canon {
|
|
||||||
prefix = fmt.Sprintf("%valt-", prefix)
|
|
||||||
}
|
|
||||||
dump, err = os.CreateTemp(os.TempDir(), prefix)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return dumps, err
|
||||||
}
|
|
||||||
dumps = append(dumps, dump.Name())
|
|
||||||
|
|
||||||
// Swap out the noop logger to the standard tracer
|
|
||||||
writer = bufio.NewWriter(dump)
|
|
||||||
vmConf = vm.Config{
|
|
||||||
Tracer: logger.NewJSONLogger(&logConfig, writer),
|
|
||||||
EnablePreimageRecording: true,
|
|
||||||
}
|
}
|
||||||
|
// Finalize the state so any modifications are written to the trie
|
||||||
|
// Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect
|
||||||
|
statedb.Finalise(evm.ChainConfig().IsEIP158(block.Number()))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
// The transaction should be traced.
|
||||||
|
// Generate a unique temporary file to dump it into.
|
||||||
|
prefix := fmt.Sprintf("block_%#x-%d-%#x-", block.Hash().Bytes()[:4], i, tx.Hash().Bytes()[:4])
|
||||||
|
if !canon {
|
||||||
|
prefix = fmt.Sprintf("%valt-", prefix)
|
||||||
|
}
|
||||||
|
var dump *os.File
|
||||||
|
dump, err := os.CreateTemp(os.TempDir(), prefix)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dumps = append(dumps, dump.Name())
|
||||||
|
// Set up the tracer and EVM for the transaction.
|
||||||
|
var (
|
||||||
|
writer = bufio.NewWriter(dump)
|
||||||
|
tracer = logger.NewJSONLogger(&logConfig, writer)
|
||||||
|
evm = vm.NewEVM(vmctx, statedb, chainConfig, vm.Config{
|
||||||
|
Tracer: tracer,
|
||||||
|
NoBaseFee: true,
|
||||||
|
})
|
||||||
|
)
|
||||||
// Execute the transaction and flush any traces to disk
|
// Execute the transaction and flush any traces to disk
|
||||||
statedb.SetTxContext(tx.Hash(), i)
|
statedb.SetTxContext(tx.Hash(), i)
|
||||||
if vmConf.Tracer.OnTxStart != nil {
|
if tracer.OnTxStart != nil {
|
||||||
vmConf.Tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
|
tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
|
||||||
}
|
|
||||||
vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
|
|
||||||
if vmConf.Tracer.OnTxEnd != nil {
|
|
||||||
vmConf.Tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, err)
|
|
||||||
}
|
}
|
||||||
|
_, err = core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(msg.GasLimit))
|
||||||
if writer != nil {
|
if writer != nil {
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"slices"
|
"slices"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
@ -1218,3 +1219,118 @@ func TestTraceBlockWithBasefee(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStandardTraceBlockToFile(t *testing.T) {
|
||||||
|
var (
|
||||||
|
// A sender who makes transactions, has some funds
|
||||||
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
|
address = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
|
funds = big.NewInt(1000000000000000)
|
||||||
|
|
||||||
|
// first contract the sender transacts with
|
||||||
|
aa = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f43")
|
||||||
|
aaCode = []byte{byte(vm.PUSH1), 0x00, byte(vm.POP)}
|
||||||
|
|
||||||
|
// second contract the sender transacts with
|
||||||
|
bb = common.HexToAddress("0x7217d81b76bdd8707601e959454e3d776aee5f44")
|
||||||
|
bbCode = []byte{byte(vm.PUSH2), 0x00, 0x01, byte(vm.POP)}
|
||||||
|
)
|
||||||
|
|
||||||
|
genesis := &core.Genesis{
|
||||||
|
Config: params.TestChainConfig,
|
||||||
|
Alloc: types.GenesisAlloc{
|
||||||
|
address: {Balance: funds},
|
||||||
|
aa: {
|
||||||
|
Code: aaCode,
|
||||||
|
Nonce: 1,
|
||||||
|
Balance: big.NewInt(0),
|
||||||
|
},
|
||||||
|
bb: {
|
||||||
|
Code: bbCode,
|
||||||
|
Nonce: 1,
|
||||||
|
Balance: big.NewInt(0),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
txHashs := make([]common.Hash, 0, 2)
|
||||||
|
backend := newTestBackend(t, 1, genesis, func(i int, b *core.BlockGen) {
|
||||||
|
b.SetCoinbase(common.Address{1})
|
||||||
|
// first tx to aa
|
||||||
|
tx, _ := types.SignTx(types.NewTx(&types.LegacyTx{
|
||||||
|
Nonce: 0,
|
||||||
|
To: &aa,
|
||||||
|
Value: big.NewInt(0),
|
||||||
|
Gas: 50000,
|
||||||
|
GasPrice: b.BaseFee(),
|
||||||
|
Data: nil,
|
||||||
|
}), types.HomesteadSigner{}, key)
|
||||||
|
b.AddTx(tx)
|
||||||
|
txHashs = append(txHashs, tx.Hash())
|
||||||
|
// second tx to bb
|
||||||
|
tx, _ = types.SignTx(types.NewTx(&types.LegacyTx{
|
||||||
|
Nonce: 1,
|
||||||
|
To: &bb,
|
||||||
|
Value: big.NewInt(1),
|
||||||
|
Gas: 100000,
|
||||||
|
GasPrice: b.BaseFee(),
|
||||||
|
Data: nil,
|
||||||
|
}), types.HomesteadSigner{}, key)
|
||||||
|
b.AddTx(tx)
|
||||||
|
txHashs = append(txHashs, tx.Hash())
|
||||||
|
})
|
||||||
|
defer backend.chain.Stop()
|
||||||
|
|
||||||
|
var testSuite = []struct {
|
||||||
|
blockNumber rpc.BlockNumber
|
||||||
|
config *StdTraceConfig
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// test that all traces in the block were outputted if no trace config is specified
|
||||||
|
blockNumber: rpc.LatestBlockNumber,
|
||||||
|
config: nil,
|
||||||
|
want: []string{
|
||||||
|
`{"pc":0,"op":96,"gas":"0x7148","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH1"}
|
||||||
|
{"pc":2,"op":80,"gas":"0x7145","gasCost":"0x2","memSize":0,"stack":["0x0"],"depth":1,"refund":0,"opName":"POP"}
|
||||||
|
{"pc":3,"op":0,"gas":"0x7143","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
|
||||||
|
{"output":"","gasUsed":"0x5"}
|
||||||
|
`,
|
||||||
|
`{"pc":0,"op":97,"gas":"0x13498","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH2"}
|
||||||
|
{"pc":3,"op":80,"gas":"0x13495","gasCost":"0x2","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"POP"}
|
||||||
|
{"pc":4,"op":0,"gas":"0x13493","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
|
||||||
|
{"output":"","gasUsed":"0x5"}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// test that only a specific tx is traced if specified
|
||||||
|
blockNumber: rpc.LatestBlockNumber,
|
||||||
|
config: &StdTraceConfig{TxHash: txHashs[1]},
|
||||||
|
want: []string{
|
||||||
|
`{"pc":0,"op":97,"gas":"0x13498","gasCost":"0x3","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"PUSH2"}
|
||||||
|
{"pc":3,"op":80,"gas":"0x13495","gasCost":"0x2","memSize":0,"stack":["0x1"],"depth":1,"refund":0,"opName":"POP"}
|
||||||
|
{"pc":4,"op":0,"gas":"0x13493","gasCost":"0x0","memSize":0,"stack":[],"depth":1,"refund":0,"opName":"STOP"}
|
||||||
|
{"output":"","gasUsed":"0x5"}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
api := NewAPI(backend)
|
||||||
|
for i, tc := range testSuite {
|
||||||
|
block, _ := api.blockByNumber(context.Background(), tc.blockNumber)
|
||||||
|
txTraces, err := api.StandardTraceBlockToFile(context.Background(), block.Hash(), tc.config)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("test index %d received error %v", i, err)
|
||||||
|
}
|
||||||
|
for j, traceFileName := range txTraces {
|
||||||
|
traceReceived, err := os.ReadFile(traceFileName)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not read trace file: %v", err)
|
||||||
|
}
|
||||||
|
if tc.want[j] != string(traceReceived) {
|
||||||
|
t.Fatalf("unexpected trace result. expected\n'%s'\n\nreceived\n'%s'\n", tc.want[j], string(traceReceived))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue