Skip to content

Commit

Permalink
feat(net): add isSyncIdle method
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusoo001 committed Jul 1, 2024
1 parent cfad0de commit a0497b7
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,6 +167,10 @@ public void setBlockBothHave(BlockId blockId) {
}

public boolean isIdle() {
return advInvRequest.isEmpty() && isSyncIdle();
}

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

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 @@ -78,7 +78,7 @@ public void testIsIdle() {
Long time = System.currentTimeMillis();
peerConnection.getAdvInvRequest().put(item, time);
f = peerConnection.isIdle();
Assert.assertTrue(f);
Assert.assertTrue(!f);

peerConnection.getAdvInvRequest().clear();
f = peerConnection.isIdle();
Expand All @@ -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 a0497b7

Please sign in to comment.