Repository navigation
fix(net): correct sync completion and chain summary request timeouts - #7000
Conversation
…quested.getKey().peekLast()
SeriousCoding789
left a comment
There was a problem hiding this comment.
The fixes here look correct to me: the request snapshot is validated before any state change, the future-height check no longer overflows, negative remainNum is rejected, and the new chain-inventory timeout uses the request's own timestamp.
One gap seems to remain beyond the terminal-response case raised in the review above. Intermediate pages (remainNum > 0) are not covered by the HELLO-head check, and a full page made up only of blocks we already have can pass every check without moving blockBothHave forward; the handler then drains the queue and calls syncNext() again. The underlying reason is that the 30-second progress check in PeerStatusCheck looks at blockBothHaveUpdateTime, which setBlockBothHave() refreshes whether or not the height increased, so activity is counted as progress.
Two possible directions:
- For
remainNum > 0, require the last block of the page to be higher thanpeer.getBlockBothHave().getNum(). An honest page starts from the highest summary block on the peer's main chain and spansSYNC_FETCH_BATCH_NUMblocks, and fork depth is bounded by the solidified height, so an honest page should always end aboveblockBothHave. - More generally, refresh
blockBothHaveUpdateTimeonly when the shared height increases, so any response shape that does not advance synchronization is caught by the existing 30-second check. The othersetBlockBothHave()callers (broadcast handling, serving fetches) would need to be checked against that change.
| public static final long ADV_TIME_OUT = 20000L; | ||
| public static final long SYNC_TIME_OUT = 5000L; | ||
| public static final long SYNC_TIME_OUT = 10000L; | ||
| public static final long NET_MAX_TRX_PER_SECOND = 700L; |
There was a problem hiding this comment.
Please also consider reducing ADV_TIME_OUT. The current 20-second timeout for block and transaction fetch requests following broadcast announcements is rather long, delaying the handling of unresponsive peers and the rescheduling of outstanding requests.
There was a problem hiding this comment.
Thanks for the suggestion. I reduced ADV_TIME_OUT from 20 seconds to 15 seconds in ae2e246. This timeout currently disconnects the peer, so I chose a conservative reduction rather than 10 seconds without broader network latency data. The focused timeout tests pass. We can revisit the threshold, or separate request retry from peer disconnection, once we have production measurements.
| } | ||
| } | ||
|
|
||
| if (chainInventoryMessage.getRemainNum() == 0 && peer.getSyncBlockToFetch().isEmpty()) { |
There was a problem hiding this comment.
Synchronization should not be marked complete here solely because remainNum is zero and the fetch queue is empty. When the remote peer returns multiple blocks, it sets needSyncFromUs=true. We still need to send an updated chain summary through syncNext so that the remote peer can return a single common block and clear that state. Local synchronization can then complete through the existing single-block branch.
Please remove this early-completion branch. Repeated responses containing known blocks from a malicious peer should be handled by separately limiting repeated exchanges that make no progress, without skipping the normal final confirmation exchange. Tests should also cover synchronization state on both peers and verify that subsequent announcement exchange resumes.
There was a problem hiding this comment.
Agreed. I removed the early completion branch in 82feab3. To prevent the previous no-progress loop, the handler now rejects a multi-block terminal response if its terminal block was already included in the requested summary; at that point, a normal peer should return a single block. A regression test covers the repeated-response case.
There was a problem hiding this comment.
The current summary membership check still allows a no-progress loop by alternating between already-known terminal blocks. Each round refreshes the progress timestamp and restarts the request timeout, allowing the loop to continue indefinitely.
For example, with a sync window lower bound of 98, a HELLO head of 99, and local blocks available through height 101:
| Request summary | Peer response (remain=0) |
Next summary |
|---|---|---|
[98,100] |
[98,99] |
[98,99] |
[98,99] |
[99,100] |
[98,100] |
Both rounds satisfy the first-block membership, continuity, and HELLO lower-bound checks, but neither downloads any new blocks.
Processing known blocks refreshes the sync progress timestamp (PeerConnection.java:246), and the subsequent summary request receives a new timestamp (SyncService.java:129). As a result, neither timeout check detects this no-progress loop.
There was a problem hiding this comment.
@3for Thanks for the concrete example. You're right: alternating already-known terminal blocks can refresh the block-progress timestamp and start a new request deadline without making progress. The current check only rejects a terminal block already present in that round's summary.
Preventing this across multiple requests needs separate progress tracking and regression tests. I'll address that in a follow-up rather than expand this PR further. I've updated the PR description to document the remaining limitation.
Keep sync completion handling while applying the release branch block lock to fetch decisions. Retain tests for both behaviors.
3for
left a comment
There was a problem hiding this comment.
LGTM now, as the known issues will be addressed in a separate PR.
Classify repeated terminal inventories as sync failures to avoid bad-protocol bans during fork switches. Rely on the merged HELLO entry gate instead of a duplicate handler check, and cover the reorg disconnect reason.
| BlockId lastBlockId = blockIds.get(blockIds.size() - 1); | ||
| long lastNum = lastBlockId.getNum(); | ||
| long helloHeadNum = hello.getHeadBlockId().getNum(); | ||
| if (lastNum < helloHeadNum) { |
There was a problem hiding this comment.
These terminal-response checks can disconnect an honest peer during a rollback or concurrent fork switch. A HELLO head is a handshake-time snapshot, not a permanent lower bound on the peer's current chain head.
For example, if a peer advertises height 200 and later rolls back to 198, an otherwise valid terminal response ending at 198 is rejected here. Separately, a fork switch while generating the response can produce a multi-block terminal inventory whose last block is already in the request summary. The new testReorgDuringSummaryResponseDisconnectsWithSyncFail explicitly reproduces and asserts that disconnect.
I see that the PR documents this as an accepted tradeoff. Changing the reason to SYNC_FAIL avoids the one-hour bad-protocol ban, but still closes the connection with the default 60-second cooldown; reconnection is not guaranteed immediately afterward. This can delay recovery precisely while peers are resolving a fork, especially when few synchronization peers are available.
Could we handle these recoverable cases with a bounded summary retry/resynchronization path instead of immediately disconnecting? The response's captured head, common ancestor, and block IDs should also come from a consistent chain view. A separate cross-request no-progress budget can bound retries without treating the original HELLO height as immutable. Please add coverage for successful recovery after rollback/concurrent reorganization, in addition to asserting the disconnect reason.
There was a problem hiding this comment.
A normal fork switch in java-tron selects a strictly longer chain. A head change from 200 to 198 would therefore be a transient state during the switch, rather than the resulting stable head. When our node is behind in current sceme, changes near the remote peer’s tip generally do not affect the range we are currently downloading.
Concurrent reads during a fork switch can still produce a response that fails these checks. We now handle this as SYNC_FAILED, avoiding the one-hour protocol-violation ban. The cooldown affects that peer; synchronization can continue through other available peers. Given the multiple sync sources normally available, this does not imply a node-wide recovery delay.
We will retain the current handling to keep this PR focused. Generating responses from a consistent chain view is a useful separate improvement. Bounded retries and a cross-request progress budget would require broader changes.
waynercheung
left a comment
There was a problem hiding this comment.
Re-reviewed at 85517a5. I found no blocking issue in the production changes. LGTM.
[SHOULD] Please open and link a follow-up issue from the Compatibility notes for:
- Cross-request no-progress detection (r4295880341).
- Consistent chain-inventory responses and recovery during concurrent fork switches (r4312910151).
[DISCUSS] #6966 targets the same branch and conflicts in ChainInventoryMsgHandler.java and its test. Please coordinate the merge order, preserve the negative-remain and overflow protections, and rerun the relevant tests after integration.
[NIT] Please refresh the stale #6993 integration note, and change BAD_MESSAGE to SYNC_FAILED in the description of the terminal-summary membership rejection.
Preserve the upstream queue-concurrency regression test and adapt sync test HELLO messages to constructor validation.
Preserve the subtraction-based overflow guard and sync completion checks. Merge release negative-remain validation and retain regression tests from both branches. Validation: clean build, CI Checkstyle, Math usage and diff checks pass. Full tests: 4986 total, 4969 passed, 16 skipped, 1 failed. All 53 sync-related regression tests pass. RelayServiceTest.testStrictHelloAuthentication also fails in isolation because its unchanged HELLO fixture omits required block hashes; the fixture and HelloMessage validator are unchanged from both parents.
Calculate the declared highest block number explicitly and reject overflow or values above the future height limit. Validation: all 26 ChainInventoryMsgHandlerTest cases and framework checkstyleMain pass.
What does this PR do?
Fix chain-summary request timeouts and stale remaining-block state at sync completion, and adjust peer request deadlines.
syncChainRequestedrequests inPeerStatusCheckand disconnect the peer withTIME_OUTwhen its request expires. The deadline uses the original request timestamp; keepalive traffic, other interactions, and block progress do not extend it.SYNC_TIME_OUTfrom 5 to 10 seconds for both chain-summary requests and requested sync blocks. ReduceADV_TIME_OUTfrom 20 to 15 seconds for advertisement inventory requests.remainNumto zero when a validated single-block response identifies a locally known block and completes the local download. Preserve the existingneedSyncFromUsupload state andSYNC_COMPLETEDstate so a later download can restart.Why are these changes required?
Block-progress and interaction timestamps do not provide a deadline for an individual unanswered chain-summary request. Checking the request timestamp closes that gap. A sync fetch can request up to 100 blocks from one peer, so increasing the shared sync timeout gives responsive peers more time to deliver a batch. The shorter advertisement timeout detects overdue inventory requests sooner.
The single-known-block completion path previously left a stale
remainNumfrom an earlier response. Clearing it keeps the remaining-block count consistent with the completed download. Checking the last block height separately also strengthens the future-height validation instead of relying only on the summed declared height.This PR has been tested by:
ChainInventoryMsgHandlerTestcoverage for download completion, preservation of upload state, later sync restarts, unknown queued blocks, pagination, malformed responses rejected before peer-state mutation, and lock-protected fetch decisions.PeerStatusCheckMockTestcoverage for per-peer request expiry, activity that must not extend the deadline, six-second requests remaining active, completed and subsequent requests, and existing inventory/sync-block timeouts.f40e6d6, checked on 2026-10-26: the Ubuntu 24 / JDK 17 / aarch64 build passed; the full PR Build workflow is still in progress. The single-node smoke test, Checkstyle and PR lint, and Math usage check passed.Follow up
Cross-request no-progress protection remains a follow-up: repeated valid responses containing only known blocks can trigger further summary exchanges without downloading new blocks.
Extra details
Completion follows the validated response and local block availability. Valid terminal responses below the peer's HELLO head remain accepted, and a multi-block terminal response may end at a block already present in the requested summary. When all returned blocks are locally known, the existing flow requests another summary so the remote peer can return a single common block and clear its upload-sync state. Unknown queued blocks continue through the fetch path.