use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processBlockHeaderRequestMessageUsingBlockInStore.
@Test
public void processBlockHeaderRequestMessageUsingBlockInStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
store.saveBlock(block);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
final SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, new BlockHeadersRequestMessage(1, block.getHash().getBytes(), 1));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_RESPONSE_MESSAGE, message.getMessageType());
final BlockHeadersResponseMessage bMessage = (BlockHeadersResponseMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlockHeaders().get(0).getHash());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processGetBlockHeadersMessage.
@Test
@Ignore("Block headers message is deprecated must be rewrited or deleted")
public void processGetBlockHeadersMessage() throws UnknownHostException {
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
final List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 10);
for (Block b : blocks) {
ImportResult result = blockchain.tryToConnect(b);
System.out.println(result);
}
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
int baseBlock = 9;
/*
TestCase represents a GetBlockHeaderMessage test case: it receives an input and the expected BlockHeaders.
if expected == null, we are not expecting to receive any message.
*/
class TestCase {
protected final GetBlockHeadersMessage gbhMessage;
protected final List<BlockHeader> expected;
public TestCase(@Nonnull final GetBlockHeadersMessage gbhMessage, final List<BlockHeader> expected) {
this.gbhMessage = gbhMessage;
this.expected = expected;
}
}
TestCase[] testCases = { new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 0), null), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getHash().getBytes(), 5), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 1).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 3).getHeader(), blocks.get(baseBlock - 4).getHeader())), new TestCase(new GetBlockHeadersMessage(0, blocks.get(baseBlock).getHash().getBytes(), 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), 1), Arrays.asList(blocks.get(baseBlock).getHeader())), new TestCase(new GetBlockHeadersMessage(blocks.get(baseBlock).getNumber(), null, 5, 1, false), Arrays.asList(blocks.get(baseBlock).getHeader(), blocks.get(baseBlock - 2).getHeader(), blocks.get(baseBlock - 4).getHeader(), blocks.get(baseBlock - 6).getHeader(), blocks.get(baseBlock - 8).getHeader())) };
for (int i = 0; i < testCases.length; i += 1) {
final TestCase testCase = testCases[i];
final SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, testCase.gbhMessage);
if (testCase.expected == null) {
Assert.assertEquals(0, sender.getMessages().size());
continue;
}
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_MESSAGE, message.getMessageType());
final List<BlockHeader> headers = ((BlockHeadersMessage) message).getBlockHeaders();
equalBlockHeaders(testCase.expected, headers);
}
}
use of co.rsk.validators.ProofOfWorkRule 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.validators.ProofOfWorkRule 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.validators.ProofOfWorkRule 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