Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net) : optimize the isIdle method #5879

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What were the considerations of this previous judgment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic has been around for a long time, and this special scenario may not have been considered before.

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
Loading