Skip to content

perf: Replace node ID by depth in TMLedgerNode#6353

Open
bthomee wants to merge 36 commits intodevelopfrom
bthomee/node_depth
Open

perf: Replace node ID by depth in TMLedgerNode#6353
bthomee wants to merge 36 commits intodevelopfrom
bthomee/node_depth

Conversation

@bthomee
Copy link
Collaborator

@bthomee bthomee commented Feb 11, 2026

High Level Overview of Change

This change introduces a new protocol version 2.3, LedgerNodeDepth, which replaces the 32-byte node ID in the TMLedgerNode proto message by a 2-byte node depth instead (1 byte for the field tag, 1 byte for the depth as it won't exceed 64) for leaf nodes.

Context of Change

An earlier bug allowed a compromised UNL validator to send a maliciously crafted TMLedgerNode message with a node ID that didn't match the actual node ID corresponding to where the leaf node was located in the SHAMap. That bug was fixed by adding an extra check, which reconstructs the node ID from the node data and its depth. Hence, we don't actually need the node ID for leaf nodes, and the much smaller node depth suffices. The extra check is now moved to the places where the TMLedgerNode messages are handled.

The current logic passes the node data around and constructs a SHAMapTreeNode at the end. However, we need this instance earlier to check if a node is a leaf. The function definitions and implementations are updated to take a tree node instead, to avoid reconstructing the instance multiple times.

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)
  • Performance (increase or change in throughput and/or latency)
  • Tests (you added tests for code that already exists, or your new feature included in this PR)
  • Documentation update
  • Chore (no impact to binary, e.g. .gitignore, formatting, dropping support for older tooling)
  • Release

API Impact

  • Public API: New feature (new methods and/or new fields)
  • Public API: Breaking change (in general, breaking changes should only impact the next api_version)
  • libxrpl change (any change that may affect libxrpl or dependents of libxrpl)
  • Peer protocol change (must be backward compatible or bump the peer protocol version)

@bthomee bthomee added the DraftRunCI Normally CI does not run on draft PRs. This opts in. label Feb 11, 2026
@bthomee bthomee marked this pull request as ready for review February 11, 2026 21:12
Copilot AI review requested due to automatic review settings February 11, 2026 21:12
Copy link
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 introduces a new fix amendment (fixLedgerNodeDepth) intended to reduce TMLedgerNode message size by replacing the serialized SHAMapNodeID (nodeid) with a smaller nodedepth field, and it relocates a leaf-position integrity check from SHAMapSync::addKnownNode to the peer-ingress paths.

Changes:

  • Add fixLedgerNodeDepth amendment and extend TMLedgerNode proto with nodedepth (deprecating nodeid).
  • Update ledger-node send/receive paths to populate/consume nodedepth instead of nodeid (gated by amendment table checks).
  • Remove the leaf position mismatch check from SHAMap::addKnownNode (moving validation to message ingress).

Reviewed changes

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

Show a summary per file
File Description
src/xrpld/overlay/detail/PeerImp.cpp Sends nodedepth instead of nodeid in TMLedgerNode replies when the amendment is considered supported.
src/xrpld/app/ledger/detail/InboundTransactions.cpp Validates incoming TMLedgerNode fields and reconstructs node IDs (attempts to use nodedepth).
src/xrpld/app/ledger/detail/InboundLedgers.cpp Applies new field-completeness checks when caching stale fetch-pack data.
src/xrpld/app/ledger/detail/InboundLedger.cpp Validates incoming nodes and reconstructs node IDs (attempts to use nodedepth) before inserting into SHAMaps.
src/libxrpl/shamap/SHAMapSync.cpp Removes the additional leaf-position consistency check inside addKnownNode.
include/xrpl/protocol/detail/features.macro Adds the new fix amendment definition entry.
include/xrpl/proto/xrpl.proto Deprecates nodeid and adds nodedepth to TMLedgerNode.

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

@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 40.54054% with 88 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.8%. Comparing base (afc660a) to head (e90fbbf).

