feat(net): fix TRX inv rate limit bug and add BLOCK inv rate limit#6731
Open
xxo1shine wants to merge 1 commit intotronprotocol:developfrom
Open
feat(net): fix TRX inv rate limit bug and add BLOCK inv rate limit#6731xxo1shine wants to merge 1 commit intotronprotocol:developfrom
xxo1shine wants to merge 1 commit intotronprotocol:developfrom
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
lvs0075
approved these changes
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Tighten and extend the inbound inventory rate limit on the P2P layer. Two fixes plus a new knob:
Fix the TRX-inv counting bug —
P2pEventHandlerImplpreviously checkedcount > maxCountIn10sagainst the existing 10-second window count only, ignoringthe size of the incoming
InventoryMessage. A peer sending a single message with thousands of hashes could slip through whenever the window count alone was stillunder the limit. The check is now
count + currentSize > maxCountIn10s, which compares the projected window against the cap.Add a BLOCK-inv rate limit — block inventories were previously unbounded. New per-peer cap on
BLOCKinventory hashes per 10s, controlled by:node.maxBlockInvPerSecond(default10, minimum1)NodeConfig.maxBlockInvPerSecond(auto-bound byConfigBeanFactory, clamped inpostProcess())Args.applyNodeConfig→CommonParameter.maxBlockInvPerSecondreference.confRefactor for readability — the inline TRX check inside
processMessagewas extracted intocheckInvRateLimit(PeerConnection, InventoryMessage), which nowhandles both
TRXandBLOCKbranches uniformly.Tests —
P2pEventHandlerImplTestaddstestCheckInvRateLimitTrxBoundaryandtestCheckInvRateLimitBlockBoundaryto cover the at-limit and over-limit casesfor both inventory types.
Why are these changes required?
The original TRX check was a TOCTOU-style undercount. A burst peer could legitimately stay below
countwhile still pushing the system overmaxCountIn10sin a single message — the gating condition simply did not include the new payload. This let an attacker amplify by batching: one message of 10,000 inventory hashes
was indistinguishable from one of 10. The fix restores the intended invariant: projected window size must stay under the cap, not the current window alone.
Unbounded BLOCK-inv was an unmonitored attack surface. TRX inv had a cap; block inv did not. A misbehaving or malicious peer could flood block-inv hashes —
each requiring lookup work on the receiver — without any throttling, consuming I/O and CPU. Adding a bounded
maxBlockInvPerSecondcloses the gap symmetrically,with a default tuned for the protocol's healthy block rate (~3 s interval, so 10/s is generous headroom for retransmits and forks).
Surfacing the limit as a config knob (not a constant) lets operators tune it without a release, which is important the first time a new limit ships in case
the default proves too tight on real networks.
Refactoring out
checkInvRateLimitkeeps the dispatch path inprocessMessageshort and makes the two inventory-type branches uniform — easier to reasonabout, and easier to add a third inventory type later without re-introducing the old undercount bug.
This PR has been tested by:
Follow up
Extra details
Fixes #6659