use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeBlockProcessorUnclesTest method addBlockWithTwoKnownUncles.
@Test
public void addBlockWithTwoKnownUncles() throws UnknownHostException {
BlockChainImpl blockChain = new BlockChainBuilder().build();
NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
Block genesis = blockChain.getBestBlock();
BlockBuilder blockBuilder = new BlockBuilder(blockChain, new BlockGenerator());
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);
SimpleMessageChannel sender = new SimpleMessageChannel();
processor.processBlock(sender, block2);
Assert.assertEquals(2, processor.getBlockchain().getBestBlock().getNumber());
Assert.assertArrayEquals(block2.getHash().getBytes(), processor.getBlockchain().getBestBlockHash());
Assert.assertTrue(sender.getGetBlockMessages().isEmpty());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeBlockProcessorUnclesTest method createNodeBlockProcessor.
private static NodeBlockProcessor createNodeBlockProcessor(BlockChainImpl blockChain) {
Block genesis = new BlockGenerator().getGenesisBlock();
genesis.setStateRoot(blockChain.getRepository().getRoot());
genesis.flushRLP();
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockChain, nodeInformation, syncConfiguration);
NodeBlockProcessor processor = new NodeBlockProcessor(store, blockChain, nodeInformation, blockSyncService, syncConfiguration);
return processor;
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeMessageHandlerTest method processBlockHeaderRequestMessageUsingBlockInBlockchain.
@Test
public void processBlockHeaderRequestMessageUsingBlockInBlockchain() throws UnknownHostException {
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 10);
for (Block b : blocks) blockchain.tryToConnect(b);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, new BlockHeadersRequestMessage(1, blocks.get(4).getHash().getBytes(), 1));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_RESPONSE_MESSAGE, message.getMessageType());
BlockHeadersResponseMessage bMessage = (BlockHeadersResponseMessage) message;
Assert.assertEquals(blocks.get(4).getHash(), bMessage.getBlockHeaders().get(0).getHash());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeMessageHandlerTest method skipProcessGenesisBlock.
@Test
public void skipProcessGenesisBlock() throws UnknownHostException {
SimpleMessageChannel sender = new SimpleMessageChannel();
PeerScoringManager scoring = createPeerScoringManager();
SimpleBlockProcessor sbp = new SimpleBlockProcessor();
NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, scoring, new DummyBlockValidationRule());
Block block = new BlockGenerator().getGenesisBlock();
Message message = new BlockMessage(block);
processor.processMessage(sender, message);
Assert.assertNotNull(sbp.getBlocks());
Assert.assertEquals(0, sbp.getBlocks().size());
Assert.assertTrue(scoring.isEmpty());
PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
Assert.assertNotNull(pscoring);
Assert.assertTrue(pscoring.isEmpty());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeMessageHandlerTest method skipAdvancedBlock.
@Test
public void skipAdvancedBlock() throws UnknownHostException {
SimpleMessageChannel sender = new SimpleMessageChannel();
PeerScoringManager scoring = createPeerScoringManager();
SimpleBlockProcessor sbp = new SimpleBlockProcessor();
sbp.setBlockGap(100000);
NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, scoring, new DummyBlockValidationRule());
Block block = new BlockGenerator().createBlock(200000, 0);
Message message = new BlockMessage(block);
processor.processMessage(sender, message);
Assert.assertNotNull(sbp.getBlocks());
Assert.assertEquals(0, sbp.getBlocks().size());
Assert.assertTrue(scoring.isEmpty());
PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
Assert.assertNotNull(pscoring);
Assert.assertTrue(pscoring.isEmpty());
}
Aggregations