Search in sources :

Example 11 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class PeerExplorer method handlePingMessage.

public void handlePingMessage(String ip, PingPeerMessage message) {
    this.sendPong(ip, message);
    Node connectedNode = this.establishedConnections.get(message.getNodeId());
    if (connectedNode == null) {
        this.sendPing(new InetSocketAddress(ip, message.getPort()), 1);
    } else {
        updateEntry(connectedNode);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Node(org.ethereum.net.rlpx.Node)

Example 12 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class PeerExplorer method collectRandomNodes.

private Set<Node> collectRandomNodes(List<Node> originalList, int elementsNbr) {
    Set<Node> ret = new HashSet<>();
    SecureRandom rnd = new SecureRandom();
    while (ret.size() < elementsNbr) {
        int i = rnd.nextInt(originalList.size());
        ret.add(originalList.get(i));
    }
    return ret;
}
Also used : Node(org.ethereum.net.rlpx.Node) SecureRandom(java.security.SecureRandom)

Example 13 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class PeerExplorer method removeConnections.

private void removeConnections(List<PeerDiscoveryRequest> expiredRequests) {
    if (CollectionUtils.isNotEmpty(expiredRequests)) {
        for (PeerDiscoveryRequest req : expiredRequests) {
            Node node = req.getRelatedNode();
            if (node != null) {
                this.establishedConnections.remove(node.getId());
                this.distanceTable.removeNode(node);
            }
        }
    }
}
Also used : Node(org.ethereum.net.rlpx.Node)

Example 14 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class NeighborsPeerMessage method create.

public static NeighborsPeerMessage create(List<Node> nodes, String check, ECKey privKey) {
    byte[][] nodeRLPs = null;
    if (nodes != null) {
        nodeRLPs = new byte[nodes.size()][];
        int i = 0;
        for (Node node : nodes) {
            nodeRLPs[i] = node.getRLP();
            ++i;
        }
    }
    byte[] rlpListNodes = RLP.encodeList(nodeRLPs);
    byte[] rlpCheck = RLP.encodeElement(check.getBytes(StandardCharsets.UTF_8));
    byte[] type = new byte[] { (byte) DiscoveryMessageType.NEIGHBORS.getTypeValue() };
    byte[] data = RLP.encodeList(rlpListNodes, rlpCheck);
    NeighborsPeerMessage neighborsMessage = new NeighborsPeerMessage();
    neighborsMessage.encode(type, data, privKey);
    neighborsMessage.nodes = nodes;
    neighborsMessage.messageId = check;
    return neighborsMessage;
}
Also used : Node(org.ethereum.net.rlpx.Node)

Example 15 with Node

use of org.ethereum.net.rlpx.Node in project rskj by rsksmart.

the class NeighborsPeerMessage method parse.

@Override
public final void parse(byte[] data) {
    RLPList list = (RLPList) RLP.decode2OneItem(data, 0);
    RLPList nodesRLP = (RLPList) list.get(0);
    nodes = new ArrayList<>();
    for (int i = 0; i < nodesRLP.size(); ++i) {
        RLPList nodeRLP = (RLPList) nodesRLP.get(i);
        Node node = new Node(nodeRLP.getRLPData());
        nodes.add(node);
    }
    RLPItem chk = (RLPItem) list.get(1);
    this.messageId = new String(chk.getRLPData(), Charset.forName("UTF-8"));
}
Also used : RLPItem(org.ethereum.util.RLPItem) Node(org.ethereum.net.rlpx.Node) RLPList(org.ethereum.util.RLPList)

Aggregations

Node (org.ethereum.net.rlpx.Node)26 Test (org.junit.Test)16 NodeDistanceTable (co.rsk.net.discovery.table.NodeDistanceTable)11 ECKey (org.ethereum.crypto.ECKey)11 ArrayList (java.util.ArrayList)9 InetSocketAddress (java.net.InetSocketAddress)8 Channel (io.netty.channel.Channel)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 NodeID (co.rsk.net.NodeID)3 OperationResult (co.rsk.net.discovery.table.OperationResult)2 SecureRandom (java.security.SecureRandom)2 HashSet (java.util.HashSet)2 PeerExplorer (co.rsk.net.discovery.PeerExplorer)1 co.rsk.net.discovery.message (co.rsk.net.discovery.message)1 PeerDiscoveryRequestBuilder (co.rsk.net.discovery.table.PeerDiscoveryRequestBuilder)1 IpUtils (co.rsk.util.IpUtils)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1