Skip to content

Commit

Permalink
Merge pull request #5921 from zeusoo001/f-isidle-method-opt
Browse files Browse the repository at this point in the history
feat(net) : optimize the isIdle method
  • Loading branch information
lvs007 authored Jul 23, 2024
2 parents ecd43b7 + ddbe8f1 commit afc3979
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ public void setBlockBothHave(BlockId blockId) {
}

public boolean isIdle() {
return advInvRequest.isEmpty() && syncBlockRequested.isEmpty() && syncChainRequested == null;
return advInvRequest.isEmpty() && isSyncIdle();
}

public boolean isSyncIdle() {
return syncBlockRequested.isEmpty() && syncChainRequested == null;
}

public void sendMessage(Message message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void processBlock(PeerConnection peer, BlockMessage blockMessage) {
blockJustReceived.put(blockMessage, peer);
}
handleFlag = true;
if (peer.isIdle()) {
if (peer.isSyncIdle()) {
if (peer.getRemainNum() > 0
&& peer.getSyncBlockToFetch().size() <= syncFetchBatchNum) {
syncNext(peer);
Expand Down Expand Up @@ -226,7 +226,7 @@ private BlockId getBlockIdByNum(long num) throws P2pException {
private void startFetchSyncBlock() {
HashMap<PeerConnection, List<BlockId>> send = new HashMap<>();
tronNetDelegate.getActivePeer().stream()
.filter(peer -> peer.isNeedSyncFromPeer() && peer.isIdle())
.filter(peer -> peer.isNeedSyncFromPeer() && peer.isSyncIdle())
.filter(peer -> peer.isFetchAble())
.forEach(peer -> {
if (!send.containsKey(peer)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ public void testIsIdle() {
Assert.assertTrue(!f);
}

@Test
public void testIsSyncIdle() {
PeerConnection peerConnection = new PeerConnection();
boolean f = peerConnection.isSyncIdle();
Assert.assertTrue(f);

Item item = new Item(Sha256Hash.ZERO_HASH, Protocol.Inventory.InventoryType.TRX);
Long time = System.currentTimeMillis();
peerConnection.getAdvInvRequest().put(item, time);
f = peerConnection.isSyncIdle();
Assert.assertTrue(f);

peerConnection.getAdvInvRequest().clear();
f = peerConnection.isSyncIdle();
Assert.assertTrue(f);

BlockCapsule.BlockId blockId = new BlockCapsule.BlockId();
peerConnection.getSyncBlockRequested().put(blockId, time);
f = peerConnection.isSyncIdle();
Assert.assertTrue(!f);

peerConnection.getSyncBlockRequested().clear();
f = peerConnection.isSyncIdle();
Assert.assertTrue(f);

peerConnection.setSyncChainRequested(new Pair<>(new LinkedList<>(), time));
f = peerConnection.isSyncIdle();
Assert.assertTrue(!f);
}

@Test
public void testOnConnect() {
PeerConnection peerConnection = new PeerConnection();
Expand Down

0 comments on commit afc3979

Please sign in to comment.