diff --git a/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java b/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java index 3c83bc51e47..958ebfe5561 100644 --- a/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java +++ b/framework/src/main/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandler.java @@ -37,8 +37,8 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep long remainNum = 0; List summaryChainIds = syncBlockChainMessage.getBlockIds(); - - LinkedList blockIds = getLostBlockIds(summaryChainIds); + BlockId headID = tronNetDelegate.getHeadBlockId(); + LinkedList blockIds = getLostBlockIds(summaryChainIds, headID); if (blockIds.size() == 0) { logger.warn("Can't get lost block Ids"); @@ -48,7 +48,7 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep peer.setNeedSyncFromUs(false); } else { peer.setNeedSyncFromUs(true); - remainNum = tronNetDelegate.getHeadBlockId().getNum() - blockIds.peekLast().getNum(); + remainNum = headID.getNum() - blockIds.peekLast().getNum(); } peer.setLastSyncBlockId(blockIds.peekLast()); @@ -85,14 +85,15 @@ private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2p return true; } - private LinkedList getLostBlockIds(List blockIds) throws P2pException { + private LinkedList getLostBlockIds(List blockIds, BlockId headID) + throws P2pException { BlockId unForkId = getUnForkId(blockIds); - LinkedList ids = getBlockIds(unForkId.getNum()); + LinkedList ids = getBlockIds(unForkId.getNum(), headID); if (ids.isEmpty() || !unForkId.equals(ids.peekFirst())) { unForkId = getUnForkId(blockIds); - ids = getBlockIds(unForkId.getNum()); + ids = getBlockIds(unForkId.getNum(), headID); } return ids; @@ -114,8 +115,7 @@ private BlockId getUnForkId(List blockIds) throws P2pException { return unForkId; } - private LinkedList getBlockIds(Long unForkNum) throws P2pException { - BlockId headID = tronNetDelegate.getHeadBlockId(); + private LinkedList getBlockIds(Long unForkNum, BlockId headID) throws P2pException { long headNum = headID.getNum(); long len = Math.min(headNum, unForkNum + NetConstants.SYNC_FETCH_BATCH_NUM); diff --git a/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java b/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java index 25108e19b70..d4ad32dd34f 100644 --- a/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java +++ b/framework/src/test/java/org/tron/core/net/messagehandler/SyncBlockChainMsgHandlerTest.java @@ -15,6 +15,7 @@ import org.tron.common.application.TronApplicationContext; import org.tron.core.Constant; import org.tron.core.capsule.BlockCapsule; +import org.tron.core.capsule.BlockCapsule.BlockId; import org.tron.core.config.DefaultConfig; import org.tron.core.config.args.Args; import org.tron.core.exception.P2pException; @@ -68,18 +69,18 @@ public void testProcessMessage() throws Exception { Assert.assertTrue(!f); Method method1 = handler.getClass().getDeclaredMethod( - "getLostBlockIds", List.class); + "getLostBlockIds", List.class, BlockId.class); method1.setAccessible(true); try { - method1.invoke(handler, blockIds); + method1.invoke(handler, blockIds, new BlockCapsule.BlockId()); } catch (InvocationTargetException e) { Assert.assertEquals("unForkId is null", e.getTargetException().getMessage()); } Method method2 = handler.getClass().getDeclaredMethod( - "getBlockIds", Long.class); + "getBlockIds", Long.class, BlockId.class); method2.setAccessible(true); - List list = (List) method2.invoke(handler, 0L); + List list = (List) method2.invoke(handler, 0L, new BlockCapsule.BlockId()); Assert.assertEquals(1, list.size()); }