From 5d62b65674907b4dfa1a02ba74fd1a185982a735 Mon Sep 17 00:00:00 2001 From: jesteban <129153821+joanestebanr@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:06:41 +0100 Subject: [PATCH] test: proxy: cover finality after a bridge re-inclusion and a tracked tx reorged out for good (#1972) Audit of the reorg scenarios of the aggkit-proxy (agglayer/pm#445). The four scenarios were already covered in unit and e2e tests; this fills what was partial: - unit: TestEngineOriginReorgedOutNeverReincluded, a tracked tx removed by a reorg and never mined again gets a fresh UnresolvedTimeout and then ends in the terminal error, with nothing left of the resolved bridge. - unit: TestActivityReorg_BridgeReincludedBecomesFinal (memory and SQLite), a bridge re-included in another block becomes final with its new block, as a single entry and without changing the revision. - e2e: scenario A2 also waits for the re-included bridge to become final. Refs agglayer/pm#445 Co-Authored-By: Claude Fable 5.2 --- .../db/sqlite_activity_reorg_test.go | 34 +++++++++++ bridgetracker/engine_reorg_test.go | 56 +++++++++++++++++++ test/e2e/README.md | 2 +- test/e2e/proxy_reorg_activity_test.go | 17 ++++++ 4 files changed, 108 insertions(+), 1 deletion(-) diff --git a/bridgetracker/db/sqlite_activity_reorg_test.go b/bridgetracker/db/sqlite_activity_reorg_test.go index 7cca48b33..e21df7398 100644 --- a/bridgetracker/db/sqlite_activity_reorg_test.go +++ b/bridgetracker/db/sqlite_activity_reorg_test.go @@ -216,6 +216,40 @@ func TestActivityReorg_BridgeReincludedInAnotherBlock(t *testing.T) { }) } +// TestActivityReorg_BridgeReincludedBecomesFinal: the finality of a bridge re-included in another +// block is the one of its new block. The entry is not final while that block is not, becomes +// final with it (still a single entry, at the new location), and that last step alone does not +// change the revision +func TestActivityReorg_BridgeReincludedBecomesFinal(t *testing.T) { + runOnBothStores(t, func(h *activityHarness) { + t := h.t + h.scan([]*domain.ScannedBridge{reorgBridge(1, 100, "0xb1", false)}) + h.unclaimed(false) + entries := h.refresh() + require.False(t, entries[0].Finalized) + + h.scan([]*domain.ScannedBridge{reorgBridge(1, 200, "0xb2", false)}) + h.unclaimed(false) + entries = h.refresh() + require.Len(t, entries, 1) + require.Equal(t, uint64(200), entries[0].Bridge.BlockNum) + require.False(t, entries[0].Finalized, "the new block is not final either") + + h.scan([]*domain.ScannedBridge{reorgBridge(1, 200, "0xb2", true)}) + h.unclaimed(false) + entries = h.refresh() + require.Len(t, entries, 1, "no duplicate left behind at the old block") + require.Equal(t, uint64(200), entries[0].Bridge.BlockNum) + require.Equal(t, bridgeservicetypes.Hash("0xb2"), entries[0].Bridge.TxHash) + require.True(t, entries[0].Finalized, "final once the block it was re-included in is final") + require.Equal(t, types.TrackerClaimStatusPending, entries[0].TrackerClaimStatus) + + require.Len(t, h.revisions, 3) + require.NotEqual(t, h.revisions[0], h.revisions[1], "the correction changes the revision") + require.Equal(t, h.revisions[1], h.revisions[2], "becoming final does not") + }) +} + // TestActivityReorg_BridgeReplacedBySameGlobalIndex: a different bridge taking the same deposit // count (so global index) replaces the entry's data func TestActivityReorg_BridgeReplacedBySameGlobalIndex(t *testing.T) { diff --git a/bridgetracker/engine_reorg_test.go b/bridgetracker/engine_reorg_test.go index b61c396a0..00eaace7a 100644 --- a/bridgetracker/engine_reorg_test.go +++ b/bridgetracker/engine_reorg_test.go @@ -427,6 +427,62 @@ func TestEngineOriginReorgedOut(t *testing.T) { require.Equal(t, types.TrackerClaimStatusReadyToClaim, tracking.ClaimStatus()) } +// TestEngineOriginReorgedOutNeverReincluded pins what the tracker reports for a tracked tx that a +// reorg removes for good: the tracking goes back to registered with a fresh UnresolvedTimeout +// (counted from the reorg, not from when the tx was first tracked) and, once that elapses without +// the tx being mined again, ends in the terminal error of a tx that does not exist, with nothing +// left of the bridge that had been resolved +func TestEngineOriginReorgedOutNeverReincluded(t *testing.T) { + id := TrackingID{NetworkID: 0, TxHash: testHash} + f := &fakeSources{bridge: l1ToL2ReorgBridge()} + facts := &reorgFacts{} + store := newMemoryRegistry(0) + engine, clock := newReorgTestEngine(t, store, f, facts) + updates, unsubscribe, err := store.Subscribe(id) + require.NoError(t, err) + defer unsubscribe() + + driveToReadyToClaim(f) + engine.tick(t.Context()) + tracking := receiveUpdate(t, updates) + require.Equal(t, types.TrackerClaimStatusReadyToClaim, tracking.ClaimStatus()) + + // tracked for longer than UnresolvedTimeout before the reorg: the timeout of the first + // resolution must not count against the tx once it is reorged out + *clock = clock.Add(3 * engine.cfg.UnresolvedTimeout) + facts.set(func(r *reorgFacts) { r.canonical = common.HexToHash("0xa2") }) + f.bridge, f.bridgeErr = nil, ErrBridgeTxNotFound + engine.tick(t.Context()) + tracking = receiveUpdate(t, updates) + require.Equal(t, types.TrackingStatusRegistered, tracking.TrackingStatus(), "a fresh UnresolvedTimeout starts at the reorg") + require.Equal(t, types.TrackerClaimStatusPending, tracking.ClaimStatus()) + require.Nil(t, tracking.Info()) + require.Nil(t, tracking.AllSteps()) + require.False(t, tracking.Failed()) + require.Len(t, mustGetTrackerActives(t, store), 1) + + // still missing just before the timeout: still looked for + *clock = clock.Add(engine.cfg.UnresolvedTimeout - time.Second) + engine.tick(t.Context()) + tracking = mustGetFrom(t, store, id) + require.Equal(t, types.TrackingStatusRegistered, tracking.TrackingStatus()) + require.False(t, tracking.Failed()) + + // never re-included: given up on, like any tx that does not exist on the network + *clock = clock.Add(time.Second) + engine.tick(t.Context()) + tracking = mustGetFrom(t, store, id) + require.Equal(t, types.TrackingStatusError, tracking.TrackingStatus()) + require.Equal(t, types.TrackerClaimStatusError, tracking.ClaimStatus()) + require.Nil(t, tracking.Info(), "the bridge resolved before the reorg must not come back") + require.Nil(t, tracking.StepIndex()) + require.Nil(t, tracking.AllSteps()) + require.True(t, tracking.Failed()) + require.NotNil(t, tracking.Error()) + require.Equal(t, types.StepErrorExhausted, tracking.Error().ErrorType) + require.Empty(t, mustGetTrackerActives(t, store), "a tx given up on leaves the active list") +} + // TestEngineOriginReincludedInAnotherBlockSameTick pins the common case: the reorged-out bridge // tx is already re-mined when the reorg is seen, so it moves to its new block in the same tick func TestEngineOriginReincludedInAnotherBlockSameTick(t *testing.T) { diff --git a/test/e2e/README.md b/test/e2e/README.md index 21a9d3d38..1ed513660 100644 --- a/test/e2e/README.md +++ b/test/e2e/README.md @@ -23,7 +23,7 @@ Every scenario goes in the same order, so a failure points at the right componen | Test (CI group) | Scenarios | | --- | --- | -| `TestProxyReorgActivity` (`proxy-reorg-activity`) | A1 bridge from network 1 reorged out; A2 same bridge re-included in another block; A3 bridge replaced by another one with the same deposit count; A4 L1 -> L2 bridge reorged out of L1; A5 bridge of network 2 seen only through the RPC fallback, reorged out; A6 final bridge, shallower reorg | +| `TestProxyReorgActivity` (`proxy-reorg-activity`) | A1 bridge from network 1 reorged out; A2 same bridge re-included in another block, then final there; A3 bridge replaced by another one with the same deposit count; A4 L1 -> L2 bridge reorged out of L1; A5 bridge of network 2 seen only through the RPC fallback, reorged out; A6 final bridge, shallower reorg | | `TestProxyReorgClaims` (`proxy-reorg-claims`) | B1 claim reorged out, claimed again, final; B2 reorg removes the claim and the GER injection; B3 final claim stays `claimed_finalized` and is not rechecked; B4 claim re-included in another block; C1 reorg removes only the GER injection | | `TestProxyReorgTracker` (`proxy-reorg-tracker-revision`) | D1 origin transaction moves and then disappears while tracked; D2 `Claimed` goes back to `WaitingClaim` and a WebSocket subscriber is told, then the connection closes once the claim is final | | `TestProxyReorgRevision` (`proxy-reorg-tracker-revision`) | E1 `revision` unchanged by refreshes and by a not-final -> final transition; E2 and E3 a claim on page 2 reorged out while a client paginates; E4 same `revision` on `aggkit-proxy-001` and `-002`, and after a restart | diff --git a/test/e2e/proxy_reorg_activity_test.go b/test/e2e/proxy_reorg_activity_test.go index 585d19823..ab226eb37 100644 --- a/test/e2e/proxy_reorg_activity_test.go +++ b/test/e2e/proxy_reorg_activity_test.go @@ -71,6 +71,23 @@ func TestProxyReorgActivity(t *testing.T) { require.Equal(t, s.item.Bridge.GlobalIndex, item.Bridge.GlobalIndex) require.False(t, item.isFinalized(), "the new block is not final either") require.NotEqual(t, s.before.Revision, after.Revision, "a bridge moved to another block must change the revision") + + // finality is the one of the new block: once that block is final the same single entry is final, + // and that alone does not change the revision. + // NOTE: an unclaimed bridge in a final block reads finalized=true (open decision (b) of #1986: the + // assertion follows the current behaviour) + _, err := l2.MineUntilFinalized(ctx, moved.BlockNumber.Uint64()) + require.NoError(t, err) + requireBridgeServed(ctx, t, s.bs, s.networkID, s.depositCount, moved, true) + final := s.waitForActivity(ctx, t, "the bridge is final at its new block", + func(a *proxyActivityResponse) bool { + item := a.find(s.networkID, s.depositCount) + return itemMatchesReceipt(item, moved) && item.isFinalized() + }) + require.Equal(t, 1, final.Count, "a corrected bridge must stay a single entry: %s", final) + require.Equal(t, claimStatusPending, final.find(s.networkID, s.depositCount).ClaimStatus, + "with the aggsender held the bridge cannot become claimable") + require.Equal(t, after.Revision, final.Revision, "going from not final to final must not change the revision") }) t.Run("A3 bridge replaced by a different one with the same deposit count", func(t *testing.T) {