From 30824faf90bf5d1f5e4a1cf0b50de80f10d16490 Mon Sep 17 00:00:00 2001 From: Martin HS Date: Wed, 21 Aug 2024 15:16:08 +0200 Subject: [PATCH] eth/tracers: avoid panic in state test runner (#30332) Make tracers more robust by handling `nil` receipt as input. Also pass in a receipt with gas used in the state test runner. Closes https://github.com/ethereum/go-ethereum/issues/30117. --------- Co-authored-by: Sina Mahmoodi --- eth/tracers/js/goja.go | 4 +++- eth/tracers/logger/logger.go | 4 +++- eth/tracers/native/call.go | 4 +++- tests/state_test_util.go | 14 ++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/eth/tracers/js/goja.go b/eth/tracers/js/goja.go index fd8f74fc54..b823ef740a 100644 --- a/eth/tracers/js/goja.go +++ b/eth/tracers/js/goja.go @@ -275,7 +275,9 @@ func (t *jsTracer) OnTxEnd(receipt *types.Receipt, err error) { } return } - t.ctx["gasUsed"] = t.vm.ToValue(receipt.GasUsed) + if receipt != nil { + t.ctx["gasUsed"] = t.vm.ToValue(receipt.GasUsed) + } } // onStart implements the Tracer interface to initialize the tracing operation. diff --git a/eth/tracers/logger/logger.go b/eth/tracers/logger/logger.go index ef1d471466..7638c3cd09 100644 --- a/eth/tracers/logger/logger.go +++ b/eth/tracers/logger/logger.go @@ -268,7 +268,9 @@ func (l *StructLogger) OnTxEnd(receipt *types.Receipt, err error) { } return } - l.usedGas = receipt.GasUsed + if receipt != nil { + l.usedGas = receipt.GasUsed + } } // StructLogs returns the captured log entries. diff --git a/eth/tracers/native/call.go b/eth/tracers/native/call.go index 2b84ecaf40..1b94dd7b67 100644 --- a/eth/tracers/native/call.go +++ b/eth/tracers/native/call.go @@ -225,7 +225,9 @@ func (t *callTracer) OnTxEnd(receipt *types.Receipt, err error) { if err != nil { return } - t.callstack[0].GasUsed = receipt.GasUsed + if receipt != nil { + t.callstack[0].GasUsed = receipt.GasUsed + } if t.config.WithLog { // Logs are not emitted when the call fails clearFailedLogs(&t.callstack[0], false) diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 416bab9472..2bf0056cbc 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -297,19 +297,17 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh if tracer := vmconfig.Tracer; tracer != nil && tracer.OnTxStart != nil { tracer.OnTxStart(evm.GetVMContext(), nil, msg.From) - if evm.Config.Tracer.OnTxEnd != nil { - defer func() { - evm.Config.Tracer.OnTxEnd(nil, err) - }() - } } // Execute the message. snapshot := st.StateDB.Snapshot() gaspool := new(core.GasPool) gaspool.AddGas(block.GasLimit()) - _, err = core.ApplyMessage(evm, msg, gaspool) + vmRet, err := core.ApplyMessage(evm, msg, gaspool) if err != nil { st.StateDB.RevertToSnapshot(snapshot) + if tracer := evm.Config.Tracer; tracer != nil && tracer.OnTxEnd != nil { + evm.Config.Tracer.OnTxEnd(nil, err) + } } // Add 0-value mining reward. This only makes a difference in the cases // where @@ -320,6 +318,10 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, snapsh // Commit state mutations into database. root, _ = st.StateDB.Commit(block.NumberU64(), config.IsEIP158(block.Number())) + if tracer := evm.Config.Tracer; tracer != nil && tracer.OnTxEnd != nil { + receipt := &types.Receipt{GasUsed: vmRet.UsedGas} + tracer.OnTxEnd(receipt, nil) + } return st, root, err }