Skip to content

fix(chainlib): a batch mixing a latest read with block 0 asks about latest, not block 0 (MAG-4346) - #593

Merged
AnnaR-prog merged 2 commits into
mainfrom
fix/MAG-4346-batch-block-zero
Nov 3, 2026
Merged

AnnaR-prog merged 2 commits into
mainfrom
fix/MAG-4346-batch-block-zero

Conversation

@avitenzer

Copy link
Copy Markdown
Contributor

Description

A JSON-RPC batch that mixes a latest read with a block 0x0 read reported requested block 0, so the cache filed it as finalized block 0 and served its latest answer for the finalized lifetime.

Jira ticket: MAG-4346

Independent of the other open tickets in this batch; it can merge in any order.

Summary

CompareRequestedBlockInBatch's two mixed-sign branches tested currentLatest > 0 and parsedBlock > 0, so block 0 fell through to Max. Every latest-style tag is a negative sentinel, and Max(LATEST, 0) is 0 — in either element order. With 0x64 in place of 0x0 the same batch reports LATEST.

The cache then filed the batch as finalized block 0, which lives for the finalized lifetime rather than the temporary one, so the batch's latest balance was served long after it had changed. The same latest read sent alone is filed under the tip and expires with the temporary TTL.

Block 0 is a numeric block like any other on this side, so both branches now test >= 0. The one-line patch is Anna Rayer's, attached to the ticket.

The earliest side is deliberately unchanged, and it is worth knowing why rather than reading it as an oversight: this function's earliest return is discarded by its only production caller (requestedBlockFold.add folds earliest itself), and the JSON-RPC batch path maps an element at block 0 to EARLIEST_BLOCK before that fold. So block 0 never reaches the earliest fold as 0 from a batch. The new table rows assert the function's own earliest values, with a comment saying they are not what a batch comes out as.

Relationship to MAG-4650 and MAG-4381

Same family, same file: a batch cached under a block that does not cover all its calls. Those two change whether a block-less element takes part in the fold — MAG-4381 made "latest + none" cacheable, MAG-4650 is the regression that produced for an element whose answer changes. Both are Anna's and neither is in this change, which touches only the mixed-sign comparison. The two sets do not overlap in code, but they do overlap in intent, so whoever lands those should read this one first.

Test plan

  • TestJsonRpcBatchMixingLatestWithBlockZeroIsNotCachedAsFinalized — Anna's attached test, which writes the batch through tryCacheWrite against a real cache server, then checks it expires with the temporary TTL and that nothing is filed under finalized block 0. Renamed from the attached file's hyphenated name to the package convention.
  • TestCompareRequestedBlockInBatch — Anna's four rows, plus the pending, safe and finalized tags in both element orders, and block 0 beside an element that failed to parse (which must still leave the whole request unjudgeable).
  • Ten of those rows and the cache test fail on main, verified with go test -overlay against origin/main's copy of common.go.
  • go test -count=1 on protocol/chainlib (6s), protocol/rpcsmartrouter (253s), protocol/relaycore and protocol/relaypolicy on the committed tree: all ok. protocol/chainlib also passes under -race on the committed tree.
  • go build -buildvcs=false ./... and go vet on the committed tree.

CI runs no unit tests on this repo, so the local runs above are the only test evidence.

🤖 Generated with Claude Code

…atest, not block 0 (MAG-4346)

CompareRequestedBlockInBatch's two mixed-sign branches tested
currentLatest > 0 and parsedBlock > 0, so block 0 fell through to Max.
Every "latest"-style tag is a negative sentinel, and Max(LATEST, 0) is 0:
a batch of eth_getBalance(a, "latest") with eth_getBalance(a, "0x0")
reported requested block 0, in either order. With 0x64 in place of 0x0
the same batch reports LATEST.

The cache then filed the batch as finalized block 0, which lives for the
finalized lifetime rather than the temporary one, so the batch's "latest"
balance was served long after it changed. The same latest read sent alone
is filed under the tip and expires with the temporary TTL.

Block 0 is a numeric block like any other on this side, so both branches
now test >= 0. The patch is Anna Rayer's, attached to the ticket.

The earliest side is deliberately unchanged. This function's earliest
return is discarded by its only production caller —
requestedBlockFold.add folds earliest itself — and the JSON-RPC batch
path maps an element at block 0 to EARLIEST_BLOCK before that fold, so
block 0 never reaches it as 0 from a batch.

Tests: Anna's TestJsonRpcBatchMixingLatestWithBlockZeroIsNotCachedAsFinalized,
which writes the batch through tryCacheWrite against a real cache server
and checks it expires with the temporary TTL and is not filed under
finalized block 0. Her four TestCompareRequestedBlockInBatch rows, plus
the pending, safe and finalized tags in both orders and block 0 beside an
element that failed to parse. Ten rows and the cache test fail on main.

Same family as MAG-4650 and MAG-4381, which change the same fold for a
block-less element: a batch cached under a block that does not cover all
its calls. Those two are Anna's and are not in this change; this one
touches only the mixed-sign comparison, not whether an element takes part
in the fold.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@avitenzer
avitenzer requested a review from a team as a code owner October 28, 2026 15:46

@AnnaR-prog AnnaR-prog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed at 269059e. I tested it merged with current main 7c1102e.

The change is correct.

  • I diffed main's CompareRequestedBlockInBatch against this one over every pair of NA, LATEST, EARLIEST, PENDING, SAFE, FINALIZED, 0, 1 and 100. Exactly ten pairs change: block 0 with NA, LATEST, PENDING, SAFE or FINALIZED, in both orders. Every pair with EARLIEST is unchanged, and [NA, 0] now gives NA, as [NA, 100] already did.
  • The only production caller is requestedBlockFold.add. It drops this function's earliest return, and CombineEarliestRequestedBlock is not touched, so the earliest block and the archive decision do not change. On the ETH1 fixture, [latest, 0x0], [0x0, latest] and [0x0, pending|safe|finalized] still go to archive.
  • The change also reaches Tendermint batches: [block "0", abci_info] was (0, LATEST) and is now (LATEST, LATEST), so that batch is no longer filed as finalized block 0 either. Batches of 0x0 with pending, safe or finalized are now not cached at all (the unresolved-tag guard skips them), where main cached them as finalized block 0.
  • With main's common.go overlaid, ten table rows and TestJsonRpcBatchMixingLatestWithBlockZeroIsNotCachedAsFinalized fail. They pass on this head, also with -race. go test ./protocol/chainlib/... and the batch/cache tests in protocol/rpcsmartrouter pass on the merged tree, and lint reports nothing new.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@AnnaR-prog AnnaR-prog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving at 517cb63: main d765031 plus this PR. go build ./..., go test ./protocol/chainlib/ and the batch and cache-write tests in protocol/rpcsmartrouter and protocol/relaycore pass on this exact tree.

@AnnaR-prog
AnnaR-prog merged commit b875f86 into main Nov 3, 2026
@AnnaR-prog
AnnaR-prog deleted the fix/MAG-4346-batch-block-zero branch November 3, 2026 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants