use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class SyncProcessorTest method processBlockHeadersResponseWithOneExistingHeader.
@Test
public void processBlockHeadersResponseWithOneExistingHeader() {
Blockchain blockchain = new BlockChainBuilder().ofSize(3);
Block block = blockchain.getBlockByNumber(2);
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
TestSystemProperties config = new TestSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, new NetBlockStore(), blockchain, new BlockNodeInformation(), syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), blockSyncService, syncConfiguration, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), syncConfiguration, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
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.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class AsyncNodeBlockProcessorTest method processSkeletonRequestWithThreeResults.
@Test
public void processSkeletonRequestWithThreeResults() {
final int skeletonStep = 192;
final Blockchain blockchain = new BlockChainBuilder().ofSize(300);
final NetBlockStore store = new NetBlockStore();
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
final TestSystemProperties config = new TestSystemProperties();
final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE);
final SimplePeer sender = new SimplePeer();
processor.processSkeletonRequest(sender, 100, 5);
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.SKELETON_RESPONSE_MESSAGE, message.getMessageType());
final SkeletonResponseMessage bMessage = (SkeletonResponseMessage) message;
Assert.assertEquals(100, bMessage.getId());
final Block b1 = blockchain.getBlockByNumber(0);
final Block b2 = blockchain.getBlockByNumber(skeletonStep);
final Block b3 = blockchain.getBestBlock();
final BlockIdentifier[] expected = { new BlockIdentifier(b1.getHash().getBytes(), b1.getNumber()), new BlockIdentifier(b2.getHash().getBytes(), b2.getNumber()), new BlockIdentifier(b3.getHash().getBytes(), b3.getNumber()) };
assertBlockIdentifiers(expected, bMessage.getBlockIdentifiers());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class AsyncNodeBlockProcessorTest method processTwoBlockListsAddingToBlockchainWithFork.
@Test(timeout = WAIT_TIME)
public void processTwoBlockListsAddingToBlockchainWithFork() throws InterruptedException {
final NetBlockStore store = new NetBlockStore();
final Blockchain blockchain = new BlockChainBuilder().ofSize(0);
final Block genesis = blockchain.getBestBlock();
final BlockGenerator blockGenerator = new BlockGenerator();
final List<Block> blocks = blockGenerator.getBlockChain(genesis, 10);
final List<Block> blocks2 = blockGenerator.getBlockChain(blocks.get(4), 20);
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
final TestSystemProperties config = new TestSystemProperties();
final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
final AsyncNodeBlockProcessorListener listener = new AsyncNodeBlockProcessorListener();
final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE, listener);
processor.start();
BlockProcessResult blockProcessResult = processor.processBlock(null, genesis);
if (blockProcessResult.isScheduledForProcessing()) {
listener.waitForBlock(genesis.getHash());
}
Assert.assertEquals(0, store.size());
Block blockToWait = null;
for (Block b : blocks) {
blockProcessResult = processor.processBlock(null, b);
if (blockProcessResult.isScheduledForProcessing()) {
blockToWait = b;
}
}
for (Block b : blocks2) {
blockProcessResult = processor.processBlock(null, b);
if (blockProcessResult.isScheduledForProcessing()) {
blockToWait = b;
}
}
if (blockToWait != null) {
listener.waitForBlock(blockToWait.getHash());
}
Assert.assertEquals(25, blockchain.getBestBlock().getNumber());
Assert.assertEquals(0, store.size());
processor.stopAndWait(WAIT_TIME);
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class AsyncNodeBlockProcessorTest method duplicatedBlock.
@Test
public void duplicatedBlock() {
final NetBlockStore store = new NetBlockStore();
final Peer sender = new SimplePeer();
final Blockchain blockchain = new BlockChainBuilder().ofSize(0);
final BlockGenerator blockGenerator = new BlockGenerator();
final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
final Block sameBlock = new Block(block.getHeader(), block.getTransactionsList(), block.getUncleList(), true, true);
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
final TestSystemProperties config = new TestSystemProperties();
final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE);
processor.processBlock(sender, block);
BlockProcessResult blockProcessResult = processor.processBlock(sender, sameBlock);
Assert.assertFalse(blockProcessResult.isScheduledForProcessing());
Assert.assertFalse(blockProcessResult.wasBlockAdded(block));
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class AsyncNodeBlockProcessorTest method processGetBlockMessageUsingBlockInStore.
@Test
public void processGetBlockMessageUsingBlockInStore() {
final Block block = new BlockGenerator().getBlock(3);
final Keccak256 blockHash = block.getHash();
final NetBlockStore store = new NetBlockStore();
store.saveBlock(block);
final Blockchain blockchain = new BlockChainBuilder().ofSize(0);
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
final TestSystemProperties config = new TestSystemProperties();
final BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE);
final AsyncNodeBlockProcessor processor = new AsyncNodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration, DummyBlockValidator.VALID_RESULT_INSTANCE, DummyBlockValidator.VALID_RESULT_INSTANCE);
final SimplePeer sender = new SimplePeer();
Assert.assertTrue(nodeInformation.getNodesByBlock(block.getHash()).isEmpty());
processor.processGetBlock(sender, block.getHash().getBytes());
Assert.assertTrue(nodeInformation.getNodesByBlock(block.getHash()).contains(sender.getPeerNodeID()));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
final BlockMessage bMessage = (BlockMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
Aggregations