-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(net): improve chain inventory generating logic #5393
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,18 @@ private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2p | |
|
|
||
| private LinkedList<BlockId> getLostBlockIds(List<BlockId> blockIds) throws P2pException { | ||
|
|
||
| BlockId unForkId = getUnForkId(blockIds); | ||
| LinkedList<BlockId> ids = getBlockIds(unForkId.getNum()); | ||
|
|
||
| if (ids.isEmpty() || !unForkId.equals(ids.peekFirst())) { | ||
| unForkId = getUnForkId(blockIds); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not mistaken, is it just a retry?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it can be solved by retrying. |
||
| ids = getBlockIds(unForkId.getNum()); | ||
| } | ||
|
|
||
| return ids; | ||
| } | ||
|
|
||
| private BlockId getUnForkId(List<BlockId> blockIds) throws P2pException { | ||
| BlockId unForkId = null; | ||
| for (int i = blockIds.size() - 1; i >= 0; i--) { | ||
| if (tronNetDelegate.containBlockInMainChain(blockIds.get(i))) { | ||
|
|
@@ -99,13 +111,17 @@ private LinkedList<BlockId> getLostBlockIds(List<BlockId> blockIds) throws P2pEx | |
| throw new P2pException(TypeEnum.SYNC_FAILED, "unForkId is null"); | ||
| } | ||
|
|
||
| return unForkId; | ||
| } | ||
|
|
||
| private LinkedList<BlockId> getBlockIds(Long unForkNum) throws P2pException { | ||
| BlockId headID = tronNetDelegate.getHeadBlockId(); | ||
| long headNum = headID.getNum(); | ||
|
|
||
| long len = Math.min(headNum, unForkId.getNum() + NetConstants.SYNC_FETCH_BATCH_NUM); | ||
| long len = Math.min(headNum, unForkNum + NetConstants.SYNC_FETCH_BATCH_NUM); | ||
|
|
||
| LinkedList<BlockId> ids = new LinkedList<>(); | ||
| for (long i = unForkId.getNum(); i <= len; i++) { | ||
| for (long i = unForkNum; i <= len; i++) { | ||
| if (i == headNum) { | ||
| ids.add(headID); | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import java.io.File; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.ArrayList; | ||
|
|
@@ -64,6 +65,21 @@ public void testProcessMessage() throws Exception { | |
| method.setAccessible(true); | ||
| boolean f = (boolean)method.invoke(handler, peer, message); | ||
| Assert.assertTrue(!f); | ||
|
|
||
| Method method1 = handler.getClass().getDeclaredMethod( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to reproduce this scenario without using reflection?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, further optimization. |
||
| "getLostBlockIds", List.class); | ||
| method1.setAccessible(true); | ||
| try { | ||
| method1.invoke(handler, blockIds); | ||
| } catch (InvocationTargetException e) { | ||
| Assert.assertEquals("unForkId is null", e.getTargetException().getMessage()); | ||
| } | ||
|
|
||
| Method method2 = handler.getClass().getDeclaredMethod( | ||
| "getBlockIds", Long.class); | ||
| method2.setAccessible(true); | ||
| List list = (List) method2.invoke(handler, 0L); | ||
| Assert.assertEquals(1, list.size()); | ||
| } | ||
|
|
||
| @After | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#5393