Search in sources :

Example 1 with PeerConnection

use of org.tron.core.net.peer.PeerConnection in project java-tron by tronprotocol.

the class SyncPool method prepareActive.

private synchronized void prepareActive() {
    List<Channel> managerActive = new ArrayList<>(channelManager.getActivePeers());
    NodeSelector nodeSelector = new NodeSelector();
    List<PeerConnection> active = new ArrayList<>();
    for (Channel channel : managerActive) {
        if (nodeSelector.test(nodeManager.getNodeHandler(channel.getNode()))) {
            active.add((PeerConnection) channel);
        }
    }
    if (active.isEmpty())
        return;
    active.sort(Comparator.comparingDouble(c -> c.getPeerStats().getAvgLatency()));
    for (PeerConnection channel : active) {
        if (!activePeers.contains(channel)) {
            peerDel.onConnectPeer(channel);
        }
    }
    activePeers.clear();
    activePeers.addAll(active);
}
Also used : NodeManager(org.tron.common.overlay.discover.NodeManager) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) PeerClient(org.tron.common.overlay.client.PeerClient) PeerConnection(org.tron.core.net.peer.PeerConnection) List(java.util.List) Component(org.springframework.stereotype.Component) PeerConnectionDelegate(org.tron.core.net.peer.PeerConnectionDelegate) Args(org.tron.core.config.args.Args) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NodeHandler(org.tron.common.overlay.discover.NodeHandler) Comparator(java.util.Comparator) Collections(java.util.Collections) Node(org.tron.common.overlay.discover.Node) PeerConnection(org.tron.core.net.peer.PeerConnection) ArrayList(java.util.ArrayList)

Example 2 with PeerConnection

use of org.tron.core.net.peer.PeerConnection in project java-tron by tronprotocol.

the class SyncPool method logActivePeers.

synchronized void logActivePeers() {
    logger.info("-------- active node.");
    for (NodeHandler nodeHandler : nodeManager.getActiveNodes()) {
        logger.info(nodeHandler.toString());
    }
    logger.info("-------- active channel {}, node in user size {}", channelManager.getActivePeers().size(), channelManager.nodesInUse().size());
    for (Channel channel : channelManager.getActivePeers()) {
        logger.info(channel.toString());
    }
    if (logger.isInfoEnabled()) {
        StringBuilder sb = new StringBuilder("Peer stats:\n");
        sb.append("Active peers\n");
        sb.append("============\n");
        Set<Node> activeSet = new HashSet<>();
        for (PeerConnection peer : new ArrayList<>(activePeers)) {
            sb.append(peer.logSyncStats()).append('\n');
            activeSet.add(peer.getNode());
        }
        sb.append("Other connected peers\n");
        sb.append("============\n");
        for (Channel peer : new ArrayList<>(channelManager.getActivePeers())) {
            if (!activeSet.contains(peer.getNode())) {
                sb.append(peer.logSyncStats()).append('\n');
            }
        }
        logger.info(sb.toString());
    }
}
Also used : NodeHandler(org.tron.common.overlay.discover.NodeHandler) PeerConnection(org.tron.core.net.peer.PeerConnection) Node(org.tron.common.overlay.discover.Node) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 3 with PeerConnection

use of org.tron.core.net.peer.PeerConnection 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 4 with PeerConnection

use of org.tron.core.net.peer.PeerConnection 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)

Example 5 with PeerConnection

use of org.tron.core.net.peer.PeerConnection in project java-tron by tronprotocol.

the class NodeImpl method startFetchSyncBlock.

private synchronized void startFetchSyncBlock() {
    // TODO: check how many block is processing and decide if fetch more
    HashMap<PeerConnection, List<BlockId>> send = new HashMap<>();
    HashSet<BlockId> request = new HashSet<>();
    getActivePeer().stream().filter(peer -> peer.isNeedSyncFromPeer() && !peer.isBusy()).forEach(peer -> {
        if (!send.containsKey(peer)) {
            // TODO: Attention multi thread here
            send.put(peer, new LinkedList<>());
        }
        for (BlockId blockId : peer.getSyncBlockToFetch()) {
            if (// TODO: clean processing block
            !request.contains(blockId) && !syncBlockIdWeRequested.containsKey(blockId)) {
                send.get(peer).add(blockId);
                request.add(blockId);
            // TODO: check max block num to fetch from one peer.
            // if (send.get(peer).size() > 200) { //Max Blocks peer get one time
            // break;
            // }
            }
        }
    });
    send.forEach((peer, blockIds) -> {
        // TODO: use collector
        blockIds.forEach(blockId -> {
            syncBlockIdWeRequested.put(blockId, System.currentTimeMillis());
            peer.getSyncBlockRequested().put(blockId, System.currentTimeMillis());
        });
        List<Sha256Hash> ids = new LinkedList<>();
        ids.addAll(blockIds);
        peer.sendMessage(new FetchInvDataMessage(ids, InventoryType.BLOCK));
    });
    send.clear();
}
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) PeerConnection(org.tron.core.net.peer.PeerConnection) FetchInvDataMessage(org.tron.core.net.message.FetchInvDataMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Sha256Hash(org.tron.common.utils.Sha256Hash) LinkedList(java.util.LinkedList) BlockId(org.tron.core.capsule.BlockCapsule.BlockId) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Aggregations

PeerConnection (org.tron.core.net.peer.PeerConnection)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 NodeHandler (org.tron.common.overlay.discover.NodeHandler)4 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Set (java.util.Set)3 Executors (java.util.concurrent.Executors)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Component (org.springframework.stereotype.Component)3 BlockId (org.tron.core.capsule.BlockCapsule.BlockId)3 TraitorPeerException (org.tron.core.exception.TraitorPeerException)3 BlockMessage (org.tron.core.net.message.BlockMessage)3 Iterables (com.google.common.collect.Iterables)2 ConcurrentSet (io.netty.util.internal.ConcurrentSet)2 Collection (java.util.Collection)2 Deque (java.util.Deque)2 HashMap (java.util.HashMap)2