use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class NodeBlockProcessorUnclesTest method addBlockWithTwoUnknownUncles.
@Test
public void addBlockWithTwoUnknownUncles() {
BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
BlockChainImpl blockChain = blockChainBuilder.build();
org.ethereum.db.BlockStore blockStore = blockChainBuilder.getBlockStore();
NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
Block genesis = blockChain.getBestBlock();
BlockBuilder blockBuilder = new BlockBuilder(blockChain, null, blockStore).trieStore(blockChainBuilder.getTrieStore());
blockBuilder.parent(blockChain.getBestBlock());
Block block1 = blockBuilder.parent(genesis).build();
Block uncle1 = blockBuilder.parent(genesis).build();
Block uncle2 = blockBuilder.parent(genesis).build();
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle1.getHeader());
uncles.add(uncle2.getHeader());
Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
processor.processBlock(null, block1);
SimplePeer sender = new SimplePeer();
processor.processBlock(sender, block2);
Assert.assertEquals(2, blockChain.getBestBlock().getNumber());
Assert.assertArrayEquals(block2.getHash().getBytes(), blockChain.getBestBlockHash());
Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method sendBlockHashRequest.
@Test
public void sendBlockHashRequest() {
Blockchain blockchain = new BlockChainBuilder().ofSize(0);
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
final ChannelManager channelManager = mock(ChannelManager.class);
Peer channel = mock(Peer.class);
when(channel.getPeerNodeID()).thenReturn(sender.getPeerNodeID());
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), null, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
processor.sendBlockHashRequest(sender, 100);
Assert.assertFalse(sender.getMessages().isEmpty());
Message message = sender.getMessages().get(0);
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.net.simples.SimplePeer in project rskj by rsksmart.
the class AsyncNodeBlockProcessorUnclesTest method addBlockWithTwoKnownUncles.
@Test(timeout = WAIT_TIME)
public void addBlockWithTwoKnownUncles() throws InterruptedException {
org.ethereum.db.BlockStore blockStore = blockChainBuilder.getBlockStore();
Block genesis = blockChain.getBestBlock();
BlockBuilder blockBuilder = new BlockBuilder(blockChain, null, blockStore).trieStore(blockChainBuilder.getTrieStore());
blockBuilder.parent(blockChain.getBestBlock());
Block block1 = blockBuilder.parent(genesis).build();
Block uncle1 = blockBuilder.parent(genesis).build();
Block uncle2 = blockBuilder.parent(genesis).build();
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle1.getHeader());
uncles.add(uncle2.getHeader());
Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
processor.processBlock(null, block1);
processor.processBlock(null, uncle1);
processor.processBlock(null, uncle2);
SimplePeer sender = new SimplePeer();
processor.processBlock(sender, block2);
listener.waitForBlock(block2.getHash());
Assert.assertEquals(2, blockChain.getBestBlock().getNumber());
Assert.assertArrayEquals(block2.getHash().getBytes(), blockChain.getBestBlockHash());
Assert.assertTrue(sender.getGetBlockMessages().isEmpty());
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class AsyncNodeBlockProcessorUnclesTest method rejectBlockWithTwoUnknownUnclesAndUnknownParent.
@Test
public void rejectBlockWithTwoUnknownUnclesAndUnknownParent() throws InterruptedException {
Block genesis = blockChain.getBestBlock();
Block block1 = new BlockBuilder(null, null, null).parent(genesis).build();
Block uncle1 = new BlockBuilder(null, null, null).parent(genesis).build();
Block uncle2 = new BlockBuilder(null, null, null).parent(genesis).build();
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle1.getHeader());
uncles.add(uncle2.getHeader());
Block block2 = new BlockBuilder(null, null, null).parent(block1).uncles(uncles).build();
SimplePeer sender = new SimplePeer();
processor.processBlock(sender, block2);
Assert.assertEquals(0, blockChain.getBestBlock().getNumber());
Assert.assertArrayEquals(genesis.getHash().getBytes(), blockChain.getBestBlockHash());
Assert.assertEquals(1, sender.getGetBlockMessages().size());
Assert.assertTrue(sender.getGetBlockMessagesHashes().contains(block1.getHash()));
}
use of co.rsk.net.simples.SimplePeer in project rskj by rsksmart.
the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.
@Test
public void syncWithAdvancedStatusAnd5Peers() {
final NetBlockStore store = new NetBlockStore();
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.ofSize(0);
BlockStore blockStore = builder.getBlockStore();
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();
TestSystemProperties config = new TestSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
final ChannelManager channelManager = mock(ChannelManager.class);
SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.DEFAULT, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.DEFAULT, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
List<SimplePeer> senders = new ArrayList<>();
int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
for (int i = 0; i < lessPeers; i++) {
SimplePeer sender = new SimplePeer();
senders.add(sender);
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
}
Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
senders.forEach(s -> processor.processStatus(s, status));
Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
Set<NodeID> knownPeersNodeIDs = processor.getKnownPeersNodeIDs();
Assert.assertTrue(senders.stream().map(SimplePeer::getPeerNodeID).allMatch(knownPeersNodeIDs::contains));
SimplePeer lastSender = new SimplePeer();
Assert.assertFalse(processor.getKnownPeersNodeIDs().contains(lastSender.getPeerNodeID()));
processor.processStatus(lastSender, status);
// now test with all senders
senders.add(lastSender);
Set<NodeID> ids = processor.getKnownPeersNodeIDs();
Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
Assert.assertFalse(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
Assert.assertEquals(1, senders.stream().map(SimplePeer::getMessages).mapToInt(List::size).sum());
Message message = senders.stream().map(SimplePeer::getMessages).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