use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class RemascStorageProviderTest method setSaveRetrieveAndGetManySiblings.
@Test
public void setSaveRetrieveAndGetManySiblings() throws IOException {
RskAddress accountAddress = randomAddress();
Repository repository = new RepositoryImplForTesting();
RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block block2 = blockGenerator.createChildBlock(block1);
Block block3 = blockGenerator.createChildBlock(block2);
Block block4 = blockGenerator.createChildBlock(block3);
Block block5 = blockGenerator.createChildBlock(block4);
Sibling sibling1 = new Sibling(genesis.getHeader(), genesis.getCoinbase(), 1);
Sibling sibling2 = new Sibling(block1.getHeader(), block1.getCoinbase(), 2);
Sibling sibling3 = new Sibling(block2.getHeader(), block2.getCoinbase(), 3);
Sibling sibling4 = new Sibling(block3.getHeader(), block3.getCoinbase(), 4);
Sibling sibling5 = new Sibling(block4.getHeader(), block4.getCoinbase(), 5);
Sibling sibling6 = new Sibling(block5.getHeader(), block5.getCoinbase(), 6);
List<Sibling> siblings0 = new ArrayList<>();
List<Sibling> siblings1 = new ArrayList<>();
List<Sibling> siblings2 = new ArrayList<>();
siblings0.add(sibling1);
siblings0.add(sibling2);
siblings1.add(sibling3);
siblings1.add(sibling4);
siblings2.add(sibling5);
siblings2.add(sibling6);
provider.getSiblings().put(Long.valueOf(0), siblings0);
provider.getSiblings().put(Long.valueOf(1), siblings1);
provider.getSiblings().put(Long.valueOf(2), siblings2);
provider.save();
RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);
SortedMap<Long, List<Sibling>> map = newProvider.getSiblings();
Assert.assertNotNull(map);
Assert.assertFalse(map.isEmpty());
Assert.assertTrue(map.containsKey(Long.valueOf(0)));
Assert.assertTrue(map.containsKey(Long.valueOf(1)));
Assert.assertTrue(map.containsKey(Long.valueOf(2)));
Assert.assertEquals(2, map.get(Long.valueOf(0)).size());
Assert.assertEquals(2, map.get(Long.valueOf(1)).size());
Assert.assertEquals(2, map.get(Long.valueOf(2)).size());
List<Sibling> list0 = map.get(Long.valueOf(0));
List<Sibling> list1 = map.get(Long.valueOf(1));
List<Sibling> list2 = map.get(Long.valueOf(2));
Assert.assertEquals(1, list0.get(0).getIncludedHeight());
Assert.assertArrayEquals(genesis.getHeader().getHash().getBytes(), list0.get(0).getHash());
Assert.assertEquals(2, list0.get(1).getIncludedHeight());
Assert.assertArrayEquals(block1.getHeader().getHash().getBytes(), list0.get(1).getHash());
Assert.assertEquals(3, list1.get(0).getIncludedHeight());
Assert.assertArrayEquals(block2.getHeader().getHash().getBytes(), list1.get(0).getHash());
Assert.assertEquals(4, list1.get(1).getIncludedHeight());
Assert.assertArrayEquals(block3.getHeader().getHash().getBytes(), list1.get(1).getHash());
Assert.assertEquals(5, list2.get(0).getIncludedHeight());
Assert.assertArrayEquals(block4.getHeader().getHash().getBytes(), list2.get(0).getHash());
Assert.assertEquals(6, list2.get(1).getIncludedHeight());
Assert.assertArrayEquals(block5.getHeader().getHash().getBytes(), list2.get(1).getHash());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class SiblingTest method siblingSerializeWithGenesis.
@Test
public void siblingSerializeWithGenesis() {
Block genesis = new BlockGenerator().getGenesisBlock();
Sibling sibling = new Sibling(genesis.getHeader(), genesis.getCoinbase(), 1);
byte[] bytes = sibling.getEncoded();
Assert.assertNotNull(bytes);
Sibling result = Sibling.create(bytes);
Assert.assertNotNull(result);
Assert.assertArrayEquals(sibling.getHash(), result.getHash());
Assert.assertEquals(sibling.getIncludedBlockCoinbase(), result.getIncludedBlockCoinbase());
Assert.assertArrayEquals(sibling.getEncoded(), result.getEncoded());
Assert.assertEquals(sibling.getIncludedHeight(), result.getIncludedHeight());
Assert.assertEquals(sibling.getPaidFees(), result.getPaidFees());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class TwoAsyncNodeTest method createNode.
private static SimpleAsyncNode createNode(int size) {
final World world = new World();
final BlockStore store = new BlockStore();
final Blockchain blockchain = world.getBlockChain();
List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), size);
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 processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
NodeMessageHandler handler = new NodeMessageHandler(config, processor, null, null, null, null, null, new DummyBlockValidationRule());
return new SimpleAsyncNode(handler);
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class TwoNodeTest method buildBlockchainPartialAndSynchronize.
@Test
@Ignore
public void buildBlockchainPartialAndSynchronize() {
SimpleNode node1 = createNode(0);
SimpleNode node2 = createNode(0);
List<Block> blocks = new BlockGenerator().getBlockChain(10);
for (Block block : blocks) {
BlockMessage message = new BlockMessage(block);
node1.receiveMessageFrom(null, message);
if (block.getNumber() <= 5)
node2.receiveMessageFrom(null, message);
}
node1.sendStatusTo(node2);
Assert.assertEquals(10, node1.getBestBlock().getNumber());
Assert.assertEquals(10, node2.getBestBlock().getNumber());
Assert.assertEquals(node1.getBestBlock().getHash(), node2.getBestBlock().getHash());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class RskMessageTest method encodeDecodeGetBlockMessage.
@Test
public void encodeDecodeGetBlockMessage() {
Block block = new BlockGenerator().getBlock(1);
GetBlockMessage message = new GetBlockMessage(block.getHash().getBytes());
RskMessage rskmessage = new RskMessage(config, message);
byte[] encoded = rskmessage.getEncoded();
Eth62MessageFactory factory = new Eth62MessageFactory(config);
EthMessage ethmessage = (EthMessage) factory.create((byte) 0x08, encoded);
Assert.assertNotNull(ethmessage);
Assert.assertEquals(EthMessageCodes.RSK_MESSAGE, ethmessage.getCommand());
RskMessage result = (RskMessage) ethmessage;
Assert.assertNotNull(result.getMessage());
Message resultMessage = result.getMessage();
Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, resultMessage.getMessageType());
Assert.assertArrayEquals(block.getHash().getBytes(), ((GetBlockMessage) resultMessage).getBlockHash());
}
Aggregations