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): fix bug of getting head block concurrently #5532

Merged
merged 4 commits into from
Oct 12, 2023
Merged
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 @@ -37,8 +37,8 @@ public void processMessage(PeerConnection peer, TronMessage msg) throws P2pExcep
long remainNum = 0;

List<BlockId> summaryChainIds = syncBlockChainMessage.getBlockIds();

LinkedList<BlockId> blockIds = getLostBlockIds(summaryChainIds);
BlockId headID = tronNetDelegate.getHeadBlockId();
LinkedList<BlockId> blockIds = getLostBlockIds(summaryChainIds, headID);

if (blockIds.size() == 0) {
logger.warn("Can't get lost block Ids");
Expand All @@ -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());
Expand Down Expand Up @@ -85,14 +85,15 @@ private boolean check(PeerConnection peer, SyncBlockChainMessage msg) throws P2p
return true;
}

private LinkedList<BlockId> getLostBlockIds(List<BlockId> blockIds) throws P2pException {
private LinkedList<BlockId> getLostBlockIds(List<BlockId> blockIds, BlockId headID)
throws P2pException {

BlockId unForkId = getUnForkId(blockIds);
LinkedList<BlockId> ids = getBlockIds(unForkId.getNum());
LinkedList<BlockId> 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;
Expand All @@ -114,8 +115,7 @@ private BlockId getUnForkId(List<BlockId> blockIds) throws P2pException {
return unForkId;
}

private LinkedList<BlockId> getBlockIds(Long unForkNum) throws P2pException {
BlockId headID = tronNetDelegate.getHeadBlockId();
private LinkedList<BlockId> getBlockIds(Long unForkNum, BlockId headID) throws P2pException {
long headNum = headID.getNum();

long len = Math.min(headNum, unForkNum + NetConstants.SYNC_FETCH_BATCH_NUM);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down