Search in sources :

Example 36 with INode

use of org.aion.p2p.INode in project aion by aionnetwork.

the class SyncMgr method requestStatus.

/**
 * Makes a status request to each active peer.
 */
private void requestStatus() {
    Thread.currentThread().setName("sync-gs");
    for (INode node : p2pMgr.getActiveNodes().values()) {
        p2pMgr.send(node.getIdHash(), node.getIdShort(), cachedReqStatus);
        stats.updateTotalRequestsToPeer(node.getIdShort(), RequestType.STATUS);
        stats.updateRequestTime(node.getIdShort(), System.nanoTime(), RequestType.STATUS);
    }
}
Also used : INode(org.aion.p2p.INode)

Example 37 with INode

use of org.aion.p2p.INode in project aion by aionnetwork.

the class SyncHeaderRequestManagerTest method generateMockPeers.

/**
 * Generates {@code count} mock peers with ids from {@code start} to {@code start + count - 1}
 * and best block number {@code id * 1_000L}.
 */
private Map<Integer, INode> generateMockPeers(int start, int count) {
    Map<Integer, INode> current = new HashMap<>();
    for (int i = start; i < count + start; i++) {
        INode mockPeer = mock(INode.class);
        when(mockPeer.getIdHash()).thenReturn(i);
        when(mockPeer.getIdShort()).thenReturn("peer" + i);
        when(mockPeer.getBestBlockNumber()).thenReturn(i * 1_000L);
        current.put(i, mockPeer);
    }
    return current;
}
Also used : INode(org.aion.p2p.INode) HashMap(java.util.HashMap)

Example 38 with INode

use of org.aion.p2p.INode in project aion by aionnetwork.

the class ResActiveNodes method encode.

@Override
public byte[] encode() {
    ByteBuffer buf = ByteBuffer.allocate(NODE_BYTES_LENGTH * this.count + 1);
    buf.put((byte) this.count);
    for (INode n : this.nodes) {
        buf.put(n.getId());
        buf.put(n.getIp());
        buf.putInt(n.getPort());
    }
    return buf.array();
}
Also used : INode(org.aion.p2p.INode) ByteBuffer(java.nio.ByteBuffer)

Example 39 with INode

use of org.aion.p2p.INode in project aion by aionnetwork.

the class P2pMgr method handleP2pMessage.

private void handleP2pMessage(final SelectionKey sk, byte act, final byte[] msgBytes) {
    ChannelBuffer rb = (ChannelBuffer) sk.attachment();
    switch(act) {
        case Act.REQ_HANDSHAKE:
            if (msgBytes.length > ReqHandshake.LEN) {
                ReqHandshake1 reqHandshake1 = ReqHandshake1.decode(msgBytes, p2pLOG);
                if (reqHandshake1 != null) {
                    handleHandshakeRequest(rb, sk.channel().hashCode(), reqHandshake1.getNodeId(), reqHandshake1.getNetId(), reqHandshake1.getPort(), reqHandshake1.getRevision());
                }
            }
            break;
        case Act.RES_HANDSHAKE:
            if (rb.getNodeIdHash() != 0) {
                if (msgBytes.length > ResHandshake.LEN) {
                    ResHandshake1 resHandshake1 = ResHandshake1.decode(msgBytes, p2pLOG);
                    if (resHandshake1 != null && resHandshake1.getSuccess()) {
                        handleHandshakeResponse(rb.getNodeIdHash(), resHandshake1.getBinaryVersion());
                    }
                }
            }
            break;
        case Act.REQ_ACTIVE_NODES:
            if (rb.getNodeIdHash() != 0) {
                INode node = nodeMgr.getActiveNode(rb.getNodeIdHash());
                if (node != null) {
                    ResActiveNodes resActiveNodes = new ResActiveNodes(p2pLOG, nodeMgr.getActiveNodesList());
                    send(node.getIdHash(), node.getIdShort(), resActiveNodes);
                }
            }
            break;
        case Act.RES_ACTIVE_NODES:
            if (isSyncSeedsOnly() || rb.getNodeIdHash() == 0) {
                break;
            }
            INode node = nodeMgr.getActiveNode(rb.getNodeIdHash());
            if (node != null) {
                node.refreshTimestamp();
                ResActiveNodes resActiveNodes = ResActiveNodes.decode(msgBytes, p2pLOG);
                if (resActiveNodes != null) {
                    List<INode> incomingNodes = resActiveNodes.getNodes();
                    for (INode incomingNode : incomingNodes) {
                        if (nodeMgr.tempNodesSize() >= getMaxTempNodes()) {
                            return;
                        }
                        if (validateNode(incomingNode)) {
                            nodeMgr.addTempNode(incomingNode);
                        }
                    }
                }
            }
            break;
        default:
            p2pLOG.debug("unknown-route act={}", act);
            break;
    }
}
Also used : INode(org.aion.p2p.INode) ReqHandshake1(org.aion.p2p.impl.zero.msg.ReqHandshake1) ResActiveNodes(org.aion.p2p.impl.zero.msg.ResActiveNodes) ResHandshake1(org.aion.p2p.impl.zero.msg.ResHandshake1)

Example 40 with INode

use of org.aion.p2p.INode in project aion by aionnetwork.

the class P2pMgr method handleHandshakeResponse.

private void handleHandshakeResponse(int nodeIdHash, String binaryVersion) {
    INode node = nodeMgr.getOutboundNode(nodeIdHash);
    if (node != null && node.getPeerMetric().notBan()) {
        node.refreshTimestamp();
        node.setBinaryVersion(binaryVersion);
        nodeMgr.movePeerToActive(node.getIdHash(), "outbound");
    }
}
Also used : INode(org.aion.p2p.INode)

Aggregations

INode (org.aion.p2p.INode)55 Test (org.junit.Test)30 BigInteger (java.math.BigInteger)11 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ECKey (org.aion.crypto.ECKey)4 IP2pMgr (org.aion.p2p.IP2pMgr)4 Msg (org.aion.p2p.Msg)4 NetworkBestBlockCallback (org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback)4 PendingTxCallback (org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback)4 TransactionBroadcastCallback (org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback)4 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)4 AionPendingStateImpl (org.aion.zero.impl.pendingState.AionPendingStateImpl)4 BlockHeader (org.aion.zero.impl.types.BlockHeader)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 SelectionKey (java.nio.channels.SelectionKey)3 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3