Search in sources :

Example 6 with KeyValueDataSource

use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.

the class IndexedBlockStoreTest method test8.

// leveldb + mapdb, multi branch, total re-branch test
@Test
public void test8() throws IOException {
    BigInteger bi = new BigInteger(32, new Random());
    String testDir = "test_db_" + bi;
    config.setDataBaseDir(testDir);
    DB indexDB = createMapDB(testDir);
    Map<Long, List<IndexedBlockStore.BlockInfo>> indexMap = createIndexMap(indexDB);
    KeyValueDataSource blocksDB = new LevelDbDataSource(config, "blocks");
    blocksDB.init();
    try {
        IndexedBlockStore indexedBlockStore = new IndexedBlockStore(indexMap, blocksDB, indexDB);
        Block genesis = Genesis.getInstance(config);
        List<Block> bestLine = getRandomChain(genesis.getHash().getBytes(), 1, 100);
        indexedBlockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
        BlockDifficulty td = BlockDifficulty.ZERO;
        for (int i = 0; i < bestLine.size(); ++i) {
            Block newBlock = bestLine.get(i);
            td = td.add(newBlock.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(newBlock, td, true);
        }
        byte[] forkParentHash = bestLine.get(60).getHash().getBytes();
        long forkParentNumber = bestLine.get(60).getNumber();
        List<Block> forkLine = getRandomChain(forkParentHash, forkParentNumber + 1, 10);
        for (int i = 0; i < forkLine.size(); ++i) {
            Block newBlock = forkLine.get(i);
            Block parentBlock = indexedBlockStore.getBlockByHash(newBlock.getParentHash().getBytes());
            td = indexedBlockStore.getTotalDifficultyForHash(parentBlock.getHash().getBytes());
            td = td.add(newBlock.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(newBlock, td, false);
        }
        Block bestBlock = bestLine.get(bestLine.size() - 1);
        Block forkBlock = forkLine.get(forkLine.size() - 1);
        assertTrue(indexedBlockStore.getBestBlock().getNumber() == 100);
        indexedBlockStore.reBranch(forkBlock);
        assertTrue(indexedBlockStore.getBestBlock().getNumber() == 71);
        // Assert that all fork moved to the main line
        for (Block currBlock : forkLine) {
            Long number = currBlock.getNumber();
            Block chainBlock = indexedBlockStore.getChainBlockByNumber(number);
            assertEquals(currBlock.getShortHash(), chainBlock.getShortHash());
        }
        // Assert that all fork moved to the main line
        // re-branch back to previous line and assert that
        // all the block really moved
        bestBlock = bestLine.get(bestLine.size() - 1);
        indexedBlockStore.reBranch(bestBlock);
        for (Block currBlock : bestLine) {
            Long number = currBlock.getNumber();
            Block chainBlock = indexedBlockStore.getChainBlockByNumber(number);
            assertEquals(currBlock.getShortHash(), chainBlock.getShortHash());
        }
    } finally {
        blocksDB.close();
        indexDB.close();
        FileUtil.recursiveDelete(testDir);
    }
}
Also used : LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) HashMapDB(org.ethereum.datasource.HashMapDB) DB(org.mapdb.DB) Test(org.junit.Test)

Example 7 with KeyValueDataSource

use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.

the class DataSourcePoolTest method openAndCloseTenTimes.

@Test
public void openAndCloseTenTimes() {
    for (int k = 0; k < 10; k++) {
        KeyValueDataSource dataSource = DataSourcePool.levelDbByName(config, "test4");
        dataSource.put(new byte[] { (byte) k }, new byte[] { (byte) k });
        byte[] result = dataSource.get(new byte[] { (byte) k });
        Assert.assertNotNull(result);
        Assert.assertEquals(1, result.length);
        Assert.assertEquals((byte) k, result[0]);
    }
    for (int k = 0; k < 10; k++) {
        DataSourcePool.closeDataSource("test4");
    }
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Test(org.junit.Test)

Example 8 with KeyValueDataSource

use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.

the class DataSourcePoolTest method openUseAndCloseDataSourceTwice.

@Test
public void openUseAndCloseDataSourceTwice() {
    KeyValueDataSource dataSource = DataSourcePool.levelDbByName(config, "test3");
    KeyValueDataSource dataSource2 = DataSourcePool.levelDbByName(config, "test3");
    Assert.assertSame(dataSource, dataSource2);
    dataSource.put(new byte[] { 0x01 }, new byte[] { 0x02 });
    DataSourcePool.closeDataSource("test3");
    byte[] result = dataSource2.get(new byte[] { 0x01 });
    Assert.assertNotNull(result);
    Assert.assertEquals(1, result.length);
    Assert.assertEquals(0x02, result[0]);
    DataSourcePool.closeDataSource("test3");
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Test(org.junit.Test)

Example 9 with KeyValueDataSource

use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.

the class HashMapDBTest method putKeyValue.

@Test
public void putKeyValue() {
    KeyValueDataSource ds = new HashMapDB();
    byte[] key = new byte[] { 0x01, 0x02 };
    byte[] value = new byte[] { 0x03, 0x03 };
    byte[] result = ds.put(key, value);
    Assert.assertNull(result);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 10 with KeyValueDataSource

use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.

the class HashMapDBTest method putAndDeleteKeyValue.

@Test
public void putAndDeleteKeyValue() {
    KeyValueDataSource ds = new HashMapDB();
    byte[] key = new byte[] { 0x01, 0x02 };
    byte[] value = new byte[] { 0x03, 0x03 };
    ds.put(key, value);
    ds.delete(key);
    byte[] result = ds.get(key);
    Assert.assertNull(result);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Aggregations

KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)26 HashMapDB (org.ethereum.datasource.HashMapDB)13 Test (org.junit.Test)13 LevelDbDataSource (org.ethereum.datasource.LevelDbDataSource)10 BlockDifficulty (co.rsk.core.BlockDifficulty)6 Block (org.ethereum.core.Block)6 DB (org.mapdb.DB)6 BigInteger (java.math.BigInteger)5 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)4 Bean (org.springframework.context.annotation.Bean)4 RskAddress (co.rsk.core.RskAddress)3 RepositoryImpl (co.rsk.db.RepositoryImpl)3 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)3 EthereumListener (org.ethereum.listener.EthereumListener)3 AdminInfo (org.ethereum.manager.AdminInfo)3 RskSystemProperties (co.rsk.config.RskSystemProperties)2 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)2 ReceiptStore (org.ethereum.db.ReceiptStore)2 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)2