Skip to content

fix(net): harden sync message resource controls - #6966

Open
xxo1shine wants to merge 1 commit into
tronprotocol:release_v4.8.3from
xxo1shine:fix/net-sync-message-validation
Open

xxo1shine wants to merge 1 commit into
tronprotocol:release_v4.8.3from
xxo1shine:fix/net-sync-message-validation

Conversation

@xxo1shine

Copy link
Copy Markdown
Collaborator

What does this PR do?
Peer-derived sync state could bypass height validation, request rate limiting, and block-fetch deduplication at boundary conditions.

Reject invalid remain counts and overflowed heights, apply sync-chain rate limiting to every request, and size the block ID cache to cover the full valid fetch window.

Fixes #6942

Why are these changes required?

This PR has been tested by:

  • Unit Tests
  • Manual Testing

Follow up

Extra details

@github-actions
github-actions Bot requested a review from 317787106 September 11, 2026 07:27
@halibobo1205 halibobo1205 added the topic:net p2p net work, synchronization label Sep 14, 2026
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.8.3 milestone Sep 14, 2026
@xxo1shine
xxo1shine changed the base branch from develop to release_v4.8.3 September 14, 2026 03:25

@lxcmyf lxcmyf 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.

[MUST] This review found two blocking issues; see the inline comments.

if (lastNum + msg.getRemainNum() > maxFutureNum) {
throw new P2pException(TypeEnum.BAD_MESSAGE, "lastNum: " + lastNum + " + remainNum: "
+ msg.getRemainNum() + " > futureMaxNum: " + maxFutureNum);
long declaredHighestNum = lastNum + msg.getRemainNum();

@lxcmyf lxcmyf Sep 17, 2026

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.

[MUST] The overflow check is still inside the headBlockId > 0 branch. At height 0, a message containing genesis plus continuous heights 1..1999 and remainNum = Long.MAX_VALUE skips the check, so the original overflow bypass remains. Always perform a checked addition or overflow check; only the futureMaxNum bound may remain conditional. Add a height=0 test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This change addresses addition overflow that makes the upper-bound comparison ineffective when height validation is enabled. When headBlockId <= 0, this validation was already skipped as part of the existing initial-sync behavior; it is not an overflow bypass introduced by this PR. We will preserve that branch behavior in this change. Whether initial sync should have an independent check on the declared highest block number can be evaluated separately.

@Getter
private Cache<Sha256Hash, Long> syncBlockIdCache = CacheBuilder.newBuilder()
.maximumSize(2 * NetConstants.SYNC_FETCH_BATCH_NUM).recordStats().build();
.maximumSize(2 * NetConstants.SYNC_FETCH_BATCH_NUM + 1).recordStats().build();

@lxcmyf lxcmyf Sep 17, 2026

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.

[MUST] Raising maximumSize to 4001 still does not guarantee that all IDs in the active window remain cached: entries survive across moving windows, and Guava segmented eviction can evict entries before the total size reaches the limit. An evicted block still inside [last-4000,last] can be requested again. Maintain an exact height-aware window and test all 4001 IDs plus advancing, out-of-order windows.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The early eviction caused by segmentation is a valid issue. I will add concurrencyLevel(1), retain the capacity of 4001, and add tests covering retention of every ID within a fixed window, out-of-order requests, and rejection of duplicate requests. The goal of this change is to prevent repeated fetching within a fixed window caused by premature cache eviction. Strict deduplication across advancing windows is outside the scope of this change. This path also has request rate limits and a per-request block count limit, so we will not introduce exact height-aware window management at this stage.

@xxo1shine
xxo1shine force-pushed the fix/net-sync-message-validation branch from d4807d7 to 4ba5db7 Compare September 18, 2026 06:56
Reject negative remain counts and overflowed declared heights when sync height validation is enabled, and apply sync-chain rate limiting to every request.

Use a single-segment block ID cache with capacity for the full 4001-height fetch window. Cover complete-window retention, out-of-order requests, and duplicate-request rejection with regression tests.
@xxo1shine
xxo1shine force-pushed the fix/net-sync-message-validation branch from 4ba5db7 to cd6f6e7 Compare September 18, 2026 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:net p2p net work, synchronization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden sync message validation and resource controls

3 participants