Search in sources :

Example 1 with BlockStore

use of co.rsk.net.BlockStore in project rskj by rsksmart.

the class BlockUtilsTest method unknowAncestorsHashes.

@Test
public void unknowAncestorsHashes() {
    BlockChainImpl blockChain = new BlockChainBuilder().build();
    BlockStore store = new BlockStore();
    Block genesis = new BlockGenerator().getGenesisBlock();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    Block block1 = new BlockBuilder().difficulty(2l).parent(genesis).build();
    Block block1b = new BlockBuilder().difficulty(1l).parent(genesis).build();
    Block block2 = new BlockBuilder().parent(block1).build();
    Block block3 = new BlockBuilder().parent(block2).build();
    store.saveBlock(block3);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, blockChain.tryToConnect(block1b));
    Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
}
Also used : BlockStore(co.rsk.net.BlockStore) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 2 with BlockStore

use of co.rsk.net.BlockStore in project rskj by rsksmart.

the class BlockUtilsTest method unknowAncestorsHashesUsingUncles.

@Test
public void unknowAncestorsHashesUsingUncles() {
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockGenerator blockGenerator = new BlockGenerator();
    Genesis genesis = blockGenerator.getGenesisBlock();
    BlockChainImpl blockChain = blockChainBuilder.setGenesis(genesis).build();
    BlockStore store = new BlockStore();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, blockGenerator);
    Block block1 = blockBuilder.parent(genesis).build();
    Block block1b = blockBuilder.parent(genesis).build();
    Block block2 = blockBuilder.parent(block1).build();
    Block uncle1 = blockBuilder.parent(block1).build();
    Block uncle2 = blockBuilder.parent(block1).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block3 = blockBuilder.parent(block2).uncles(uncles).build();
    store.saveBlock(block3);
    blockChain.tryToConnect(genesis);
    blockChain.tryToConnect(block1);
    blockChain.tryToConnect(block1b);
    Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(3, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    Assert.assertTrue(hashes.contains(uncle1.getHash()));
    Assert.assertTrue(hashes.contains(uncle2.getHash()));
}
Also used : BlockStore(co.rsk.net.BlockStore) ArrayList(java.util.ArrayList) Genesis(org.ethereum.core.Genesis) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 Keccak256 (co.rsk.crypto.Keccak256)2 BlockStore (co.rsk.net.BlockStore)2 BlockBuilder (co.rsk.test.builders.BlockBuilder)2 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)2 Block (org.ethereum.core.Block)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 BlockHeader (org.ethereum.core.BlockHeader)1 Genesis (org.ethereum.core.Genesis)1