Search in sources :

Example 6 with Channel

use of org.ethereum.net.server.Channel 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)

Example 7 with Channel

use of org.ethereum.net.server.Channel 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 8 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncPool method logActivePeers.

void logActivePeers() {
    synchronized (activePeers) {
        if (activePeers.isEmpty()) {
            return;
        }
        logger.info("\n");
        logger.info("Active peers");
        logger.info("============");
        for (Channel peer : activePeers) {
            peer.logSyncStats();
        }
    }
}
Also used : Channel(org.ethereum.net.server.Channel)

Example 9 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class NodeMessageHandlerTest method processStatusMessageUsingSyncProcessor.

@Test()
public void processStatusMessageUsingSyncProcessor() throws UnknownHostException {
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    final ChannelManager channelManager = mock(ChannelManager.class);
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
    final Message[] msg = new Message[1];
    when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
        msg[0] = invocation.getArgumentAt(1, Message.class);
        return true;
    });
    final NodeMessageHandler handler = NodeMessageHandlerUtil.createHandlerWithSyncProcessor(SyncConfiguration.IMMEDIATE_FOR_TESTING, channelManager);
    BlockGenerator blockGenerator = new BlockGenerator();
    final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
    final Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
    final Message message = new StatusMessage(status);
    handler.processMessage(sender, message);
    Assert.assertNotNull(msg[0]);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, msg[0].getMessageType());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 10 with Channel

use of org.ethereum.net.server.Channel in project rskj by rsksmart.

the class SyncProcessorTest method processStatusWithAdvancedPeers.

@Test
public void processStatusWithAdvancedPeers() {
    final BlockStore store = new BlockStore();
    Blockchain blockchain = BlockChainBuilder.ofSize(0);
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(100, hash, parentHash, blockchain.getTotalDifficulty().add(new BlockDifficulty(BigInteger.TEN)));
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
    SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    Channel channel = mock(Channel.class);
    when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
    final List<Message> messages = new ArrayList<>();
    when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
        messages.add(invocation.getArgumentAt(1, Message.class));
        return true;
    });
    SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
    processor.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(1, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertFalse(ids.isEmpty());
    Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
    Assert.assertFalse(messages.isEmpty());
    Assert.assertEquals(1, messages.size());
    Message message = messages.get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Aggregations

Channel (org.ethereum.net.server.Channel)17 ChannelManager (org.ethereum.net.server.ChannelManager)13 Test (org.junit.Test)13 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)10 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 RskSystemProperties (co.rsk.config.RskSystemProperties)7 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)7 BlockDifficulty (co.rsk.core.BlockDifficulty)4 NodeID (co.rsk.net.NodeID)3 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)3 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 DownloadingHeadersSyncState (co.rsk.net.sync.DownloadingHeadersSyncState)2 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Coin (co.rsk.core.Coin)1 DifficultyCalculator (co.rsk.core.DifficultyCalculator)1 BlockExecutor (co.rsk.core.bc.BlockExecutor)1 MessageChannel (co.rsk.net.MessageChannel)1