Search in sources :

Example 21 with KeyValueDataSource

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);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 22 with KeyValueDataSource

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);
    }
}
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 23 with KeyValueDataSource

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;
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) HashMap(java.util.HashMap) AdminInfo(org.ethereum.manager.AdminInfo) HashMapDB(org.ethereum.datasource.HashMapDB) BlockValidator(co.rsk.validators.BlockValidator) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) RepositoryImpl(co.rsk.db.RepositoryImpl) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) RskAddress(co.rsk.core.RskAddress) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource)

Example 24 with KeyValueDataSource

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();
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) TrieImpl(co.rsk.trie.TrieImpl) RskAddress(co.rsk.core.RskAddress) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) TrieStore(co.rsk.trie.TrieStore)

Example 25 with KeyValueDataSource

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);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) Bean(org.springframework.context.annotation.Bean)

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