use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class SyncProcessorTest method processBlockHashResponseWithUnknownHash.
@Test(expected = Exception.class)
public void processBlockHashResponseWithUnknownHash() {
Blockchain blockchain = BlockChainBuilder.ofSize(0);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
SyncProcessor processor = new SyncProcessor(config, blockchain, null, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.processStatus(sender, new Status(100, null));
}
use of co.rsk.validators.ProofOfWorkRule 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 co.rsk.validators.ProofOfWorkRule 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.validators.ProofOfWorkRule 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());
}
use of co.rsk.validators.ProofOfWorkRule 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());
}
Aggregations