Search in sources :

Example 11 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class IndexedBlockStoreTest method rewind.

@Test
public void rewind() {
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(mock(BlockFactory.class), mock(KeyValueDataSource.class), new HashMapBlocksIndex());
    long blocksToGenerate = 14;
    for (long i = 0; i < blocksToGenerate; i++) {
        Block block = mock(Block.class);
        Keccak256 blockHash = randomHash();
        when(block.getHash()).thenReturn(blockHash);
        when(block.getNumber()).thenReturn(i);
        indexedBlockStore.saveBlock(block, ZERO, true);
    }
    Block bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is(blocksToGenerate - 1));
    long blockToRewind = blocksToGenerate / 2;
    indexedBlockStore.rewind(blockToRewind);
    bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is(blockToRewind));
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 12 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class IndexedBlockStoreTest method test3.

@Test
@Ignore
public void test3() {
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockDifficulty cummDiff = BlockDifficulty.ZERO;
    for (Block block : blocks) {
        cummDiff = cummDiff.add(block.getCumulativeDifficulty());
        indexedBlockStore.saveBlock(block, cummDiff, true);
    }
    indexedBlockStore.flush();
    // testing:   getTotalDifficultyForHash(byte[])
    // testing:   getMaxNumber()
    long bestIndex = blocks.get(blocks.size() - 1).getNumber();
    assertEquals(bestIndex, indexedBlockStore.getMaxNumber());
    assertEquals(cumDifficulty, indexedBlockStore.getTotalDifficultyForHash(blocks.get(blocks.size() - 1).getHash().getBytes()));
    // testing:  getBlockByHash(byte[])
    Block block = blocks.get(50);
    Block block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getBlockByHash(Hex.decode("00112233"));
    assertEquals(null, block_);
    // testing:  getChainBlockByNumber(long)
    block = blocks.get(50);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getChainBlockByNumber(10000);
    assertEquals(null, block_);
    // testing: getBlocksInformationByNumber(long)
    block = blocks.get(50);
    BlockInformation blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(150);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(0);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    block = blocks.get(8003);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    int blocksNum = indexedBlockStore.getBlocksInformationByNumber(10000).size();
    assertEquals(0, blocksNum);
    // testing: getListHashesEndWith(byte[], long)
    block = blocks.get(8003);
    List<byte[]> hashList = indexedBlockStore.getListHashesEndWith(block.getHash().getBytes(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(8003 - i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
    // testing: getListHashesStartWith(long, long)
    block = blocks.get(7003);
    hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(7003 + i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class IndexedBlockStoreTest method test2.

// save some load, and check it exist
@Test
@Ignore
public void test2() {
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockDifficulty cummDiff = BlockDifficulty.ZERO;
    for (Block block : blocks) {
        cummDiff = cummDiff.add(block.getCumulativeDifficulty());
        indexedBlockStore.saveBlock(block, cummDiff, true);
    }
    // testing:   getTotalDifficultyForHash(byte[])
    // testing:   getMaxNumber()
    long bestIndex = blocks.get(blocks.size() - 1).getNumber();
    assertEquals(bestIndex, indexedBlockStore.getMaxNumber());
    assertEquals(cumDifficulty, indexedBlockStore.getTotalDifficultyForHash(blocks.get(blocks.size() - 1).getHash().getBytes()));
    // testing:  getBlockByHash(byte[])
    Block block = blocks.get(50);
    Block block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getBlockByHash(Hex.decode("00112233"));
    assertEquals(null, block_);
    // testing:  getChainBlockByNumber(long)
    block = blocks.get(50);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getChainBlockByNumber(10000);
    assertEquals(null, block_);
    // testing: getBlocksInformationByNumber(long)
    block = blocks.get(50);
    BlockInformation blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(150);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(0);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    block = blocks.get(8003);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    int blocksNum = indexedBlockStore.getBlocksInformationByNumber(10000).size();
    assertEquals(0, blocksNum);
    // testing: getListHashesEndWith(byte[], long)
    block = blocks.get(8003);
    List<byte[]> hashList = indexedBlockStore.getListHashesEndWith(block.getHash().getBytes(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(8003 - i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
    // testing: getListHashesStartWith(long, long)
    block = blocks.get(7003);
    hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(7003 + i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class IndexedBlockStoreTest method test9.

// test index merging during the flush
@Test
@Ignore("Ethereum block format")
public void test9() {
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    // blocks with the same block number
    Block block1 = blockFactory.decodeBlock(Hex.decode("f90202f901fda0ad0d51e8d64c364a7b77ef2fe252f3f4df0940c7cfa69cedc1" + "fbd6ea66894936a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413" + "f0a142fd40d493479414a3bc0f103706650a19c5d24e5c4cf1ea5af78ea0e058" + "0f4fdd1e3ae8346efaa6b1018605361f6e2fb058580e31414c8cbf5b0d49a056" + "e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0" + "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + "b901000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000008605065cf2c43a8303e52e832fefd8808455fcbe1b80a017247341fd5d" + "2f1d384682fea9302065a95dbd3e4f8260dde88a386f3cb95be3880f3fc8d5e0c87378c0c0"));
    Block block2 = blockFactory.decodeBlock(Hex.decode("f90218f90213a0c63fc3626abc6f6ba695064e973126cccc6fd513d4f53485e1" + "1794a8855e8b2ba01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413" + "f0a142fd40d49347941dcb8d1f0fcc8cbc8c2d76528e877f915e299fbea0ccb2" + "ed2a8c585409fe5530d36320bc8c1406454b32a9e419e890ea49489e534aa056" + "e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0" + "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421" + "b901000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000000" + "0000008605079eb238d88303e52e832fefd8808455fcbe2596d5830101038447" + "65746885676f312e35856c696e7578a0a673a429161eb32e6d0887b2bce2b12b" + "1edd6e4b4cf55371853cba13d57118bd88d44d3609c7e203c7c0c0"));
    indexedBlockStore.saveBlock(block1, block1.getCumulativeDifficulty(), true);
    indexedBlockStore.flush();
    indexedBlockStore.saveBlock(block2, block2.getCumulativeDifficulty(), true);
    indexedBlockStore.flush();
    assertEquals(block1.getCumulativeDifficulty(), indexedBlockStore.getTotalDifficultyForHash(block1.getHash().getBytes()));
    assertEquals(block2.getCumulativeDifficulty(), indexedBlockStore.getTotalDifficultyForHash(block2.getHash().getBytes()));
}
Also used : Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class BlockValidatorTest method getBlockOneAncestorSet.

@Test
public void getBlockOneAncestorSet() {
    IndexedBlockStore store = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    Block block = new BlockGenerator().createChildBlock(genesis);
    Set<Keccak256> ancestors = FamilyUtils.getAncestors(store, block, 6);
    Assert.assertFalse(ancestors.isEmpty());
    Assert.assertTrue(ancestors.contains(genesis.getHash()));
    Assert.assertFalse(ancestors.contains(block.getHash()));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Aggregations

HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)24 HashMapDB (org.ethereum.datasource.HashMapDB)23 Test (org.junit.Test)23 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)19 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)13 Block (org.ethereum.core.Block)8 BlockDifficulty (co.rsk.core.BlockDifficulty)5 Keccak256 (co.rsk.crypto.Keccak256)5 Ignore (org.junit.Ignore)4 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)3 ArrayList (java.util.ArrayList)3 BlockStore (org.ethereum.db.BlockStore)3 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)2 MutableTrieImpl (co.rsk.db.MutableTrieImpl)2 RepositoryLocator (co.rsk.db.RepositoryLocator)2 Trie (co.rsk.trie.Trie)2 TrieStore (co.rsk.trie.TrieStore)2 BigInteger (java.math.BigInteger)2 BlockFactory (org.ethereum.core.BlockFactory)2