use of org.ethereum.net.server.Channel in project rskj by rsksmart.
the class SyncProcessorTest method sendBlockHashRequest.
@Test
public void sendBlockHashRequest() {
Blockchain blockchain = BlockChainBuilder.ofSize(0);
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 Message[] msg = new Message[1];
when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
msg[0] = invocation.getArgumentAt(1, Message.class);
return true;
});
SyncProcessor processor = new SyncProcessor(config, blockchain, null, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
processor.sendBlockHashRequest(100);
Message message = msg[0];
Assert.assertNotNull(message);
Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
BlockHashRequestMessage request = (BlockHashRequestMessage) message;
Assert.assertNotEquals(0, request.getId());
Assert.assertEquals(100, request.getHeight());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of org.ethereum.net.server.Channel in project rskj by rsksmart.
the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.
@Test
public void syncWithAdvancedStatusAnd5Peers() {
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);
final ChannelManager channelManager = mock(ChannelManager.class);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.DEFAULT, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
List<SimpleMessageChannel> senders = new ArrayList<>();
List<Channel> channels = new ArrayList<>();
Map<NodeID, List<Message>> messagesByNode = new HashMap<>();
int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
for (int i = 0; i < lessPeers; i++) {
SimpleMessageChannel sender = new SimpleMessageChannel();
senders.add(sender);
Channel channel = mock(Channel.class);
messagesByNode.put(sender.getPeerNodeID(), new ArrayList<>());
when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
messagesByNode.get(sender.getPeerNodeID()).add(invocation.getArgumentAt(1, Message.class));
return true;
});
}
messagesByNode.values().forEach(m -> Assert.assertTrue(m.isEmpty()));
senders.forEach(s -> processor.processStatus(s, status));
messagesByNode.values().forEach(m -> Assert.assertTrue(m.isEmpty()));
Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
Set<NodeID> ids = processor.getKnownPeersNodeIDs();
senders.stream().map(SimpleMessageChannel::getPeerNodeID).forEach(peerId -> Assert.assertTrue(ids.contains(peerId)));
SimpleMessageChannel lastSender = new SimpleMessageChannel();
Channel channel = mock(Channel.class);
when(channel.getNodeId()).thenReturn(lastSender.getPeerNodeID());
Assert.assertFalse(ids.contains(lastSender.getPeerNodeID()));
processor.processStatus(lastSender, status);
// now test with all senders
senders.add(lastSender);
Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
Assert.assertFalse(messagesByNode.values().stream().allMatch(m -> m.isEmpty()));
Assert.assertEquals(1, messagesByNode.values().stream().mapToInt(List::size).sum());
Message message = messagesByNode.values().stream().filter(m -> !m.isEmpty()).findFirst().get().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
use of org.ethereum.net.server.Channel in project rskj by rsksmart.
the class SyncProcessorTest method findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks.
@Test
public void findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks() {
Blockchain blockchain = BlockChainBuilder.ofSize(0);
Blockchain advancedBlockchain = BlockChainBuilder.ofSize(100);
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;
});
BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new DummyBlockValidationRule(), DIFFICULTY_CALCULATOR);
processor.processStatus(sender, StatusUtils.fromBlockchain(advancedBlockchain));
BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
long[] expectedHeights = new long[] { 50, 25, 12, 6, 3, 1 };
for (int k = 0; k < expectedHeights.length; k++) {
Assert.assertEquals(k + 2, messages.size());
Message message = messages.get(k + 1);
Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
BlockHashRequestMessage request = (BlockHashRequestMessage) message;
long requestId = request.getId();
Assert.assertEquals(expectedHeights[k], request.getHeight());
Block block = advancedBlockchain.getBlockByNumber(expectedHeights[k]);
processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
}
Assert.assertEquals(expectedHeights.length + 2, messages.size());
Message message = messages.get(messages.size() - 1);
Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
SkeletonRequestMessage request = (SkeletonRequestMessage) message;
Assert.assertEquals(0, request.getStartNumber());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of org.ethereum.net.server.Channel in project rskj by rsksmart.
the class SyncProcessorTest method processSkeletonResponseWithConnectionPoint.
@Test
public void processSkeletonResponseWithConnectionPoint() {
Blockchain blockchain = BlockChainBuilder.ofSize(25);
final BlockStore store = new BlockStore();
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 Message[] msg = new Message[1];
when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
msg[0] = 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);
int connectionPoint = 25;
int step = 10;
int linkCount = 10;
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), connectionPoint);
processor.startDownloadingSkeleton(connectionPoint);
List<BlockIdentifier> blockIdentifiers = buildSkeleton(blockchain, connectionPoint, step, linkCount);
SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
processor.registerExpectedMessage(response);
processor.processSkeletonResponse(sender, response);
Message message = msg[0];
Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
Assert.assertEquals(5, request.getCount());
Assert.assertArrayEquals(blockIdentifiers.get(1).getHash(), request.getHash());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of org.ethereum.net.server.Channel 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());
}
Aggregations