Files with missing lines Patch % Lines
src/xrpld/app/ledger/detail/InboundLedger.cpp 0.0% 36 Missing ⚠️
...rc/xrpld/app/ledger/detail/InboundTransactions.cpp 0.0% 16 Missing ⚠️
src/xrpld/overlay/detail/PeerImp.cpp 0.0% 12 Missing ⚠️
src/libxrpl/shamap/SHAMapSync.cpp 66.7% 10 Missing ⚠️
src/xrpld/app/ledger/detail/InboundLedgers.cpp 0.0% 7 Missing ⚠️
src/xrpld/app/ledger/detail/TransactionAcquire.cpp 0.0% 4 Missing ⚠️
src/xrpld/app/ledger/detail/LedgerNodeHelpers.cpp 92.1% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #6353     +/-   ##
=========================================
- Coverage     79.8%   79.8%   -0.0%     
=========================================
  Files          858     859      +1     
  Lines        67757   67834     +77     
  Branches      7557    7569     +12     
=========================================
+ Hits         54064   54121     +57     
- Misses       13693   13713     +20     
Files with missing lines Coverage Δ
include/xrpl/basics/Slice.h 96.9% <100.0%> (+0.1%) ⬆️
include/xrpl/shamap/SHAMap.h 100.0% <ø> (ø)
src/libxrpl/shamap/SHAMap.cpp 91.4% <100.0%> (ø)
src/libxrpl/shamap/SHAMapNodeID.cpp 95.9% <100.0%> (+18.4%) ⬆️
src/xrpld/app/ledger/InboundLedger.h 66.7% <ø> (ø)
src/xrpld/app/ledger/detail/TransactionAcquire.h 0.0% <ø> (ø)
src/xrpld/overlay/Peer.h 100.0% <ø> (ø)
src/xrpld/overlay/detail/ProtocolVersion.cpp 88.1% <ø> (ø)
src/xrpld/app/ledger/detail/LedgerNodeHelpers.cpp 92.1% <92.1%> (ø)
src/xrpld/app/ledger/detail/TransactionAcquire.cpp 0.0% <0.0%> (ø)
... and 5 more

... and 7 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bthomee bthomee marked this pull request as draft February 11, 2026 22:17
Copy link
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 7 out of 7 changed files in this pull request and generated 3 comments.


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

Copy link
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 8 out of 8 changed files in this pull request and generated 6 comments.


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

Copy link
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 10 out of 10 changed files in this pull request and generated 6 comments.


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

Copy link
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 16 out of 16 changed files in this pull request and generated 7 comments.


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

Copy link
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 20 out of 20 changed files in this pull request and generated 4 comments.


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

Copy link
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 20 out of 20 changed files in this pull request and generated 3 comments.


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

takeNodes(
std::vector<std::pair<SHAMapNodeID, Slice>> const& data,
std::shared_ptr<Peer> const&);
std::vector<std::pair<SHAMapNodeID, intr_ptr::SharedPtr<SHAMapTreeNode>>>&& data,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can be an impactful and measurable optimisation. It would be useful to benchmark it. If the black-box test does not pick an improvement, then we should do CPU profiling of the takeNodes stack.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can use your help with the latter, if the performance benchmark doesn't show much difference. The run I started this morning doesn't contain the r-value change yet, so I'll start another one when it's done but then with the r-value change.

Copilot AI review requested due to automatic review settings March 2, 2026 16:59
Copy link
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 21 out of 21 changed files in this pull request and generated 3 comments.


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

Comment on lines +579 to 583
if (childHash != treeNode->getHash())
{
JLOG(journal_.warn()) << "Corrupt node received";
return SHAMapAddNode::invalid();
}
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

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

SHAMap::addKnownNode no longer verifies that a received leaf node’s key is consistent with the nodeID position (the extra post-hash validation that prevents “leaf at wrong position” inconsistencies). Since addKnownNode is a public sync entry-point, keeping this validation here provides defense-in-depth and avoids relying on every caller to remember this security-sensitive check; consider reintroducing the leaf position/key consistency validation (or an equivalent invariant check) before canonicalizing/inserting the node.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All code paths leading to SHAMap::addKnownNode use data that has been validated, i.e. the ledger node proto message is valid, the data field correctly converts into a tree node, and the nodeid/depth field correctly converts into a node ID. As such, this is not an issue.

Copilot AI review requested due to automatic review settings March 2, 2026 17:17
Copy link
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 21 out of 21 changed files in this pull request and generated 1 comment.


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

Copy link
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 21 out of 21 changed files in this pull request and generated no new comments.


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

@bthomee bthomee requested a review from vlntb March 2, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

QE test required RippleX QE Team must look at this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants