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);
}
}
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");
}
}
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");
}
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);
}
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);
}
Aggregations