Search in sources :

Example 1 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class ImportLightTest method createBlockchain.

public static BlockChainImpl createBlockchain(Genesis genesis) {
    RskSystemProperties config = new RskSystemProperties();
    config.setBlockchainConfig(new GenesisConfig(new GenesisConfig.GenesisConstants() {

        @Override
        public BlockDifficulty getMinimumDifficulty() {
            return new BlockDifficulty(BigInteger.ONE);
        }
    }));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    EthereumListenerAdapter listener = new EthereumListenerAdapter();
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, listener, new AdminInfo(), new DummyBlockValidator());
    blockchain.setNoValidation(true);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
    blockchain.setTransactionPool(transactionPool);
    Repository track = repository.startTracking();
    for (RskAddress addr : genesis.getPremine().keySet()) {
        track.createAccount(addr);
        track.addBalance(addr, genesis.getPremine().get(addr).getAccountState().getBalance());
    }
    track.commit();
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    blockchain.setBestBlock(genesis);
    blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
    return blockchain;
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) AdminInfo(org.ethereum.manager.AdminInfo) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) EthereumListenerAdapter(org.ethereum.listener.EthereumListenerAdapter) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) BlockDifficulty(co.rsk.core.BlockDifficulty) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) RskSystemProperties(co.rsk.config.RskSystemProperties) ReceiptStore(org.ethereum.db.ReceiptStore) GenesisConfig(org.ethereum.config.blockchain.GenesisConfig)

Example 2 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class ContractDetailsImplTest method usingSameExternalStorage.

@Test
public void usingSameExternalStorage() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Trie trie = new TrieImpl(store, false);
    byte[] accountAddress = randomAddress();
    ContractDetailsImpl details = new ContractDetailsImpl(config, accountAddress, trie, null);
    int nkeys = IN_MEMORY_STORAGE_LIMIT;
    for (int k = 1; k <= nkeys + 1; k++) details.put(new DataWord(k), new DataWord(k * 2));
    Assert.assertTrue(details.hasExternalStorage());
    details.syncStorage();
    ContractDetailsImpl details1 = new ContractDetailsImpl(config, details.getEncoded());
    ContractDetailsImpl details2 = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertTrue(details1.hasExternalStorage());
    Assert.assertTrue(details2.hasExternalStorage());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details1.get(new DataWord(k)));
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details2.get(new DataWord(k)));
    details1.syncStorage();
    details2.syncStorage();
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) TrieImpl(co.rsk.trie.TrieImpl) DataWord(org.ethereum.vm.DataWord) TestUtils.randomDataWord(org.ethereum.TestUtils.randomDataWord) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 3 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class ContractDetailsImplTest method externalStorageTransition.

@Test
public void externalStorageTransition() {
    byte[] address = randomAddress();
    byte[] code = randomBytes(512);
    Map<DataWord, DataWord> elements = new HashMap<>();
    HashMapDB externalStorage = new HashMapDB();
    ContractDetailsImpl original = new ContractDetailsImpl(config, address, new TrieImpl(new TrieStoreImpl(externalStorage), true), code);
    for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT - 1; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    ContractDetails deserialized = new ContractDetailsImpl(config, original.getEncoded());
    // adds keys for in-memory storage limit overflow
    for (int i = 0; i < 10; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        deserialized.put(key, value);
    }
    deserialized.syncStorage();
    deserialized = new ContractDetailsImpl(config, deserialized.getEncoded());
    Map<DataWord, DataWord> storage = deserialized.getStorage();
    Assert.assertEquals(elements.size(), storage.size());
    for (DataWord key : elements.keySet()) {
        Assert.assertEquals(elements.get(key), storage.get(key));
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) TrieImpl(co.rsk.trie.TrieImpl) DataWord(org.ethereum.vm.DataWord) TestUtils.randomDataWord(org.ethereum.TestUtils.randomDataWord) HashMapDB(org.ethereum.datasource.HashMapDB) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 4 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class RepositoryImplTest method flushNoReconnect.

@Test
public void flushNoReconnect() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    RepositoryImpl repository = new RepositoryImpl(config, store);
    RskAddress accAddress = randomAccountAddress();
    byte[] initialRoot = repository.getRoot();
    repository.createAccount(accAddress);
    repository.flushNoReconnect();
    Assert.assertTrue(repository.isExist(accAddress));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) RskAddress(co.rsk.core.RskAddress) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TrieImplHashTest(co.rsk.trie.TrieImplHashTest) Test(org.junit.Test)

Example 5 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.

the class BlockChainImplTest method addInvalidMGPBlock.

@Test
public void addInvalidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(repository).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Block genesis = getGenesisBlock(blockChain);
    Block block = new BlockBuilder().minGasPrice(BigInteger.ONE).parent(genesis).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(1L), BigInteger.TEN);
    tx.sign(new byte[] { 22, 11, 00 });
    txs.add(tx);
    block = new BlockBuilder().transactions(txs).minGasPrice(BigInteger.valueOf(11L)).parent(genesis).build();
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

TrieStoreImpl (co.rsk.trie.TrieStoreImpl)38 HashMapDB (org.ethereum.datasource.HashMapDB)34 TrieStore (co.rsk.trie.TrieStore)25 Trie (co.rsk.trie.Trie)21 Test (org.junit.Test)20 MutableRepository (org.ethereum.db.MutableRepository)12 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 RskAddress (co.rsk.core.RskAddress)8 DataWord (org.ethereum.vm.DataWord)8 TrieImpl (co.rsk.trie.TrieImpl)7 Repository (org.ethereum.core.Repository)7 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)7 RepositoryImpl (co.rsk.db.RepositoryImpl)6 ArrayList (java.util.ArrayList)6 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6 MutableTrieImpl (co.rsk.db.MutableTrieImpl)5 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)5 RskSystemProperties (co.rsk.config.RskSystemProperties)4 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)4 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)4