Search in sources :

Example 61 with BlockGenerator

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());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 62 with BlockGenerator

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());
}
Also used : Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 63 with BlockGenerator

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);
}
Also used : DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Blockchain(org.ethereum.core.Blockchain) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimpleAsyncNode(co.rsk.net.simples.SimpleAsyncNode) Block(org.ethereum.core.Block) SyncConfiguration(co.rsk.net.sync.SyncConfiguration)

Example 64 with BlockGenerator

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());
}
Also used : BlockMessage(co.rsk.net.messages.BlockMessage) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimpleNode(co.rsk.net.simples.SimpleNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 65 with BlockGenerator

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());
}
Also used : EthMessage(org.ethereum.net.eth.message.EthMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) Eth62MessageFactory(org.ethereum.net.eth.message.Eth62MessageFactory) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Aggregations

BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)250 Test (org.junit.Test)237 Block (org.ethereum.core.Block)104 ArrayList (java.util.ArrayList)39 Blockchain (org.ethereum.core.Blockchain)38 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)36 RskSystemProperties (co.rsk.config.RskSystemProperties)35 SimpleBlock (co.rsk.peg.simples.SimpleBlock)34 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)33 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)31 HashMapDB (org.ethereum.datasource.HashMapDB)22 BlockBuilder (co.rsk.test.builders.BlockBuilder)17 Keccak256 (co.rsk.crypto.Keccak256)15 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)15 BlockStore (org.ethereum.db.BlockStore)15 World (co.rsk.test.World)14 RepositoryImpl (co.rsk.db.RepositoryImpl)13 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)13 Ignore (org.junit.Ignore)12 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)10