Skip to content

Optimize receipt canonicality with OnUpdateMainChain batching#10446

Open
benaadams wants to merge 9 commits intomasterfrom
main-chain-update
Open

Optimize receipt canonicality with OnUpdateMainChain batching#10446
benaadams wants to merge 9 commits intomasterfrom
main-chain-update

Conversation

@benaadams
Copy link
Copy Markdown
Member

@benaadams benaadams commented Feb 7, 2026

Changes

  • Use OnUpdateMainChain event for batched tx-index writes instead of per-block EnsureCanonical calls, reducing write batch overhead during multi-block chain updates
  • Fire OnUpdateMainChain before the per-block MoveToMain loop so tx indices are committed before NewCanonicalReceipts notifies JSON-RPC subscribers
  • Extract ShouldIndexBlock and WriteCanonicalTxIndex helpers to share logic between single-block and batch paths
  • Add IDisposable to PersistentReceiptStorage to unsubscribe both event handlers
  • Use PutSpan instead of BytesToArray() for non-compact tx index writes to avoid allocation
  • Add early return in RemoveBlockTx when block has no transactions
  • Simplify BlockTree.UpdateMainChain ascending-order check and DEBUG validation loop

Types of changes

What types of changes does your code introduce?

  • Optimization
  • Refactoring

Testing

Requires testing

  • Yes

If yes, did you write tests?

  • Yes

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes receipt canonicality updates during multi-block main-chain updates by propagating main-chain update batch metadata through BlockTree events and using that metadata to batch transaction-index writes in PersistentReceiptStorage.

Changes:

  • Extend BlockReplacementEventArgs with main-chain update batching metadata (while preserving existing constructor call sites).
  • Propagate batch metadata from BlockTree.UpdateMainChain / MoveToMain into BlockAddedToMain events.
  • Batch canonical tx-index writes in PersistentReceiptStorage across a main-chain update (with bounded flushing and stale/out-of-order event hardening) and add targeted tests.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Nethermind/Nethermind.Core/BlockReplacementEventArgs.cs Adds batch metadata fields and new constructors for compatibility.
src/Nethermind/Nethermind.Core/BlockProcessingConstants.cs Introduces shared constant for bounding uncommitted block processing/batching.
src/Nethermind/Nethermind.Consensus/Processing/BranchProcessor.cs Reuses shared MaxUncommittedBlocks constant instead of a local copy.
src/Nethermind/Nethermind.Blockchain/Receipts/PersistentReceiptStorage.cs Implements canonical tx-index batching keyed by main-chain update metadata; adds bounded flush + stale batch handling.
src/Nethermind/Nethermind.Blockchain/BlockTree.cs Tags BlockAddedToMain events with update-batch metadata during multi-block UpdateMainChain.
src/Nethermind/Nethermind.Blockchain.Test/Receipts/PersistentReceiptStorageTests.cs Adds tests covering commit-on-last behavior, batch-id changes, stale events, and max-batch flush.
src/Nethermind/Nethermind.Blockchain.Test/BlockTreeTests.cs Adds tests validating batch metadata presence for multi-block updates and absence for single-block updates.
.gitignore Ignores /.dotnet_cli.
Comments suppressed due to low confidence (1)

src/Nethermind/Nethermind.Blockchain/BlockTree.cs:978

  • These 'if' statements can be combined.
            if (isBatchUpdate)
            {
                if (blocks[^1].Number < blocks[0].Number)
                {
                    ascendingOrder = false;
                }
            }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Remove ConcurrentDictionary.IsEmpty check in FindBlockHash; TryGetValue
  is lock-free and handles the empty case without acquiring bucket locks.
- Simplify DEBUG gap-check loop by starting at i=1 instead of nested if.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
LukaszRozmej and others added 2 commits February 9, 2026 09:30
Replace per-block batch accumulation in BlockAddedToMain with a single
OnUpdateMainChain handler that writes all tx indices in one WriteBatch.
Remove batch metadata from BlockReplacementEventArgs and MoveToMain.
@benaadams benaadams changed the title Optimize receipt canonicality path for UpdateMainChain Optimize receipt canonicality with OnUpdateMainChain batching Feb 10, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

4 participants