Skip to content

fix: ignore validations for ledgers outside the connected network - #1395

Merged
cybele-ripple merged 6 commits into
mainfrom
extra-ledger-feed-rows
Nov 25, 2026
Merged

cybele-ripple merged 6 commits into
mainfrom
extra-ledger-feed-rows

Conversation

@cybele-ripple

@cybele-ripple cybele-ripple commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

High Level Overview of Change

The ledger feed treats every message on the validations stream as belonging to the connected network, so validations for ledgers of other networks are rendered as ledger rows. This PR anchors the feed on the ledger index the connected node itself reports and ignores validations whose index falls outside a window around it.

  • StreamsProvider now tracks the connected node's latest ledger index (currentLedgerRef), updated only from node-authoritative sources: the ledger stream and our own ledger responses.
  • onValidation ignores validations outside [current - MAX_LEDGERS_SHOWN, current + MAX_LEDGER_LOOKAHEAD].
  • Ledger entries added before the anchor was known are pruned once it arrives.
  • processValidationQueue skips a validation whose ledger is no longer in state instead of dereferencing it.
  • New test file covering each of these branches.

Context of Change

On devnet.xrpl.org the feed interleaves three unrelated index ranges - devnet's own ~7.03M alongside ~22.4M and ~108M rows. The foreign rows have no close time, transaction count or UNL count, because nothing but a validation ever backs them, and they sort above this network's own rows in the feed.

The input really is mixed: subscribing to s.devnet.rippletest.net for 40s returned 1,535 validations, of which only 91 were in devnet's range; the rest were in the 22.4M and 108M ranges. This is not specific to one node - the same capture against s1.ripple.com showed devnet- and testnet-range validations on mainnet's stream too. The node-side cause is being followed up separately with the server team; this PR makes the Explorer resilient to it regardless of what reaches the stream.

A client-side check is needed because a node cannot label these for us. Validations carry no network identifier, so a node cannot distinguish one relayed from another network from one signed by a validator it simply doesn't know. The network_id field on a validationReceived message is the serving node's own id - NetworkOPsImp::pubValidation populates it from getNetworkIDService().getNetworkID() - so it is identical for every message on a connection and cannot be used as a filter. Worth stating explicitly, since it is the obvious thing to reach for.

The guard this replaces derived a floor from the first index observed, so it depended on stream arrival order and did not bound the index range in both directions. Anchoring on the connected node instead makes the accepted range explicit and independent of what arrives first.

processValidationQueue is included because it is reachable through the same path: it dereferenced ledger.hashes before its own if (ledger) guard, and that entry may already have been removed by truncation between a validation being queued and the queue being processed. The lookup is now checked before use.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Translation Updates
  • Release

Codebase Modernization

  • Updated files to React Hooks
  • Updated files to TypeScript

StreamsProvider.tsx is already a hooks-based TypeScript component; no conversion was needed. The new test file follows the existing LedgersPage.test.js harness, which is JS.

Before / After

Before - devnet.xrpl.org, three index ranges in one feed:

108393429  TOTAL: 1
 22401817  TOTAL: 1
 22401816  TOTAL: 1
  7027378  9:26:42 AM  TXN COUNT: 8  UNL: 5/5
  7027377  9:26:41 AM  TXN COUNT: 6  UNL: 5/5

After - same branch run against s.devnet.rippletest.net, observed for 25s: 14 distinct ledgers, 7035106 -> 7035119, no gaps, nothing outside devnet's range, every row with a close time and UNL 5/5.

Test Plan

New: src/containers/Ledgers/test/ForeignValidations.test.js - 5 cases, one per new branch:

  1. an out-of-range validation adds no row
  2. an out-of-range validation arriving before any node ledger does not displace this network's feed
  3. a sustained series of out-of-range validations leaves this network's rows intact
  4. a validation slightly ahead of the last closed ledger is still accepted (the lookahead boundary)
  5. a validation with a non-numeric ledger index is dropped

All 5 pass. 4 of the 5 fail with the fix reverted and the tests kept - case 4 passes either way, as expected, since it asserts existing behaviour is preserved.

Also run:

  • jest --ci full suite: 1905 tests. 40 failures in LedgersPage.test.js and amendmentUtils.test.ts, pre-existing and identical with the change reverted on the same base commit (82d08478). The LedgersPage one is container.querySelector('.ledger .hash .missed') returning null at line 174, which looks like markup drift rather than anything in this change.
  • eslint --max-warnings 0 and prettier --check on both files: clean.
  • tsc --noEmit: clean.
  • Manual run against live devnet, as described under Before / After.

Future Tasks

  • The pre-existing LedgersPage.test.js failures are worth a separate look; untouched here.
  • Optional defence in depth: drop validations whose validation_public_key is not in the per-network validator list VHSValidatorsProvider already fetches. Deliberately not the primary filter - it is unavailable until that request resolves and would hide a genuinely new validator on the network.

🤖 Generated with Claude Code

The `validations` stream carries untrusted validations, and a node cannot
tell one relayed from another network apart from one signed by a validator
it does not know: validations carry no network identifier, and the
`network_id` on a stream message is the serving node's own id, identical
for every message on a connection.

