use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeMessageHandlerTest method processGetBlockHeaderMessageUsingEmptyStore.
@Test
public void processGetBlockHeaderMessageUsingEmptyStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
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 DummyBlockValidationRule());
final SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, new GetBlockHeadersMessage(block.getHash().getBytes(), 1));
Assert.assertTrue(sender.getMessages().isEmpty());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class NodeMessageHandlerTest method processGetBlockMessageUsingBlockInStore.
@Test
public void processGetBlockMessageUsingBlockInStore() 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 GetBlockMessage(block.getHash().getBytes()));
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());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class OneAsyncNodeTest method buildBlockchainInReverse.
@Test
public void buildBlockchainInReverse() throws InterruptedException {
SimpleAsyncNode node = createNode();
List<Block> blocks = new BlockGenerator().getBlockChain(getGenesis(), 10);
List<Block> reverse = new ArrayList<>();
for (Block block : blocks) reverse.add(0, block);
for (Block block : reverse) node.receiveMessageFrom(null, new BlockMessage(block));
node.waitExactlyNTasksWithTimeout(10);
node.joinWithTimeout();
Assert.assertEquals(blocks.size(), node.getBestBlock().getNumber());
Assert.assertEquals(blocks.get(blocks.size() - 1).getHash(), node.getBestBlock().getHash());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class OneNodeTest method buildBlockchainInReverse.
@Test
public void buildBlockchainInReverse() {
SimpleNode node = SimpleNode.createNode();
List<Block> blocks = new BlockGenerator().getBlockChain(getGenesis(), 10);
List<Block> reverse = new ArrayList<>();
for (Block block : blocks) reverse.add(0, block);
for (Block block : reverse) node.receiveMessageFrom(null, new BlockMessage(block));
Assert.assertEquals(blocks.size(), node.getBestBlock().getNumber());
Assert.assertEquals(blocks.get(blocks.size() - 1).getHash(), node.getBestBlock().getHash());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.
@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
Account senderAccount = createAccount("sender");
Account receiverAccount = createAccount("receiver");
List<Account> accounts = new ArrayList<Account>();
List<Coin> balances = new ArrayList<>();
accounts.add(senderAccount);
balances.add(Coin.valueOf(20000000));
accounts.add(receiverAccount);
balances.add(Coin.ZERO);
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(0, false, accounts, balances);
Block genesis = blockchain.getBestBlock();
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
Transaction tx = createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO);
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
Block block = new BlockGenerator().createChildBlock(genesis, txs, blockchain.getRepository().getRoot());
BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
Assert.assertEquals(1, block.getTransactionsList().size());
blockExecutor.executeAndFillAll(block, genesis);
Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
Assert.assertEquals(1, block.getTransactionsList().size());
Assert.assertEquals(1, 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 = block.getTransactionsList();
List<BlockHeader> uncles = block.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(1, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Aggregations