use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers.
@Test
public void dontSyncWithoutAdvancedPeerAfterTimeoutWaitingPeers() {
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(0);
byte[] hash = HashUtil.randomHash();
byte[] parentHash = HashUtil.randomHash();
Status status = new Status(0, hash, parentHash, blockchain.getTotalDifficulty());
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(), getChannelManager(), SyncConfiguration.DEFAULT, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
processor.processStatus(sender, status);
Assert.assertEquals(1, processor.getPeersCount());
Assert.assertEquals(0, processor.getNoAdvancedPeers());
Set<NodeID> ids = processor.getKnownPeersNodeIDs();
Assert.assertFalse(ids.isEmpty());
Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
Assert.assertTrue(sender.getMessages().isEmpty());
processor.onTimePassed(Duration.ofMinutes(1));
Assert.assertTrue(sender.getMessages().isEmpty());
processor.onTimePassed(Duration.ofMinutes(1));
Assert.assertTrue(sender.getMessages().isEmpty());
}
use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method processSkeletonResponseWithoutBlockIdentifiers.
@Test
public void processSkeletonResponseWithoutBlockIdentifiers() {
Blockchain blockchain = BlockChainBuilder.ofSize(0);
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<BlockIdentifier> blockIdentifiers = new ArrayList<>();
SkeletonResponseMessage response = new SkeletonResponseMessage(new Random().nextLong(), blockIdentifiers);
processor.registerExpectedMessage(response);
processor.processSkeletonResponse(sender, response);
Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
Assert.assertTrue(sender.getMessages().isEmpty());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of co.rsk.config.RskSystemProperties 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());
}
use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method processStatusWithPeerWithSameDifficulty.
@Test
public void processStatusWithPeerWithSameDifficulty() {
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(100);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
byte[] hash = HashUtil.randomHash();
byte[] parentHash = HashUtil.randomHash();
Status status = new Status(blockchain.getStatus().getBestBlockNumber(), hash, parentHash, blockchain.getStatus().getTotalDifficulty());
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(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.processStatus(sender, status);
Assert.assertEquals(1, processor.getPeersCount());
Assert.assertEquals(0, processor.getNoAdvancedPeers());
Assert.assertTrue(sender.getMessages().isEmpty());
// is null when we're not syncing
Assert.assertEquals(0, processor.getExpectedResponses().size());
Assert.assertFalse(processor.isPeerSyncing(sender.getPeerNodeID()));
}
use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseAddsToBlockchain.
@Test
public void processBodyResponseAddsToBlockchain() {
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(10);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
Assert.assertEquals(11, block.getNumber());
Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
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(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
List<Transaction> transactions = blockchain.getBestBlock().getTransactionsList();
List<BlockHeader> uncles = blockchain.getBestBlock().getUncleList();
long lastRequestId = new Random().nextLong();
BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
processor.registerExpectedMessage(response);
Deque<BlockHeader> headerStack = new ArrayDeque<>();
headerStack.add(block.getHeader());
List<Deque<BlockHeader>> headers = new ArrayList<>();
headers.add(headerStack);
List<BlockIdentifier> bids = new ArrayList<>();
bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
processor.startDownloadingBodies(headers, Collections.singletonMap(sender.getPeerNodeID(), bids));
((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
processor.processBodyResponse(sender, response);
Assert.assertEquals(11, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Aggregations