Search in sources :

Example 1 with BlockMessage

use of org.tron.core.net.message.BlockMessage in project java-tron by tronprotocol.

the class NodeImpl method broadcast.

/**
 * broadcast msg.
 *
 * @param msg msg to bradcast
 */
public void broadcast(Message msg) {
    InventoryType type;
    if (msg instanceof BlockMessage) {
        logger.info("Ready to broadcast a block, Its hash is " + msg.getMessageId());
        freshBlockId.offer(((BlockMessage) msg).getBlockId());
        blockToAdvertise.add(((BlockMessage) msg).getBlockId());
        type = InventoryType.BLOCK;
    } else if (msg instanceof TransactionMessage) {
        trxToAdvertise.add(msg.getMessageId());
        type = InventoryType.TRX;
    } else {
        return;
    }
    // TODO: here need to cache fresh message to let peer fetch these data not from DB
    advObjToSpread.put(msg.getMessageId(), type);
}
Also used : BlockMessage(org.tron.core.net.message.BlockMessage) TransactionMessage(org.tron.core.net.message.TransactionMessage) InventoryType(org.tron.protos.Protocol.Inventory.InventoryType)

Example 2 with BlockMessage

use of org.tron.core.net.message.BlockMessage in project java-tron by tronprotocol.

the class NodeImpl method processAdvBlock.

private void processAdvBlock(PeerConnection peer, BlockCapsule block) {
    // TODO: lack the complete flow.
    if (!freshBlockId.contains(block.getBlockId())) {
        try {
            LinkedList<Sha256Hash> trxIds = del.handleBlock(block, false);
            freshBlockId.offer(block.getBlockId());
            trxIds.forEach(trxId -> advObjToFetch.remove(trxId));
            // TODO:save message cache again.
            getActivePeer().stream().filter(p -> p.getAdvObjSpreadToUs().containsKey(block.getBlockId())).forEach(p -> {
                p.setHeadBlockWeBothHave(block.getBlockId());
                p.setHeadBlockTimeWeBothHave(block.getTimeStamp());
            });
            getActivePeer().forEach(p -> p.cleanInvGarbage());
            // rebroadcast
            broadcast(new BlockMessage(block));
        } catch (BadBlockException e) {
            badAdvObj.put(block.getBlockId(), System.currentTimeMillis());
        } catch (UnLinkedBlockException e) {
            // reSync
            startSyncWithPeer(peer);
        }
    }
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) BlockCapsule(org.tron.core.capsule.BlockCapsule) ConcurrentSet(io.netty.util.internal.ConcurrentSet) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) NodeHandler(org.tron.common.overlay.discover.NodeHandler) TransactionMessage(org.tron.core.net.message.TransactionMessage) NodeConstant(org.tron.core.config.Parameter.NodeConstant) Pair(javafx.util.Pair) InventoryMessage(org.tron.core.net.message.InventoryMessage) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) MessageTypes(org.tron.core.net.message.MessageTypes) UnReachBlockException(org.tron.core.exception.UnReachBlockException) ExecutorLoop(org.tron.common.utils.ExecutorLoop) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Executors(java.util.concurrent.Executors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Entry(java.util.Map.Entry) Queue(java.util.Queue) TronException(org.tron.core.exception.TronException) BlockConstant(org.tron.core.config.Parameter.BlockConstant) BadTransactionException(org.tron.core.exception.BadTransactionException) Iterables(com.google.common.collect.Iterables) SyncBlockChainMessage(org.tron.core.net.message.SyncBlockChainMessage) ItemNotFound(org.tron.core.net.message.ItemNotFound) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TronState(org.tron.common.overlay.server.Channel.TronState) PeerConnectionDelegate(org.tron.core.net.peer.PeerConnectionDelegate) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BadBlockException(org.tron.core.exception.BadBlockException) LinkedList(java.util.LinkedList) Sha256Hash(org.tron.common.utils.Sha256Hash) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) BlockInventoryMessage(org.tron.core.net.message.BlockInventoryMessage) InventoryType(org.tron.protos.Protocol.Inventory.InventoryType) NetConstants(org.tron.core.config.Parameter.NetConstants) SyncPool(org.tron.common.overlay.server.SyncPool) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) TraitorPeerException(org.tron.core.exception.TraitorPeerException) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) TimeUnit(java.util.concurrent.TimeUnit) TronMessage(org.tron.core.net.message.TronMessage) PeerConnection(org.tron.core.net.peer.PeerConnection) Component(org.springframework.stereotype.Component) ReasonCode(org.tron.common.overlay.message.ReasonCode) Time(org.tron.common.utils.Time) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException) BadBlockException(org.tron.core.exception.BadBlockException) BlockMessage(org.tron.core.net.message.BlockMessage) Sha256Hash(org.tron.common.utils.Sha256Hash) UnLinkedBlockException(org.tron.core.exception.UnLinkedBlockException)

