diff --git a/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java b/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java index a13240fa8ff..ab379ec8b75 100644 --- a/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java +++ b/framework/src/main/java/org/tron/core/net/peer/PeerConnection.java @@ -293,6 +293,14 @@ public static boolean needToLog(Message msg) { return true; } + public synchronized boolean checkAndPutAdvInvRequest(Item key, Long value) { + if (advInvRequest.containsKey(key)) { + return false; + } + advInvRequest.put(key, value); + return true; + } + @Override public boolean equals(Object o) { if (!(o instanceof PeerConnection)) { diff --git a/framework/src/main/java/org/tron/core/net/service/adv/AdvService.java b/framework/src/main/java/org/tron/core/net/service/adv/AdvService.java index b6ea7a3445f..03668d01837 100644 --- a/framework/src/main/java/org/tron/core/net/service/adv/AdvService.java +++ b/framework/src/main/java/org/tron/core/net/service/adv/AdvService.java @@ -281,8 +281,9 @@ private void consumerInvToFetch() { && invSender.getSize(peer) < MAX_TRX_FETCH_PER_PEER) .sorted(Comparator.comparingInt(peer -> invSender.getSize(peer))) .findFirst().ifPresent(peer -> { - invSender.add(item, peer); - peer.getAdvInvRequest().put(item, now); + if (peer.checkAndPutAdvInvRequest(item, now)) { + invSender.add(item, peer); + } invToFetch.remove(item); }); }); diff --git a/framework/src/main/java/org/tron/core/net/service/fetchblock/FetchBlockService.java b/framework/src/main/java/org/tron/core/net/service/fetchblock/FetchBlockService.java index f3699c3be2e..de20a17af0b 100644 --- a/framework/src/main/java/org/tron/core/net/service/fetchblock/FetchBlockService.java +++ b/framework/src/main/java/org/tron/core/net/service/fetchblock/FetchBlockService.java @@ -117,8 +117,8 @@ private void fetchBlockProcess(FetchBlockInfo fetchBlock) { if (optionalPeerConnection.isPresent()) { optionalPeerConnection.ifPresent(firstPeer -> { - if (shouldFetchBlock(firstPeer, fetchBlock)) { - firstPeer.getAdvInvRequest().put(item, System.currentTimeMillis()); + if (shouldFetchBlock(firstPeer, fetchBlock) + && firstPeer.checkAndPutAdvInvRequest(item, System.currentTimeMillis())) { firstPeer.sendMessage(new FetchInvDataMessage(Collections.singletonList(item.getHash()), item.getType())); this.fetchBlockInfo = null; diff --git a/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java b/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java index a6189346ae5..556eec980ed 100644 --- a/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java +++ b/framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java @@ -85,6 +85,7 @@ private void testBroadcast() { try { peer = context.getBean(PeerConnection.class); + Assert.assertFalse(peer.isDisconnect()); p2pEventHandler = context.getBean(P2pEventHandlerImpl.class); List peers = Lists.newArrayList(); @@ -96,6 +97,9 @@ private void testBroadcast() { service.broadcast(msg); Item item = new Item(blockCapsule.getBlockId(), InventoryType.BLOCK); Assert.assertNotNull(service.getMessage(item)); + peer.checkAndPutAdvInvRequest(item, System.currentTimeMillis()); + boolean res = peer.checkAndPutAdvInvRequest(item, System.currentTimeMillis()); + Assert.assertFalse(res); } catch (NullPointerException e) { System.out.println(e); }