Skip to content

Commit ffdac33

Browse files
committed
test(net): cover sync refetch after owner disconnect
Document the extra fetch permitted when releasing a disconnected peer from the request cache. Extend the two-peer regression to verify survivor ownership, subsequent deduplication and disconnect cleanup.
1 parent 8f6c4f9 commit ffdac33

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public void onDisconnect(PeerConnection peer) {
159159
// Received and processed blocks no longer appear in the peer's outstanding requests.
160160
requestBlockIds.asMap().forEach((blockId, owner) -> {
161161
if (peer.equals(owner)) {
162+
// Another peer may refetch an already-solid queued block after its owner is released.
162163
invalid(blockId, peer);
163164
}
164165
});

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void testDisconnectReleasesReceivedAndProcessedRequests() {
174174
}
175175

176176
@Test
177-
public void testSolidBlockDoesNotRefetchBeforeDisconnect() throws Exception {
177+
public void testSolidBlockRefetchesOnceAfterOwnerDisconnect() throws Exception {
178178
SyncService sync = new SyncService();
179179
try {
180180
TronNetDelegate delegate = mock(TronNetDelegate.class);
@@ -192,13 +192,27 @@ public void sendMessage(Message message) {
192192
sent.add(message);
193193
}
194194
};
195+
List<Message> otherSent = new ArrayList<>();
196+
PeerConnection other = new PeerConnection() {
197+
@Override
198+
public void sendMessage(Message message) {
199+
otherSent.add(message);
200+
}
201+
};
195202
Channel channel = mock(Channel.class);
196203
Mockito.when(channel.getInetAddress()).thenReturn(inetSocketAddress.getAddress());
197204
Mockito.when(channel.getInetSocketAddress()).thenReturn(inetSocketAddress);
198205
source.setChannel(channel);
199206
source.setFetchAble(true);
200207
source.getSyncBlockToFetch().add(blockId);
201-
Mockito.when(delegate.getActivePeer()).thenReturn(Collections.singletonList(source));
208+
InetSocketAddress otherAddress = new InetSocketAddress("127.0.0.3", 10001);
209+
Channel otherChannel = mock(Channel.class);
210+
Mockito.when(otherChannel.getInetAddress()).thenReturn(otherAddress.getAddress());
211+
Mockito.when(otherChannel.getInetSocketAddress()).thenReturn(otherAddress);
212+
other.setChannel(otherChannel);
213+
other.setFetchAble(true);
214+
other.getSyncBlockToFetch().add(blockId);
215+
Mockito.when(delegate.getActivePeer()).thenReturn(Arrays.asList(source, other));
202216
Cache<BlockCapsule.BlockId, PeerConnection> requests =
203217
(Cache<BlockCapsule.BlockId, PeerConnection>)
204218
ReflectUtils.getFieldObject(sync, "requestBlockIds");
@@ -209,6 +223,7 @@ public void sendMessage(Message message) {
209223
Assert.assertEquals(Collections.singletonList(blockId),
210224
((FetchInvDataMessage) sent.get(0)).getHashList());
211225
Assert.assertTrue(source.getSyncBlockRequested().containsKey(blockId));
226+
Assert.assertTrue(otherSent.isEmpty());
212227

213228
BlockMsgHandler handler = new BlockMsgHandler();
214229
ReflectUtils.setFieldValue(handler, "syncService", sync);
@@ -226,18 +241,50 @@ public void sendMessage(Message message) {
226241
ReflectUtils.getFieldObject(sync, "blockWaitToProcess");
227242
Assert.assertTrue(waiting.isEmpty());
228243
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(sync, "fetchFlag"));
229-
Mockito.verify(delegate, Mockito.never())
230-
.processBlock(Mockito.any(BlockCapsule.class), Mockito.anyBoolean());
231-
232244
for (int i = 0; i < 3; i++) {
233245
ReflectUtils.invokeMethod(sync, "startFetchSyncBlock");
234246
}
235247
Assert.assertEquals(1, sent.size());
248+
Assert.assertTrue(otherSent.isEmpty());
236249
Assert.assertTrue(source.getSyncBlockRequested().isEmpty());
237250
Assert.assertEquals(blockId, source.getSyncBlockToFetch().peek());
251+
Assert.assertEquals(blockId, other.getSyncBlockToFetch().peek());
238252

253+
Mockito.when(delegate.getActivePeer()).thenReturn(Collections.singletonList(other));
254+
ReflectUtils.setFieldValue(sync, "fetchFlag", false);
239255
sync.onDisconnect(source);
240256
Assert.assertNull(requests.getIfPresent(blockId));
257+
Assert.assertTrue((boolean) ReflectUtils.getFieldObject(sync, "fetchFlag"));
258+
259+
ReflectUtils.invokeMethod(sync, "startFetchSyncBlock");
260+
Assert.assertEquals(1, sent.size());
261+
Assert.assertEquals(1, otherSent.size());
262+
Assert.assertTrue(otherSent.get(0) instanceof FetchInvDataMessage);
263+
Assert.assertEquals(Collections.singletonList(blockId),
264+
((FetchInvDataMessage) otherSent.get(0)).getHashList());
265+
Assert.assertSame(other, requests.getIfPresent(blockId));
266+
Assert.assertTrue(other.getSyncBlockRequested().containsKey(blockId));
267+
Assert.assertFalse(requests.asMap().containsValue(source));
268+
269+
handler.processMessage(other, new BlockMessage(block));
270+
Assert.assertTrue(other.getSyncBlockRequested().isEmpty());
271+
Assert.assertTrue(other.getSyncBlockInProcess().contains(blockId));
272+
ReflectUtils.invokeMethod(sync, "handleSyncBlock");
273+
Assert.assertTrue(other.getSyncBlockInProcess().isEmpty());
274+
Assert.assertTrue(waiting.isEmpty());
275+
Assert.assertSame(other, requests.getIfPresent(blockId));
276+
Assert.assertEquals(blockId, other.getSyncBlockToFetch().peek());
277+
278+
for (int i = 0; i < 3; i++) {
279+
ReflectUtils.invokeMethod(sync, "startFetchSyncBlock");
280+
}
281+
Assert.assertEquals(1, otherSent.size());
282+
Assert.assertTrue(other.getSyncBlockRequested().isEmpty());
283+
Mockito.verify(delegate, Mockito.never())
284+
.processBlock(Mockito.any(BlockCapsule.class), Mockito.anyBoolean());
285+
286+
sync.onDisconnect(other);
287+
Assert.assertNull(requests.getIfPresent(blockId));
241288
} finally {
242289
sync.close();
243290
}

0 commit comments

Comments
 (0)