Search in sources :

Example 6 with NodeID

use of co.rsk.net.NodeID in project rskj by rsksmart.

the class DecidingSyncStateTest method startsSyncingWith5Peers.

@Test
public void startsSyncingWith5Peers() {
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    SimpleSyncEventsHandler syncEventsHandler = new SimpleSyncEventsHandler();
    SimpleSyncInformation syncInformation = new SimpleSyncInformation();
    ChannelManager channelManager = RskMockFactory.getChannelManager();
    PeersInformation knownPeers = new PeersInformation(syncInformation, channelManager, syncConfiguration);
    SyncState syncState = new DecidingSyncState(syncConfiguration, syncEventsHandler, syncInformation, knownPeers);
    Collection<Channel> peers = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
        NodeID nodeID = new NodeID(HashUtil.randomPeerId());
        Channel channel = mock(Channel.class);
        when(channel.getNodeId()).thenReturn(nodeID);
        peers.add(channel);
        when(channelManager.getActivePeers()).thenReturn(peers);
        knownPeers.registerPeer(nodeID).setStatus(StatusUtils.getFakeStatus());
        syncState.newPeerStatus();
    }
    Assert.assertTrue(syncEventsHandler.startSyncingWasCalled());
}
Also used : ChannelManager(org.ethereum.net.server.ChannelManager) Channel(org.ethereum.net.server.Channel) ArrayList(java.util.ArrayList) NodeID(co.rsk.net.NodeID) Test(org.junit.Test)

Example 7 with NodeID

use of co.rsk.net.NodeID in project rskj by rsksmart.

the class Web3ImplScoringTest method getPeerList.

@Test
public void getPeerList() throws UnknownHostException {
    NodeID node = generateNodeID();
    InetAddress address = generateNonLocalIPAddressV4();
    PeerScoringManager peerScoringManager = createPeerScoringManager();
    peerScoringManager.recordEvent(node, address, EventType.VALID_BLOCK);
    peerScoringManager.recordEvent(node, address, EventType.VALID_TRANSACTION);
    peerScoringManager.recordEvent(node, address, EventType.VALID_BLOCK);
    Web3Impl web3 = createWeb3(peerScoringManager);
    PeerScoringInformation[] result = web3.sco_peerList();
    Assert.assertNotNull(result);
    Assert.assertEquals(2, result.length);
    PeerScoringInformation info = result[0];
    Assert.assertEquals(Hex.toHexString(node.getID()).substring(0, 8), info.getId());
    Assert.assertEquals(2, info.getValidBlocks());
    Assert.assertEquals(0, info.getInvalidBlocks());
    Assert.assertEquals(1, info.getValidTransactions());
    Assert.assertEquals(0, info.getInvalidTransactions());
    Assert.assertTrue(info.getScore() > 0);
    info = result[1];
    Assert.assertEquals(address.getHostAddress(), info.getId());
    Assert.assertEquals(2, info.getValidBlocks());
    Assert.assertEquals(0, info.getInvalidBlocks());
    Assert.assertEquals(1, info.getValidTransactions());
    Assert.assertEquals(0, info.getInvalidTransactions());
    Assert.assertTrue(info.getScore() > 0);
}
Also used : PeerScoringManager(co.rsk.scoring.PeerScoringManager) PeerScoringInformation(co.rsk.scoring.PeerScoringInformation) NodeID(co.rsk.net.NodeID) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 8 with NodeID

use of co.rsk.net.NodeID in project rskj by rsksmart.

the class HandshakeHandler method recordEvent.

private void recordEvent(ChannelHandlerContext ctx, EventType event) {
    SocketAddress socketAddress = ctx.channel().remoteAddress();
    // TODO(mmarquez): what if it is not ??
    if (socketAddress instanceof InetSocketAddress) {
        NodeID nodeID = channel.getNodeId();
        InetAddress address = ((InetSocketAddress) socketAddress).getAddress();
        peerScoringManager.recordEvent(nodeID, address, event);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NodeID(co.rsk.net.NodeID) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress)

Example 9 with NodeID

use of co.rsk.net.NodeID in project rskj by rsksmart.

the class PeerExplorer method addConnection.

private void addConnection(PongPeerMessage message, String ip, int port) {
    Node senderNode = new Node(message.getNodeId().getID(), ip, port);
    if (!StringUtils.equals(senderNode.getHexId(), this.localNode.getHexId())) {
        OperationResult result = this.distanceTable.addNode(senderNode);
        if (result.isSuccess()) {
            NodeID senderId = senderNode.getId();
            this.establishedConnections.put(senderId, senderNode);
            logger.debug("New Peer found ip:[{}] port[{}]", ip, port);
        } else {
            this.challengeManager.startChallenge(result.getAffectedEntry().getNode(), senderNode, this);
        }
    }
}
Also used : Node(org.ethereum.net.rlpx.Node) NodeID(co.rsk.net.NodeID) OperationResult(co.rsk.net.discovery.table.OperationResult)

Example 10 with NodeID

use of co.rsk.net.NodeID in project rskj by rsksmart.

the class PeerExplorer method handleFindNode.

public void handleFindNode(FindNodePeerMessage message) {
    NodeID nodeId = message.getNodeId();
    Node connectedNode = this.establishedConnections.get(nodeId);
    if (connectedNode != null) {
        List<Node> nodesToSend = this.distanceTable.getClosestNodes(nodeId);
        logger.debug("About to send [{}] neighbors to ip[{}] port[{}] nodeId[{}]", nodesToSend.size(), connectedNode.getHost(), connectedNode.getPort(), connectedNode.getHexIdShort());
        this.sendNeighbors(connectedNode.getAddress(), nodesToSend, message.getMessageId());
        updateEntry(connectedNode);
    }
}
Also used : Node(org.ethereum.net.rlpx.Node) NodeID(co.rsk.net.NodeID)

Aggregations

NodeID (co.rsk.net.NodeID)27 Test (org.junit.Test)19 InetAddress (java.net.InetAddress)8 Block (org.ethereum.core.Block)4 RskSystemProperties (co.rsk.config.RskSystemProperties)3 Metrics (co.rsk.net.Metrics)3 Status (co.rsk.net.Status)3 RskMessage (co.rsk.net.eth.RskMessage)3 co.rsk.net.messages (co.rsk.net.messages)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 java.util (java.util)3 ArrayList (java.util.ArrayList)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 Executors (java.util.concurrent.Executors)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 CollectionUtils (org.apache.commons.collections4.CollectionUtils)3