use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class SyncProcessorTest method syncWithAdvancedPeerAfterTimeoutWaitingPeers.
@Test
public void syncWithAdvancedPeerAfterTimeoutWaitingPeers() {
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);
SyncConfiguration syncConfiguration = SyncConfiguration.DEFAULT;
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, 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.assertTrue(messages.isEmpty());
processor.onTimePassed(syncConfiguration.getTimeoutWaitingPeers().dividedBy(2));
Assert.assertTrue(messages.isEmpty());
processor.onTimePassed(syncConfiguration.getTimeoutWaitingPeers().dividedBy(2));
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());
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class SyncProcessorTest method processBlockHeadersResponseWithOneExistingHeader.
@Test
public void processBlockHeadersResponseWithOneExistingHeader() {
Blockchain blockchain = BlockChainBuilder.ofSize(3);
Block block = blockchain.getBlockByNumber(2);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, new BlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), syncConfiguration, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
List<BlockHeader> headers = new ArrayList<>();
headers.add(block.getHeader());
BlockHeadersResponseMessage response = new BlockHeadersResponseMessage(new Random().nextLong(), headers);
processor.registerExpectedMessage(response);
processor.processBlockHeadersResponse(sender, response);
Assert.assertEquals(0, sender.getMessages().size());
Assert.assertEquals(0, processor.getExpectedResponses().size());
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class ThreeAsyncNodeUsingSyncProcessorTest method synchronizeNewNodeWithThreePeers400Different.
@Test
public void synchronizeNewNodeWithThreePeers400Different() {
Blockchain b1 = BlockChainBuilder.ofSize(0, true);
Blockchain b2 = BlockChainBuilder.copyAndExtend(b1, 200);
Blockchain b3 = BlockChainBuilder.copyAndExtend(b2, 200);
SimpleAsyncNode node1 = SimpleAsyncNode.createDefaultNode(b2);
SimpleAsyncNode node2 = SimpleAsyncNode.createDefaultNode(b2);
SimpleAsyncNode node3 = SimpleAsyncNode.createDefaultNode(b3);
SyncConfiguration syncConfiguration = new SyncConfiguration(3, 1, 10, 100, 20, 192);
SimpleAsyncNode node4 = SimpleAsyncNode.createNode(b1, syncConfiguration);
Assert.assertEquals(200, node1.getBestBlock().getNumber());
Assert.assertEquals(200, node2.getBestBlock().getNumber());
Assert.assertEquals(400, node3.getBestBlock().getNumber());
Assert.assertEquals(0, node4.getBestBlock().getNumber());
node1.sendFullStatusTo(node4);
node2.sendFullStatusTo(node4);
node3.sendFullStatusTo(node4);
// sync setup
int setupRequests = SyncUtils.syncSetupRequests(400, 0, syncConfiguration);
node4.waitUntilNTasksWithTimeout(setupRequests);
// synchronize 50 new blocks from node 1
node4.waitExactlyNTasksWithTimeout(400 + setupRequests - 10);
Assert.assertTrue(node3.getSyncProcessor().getExpectedResponses().isEmpty());
Assert.assertTrue(node4.getSyncProcessor().getExpectedResponses().isEmpty());
Assert.assertEquals(400, node4.getBestBlock().getNumber());
Assert.assertEquals(node4.getBestBlock().getHash(), node3.getBestBlock().getHash());
Assert.assertTrue(node1.getSyncProcessor().getExpectedResponses().isEmpty());
Assert.assertTrue(node2.getSyncProcessor().getExpectedResponses().isEmpty());
Assert.assertTrue(node3.getSyncProcessor().getExpectedResponses().isEmpty());
Assert.assertTrue(node4.getSyncProcessor().getExpectedResponses().isEmpty());
node1.joinWithTimeout();
node2.joinWithTimeout();
node3.joinWithTimeout();
node4.joinWithTimeout();
Assert.assertFalse(node1.getSyncProcessor().isPeerSyncing(node4.getNodeID()));
Assert.assertFalse(node2.getSyncProcessor().isPeerSyncing(node4.getNodeID()));
Assert.assertFalse(node3.getSyncProcessor().isPeerSyncing(node4.getNodeID()));
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class TwoAsyncNodeTest method createNode.
private static SimpleAsyncNode createNode(int size) {
final World world = new World();
final BlockStore store = new BlockStore();
final Blockchain blockchain = world.getBlockChain();
List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), size);
for (Block b : blocks) blockchain.tryToConnect(b);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
NodeMessageHandler handler = new NodeMessageHandler(config, processor, null, null, null, null, null, new DummyBlockValidationRule());
return new SimpleAsyncNode(handler);
}
use of co.rsk.net.sync.SyncConfiguration in project rskj by rsksmart.
the class NodeBlockProcessorTest method processGetBlockMessageUsingEmptyStore.
@Test
public void processGetBlockMessageUsingEmptyStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final BlockStore store = new BlockStore();
final Blockchain blockchain = BlockChainBuilder.ofSize(0);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
processor.processGetBlock(sender, block.getHash().getBytes());
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
Assert.assertTrue(sender.getMessages().isEmpty());
}
Aggregations