Skip to content

Commit 78b8632

Browse files
committed
fix(net): release sync reservations added after disconnect
Recheck peer disconnection before sending sync fetch requests and conditionally remove reservations written after disconnect cleanup. Add a deterministic regression proving a surviving peer can retry the block.
1 parent ffdac33 commit 78b8632

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ private void startFetchSyncBlock() {
275275
});
276276

277277
send.forEach((peer, blockIds) -> {
278+
if (peer.isDisconnect()) {
279+
// Disconnect cleanup may have finished before this pass wrote its reservations.
280+
blockIds.forEach(blockId -> invalid(blockId, peer));
281+
return;
282+
}
278283
if (!blockIds.isEmpty()) {
279284
peer.sendMessage(new FetchInvDataMessage(new LinkedList<>(blockIds), InventoryType.BLOCK));
280285
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,65 @@ public void sendMessage(Message message) {
290290
}
291291
}
292292

293+
@Test
294+
public void testDisconnectBeforeReservationReleasesRequestForRetry() throws Exception {
295+
SyncService sync = new SyncService();
296+
try {
297+
TronNetDelegate delegate = mock(TronNetDelegate.class);
298+
ReflectUtils.setFieldValue(sync, "tronNetDelegate", delegate);
299+
PeerConnection closing = Mockito.spy(new PeerConnection());
300+
PeerConnection survivor = Mockito.spy(new PeerConnection());
301+
Channel channel = new Channel();
302+
ReflectUtils.setFieldValue(channel, "inetSocketAddress", inetSocketAddress);
303+
ReflectUtils.setFieldValue(channel, "inetAddress", inetSocketAddress.getAddress());
304+
closing.setChannel(channel);
305+
Channel otherChannel = mock(Channel.class);
306+
InetSocketAddress otherAddress = new InetSocketAddress("127.0.0.3", 10001);
307+
Mockito.when(otherChannel.getInetSocketAddress()).thenReturn(otherAddress);
308+
Mockito.when(otherChannel.getInetAddress()).thenReturn(otherAddress.getAddress());
309+
survivor.setChannel(otherChannel);
310+
closing.setFetchAble(true);
311+
survivor.setFetchAble(true);
312+
Mockito.doNothing().when(closing).sendMessage(Mockito.any(Message.class));
313+
Mockito.doNothing().when(survivor).sendMessage(Mockito.any(Message.class));
314+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1);
315+
closing.getSyncBlockToFetch().add(blockId);
316+
survivor.getSyncBlockToFetch().add(blockId);
317+
Mockito.when(delegate.getActivePeer()).thenReturn(Arrays.asList(closing, survivor));
318+
Cache<BlockCapsule.BlockId, PeerConnection> requests = Mockito.spy(
319+
(Cache<BlockCapsule.BlockId, PeerConnection>)
320+
ReflectUtils.getFieldObject(sync, "requestBlockIds"));
321+
ReflectUtils.setFieldValue(sync, "requestBlockIds", requests);
322+
Mockito.doAnswer(call -> {
323+
// Finish disconnect cleanup after selection but before the reservation is written.
324+
channel.markDisconnected();
325+
Mockito.when(delegate.getActivePeer()).thenReturn(Collections.singletonList(survivor));
326+
sync.onDisconnect(closing);
327+
closing.getSyncBlockToFetch().clear();
328+
closing.getSyncBlockRequested().clear();
329+
Assert.assertNull(requests.getIfPresent(blockId));
330+
return call.callRealMethod();
331+
}).when(requests).put(blockId, closing);
332+
333+
ReflectUtils.invokeMethod(sync, "startFetchSyncBlock");
334+
335+
Assert.assertTrue(closing.isDisconnect());
336+
Assert.assertNull(requests.getIfPresent(blockId));
337+
Assert.assertFalse(requests.asMap().containsValue(closing));
338+
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(sync, "fetchFlag"));
339+
Mockito.verify(closing, Mockito.never()).sendMessage(Mockito.any(Message.class));
340+
Mockito.verify(survivor, Mockito.never()).sendMessage(Mockito.any(Message.class));
341+
342+
ReflectUtils.invokeMethod(sync, "startFetchSyncBlock");
343+
344+
Assert.assertSame(survivor, requests.getIfPresent(blockId));
345+
Assert.assertTrue(survivor.getSyncBlockRequested().containsKey(blockId));
346+
Mockito.verify(survivor).sendMessage(Mockito.any(FetchInvDataMessage.class));
347+
} finally {
348+
sync.close();
349+
}
350+
}
351+
293352
@Test
294353
public void testStartFetchSyncBlock() throws Exception {
295354
Field field = PeerManager.class.getDeclaredField("peers");

0 commit comments

Comments
 (0)