use of org.tron.core.exception.UnReachBlockException in project java-tron by tronprotocol.
the class NodeImpl method onHandleSyncBlockChainMessage.
private void onHandleSyncBlockChainMessage(PeerConnection peer, SyncBlockChainMessage syncMsg) {
// logger.info("on handle sync block chain message");
peer.setTronState(TronState.SYNCING);
LinkedList<BlockId> blockIds;
List<BlockId> summaryChainIds = syncMsg.getBlockIds();
long remainNum = 0;
try {
blockIds = del.getLostBlockIds(summaryChainIds);
} catch (UnReachBlockException e) {
// TODO: disconnect this peer casue this peer can not switch
logger.debug(e.getMessage(), e);
return;
}
if (blockIds.isEmpty()) {
peer.setNeedSyncFromUs(false);
} else if (blockIds.size() == 1 && !summaryChainIds.isEmpty() && summaryChainIds.contains(blockIds.peekFirst())) {
peer.setNeedSyncFromUs(false);
} else {
peer.setNeedSyncFromUs(true);
remainNum = del.getHeadBlockId().getNum() - blockIds.peekLast().getNum();
}
if (!peer.isNeedSyncFromPeer() && !summaryChainIds.isEmpty() && !del.contain(Iterables.getLast(summaryChainIds), MessageTypes.BLOCK)) {
startSyncWithPeer(peer);
}
peer.sendMessage(new ChainInventoryMessage(blockIds, remainNum));
}
use of org.tron.core.exception.UnReachBlockException in project java-tron by tronprotocol.
the class NodeDelegateImpl method getLostBlockIds.
@Override
public LinkedList<BlockId> getLostBlockIds(List<BlockId> blockChainSummary) throws UnReachBlockException {
// todo: return the blocks it should be have.
if (dbManager.getHeadBlockNum() == 0) {
return new LinkedList<>();
}
BlockId unForkedBlockId = null;
if (blockChainSummary.isEmpty() || blockChainSummary.size() == 1) {
unForkedBlockId = dbManager.getGenesisBlockId();
} else {
// todo: find a block we all know between the summary and my db.
Collections.reverse(blockChainSummary);
unForkedBlockId = blockChainSummary.stream().filter(blockId -> dbManager.containBlock(blockId)).findFirst().orElseThrow(UnReachBlockException::new);
// todo: can not find any same block form peer's summary and my db.
}
// todo: limit the count of block to send peer by one time.
long unForkedBlockIdNum = unForkedBlockId.getNum();
long len = Longs.min(dbManager.getHeadBlockNum(), unForkedBlockIdNum + NodeConstant.SYNC_FETCH_BATCH_NUM);
return LongStream.rangeClosed(unForkedBlockIdNum, len).filter(num -> num > 0).mapToObj(num -> dbManager.getBlockIdByNum(num)).collect(Collectors.toCollection(LinkedList::new));
}
Aggregations