Example 3 with BlockMessage

use of org.tron.core.net.message.BlockMessage in project java-tron by tronprotocol.

the class NodeImpl method onHandleFetchDataMessage.

private void onHandleFetchDataMessage(PeerConnection peer, FetchInvDataMessage fetchInvDataMsg) {
    logger.info("on handle fetch block message");
    MessageTypes type = fetchInvDataMsg.getInvMessageType();
    // TODO:maybe can use message cache here
    final BlockCapsule[] blocks = { del.getGenesisBlock() };
    // get data and send it one by one
    fetchInvDataMsg.getHashList().forEach(hash -> {
        if (del.contain(hash, type)) {
            Message msg = del.getData(hash, type);
            if (type.equals(MessageTypes.BLOCK)) {
                blocks[0] = ((BlockMessage) msg).getBlockCapsule();
            }
            peer.sendMessage(msg);
        } else {
            peer.sendMessage(new ItemNotFound());
        }
    });
    if (blocks[0] != null) {
        peer.setHeadBlockWeBothHave(blocks[0].getBlockId());
        peer.setHeadBlockTimeWeBothHave(blocks[0].getTimeStamp());
    }
}
Also used : TransactionMessage(org.tron.core.net.message.TransactionMessage) InventoryMessage(org.tron.core.net.message.InventoryMessage) SyncBlockChainMessage(org.tron.core.net.message.SyncBlockChainMessage) BlockMessage(org.tron.core.net.message.BlockMessage) Message(org.tron.common.overlay.message.Message) BlockInventoryMessage(org.tron.core.net.message.BlockInventoryMessage) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) ChainInventoryMessage(org.tron.core.net.message.ChainInventoryMessage) TronMessage(org.tron.core.net.message.TronMessage) ItemNotFound(org.tron.core.net.message.ItemNotFound) MessageTypes(org.tron.core.net.message.MessageTypes) BlockCapsule(org.tron.core.capsule.BlockCapsule)

Example 4 with BlockMessage

use of org.tron.core.net.message.BlockMessage in project java-tron by tronprotocol.

the class NodeImpl method onHandleChainInventoryMessage.

