Search in sources :

Example 1 with UnReachBlockException

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

Example 2 with UnReachBlockException

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));
}
Also used : BadTransactionException(org.tron.core.exception.BadTransactionException) ContractValidateException(org.tron.core.exception.ContractValidateException) BlockStore(org.tron.core.db.BlockStore) HighFreqException(org.tron.core.exception.HighFreqException) Deque(java.util.Deque) BlockCapsule(org.tron.core.capsule.BlockCapsule) ContractExeException(org.tron.core.exception.ContractExeException) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) Manager(org.tron.core.db.Manager) BadBlockException(org.tron.core.exception.BadBlockException) TransactionMessage(org.tron.core.net.message.TransactionMessage) LinkedList(java.util.LinkedList) Sha256Hash(org.tron.common.utils.Sha256Hash) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) NodeConstant(org.tron.core.config.Parameter.NodeConstant) Longs(com.google.common.primitives.Longs) LongStream(java.util.stream.LongStream) MessageTypes(org.tron.core.net.message.MessageTypes) UnReachBlockException(org.tron.core.exception.UnReachBlockException) Collectors(java.util.stream.Collectors) TransactionCapsule(org.tron.core.capsule.TransactionCapsule) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) Collections(java.util.Collections) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) LinkedList(java.util.LinkedList)

Aggregations

BlockId (org.tron.core.capsule.BlockCapsule.BlockId)2 UnReachBlockException (org.tron.core.exception.UnReachBlockException)2 Longs (com.google.common.primitives.Longs)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 LongStream (java.util.stream.LongStream)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Message (org.tron.common.overlay.message.Message)1 Sha256Hash (org.tron.common.utils.Sha256Hash)1 BlockCapsule (org.tron.core.capsule.BlockCapsule)1 TransactionCapsule (org.tron.core.capsule.TransactionCapsule)1 NodeConstant (org.tron.core.config.Parameter.NodeConstant)1 BlockStore (org.tron.core.db.BlockStore)1 Manager (org.tron.core.db.Manager)1 BadBlockException (org.tron.core.exception.BadBlockException)1 BadTransactionException (org.tron.core.exception.BadTransactionException)1 ContractExeException (org.tron.core.exception.ContractExeException)1