use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.
the class WalletFactory method createPersistentWallet.
public static Wallet createPersistentWallet(String storeName) {
KeyValueDataSource ds = new LevelDbDataSource(new RskSystemProperties(), storeName);
ds.init();
return new Wallet(ds);
}
use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.
the class IndexedBlockStoreTest method test7.
// leveldb + mapdb, multi branch, total re-branch test
@Test
public void test7() 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 = genesis.getCumulativeDifficulty();
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, 50);
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);
indexedBlockStore.reBranch(forkBlock);
} finally {
blocksDB.close();
indexDB.close();
FileUtil.recursiveDelete(testDir);
}
}
use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.
the class BlockChainBuilder method build.
public BlockChainImpl build(boolean withoutCleaner) {
if (repository == null)
repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB().setClearOnClose(false)));
if (blockStore == null) {
blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
}
if (receiptStore == null) {
KeyValueDataSource ds = new HashMapDB();
ds.init();
receiptStore = new ReceiptStoreImpl(ds);
}
if (txinfos != null && !txinfos.isEmpty())
for (TransactionInfo txinfo : txinfos) receiptStore.add(txinfo.getBlockHash(), txinfo.getIndex(), txinfo.getReceipt());
EthereumListener listener = new BlockExecutorTest.SimpleEthereumListener();
BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(repository).blockStore(blockStore);
BlockValidator blockValidator = validatorBuilder.build();
if (this.adminInfo == null)
this.adminInfo = new AdminInfo();
BlockChainImpl blockChain = new BlockChainImpl(config, this.repository, this.blockStore, receiptStore, null, listener, this.adminInfo, blockValidator);
if (this.testing) {
blockChain.setBlockValidator(new DummyBlockValidator());
blockChain.setNoValidation(true);
}
TransactionPoolImpl transactionPool;
if (withoutCleaner) {
transactionPool = new TransactionPoolImplNoCleaner(config, blockChain.getRepository(), blockChain.getBlockStore(), receiptStore, new ProgramInvokeFactoryImpl(), new BlockExecutorTest.SimpleEthereumListener(), 10, 100);
} else {
transactionPool = new TransactionPoolImpl(config, blockChain.getRepository(), blockChain.getBlockStore(), receiptStore, new ProgramInvokeFactoryImpl(), new BlockExecutorTest.SimpleEthereumListener(), 10, 100);
}
blockChain.setTransactionPool(transactionPool);
if (this.genesis != null) {
for (RskAddress addr : this.genesis.getPremine().keySet()) {
this.repository.createAccount(addr);
this.repository.addBalance(addr, this.genesis.getPremine().get(addr).getAccountState().getBalance());
}
Repository track = this.repository.startTracking();
new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
track.commit();
this.genesis.setStateRoot(this.repository.getRoot());
this.genesis.flushRLP();
blockChain.setBestBlock(this.genesis);
blockChain.setTotalDifficulty(this.genesis.getCumulativeDifficulty());
}
if (this.blocks != null) {
BlockExecutor blockExecutor = new BlockExecutor(config, repository, receiptStore, blockStore, listener);
for (Block b : this.blocks) {
blockExecutor.executeAndFillAll(b, blockChain.getBestBlock());
blockChain.tryToConnect(b);
}
}
return blockChain;
}
use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.
the class DoPrune method doPrune.
public void doPrune(String[] args) throws Exception {
logger.info("Pruning Database");
int blocksToProcess = DEFAULT_BLOCKS_TO_PROCESS;
RskAddress contractAddress = DEFAULT_CONTRACT_ADDRESS;
CLIInterface.call(rskSystemProperties, args);
logger.info("Running {}, core version: {}-{}", rskSystemProperties.genesisInfo(), rskSystemProperties.projectVersion(), rskSystemProperties.projectVersionModifier());
BuildInfo.printInfo();
long height = this.blockchain.getBestBlock().getNumber();
String dataSourceName = getDataSourceName(contractAddress);
logger.info("Datasource Name {}", dataSourceName);
logger.info("Blockchain height {}", height);
TrieImpl source = new TrieImpl(new TrieStoreImpl(levelDbByName(this.rskSystemProperties, dataSourceName)), true);
KeyValueDataSource targetDataSource = levelDbByName(this.rskSystemProperties, dataSourceName + "B");
TrieStore targetStore = new TrieStoreImpl(targetDataSource);
this.processBlocks(height - blocksToProcess, source, contractAddress, targetStore);
targetDataSource.close();
}
use of org.ethereum.datasource.KeyValueDataSource in project rskj by rsksmart.
the class RskFactory method getWallet.
@Bean
public Wallet getWallet(RskSystemProperties config) {
if (!config.isWalletEnabled()) {
logger.info("Local wallet disabled");
return null;
}
logger.info("Local wallet enabled");
KeyValueDataSource ds = new LevelDbDataSource(config, "wallet");
ds.init();
return new Wallet(ds);
}
Aggregations