private void onHandleChainInventoryMessage(PeerConnection peer, ChainInventoryMessage msg) {
    // logger.info("on handle block chain inventory message");
    try {
        if (peer.getSyncChainRequested() != null) {
            // List<BlockId> blockIds = msg.getBlockIds();
            Deque<BlockId> blockIdWeGet = new LinkedList<>(msg.getBlockIds());
            // check if the peer is a traitor
            if (!blockIdWeGet.isEmpty()) {
                long num = blockIdWeGet.peek().getNum();
                for (BlockId id : blockIdWeGet) {
                    if (id.getNum() != num++) {
                        throw new TraitorPeerException("We get a not continuous block inv from " + peer);
                    }
                }
                if (peer.getSyncChainRequested().getKey().isEmpty()) {
                    if (blockIdWeGet.peek().getNum() != 1) {
                        throw new TraitorPeerException("We want a block inv starting from beginning from " + peer);
                    }
                } else {
                    boolean isFound = false;
                    for (BlockId id : blockIdWeGet) {
                        if (id.equals(blockIdWeGet.peek())) {
                            isFound = true;
                        }
                    }
                    if (!isFound) {
                        throw new TraitorPeerException("We get a unlinked block chain from " + peer);
                    }
                }
            }
            // check finish
            // here this peer's answer is legal
            peer.setSyncChainRequested(null);
            if (msg.getRemainNum() == 0 && (blockIdWeGet.isEmpty() || (blockIdWeGet.size() == 1 && del.containBlock(blockIdWeGet.peek()))) && peer.getSyncBlockToFetch().isEmpty() && peer.getUnfetchSyncNum() == 0) {
                peer.setNeedSyncFromPeer(false);
                unSyncNum = getUnSyncNum();
                if (unSyncNum == 0) {
                    del.syncToCli(0);
                }
                // TODO: if sync finish call del.syncToCli();
                return;
            }
            if (!blockIdWeGet.isEmpty() && peer.getSyncBlockToFetch().isEmpty()) {
                boolean isFound = false;
                for (PeerConnection peerToCheck : getActivePeer()) {
                    if (!peerToCheck.equals(peer) && !peerToCheck.getSyncBlockToFetch().isEmpty() && peerToCheck.getSyncBlockToFetch().peekFirst().equals(blockIdWeGet.peekFirst())) {
                        isFound = true;
                        break;
                    }
                }
                if (!isFound) {
                    while (!blockIdWeGet.isEmpty() && del.containBlock(blockIdWeGet.peek())) {
                        peer.setHeadBlockWeBothHave(blockIdWeGet.peek());
                        peer.setHeadBlockTimeWeBothHave(del.getBlockTime(blockIdWeGet.peek()));
                        blockIdWeGet.poll();
                    }
                }
            } else if (!blockIdWeGet.isEmpty()) {
                while (!peer.getSyncBlockToFetch().isEmpty()) {
                    if (!peer.getSyncBlockToFetch().peekLast().equals(blockIdWeGet.peekFirst())) {
                        blockIdWeGet.pop();
                    } else {
                        break;
                    }
                }
                if (peer.getSyncBlockToFetch().isEmpty()) {
                    updateBlockWeBothHave(peer, ((BlockMessage) del.getData(blockIdWeGet.peek(), MessageTypes.BLOCK)).getBlockCapsule());
                }
                // poll the block we both have.
                blockIdWeGet.pop();
            }
            // sew it
            peer.getSyncBlockToFetch().addAll(blockIdWeGet);
            peer.setUnfetchSyncNum(msg.getRemainNum());
            long newUnSyncNum = getUnSyncNum();
            if (unSyncNum != newUnSyncNum) {
                unSyncNum = newUnSyncNum;
                del.syncToCli(unSyncNum);
            }
            if (msg.getRemainNum() == 0) {
                if (!peer.getSyncBlockToFetch().isEmpty()) {
                    startFetchSyncBlock();
                } else {
                    // let peer know we are sync.
                    syncNextBatchChainIds(peer);
                }
            } else {
                if (peer.getSyncBlockToFetch().size() > NodeConstant.SYNC_FETCH_BATCH_NUM) {
                    // one batch by one batch.
                    startFetchSyncBlock();
                } else {
                    syncNextBatchChainIds(peer);
                }
            }
        // TODO: check head block time is legal here
        // TODO: refresh sync status to cli. call del.syncToCli() here
        } else {
            throw new TraitorPeerException("We don't send sync request to " + peer);
        }
    } catch (TraitorPeerException e) {
        banTraitorPeer(peer);
    }
}
Also used : BlockMessage(org.tron.core.net.message.BlockMessage) PeerConnection(org.tron.core.net.peer.PeerConnection) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) TraitorPeerException(org.tron.core.exception.TraitorPeerException) LinkedList(java.util.LinkedList)

Aggregations

BlockMessage (org.tron.core.net.message.BlockMessage)4 TransactionMessage (org.tron.core.net.message.TransactionMessage)3 LinkedList (java.util.LinkedList)2 Message (org.tron.common.overlay.message.Message)2 BlockCapsule (org.tron.core.capsule.BlockCapsule)2 BlockId (org.tron.core.capsule.BlockCapsule.BlockId)2 TraitorPeerException (org.tron.core.exception.TraitorPeerException)2 BlockInventoryMessage (org.tron.core.net.message.BlockInventoryMessage)2 ChainInventoryMessage (org.tron.core.net.message.ChainInventoryMessage)2 FetchInvDataMessage (org.tron.core.net.message.FetchInvDataMessage)2 InventoryMessage (org.tron.core.net.message.InventoryMessage)2 ItemNotFound (org.tron.core.net.message.ItemNotFound)2 MessageTypes (org.tron.core.net.message.MessageTypes)2 SyncBlockChainMessage (org.tron.core.net.message.SyncBlockChainMessage)2 TronMessage (org.tron.core.net.message.TronMessage)2 PeerConnection (org.tron.core.net.peer.PeerConnection)2 Iterables (com.google.common.collect.Iterables)1 ConcurrentSet (io.netty.util.internal.ConcurrentSet)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1