Search in sources :

Example 21 with INode

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

the class NodeMgrTest method testDumpNodeInfo.

@Test(timeout = 30_000)
public void testDumpNodeInfo() {
    String dump = nMgr.dumpNodeInfo("testId", false);
    assertNotNull(dump);
    INode node = nMgr.allocNode(ip1, 1);
    addNodetoOutbound(node, UUID.randomUUID());
    nMgr.movePeerToActive(node.getIdHash(), "outbound");
    String dump2 = nMgr.dumpNodeInfo("testId", false);
    assertEquals(dump.length(), dump2.length());
    String dump3 = nMgr.dumpNodeInfo("testId", true);
    assertTrue(dump3.length() > dump2.length());
}
Also used : INode(org.aion.p2p.INode) Test(org.junit.Test)

Example 22 with INode

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

the class NodeMgrTest method testMoveInboundToActive.

@Test(timeout = 30_000)
public void testMoveInboundToActive() {
    INode node = nMgr.allocNode(ip2, 1);
    addNodetoInbound(node, UUID.fromString(nodeId1));
    nMgr.movePeerToActive(node.getChannel().hashCode(), "inbound");
    assertNull(nMgr.getInboundNode(node.getChannel().hashCode()));
    INode activeNode = nMgr.getActiveNode(node.getIdHash());
    assertNotNull(activeNode);
    assertEquals(node, activeNode);
}
Also used : INode(org.aion.p2p.INode) Test(org.junit.Test)

Example 23 with INode

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

the class NodeMgr method movePeerToActive.

public void movePeerToActive(int _hash, String _type) {
    Map<Integer, INode> nodes = (_type.contentEquals("inbound")) ? inboundNodes : outboundNodes;
    INode node = nodes.remove(_hash);
    if (node != null) {
        if (p2pLOG.isTraceEnabled()) {
            p2pLOG.trace("<movePeerToActive: {} {}>", _type, node.toString());
        }
        // noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (node) {
            if (activeNodes.size() >= maxActiveNodes) {
                p2pMgr.closeSocket(node.getChannel(), _type + " -> active, active full");
                return;
            }
            if (p2pMgr.isSelf(node)) {
                p2pMgr.closeSocket(node.getChannel(), _type + " -> active, self-connected");
                return;
            }
            node.setConnection(_type);
            node.setFromBootList(seedIps.contains(node.getIpStr()));
            INode previous = activeNodes.putIfAbsent(node.getIdHash(), node);
            if (previous != null) {
                p2pMgr.closeSocket(node.getChannel(), _type + " -> active, node " + previous.getIdShort() + " exits");
            } else if (!activeIpAllow(node.getIpStr())) {
                p2pMgr.closeSocket(node.getChannel(), _type + " -> active, ip " + node.getIpStr() + " exits");
            }
            if (p2pLOG.isDebugEnabled()) {
                p2pLOG.debug("<{} -> active node-id={} ip={}>", _type, node.getIdShort(), node.getIpStr());
            }
        }
    } else {
        if (p2pLOG.isTraceEnabled()) {
            p2pLOG.trace("<movePeerToActive empty {} {}>", _type, _hash);
        }
    }
}
Also used : BigInteger(java.math.BigInteger) INode(org.aion.p2p.INode)

Example 24 with INode

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

the class NodeMgr method timeoutActive.

private void timeoutActive(long now) {
    OptionalDouble average = activeNodes.values().stream().mapToLong(n -> now - n.getTimestamp()).average();
    this.avgLatency = (int) average.orElse(0);
    long timeout = ((long) average.orElse(4000)) * 5;
    timeout = Math.max(MIN_TIMEOUT_ACTIVE_NODES, Math.min(timeout, MAX_TIMEOUT_ACTIVE_NODES));
    if (p2pLOG.isDebugEnabled()) {
        p2pLOG.debug("<average-delay={}ms>", this.avgLatency);
    }
    try {
        Iterator<Map.Entry<Integer, INode>> it = activeNodes.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, INode> entry = it.next();
            INode node = entry.getValue();
            if (now - node.getTimestamp() > timeout) {
                p2pMgr.closeSocket(node.getChannel(), "active-timeout ip=" + node.getIpStr());
                it.remove();
            } else if (!node.getChannel().isConnected()) {
                p2pMgr.closeSocket(node.getChannel(), "channel-already-closed node=" + node.getIdShort() + " ip=" + node.getIpStr());
                it.remove();
            }
        }
    } catch (IllegalStateException e) {
        p2pLOG.info("<timeoutActive IllegalStateException>", e);
    }
}
Also used : IP2pMgr(org.aion.p2p.IP2pMgr) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) OptionalDouble(java.util.OptionalDouble) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HashMap(java.util.HashMap) Random(java.util.Random) Deque(java.util.Deque) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) HashSet(java.util.HashSet) INodeMgr(org.aion.p2p.INodeMgr) List(java.util.List) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Map(java.util.Map) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) INode(org.aion.p2p.INode) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) INode(org.aion.p2p.INode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) OptionalDouble(java.util.OptionalDouble)

Example 25 with INode

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

the class ResActiveNodesTest method testEncodeDecode.

@Test
public void testEncodeDecode() {
    int m = ThreadLocalRandom.current().nextInt(0, 20);
    List<INode> srcNodes = new ArrayList<>();
    for (int i = 0; i < m; i++) {
        srcNodes.add(randomNode());
    }
    ResActiveNodes res = ResActiveNodes.decode(new ResActiveNodes(p2pLOG, srcNodes).encode(), p2pLOG);
    assertEquals(res.getNodes().size(), m);
    List<INode> tarNodes = res.getNodes();
    for (int i = 0; i < m; i++) {
        INode srcNode = srcNodes.get(i);
        INode tarNode = tarNodes.get(i);
        assertArrayEquals(srcNode.getId(), tarNode.getId());
        assertEquals(srcNode.getIdHash(), tarNode.getIdHash());
        assertArrayEquals(srcNode.getIp(), tarNode.getIp());
        assertEquals(srcNode.getIpStr(), tarNode.getIpStr());
        assertEquals(srcNode.getPort(), tarNode.getPort());
    }
}
Also used : INode(org.aion.p2p.INode) ArrayList(java.util.ArrayList) Test(org.junit.Test)

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