Search in sources :

Example 16 with NodeID

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

the class WriterMessageRecorderTest method recordRskMessageWithSender.

@Test
public void recordRskMessageWithSender() throws IOException {
    Message message = createRskMessage();
    StringWriter writer = new StringWriter();
    BufferedWriter bwriter = new BufferedWriter(writer);
    Random random = new Random();
    byte[] nodeId = new byte[10];
    random.nextBytes(nodeId);
    NodeID sender = new NodeID(nodeId);
    WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, null);
    recorder.recordMessage(sender, message);
    bwriter.close();
    String result = writer.toString();
    String encoded = Hex.toHexString(message.getEncoded());
    Assert.assertTrue(result.contains("," + encoded + "," + Hex.toHexString(nodeId)));
    Assert.assertTrue(result.contains(",0,RSK_MESSAGE,GET_BLOCK_MESSAGE,"));
}
Also used : Message(org.ethereum.net.message.Message) GetBlockMessage(co.rsk.net.messages.GetBlockMessage) TransactionsMessage(org.ethereum.net.eth.message.TransactionsMessage) StringWriter(java.io.StringWriter) Random(java.util.Random) NodeID(co.rsk.net.NodeID) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 17 with NodeID

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

the class DecidingSyncStateTest method doesntStartSyncingIfAllPeersHaveLowerDifficulty.

@Test
public void doesntStartSyncingIfAllPeersHaveLowerDifficulty() {
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    SimpleSyncEventsHandler syncEventsHandler = new SimpleSyncEventsHandler();
    SimpleSyncInformation syncInformation = new SimpleSyncInformation().withWorsePeers();
    PeersInformation knownPeers = new PeersInformation(syncInformation, RskMockFactory.getChannelManager(), syncConfiguration);
    SyncState syncState = new DecidingSyncState(syncConfiguration, syncEventsHandler, syncInformation, knownPeers);
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
    knownPeers.registerPeer(new NodeID(HashUtil.randomPeerId()));
    syncState.newPeerStatus();
    syncState.tick(Duration.ofMinutes(2));
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
}
Also used : NodeID(co.rsk.net.NodeID) Test(org.junit.Test)

Example 18 with NodeID

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

the class DecidingSyncStateTest method startsSyncingWith5NonRepeatedPeers.

@Test
public void startsSyncingWith5NonRepeatedPeers() {
    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<>();
    NodeID peerToRepeat = new NodeID(HashUtil.randomPeerId());
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(peerToRepeat);
    peers.add(channel);
    for (int i = 0; i < 10; i++) {
        Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
        knownPeers.registerPeer(peerToRepeat).setStatus(StatusUtils.getFakeStatus());
        syncState.newPeerStatus();
    }
    for (int i = 0; i < 4; i++) {
        Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
        NodeID nodeID = new NodeID(HashUtil.randomPeerId());
        knownPeers.registerPeer(nodeID).setStatus(StatusUtils.getFakeStatus());
        channel = mock(Channel.class);
        when(channel.getNodeId()).thenReturn(nodeID);
        peers.add(channel);
        when(channelManager.getActivePeers()).thenReturn(peers);
        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 19 with NodeID

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

the class DecidingSyncStateTest method doesntStartSyncingWith1PeerBeforeTimeout.

@Test
public void doesntStartSyncingWith1PeerBeforeTimeout() {
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    SimpleSyncEventsHandler syncEventsHandler = new SimpleSyncEventsHandler();
    SimpleSyncInformation syncInformation = new SimpleSyncInformation();
    PeersInformation knownPeers = new PeersInformation(syncInformation, RskMockFactory.getChannelManager(), syncConfiguration);
    SyncState syncState = new DecidingSyncState(syncConfiguration, syncEventsHandler, syncInformation, knownPeers);
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
    knownPeers.registerPeer(new NodeID(HashUtil.randomPeerId()));
    syncState.newPeerStatus();
    syncState.tick(syncConfiguration.getTimeoutWaitingPeers().minusSeconds(1L));
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
}
Also used : NodeID(co.rsk.net.NodeID) Test(org.junit.Test)

Example 20 with NodeID

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

the class DecidingSyncStateTest method doesntStartSyncingIfAllPeersHaveBadReputation.

@Test
public void doesntStartSyncingIfAllPeersHaveBadReputation() {
    SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
    SimpleSyncEventsHandler syncEventsHandler = new SimpleSyncEventsHandler();
    SimpleSyncInformation syncInformation = new SimpleSyncInformation().withBadReputation();
    PeersInformation knownPeers = new PeersInformation(syncInformation, RskMockFactory.getChannelManager(), syncConfiguration);
    SyncState syncState = new DecidingSyncState(syncConfiguration, syncEventsHandler, syncInformation, knownPeers);
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
    knownPeers.registerPeer(new NodeID(HashUtil.randomPeerId()));
    syncState.newPeerStatus();
    syncState.tick(Duration.ofMinutes(2));
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
}
Also used : NodeID(co.rsk.net.NodeID) Test(org.junit.Test)

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