Skip to content

Commit e7858ea

Browse files
authored
Merge pull request #5094 from chengtx01/fix_no_request
fix(net):fix block fetching issue in FetchBlockService
2 parents 415dc52 + 74c8179 commit e7858ea

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

framework/src/main/java/org/tron/core/net/peer/PeerConnection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ public static boolean needToLog(Message msg) {
293293
return true;
294294
}
295295

296+
public synchronized boolean checkAndPutAdvInvRequest(Item key, Long value) {
297+
if (advInvRequest.containsKey(key)) {
298+
return false;
299+
}
300+
advInvRequest.put(key, value);
301+
return true;
302+
}
303+
296304
@Override
297305
public boolean equals(Object o) {
298306
if (!(o instanceof PeerConnection)) {

framework/src/main/java/org/tron/core/net/service/adv/AdvService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,9 @@ private void consumerInvToFetch() {
281281
&& invSender.getSize(peer) < MAX_TRX_FETCH_PER_PEER)
282282
.sorted(Comparator.comparingInt(peer -> invSender.getSize(peer)))
283283
.findFirst().ifPresent(peer -> {
284-
invSender.add(item, peer);
285-
peer.getAdvInvRequest().put(item, now);
284+
if (peer.checkAndPutAdvInvRequest(item, now)) {
285+
invSender.add(item, peer);
286+
}
286287
invToFetch.remove(item);
287288
});
288289
});

framework/src/main/java/org/tron/core/net/service/fetchblock/FetchBlockService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ private void fetchBlockProcess(FetchBlockInfo fetchBlock) {
117117

118118
if (optionalPeerConnection.isPresent()) {
119119
optionalPeerConnection.ifPresent(firstPeer -> {
120-
if (shouldFetchBlock(firstPeer, fetchBlock)) {
121-
firstPeer.getAdvInvRequest().put(item, System.currentTimeMillis());
120+
if (shouldFetchBlock(firstPeer, fetchBlock)
121+
&& firstPeer.checkAndPutAdvInvRequest(item, System.currentTimeMillis())) {
122122
firstPeer.sendMessage(new FetchInvDataMessage(Collections.singletonList(item.getHash()),
123123
item.getType()));
124124
this.fetchBlockInfo = null;

framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private void testBroadcast() {
8585

8686
try {
8787
peer = context.getBean(PeerConnection.class);
88+
Assert.assertFalse(peer.isDisconnect());
8889
p2pEventHandler = context.getBean(P2pEventHandlerImpl.class);
8990

9091
List<PeerConnection> peers = Lists.newArrayList();
@@ -96,6 +97,9 @@ private void testBroadcast() {
9697
service.broadcast(msg);
9798
Item item = new Item(blockCapsule.getBlockId(), InventoryType.BLOCK);
9899
Assert.assertNotNull(service.getMessage(item));
100+
peer.checkAndPutAdvInvRequest(item, System.currentTimeMillis());
101+
boolean res = peer.checkAndPutAdvInvRequest(item, System.currentTimeMillis());
102+
Assert.assertFalse(res);
99103
} catch (NullPointerException e) {
100104
System.out.println(e);
101105
}

0 commit comments

Comments
 (0)