StreamsProvider added a ledger row for every validation it received, so
indexes from other networks appeared in the feed. The only guard was a
floor taken from the first index seen, which discarded lower foreign
indexes but nothing above them, and depended on stream arrival order.

Anchor on the ledger index the connected node reports, from the `ledger`
stream and from our own `ledger` requests, and ignore validations outside
a window around it. Entries added before the anchor was known are pruned
once it arrives. A validation for a ledger no longer in state is now
skipped rather than dereferenced, which previously threw inside the
setLedgers updater and stopped the feed rendering.

@ripple-code-reviewer ripple-code-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 1 non-blocking issue — see inline.

`Processing ledger data: ${JSON.stringify(data)}`,
'StreamsProvider.onLedger',
)
currentLedgerRef.current = Math.max(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anchor advances in stream without pruning. Apply pruneOutOfWindowLedgers with isNewAnchor check:

    const isNewAnchor = Number(data.ledger_index) > currentLedgerRef.current
    currentLedgerRef.current = Math.max(
      currentLedgerRef.current,
      Number(data.ledger_index),
    )
    setLedgers((previousLedgers) => {
      const retained = isNewAnchor
        ? pruneOutOfWindowLedgers(previousLedgers, currentLedgerRef.current)
        : previousLedgers
      if (!retained[data.ledger_index]) {
        return { ...retained, [data.ledger_index]: {} }
      }
      const newLedger = Object.assign(retained[data.ledger_index], {
        closeTime: convertRippleDate(data.ledger_time),
        txCount: data.transaction_count,
      })
      return { ...retained, [data.ledger_index]: newLedger }
    })

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added so onLedger now computes isNewAnchor and calls pruneOutOfWindowLedgers before merging in the new entry here

@ripple-code-reviewer ripple-code-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found.

@ripple-code-reviewer ripple-code-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found.

🧭 Merge confidence: 90% — no-regression estimate

  • ⚠️ Large diff (-2) — 337 changed lines.

  • ⚠️ Inconclusive: Replacing addLedger in onLedger with an inline setLedgers that prunes and inserts changes the ledger count or ordering that LedgersPage tests and updateMetrics rely on. (-5)

    • Evidence: The inline insert in onLedger builds the same entry shape as addLedger and truncates with truncateLedgers(retained, MAX_LEDGERS_SHOWN - 1). The test at LedgersPage.test.js:154 expects 2 .ledger rows after only prevLedgerMessage. I could not trace where the second row comes from, since that needs the ledger-response path and a test run. That path now also prunes and advances the anchor.
Checked and refuted (2)
  • ✅ Checked: Removing firstLedgerRef and the old floor check breaks other readers of that ref or other code paths that call addLedger.

    • Evidence: grep_repo for firstLedgerRef across src returns no matches, so nothing else uses it. addLedger in StreamsProvider.tsx (lines 138-153) is now a plain add-if-missing helper. onValidation still calls it. onLedger now does its own setLedgers insert.
  • ✅ Checked: The new window check in onValidation drops validations that existing tests expect to render, such as the Network validators test, whose mock validation has index 44921927 and is unrelated to any ledger message.

    • Evidence: isLedgerIndexInWindow returns true when currentLedgerRef is 0, which is the case before any ledger message arrives. src/containers/Network/test/validators.test.js sends only mockValidation.json (ledger_index 44921927). I found no ledger message in that test's mocks, only mockNodes.json data, so the anchor stays 0 and the validation is accepted. In LedgersPage.test.js (lines 150-161) the validation index 68992561 is within 50 below / 10 above the prevLedger anchor 68992560, so it is accepted too. I did not trace the Network test's socket mock to confirm it never returns a ledger response that sets the anchor.

From reading the code only: no tests, lint or typecheck ran here, and CI is not counted.

@pdp2121
pdp2121 self-requested a review November 24, 2026 18:24

@pdp2121 pdp2121 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anchoring on the connected node's ledger index makes sense and the ledger list looks right in the common case. A few things inline: one gap where out-of-range validations still reach validators, some edge cases the new window introduces, and a hole in the test coverage.

Two things I noticed that are not from this PR and are fine as follow-ups:

  • currentLedgerRef only ever moves up, so if the node's ledger index goes down while the page is open (devnet reset, restarted custom network) the feed stays on the old rows until reload. firstLedgerRef had the same limitation.
  • onLedger is registered once from the first render ([] deps), so it reads first-render values: updateMetrics() computes over an empty ledgers, and !quorum is always true so nUNL and quorum are refetched on every ledger close rather than only on flag ledgers.

Comment on lines +339 to +343
if (!ledger) {
// The ledger was truncated out of state, or pruned as out of range,
// between this validation being queued and the queue being processed.
return
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This skips the ledger update, but the setValidators call just below still runs over the whole queue. A validation accepted before the anchor was known (isLedgerIndexInWindow returns true while current is 0) therefore still ends up in validators, and nothing removes it afterwards. On the Validators page those keys stay in the table, hexagons and count until reload.

Suggest filtering the queue once when draining it, with isLedgerIndexInWindow(Number(validation.ledger_index), currentLedgerRef.current), and using the filtered list for both setLedgers and setValidators.

That alone doesn't cover a queue drained before the anchor arrives (the first ledger response can take longer than the 200ms throttle). For that case, either hold the queue until currentLedgerRef.current is set, or drop out-of-window entries from validators when the anchor is first set.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filtered the queue against isLedgerIndexInWindow() before it feeds both setLedgers and setValidators, so a validation that was accepted before the anchor was known gets dropped here once the anchor has moved past. Change here

Comment on lines +54 to +57
return (
index >= current - MAX_LEDGERS_SHOWN &&
index <= current + MAX_LEDGER_LOOKAHEAD
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the connected node's reported ledger is more than 10 behind the validations it relays (a node that is still catching up, or one whose ledger stream lags)?

Every current validation is dropped here and never replayed, so when the node later reports those ledgers the rows have no hashes and never show as validated. Before this change, anything at or above the first index seen was accepted.

The other ranges in the PR description are millions of ledgers away, so a much larger lookahead would still filter them. Alternatively, buffer ahead-of-window validations for a bounded time and apply them when the anchor advances.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up updating MAX_LEDGER_LOOKAHEAD to 50. Open to buffer and replay method if that is preferred.

Comment on lines 316 to 318
if (!ledgersRef.current[index]) {
addLedger(index)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the firstLedgerRef floor gone, a late validation for any index in [current - 50, current) that has no row creates one here. Only onLedger fetches ledger contents, so that row never gets a close time, transaction count or fees.

Example: fresh load anchored at 68992561, then a validation for 68992530 arrives. The feed gets a 68992530 row with no close time that stays until it scrolls out.

Related edge: the lower bound admits current - 50 while only 50 rows are kept, and addLedger inserts on top of truncateLedgers(previous, 49). With 50 populated rows, a validation for current - 50 evicts the populated current - 49 row and replaces it with an empty one.

One change covers both: once the anchor is known, let validations create rows only for indexes above currentLedgerRef.current, and for indexes at or below it only attach to a row that already exists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onValidation now only calls addLedger when the anchor is unknown or the index is above it.

Comment on lines +201 to +205
const isNewAnchor = Number(data.ledger_index) > currentLedgerRef.current
currentLedgerRef.current = Math.max(
currentLedgerRef.current,
Number(data.ledger_index),
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data.ledger_index is ever missing or non-numeric, Math.max(..., NaN) makes the ref NaN. From then on !current is true in isLedgerIndexInWindow, so every validation is accepted again, and no later Math.max can bring the ref back. It also adds a row keyed undefined.

populateFromLedgerResponse already guards against this. An early return here when !Number.isFinite(Number(data.ledger_index)), before touching the ref or state, would match it. Unlikely input, but the filter would switch off silently for the rest of the session.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the same guard populateFromLedgerResponse already has. onLedger now has an early return and logs an error when data.ledger_index isn't finite before updating currentLedgerRef here

Comment on lines +360 to 363
ledger.hashes = [...ledger.hashes]
ledger.hashes[matchingHashIndex] = {
...matchingHash,
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, and not new in this PR (these lines were only re-indented): when the hash is new, matchingHashIndex is still -1 from the findIndex above, so this assigns to hashes[-1] (a stray property on the array) rather than replacing the element that was just pushed. Using the pushed element's index (ledger.hashes.length - 1) in that case would fix it. Fine to leave for a follow-up.

@cybele-ripple cybele-ripple Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in this PR: hashIndex now falls back to ledger.hashes.length - 1 when matchingHashIndex is -1, so the just-pushed hash is the one updated

expect(renderedIndexes(container)).not.toContain('999999999')
})

it('still shows this network once an out-of-range validation arrives first', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pruning added to onLedger isn't exercised by these cases. I forced isNewAnchor to false there and all 5 still pass. The prune in populateFromLedgerResponse is covered: disabling that one as well makes this case fail.

A case where the ledger stream is the first thing to set the anchor (initial ledger request rejected or delayed) would cover it.

It would also be worth asserting on validators, since nothing here checks that out-of-range keys stay out of it (see my comment on processValidationQueue).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added new ValidatorsProbe component that reads useStreams() and renders validator keys for assertions. Added new tests for "prunes an out-of-range ledger when the ledger stream sets the anchor first" and "does not leak an out-of-range validation into validators once the anchor is known" in this file

- drop queued validations once they fall outside the window, instead of
  letting them leak into `validators` unfiltered
- raise MAX_LEDGER_LOOKAHEAD so a catching-up node's validations aren't
  dropped outright
- only let a validation create a new ledger row above the known anchor
- guard onLedger against a non-numeric ledger_index poisoning the anchor
- fix a stray hashes[-1] write when a new hash has no matching index
- add regression tests for the onLedger-level prune and the validators leak

@ripple-code-reviewer ripple-code-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found.

@ripple-code-reviewer ripple-code-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found.

@cybele-ripple
cybele-ripple merged commit 5532e4c into main Nov 25, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants