Search in sources :

Example 1 with ChainInventoryMessage

use of org.tron.core.net.message.ChainInventoryMessage 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));
}
Also used : UnReachBlockException(org.tron.core.exception.UnReachBlockException) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) BlockId(org.tron.core.capsule.BlockCapsule.BlockId)

Aggregations

BlockId (org.tron.core.capsule.BlockCapsule.BlockId)1 UnReachBlockException (org.tron.core.exception.UnReachBlockException)1 ChainInventoryMessage (org.tron.core.net.message.ChainInventoryMessage)1