Skip to content

Commit ddbe8f1

Browse files
committed
feat(net): add isSyncIdle method
1 parent 0dc7a3f commit ddbe8f1

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public void setBlockBothHave(BlockId blockId) {
167167
}
168168

169169
public boolean isIdle() {
170+
return advInvRequest.isEmpty() && isSyncIdle();
171+
}
172+
173+
public boolean isSyncIdle() {
170174
return syncBlockRequested.isEmpty() && syncChainRequested == null;
171175
}
172176

framework/src/main/java/org/tron/core/net/service/sync/SyncService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void processBlock(PeerConnection peer, BlockMessage blockMessage) {
134134
blockJustReceived.put(blockMessage, peer);
135135
}
136136
handleFlag = true;
137-
if (peer.isIdle()) {
137+
if (peer.isSyncIdle()) {
138138
if (peer.getRemainNum() > 0
139139
&& peer.getSyncBlockToFetch().size() <= syncFetchBatchNum) {
140140
syncNext(peer);
@@ -226,7 +226,7 @@ private BlockId getBlockIdByNum(long num) throws P2pException {
226226
private void startFetchSyncBlock() {
227227
HashMap<PeerConnection, List<BlockId>> send = new HashMap<>();
228228
tronNetDelegate.getActivePeer().stream()
229-
.filter(peer -> peer.isNeedSyncFromPeer() && peer.isIdle())
229+
.filter(peer -> peer.isNeedSyncFromPeer() && peer.isSyncIdle())
230230
.filter(peer -> peer.isFetchAble())
231231
.forEach(peer -> {
232232
if (!send.containsKey(peer)) {

framework/src/test/java/org/tron/core/net/peer/PeerConnectionTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void testIsIdle() {
7878
Long time = System.currentTimeMillis();
7979
peerConnection.getAdvInvRequest().put(item, time);
8080
f = peerConnection.isIdle();
81-
Assert.assertTrue(f);
81+
Assert.assertTrue(!f);
8282

8383
peerConnection.getAdvInvRequest().clear();
8484
f = peerConnection.isIdle();
@@ -98,6 +98,36 @@ public void testIsIdle() {
9898
Assert.assertTrue(!f);
9999
}
100100

101+
@Test
102+
public void testIsSyncIdle() {
103+
PeerConnection peerConnection = new PeerConnection();
104+
boolean f = peerConnection.isSyncIdle();
105+
Assert.assertTrue(f);
106+
107+
Item item = new Item(Sha256Hash.ZERO_HASH, Protocol.Inventory.InventoryType.TRX);
108+
Long time = System.currentTimeMillis();
109+
peerConnection.getAdvInvRequest().put(item, time);
110+
f = peerConnection.isSyncIdle();
111+
Assert.assertTrue(f);
112+
113+
peerConnection.getAdvInvRequest().clear();
114+
f = peerConnection.isSyncIdle();
115+
Assert.assertTrue(f);
116+
117+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId();
118+
peerConnection.getSyncBlockRequested().put(blockId, time);
119+
f = peerConnection.isSyncIdle();
120+
Assert.assertTrue(!f);
121+
122+
peerConnection.getSyncBlockRequested().clear();
123+
f = peerConnection.isSyncIdle();
124+
Assert.assertTrue(f);
125+
126+
peerConnection.setSyncChainRequested(new Pair<>(new LinkedList<>(), time));
127+
f = peerConnection.isSyncIdle();
128+
Assert.assertTrue(!f);
129+
}
130+
101131
@Test
102132
public void testOnConnect() {
103133
PeerConnection peerConnection = new PeerConnection();

0 commit comments

Comments
 (0)