Search in sources :

Example 1 with NodeID

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

the class PeerScoringManagerTest method notGoodReputationByNodeIDExpires.

@Test
public void notGoodReputationByNodeIDExpires() throws UnknownHostException, InterruptedException {
    NodeID id = generateNodeID();
    PeerScoringManager manager = createPeerScoringManager();
    manager.recordEvent(id, null, EventType.INVALID_BLOCK);
    Assert.assertEquals(1, manager.getPeerScoring(id).getEventCounter(EventType.INVALID_BLOCK));
    Assert.assertFalse(manager.hasGoodReputation(id));
    Assert.assertNotEquals(0, manager.getPeerScoring(id).getTimeLostGoodReputation());
    Assert.assertFalse(manager.hasGoodReputation(id));
    Assert.assertNotEquals(0, manager.getPeerScoring(id).getTimeLostGoodReputation());
    Assert.assertEquals(1, manager.getPeerScoring(id).getEventCounter(EventType.INVALID_BLOCK));
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(manager.hasGoodReputation(id));
    Assert.assertEquals(0, manager.getPeerScoring(id).getTimeLostGoodReputation());
    Assert.assertEquals(0, manager.getPeerScoring(id).getEventCounter(EventType.INVALID_BLOCK));
    Assert.assertTrue(manager.getPeerScoring(id).isEmpty());
}
Also used : NodeID(co.rsk.net.NodeID) Test(org.junit.Test)

Example 2 with NodeID

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

the class PeerScoringManagerTest method getEmptyNodeStatusFromUnknownNodeId.

@Test
public void getEmptyNodeStatusFromUnknownNodeId() {
    NodeID id = generateNodeID();
    PeerScoringManager manager = createPeerScoringManager();
    PeerScoring result = manager.getPeerScoring(id);
    Assert.assertNotNull(result);
    Assert.assertTrue(result.isEmpty());
}
Also used : NodeID(co.rsk.net.NodeID) Test(org.junit.Test)

Example 3 with NodeID

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

the class PeerScoringManagerTest method getPeersInformationFromManagerWithThreeEvents.

@Test
public void getPeersInformationFromManagerWithThreeEvents() throws UnknownHostException {
    PeerScoringManager manager = createPeerScoringManager();
    NodeID node = generateNodeID();
    InetAddress address = generateIPAddressV4();
    manager.recordEvent(node, address, EventType.VALID_BLOCK);
    manager.recordEvent(node, address, EventType.VALID_TRANSACTION);
    manager.recordEvent(node, address, EventType.VALID_BLOCK);
    List<PeerScoringInformation> result = manager.getPeersInformation();
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isEmpty());
    Assert.assertEquals(2, result.size());
    PeerScoringInformation info = result.get(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.assertEquals(0, info.getPunishments());
    Assert.assertEquals(0, info.getSuccessfulHandshakes());
    Assert.assertTrue(info.getScore() > 0);
    info = result.get(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.assertEquals(0, info.getPunishments());
    Assert.assertEquals(0, info.getSuccessfulHandshakes());
    Assert.assertTrue(info.getScore() > 0);
}
Also used : NodeID(co.rsk.net.NodeID) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 4 with NodeID

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

the class WriterMessageRecorderTest method filterMessage.

@Test
public void filterMessage() throws IOException {
    Message message = createRskMessage();
    StringWriter writer = new StringWriter();
    BufferedWriter bwriter = new BufferedWriter(writer);
    List<String> commands = new ArrayList<>();
    commands.add("TRANSACTIONS");
    MessageFilter filter = new MessageFilter(commands);
    Random random = new Random();
    byte[] nodeId = new byte[10];
    random.nextBytes(nodeId);
    NodeID sender = new NodeID(nodeId);
    WriterMessageRecorder recorder = new WriterMessageRecorder(bwriter, filter);
    recorder.recordMessage(sender, message);
    bwriter.close();
    String result = writer.toString();
    Assert.assertEquals(0, result.length());
}
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) ArrayList(java.util.ArrayList) NodeID(co.rsk.net.NodeID) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 5 with NodeID

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

the class DecidingSyncStateTest method startsSyncingWith1PeerAfter2Minutes.

@Test
public void startsSyncingWith1PeerAfter2Minutes() {
    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);
    Assert.assertFalse(syncEventsHandler.startSyncingWasCalled());
    Channel channel = mock(Channel.class);
    NodeID nodeID = new NodeID(HashUtil.randomPeerId());
    when(channel.getNodeId()).thenReturn(nodeID);
    Collection<Channel> peers = Collections.singletonList(channel);
    when(channelManager.getActivePeers()).thenReturn(peers);
    knownPeers.registerPeer(nodeID);
    syncState.newPeerStatus();
    syncState.tick(Duration.ofMinutes(2));
    Assert.assertTrue(syncEventsHandler.startSyncingWasCalled());
}
Also used : ChannelManager(org.ethereum.net.server.ChannelManager) Channel(org.ethereum.net.server.Channel) 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