Skip to content

Commit 83151aa

Browse files
authored
feat(net):improve chain inventory generating logic (#5393)
1 parent a9c4f43 commit 83151aa

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2p
8787

8888
private LinkedList<BlockId> getLostBlockIds(List<BlockId> blockIds) throws P2pException {
8989

90+
BlockId unForkId = getUnForkId(blockIds);
91+
LinkedList<BlockId> ids = getBlockIds(unForkId.getNum());
92+
93+
if (ids.isEmpty() || !unForkId.equals(ids.peekFirst())) {
94+
unForkId = getUnForkId(blockIds);
95+
ids = getBlockIds(unForkId.getNum());
96+
}
97+
98+
return ids;
99+
}
100+
101+
private BlockId getUnForkId(List<BlockId> blockIds) throws P2pException {
90102
BlockId unForkId = null;
91103
for (int i = blockIds.size() - 1; i >= 0; i--) {
92104
if (tronNetDelegate.containBlockInMainChain(blockIds.get(i))) {
@@ -99,13 +111,17 @@ private LinkedList<BlockId> getLostBlockIds(List<BlockId> blockIds) throws P2pEx
99111
throw new P2pException(TypeEnum.SYNC_FAILED, "unForkId is null");
100112
}
101113

114+
return unForkId;
115+
}
116+
117+
private LinkedList<BlockId> getBlockIds(Long unForkNum) throws P2pException {
102118
BlockId headID = tronNetDelegate.getHeadBlockId();
103119
long headNum = headID.getNum();
104120

105-
long len = Math.min(headNum, unForkId.getNum() + NetConstants.SYNC_FETCH_BATCH_NUM);
121+
long len = Math.min(headNum, unForkNum + NetConstants.SYNC_FETCH_BATCH_NUM);
106122

107123
LinkedList<BlockId> ids = new LinkedList<>();
108-
for (long i = unForkId.getNum(); i <= len; i++) {
124+
for (long i = unForkNum; i <= len; i++) {
109125
if (i == headNum) {
110126
ids.add(headID);
111127
} else {

framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.lang.reflect.Field;
5+
import java.lang.reflect.InvocationTargetException;
56
import java.lang.reflect.Method;
67
import java.net.InetSocketAddress;
78
import java.util.ArrayList;
@@ -64,6 +65,21 @@ public void testProcessMessage() throws Exception {
6465
method.setAccessible(true);
6566
boolean f = (boolean)method.invoke(handler, peer, message);
6667
Assert.assertTrue(!f);
68+
69+
Method method1 = handler.getClass().getDeclaredMethod(
70+
"getLostBlockIds", List.class);
71+
method1.setAccessible(true);
72+
try {
73+
method1.invoke(handler, blockIds);
74+
} catch (InvocationTargetException e) {
75+
Assert.assertEquals("unForkId is null", e.getTargetException().getMessage());
76+
}
77+
78+
Method method2 = handler.getClass().getDeclaredMethod(
79+
"getBlockIds", Long.class);
80+
method2.setAccessible(true);
81+
List list = (List) method2.invoke(handler, 0L);
82+
Assert.assertEquals(1, list.size());
6783
}
6884

6985
@After

0 commit comments

Comments
 (0)