From 1862333296965edeac5d0b47d97f878dd2e3d1a0 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Tue, 8 Oct 2024 20:09:28 +0200 Subject: [PATCH] fix journal cur rev Id --- core/tracing/journal.go | 9 +++++---- core/tracing/journal_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/tracing/journal.go b/core/tracing/journal.go index b5391eb8d0..bc6fb9ccbc 100644 --- a/core/tracing/journal.go +++ b/core/tracing/journal.go @@ -38,7 +38,7 @@ type journal struct { validRevisions []revision nextRevisionId int - curRevisionId int + revIds []int } type entry interface { @@ -194,17 +194,18 @@ func (j *journal) OnTxEnd(receipt *types.Receipt, err error) { } func (j *journal) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { - j.curRevisionId = j.snapshot() + j.revIds = append(j.revIds, j.snapshot()) if j.hooks != nil && j.hooks.OnEnter != nil { j.hooks.OnEnter(depth, typ, from, to, input, gas, value) } } func (j *journal) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) { + revId := j.revIds[len(j.revIds)-1] + j.revIds = j.revIds[:len(j.revIds)-1] if reverted { - j.revertToSnapshot(j.curRevisionId, j.hooks) + j.revertToSnapshot(revId, j.hooks) } - j.curRevisionId-- if j.hooks != nil && j.hooks.OnExit != nil { j.hooks.OnExit(depth, output, gasUsed, err, reverted) } diff --git a/core/tracing/journal_test.go b/core/tracing/journal_test.go index 8681817b17..bcc47f0621 100644 --- a/core/tracing/journal_test.go +++ b/core/tracing/journal_test.go @@ -96,3 +96,31 @@ func TestJournalTopRevert(t *testing.T) { t.Fatalf("unexpected nonce: %v", tr.nonce) } } + +func TestJournalNestedCalls(t *testing.T) { + tr := &testTracer{} + wr, err := WrapWithJournal(&Hooks{OnBalanceChange: tr.OnBalanceChange, OnNonceChange: tr.OnNonceChange}) + if err != nil { + t.Fatalf("failed to wrap test tracer: %v", err) + } + addr := common.HexToAddress("0x1234") + wr.OnEnter(0, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnEnter(1, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnBalanceChange(addr, big.NewInt(0), big.NewInt(100), BalanceChangeUnspecified) + wr.OnEnter(2, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnExit(2, nil, 100, nil, false) + wr.OnEnter(2, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnExit(2, nil, 100, nil, false) + wr.OnEnter(2, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnBalanceChange(addr, big.NewInt(100), big.NewInt(200), BalanceChangeUnspecified) + wr.OnExit(2, nil, 100, nil, false) + wr.OnEnter(2, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnExit(2, nil, 100, errors.New("revert"), true) + wr.OnEnter(2, 0, addr, addr, nil, 1000, big.NewInt(0)) + wr.OnExit(2, nil, 100, errors.New("revert"), true) + wr.OnExit(1, nil, 100, errors.New("revert"), true) + wr.OnExit(0, nil, 150, nil, false) + if tr.bal.Cmp(big.NewInt(0)) != 0 { + t.Fatalf("unexpected balance: %v", tr.bal) + } +}