Skip to content

Commit 729a99b

Browse files
authored
Merge pull request #5922 from zeusoo001/f-sync-fetch-block-opt
feat(net): optimize fetch inventory message check logic
2 parents afc3979 + 0779c00 commit 729a99b

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

framework/src/main/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ private void check(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) thr
164164
throw new P2pException(TypeEnum.BAD_MESSAGE,
165165
"minBlockNum: " + minBlockNum + ", blockNum: " + blockNum);
166166
}
167+
if (blockNum > peer.getLastSyncBlockId().getNum()) {
168+
throw new P2pException(TypeEnum.BAD_MESSAGE,
169+
"maxBlockNum: " + peer.getLastSyncBlockId().getNum() + ", blockNum: " + blockNum);
170+
}
167171
if (peer.getSyncBlockIdCache().getIfPresent(hash) != null) {
168172
throw new P2pException(TypeEnum.BAD_MESSAGE,
169173
new BlockId(hash).getString() + " is exist");

framework/src/test/java/org/tron/core/net/messagehandler/FetchInvDataMsgHandlerTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import org.tron.core.net.service.adv.AdvService;
2222
import org.tron.protos.Protocol;
2323

24-
25-
2624
public class FetchInvDataMsgHandlerTest {
2725

2826
@Test
@@ -62,4 +60,37 @@ public void testProcessMessage() throws Exception {
6260
new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK));
6361
Assert.assertNotNull(syncBlockIdCache.getIfPresent(blockId));
6462
}
63+
64+
@Test
65+
public void testSyncFetchCheck() {
66+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L);
67+
List<Sha256Hash> blockIds = new LinkedList<>();
68+
blockIds.add(blockId);
69+
FetchInvDataMessage msg =
70+
new FetchInvDataMessage(blockIds, Protocol.Inventory.InventoryType.BLOCK);
71+
72+
PeerConnection peer = Mockito.mock(PeerConnection.class);
73+
Mockito.when(peer.isNeedSyncFromUs()).thenReturn(true);
74+
Cache<Item, Long> advInvSpread = CacheBuilder.newBuilder().maximumSize(100)
75+
.expireAfterWrite(1, TimeUnit.HOURS).recordStats().build();
76+
Mockito.when(peer.getAdvInvSpread()).thenReturn(advInvSpread);
77+
78+
FetchInvDataMsgHandler fetchInvDataMsgHandler = new FetchInvDataMsgHandler();
79+
80+
try {
81+
Mockito.when(peer.getLastSyncBlockId())
82+
.thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1000L));
83+
fetchInvDataMsgHandler.processMessage(peer, msg);
84+
} catch (Exception e) {
85+
Assert.assertEquals(e.getMessage(), "maxBlockNum: 1000, blockNum: 10000");
86+
}
87+
88+
try {
89+
Mockito.when(peer.getLastSyncBlockId())
90+
.thenReturn(new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 20000L));
91+
fetchInvDataMsgHandler.processMessage(peer, msg);
92+
} catch (Exception e) {
93+
Assert.assertEquals(e.getMessage(), "minBlockNum: 16000, blockNum: 10000");
94+
}
95+
}
6596
}

0 commit comments

Comments
 (0)