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());
}
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());
}
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();
}
}
}
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());
}
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());
}
Aggregations