Skip to content

Commit abaac41

Browse files
authored
fix(net): resume block fetching after pending blocks are processed (#7029)
* fix(net): resume sync fetching after pending blocks drain A fetch attempt consumes fetchFlag even when the in-flight block window is full. Processing buffered blocks then frees capacity without scheduling another fetch, which can stall synchronization until peer timeouts and reconnections. Track capacity released while processing or removing buffered blocks and set fetchFlag after the handling pass. Reuse the existing fetch scheduler, request deduplication, and in-flight budget checks. Add a regression test that fills the window, verifies fetching is throttled, processes the buffered block, and confirms the next block can be requested without another inbound message. Validation: all 6 SyncServiceTest tests passed; framework lint passed using its cached result; git diff --check passed. * fix(test): sort sync regression test imports CI checkstyleTest rejected the PbftDataSyncHandler import because BlockMessage must precede it in lexicographical order. Move the import to the required position without changing test behavior. Validation: framework checkstyleMain and checkstyleTest passed; git diff --check passed.
1 parent 4d658d5 commit abaac41

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎framework/src/main/java/org/tron/core/net/service/sync/SyncService.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ private synchronized void handleSyncBlock() {
287287
}
288288

289289
final boolean[] isProcessed = {true};
290+
final boolean[] capacityReleased = {false};
290291
long solidNum = tronNetDelegate.getSolidBlockId().getNum();
291292

292293
while (isProcessed[0]) {
@@ -298,11 +299,13 @@ private synchronized void handleSyncBlock() {
298299
BlockId blockId = unparsedBlock.getBlockId();
299300
if (peerConnection.isDisconnect()) {
300301
blockWaitToProcess.remove(unparsedBlock);
302+
capacityReleased[0] = true;
301303
invalid(blockId, peerConnection);
302304
return;
303305
}
304306
if (blockId.getNum() <= solidNum) {
305307
blockWaitToProcess.remove(unparsedBlock);
308+
capacityReleased[0] = true;
306309
peerConnection.getSyncBlockInProcess().remove(blockId);
307310
return;
308311
}
@@ -314,6 +317,7 @@ private synchronized void handleSyncBlock() {
314317
});
315318
if (isFound[0]) {
316319
blockWaitToProcess.remove(unparsedBlock);
320+
capacityReleased[0] = true;
317321
isProcessed[0] = true;
318322
BlockCapsule block;
319323
try {
@@ -328,6 +332,10 @@ private synchronized void handleSyncBlock() {
328332
}
329333
});
330334
}
335+
if (capacityReleased[0]) {
336+
// A full window may have consumed the last fetch trigger before processing freed capacity.
337+
fetchFlag = true;
338+
}
331339
}
332340

333341
private void processSyncBlock(BlockCapsule block, PeerConnection peerConnection) {

‎framework/src/test/java/org/tron/core/net/services/SyncServiceTest.java‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.tron.core.net.TronNetDelegate;
2525
import org.tron.core.net.message.adv.BlockMessage;
2626
import org.tron.core.net.message.adv.FetchInvDataMessage;
27+
import org.tron.core.net.messagehandler.PbftDataSyncHandler;
2728
import org.tron.core.net.peer.PeerConnection;
2829
import org.tron.core.net.peer.PeerManager;
2930
import org.tron.core.net.peer.TronState;
@@ -227,6 +228,48 @@ public void testStartFetchSyncBlock() throws Exception {
227228
blockWaitToProcess.clear();
228229
}
229230

231+
@Test
232+
public void testProcessingBlockResumesThrottledFetch() throws Exception {
233+
SyncService sync = new SyncService();
234+
TronNetDelegate delegate = mock(TronNetDelegate.class);
235+
ReflectUtils.setFieldValue(sync, "tronNetDelegate", delegate);
236+
ReflectUtils.setFieldValue(sync, "pbftDataSyncHandler", mock(PbftDataSyncHandler.class));
237+
ReflectUtils.setFieldValue(sync, "maxPendingBlockSize", 1);
238+
PeerConnection syncingPeer = context.getBean(PeerConnection.class);
239+
Channel channel = mock(Channel.class);
240+
Mockito.when(channel.getInetSocketAddress()).thenReturn(inetSocketAddress);
241+
Mockito.when(channel.getInetAddress()).thenReturn(inetSocketAddress.getAddress());
242+
syncingPeer.setChannel(channel);
243+
syncingPeer.setNeedSyncFromPeer(true);
244+
syncingPeer.setFetchAble(true);
245+
Mockito.when(delegate.getActivePeer()).thenReturn(Collections.singletonList(syncingPeer));
246+
Mockito.when(delegate.getSolidBlockId())
247+
.thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 0));
248+
Mockito.when(delegate.getBlockLock()).thenReturn(new Object());
249+
BlockCapsule block = new BlockCapsule(Protocol.Block.newBuilder().setBlockHeader(
250+
Protocol.BlockHeader.newBuilder().setRawData(
251+
Protocol.BlockHeader.raw.newBuilder().setNumber(1))).build());
252+
BlockCapsule.BlockId next = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 2);
253+
syncingPeer.getSyncBlockToFetch().add(block.getBlockId());
254+
syncingPeer.getSyncBlockToFetch().add(next);
255+
syncingPeer.getSyncBlockInProcess().add(block.getBlockId());
256+
Map<UnparsedBlock, PeerConnection> waiting = (Map<UnparsedBlock, PeerConnection>)
257+
ReflectUtils.getFieldObject(sync, "blockWaitToProcess");
258+
waiting.put(new UnparsedBlock(block.getBlockId(), block.getData()), syncingPeer);
259+
Method fetch = SyncService.class.getDeclaredMethod("startFetchSyncBlock");
260+
fetch.setAccessible(true);
261+
fetch.invoke(sync);
262+
Assert.assertTrue(syncingPeer.getSyncBlockRequested().isEmpty());
263+
Method handle = SyncService.class.getDeclaredMethod("handleSyncBlock");
264+
handle.setAccessible(true);
265+
handle.invoke(sync);
266+
Assert.assertTrue("Freeing the window must wake the fetch scheduler",
267+
(boolean) ReflectUtils.getFieldObject(sync, "fetchFlag"));
268+
fetch.invoke(sync);
269+
Assert.assertEquals(Collections.singleton(next), syncingPeer.getSyncBlockRequested().keySet());
270+
sync.close();
271+
}
272+
230273
@Test
231274
public void sameAddressPeersKeepSyncRequestsAndResponsesIndependent() throws Exception {
232275
InetSocketAddress address = new InetSocketAddress("127.0.0.2", 10001);

0 commit comments

Comments
 (0)