Search in sources :

Example 16 with TrieStoreImpl

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

the class RskContext method buildTrieStore.

protected synchronized TrieStore buildTrieStore(Path trieStorePath) {
    checkIfNotClosed();
    int statesCacheSize = getRskSystemProperties().getStatesCacheSize();
    KeyValueDataSource ds = LevelDbDataSource.makeDataSource(trieStorePath);
    if (statesCacheSize != 0) {
        CacheSnapshotHandler cacheSnapshotHandler = getRskSystemProperties().shouldPersistStatesCacheSnapshot() ? new CacheSnapshotHandler(resolveCacheSnapshotPath(trieStorePath)) : null;
        ds = new DataSourceWithCache(ds, statesCacheSize, cacheSnapshotHandler);
    }
    return new TrieStoreImpl(ds);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) DataSourceWithCache(org.ethereum.datasource.DataSourceWithCache) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) CacheSnapshotHandler(org.ethereum.datasource.CacheSnapshotHandler)

Example 17 with TrieStoreImpl

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

the class BridgeUtilsTest method isFreeBridgeTx.

private void isFreeBridgeTx(boolean expected, RskAddress destinationAddress, byte[] privKeyBytes) {
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(constants.getBridgeConstants().getBtcParams()), constants.getBridgeConstants(), activationConfig);
    Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
    org.ethereum.core.Transaction rskTx = CallTransaction.createCallTransaction(0, 1, 1, destinationAddress, 0, Bridge.UPDATE_COLLECTIONS, constants.getChainId());
    rskTx.sign(privKeyBytes);
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Repository repository = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(trieStore, new Trie())));
    Block rskExecutionBlock = new BlockGenerator().createChildBlock(getGenesisInstance(trieStore));
    bridge.init(rskTx, rskExecutionBlock, repository.startTracking(), null, null, null);
    Assert.assertEquals(expected, BridgeUtils.isFreeBridgeTx(rskTx, constants, activationConfig.forBlock(rskExecutionBlock.getNumber())));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) MutableTrieCache(co.rsk.db.MutableTrieCache) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Block(org.ethereum.core.Block) MutableTrieImpl(co.rsk.db.MutableTrieImpl) Transaction(org.ethereum.core.Transaction) Trie(co.rsk.trie.Trie)

Example 18 with TrieStoreImpl

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

the class TransactionTest method multiSuicideTest.

@Test
public void multiSuicideTest() throws IOException, InterruptedException {
    /*
        Original contract

        pragma solidity ^0.4.3;

        contract PsychoKiller {
            function () payable {}

            function homicide() {
                suicide(msg.sender);
            }

            function multipleHomicide() {
                PsychoKiller k  = this;
                k.homicide();
                k.homicide();
                k.homicide();
                k.homicide();
            }
        }

         */
    BigInteger nonce = config.getNetworkConstants().getInitialNonce();
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    MutableRepository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
    IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
    Blockchain blockchain = ImportLightTest.createBlockchain(new TestGenesisLoader(trieStore, getClass().getResourceAsStream("/genesis/genesis-light.json"), nonce, false, true, true).load(), config, repository, blockStore, trieStore);
    ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
    System.out.println("address: " + ByteUtil.toHexString(sender.getAddress()));
    String code = "6060604052341561000c57fe5b5b6102938061001c6000396000f3006060604052361561004a576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806309e587a514610053578063de990da914610065575b6100515b5b565b005b341561005b57fe5b610063610077565b005b341561006d57fe5b610075610092565b005b3373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60003090508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156100fa57fe5b60325a03f1151561010757fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561016d57fe5b60325a03f1151561017a57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156101e057fe5b60325a03f115156101ed57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561025357fe5b60325a03f1151561026057fe5b5050505b505600a165627a7a72305820084e74021c556522723b6725354378df2fb4b6732f82dd33f5daa29e2820b37c0029";
    String abi = "[{\"constant\":false,\"inputs\":[],\"name\":\"homicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"multipleHomicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"payable\":true,\"type\":\"fallback\"}]";
    Transaction tx = createTx(sender, new byte[0], Hex.decode(code), repository);
    executeTransaction(blockchain, blockStore, tx, repository, blockTxSignatureCache);
    byte[] contractAddress = tx.getContractAddress().getBytes();
    CallTransaction.Contract contract1 = new CallTransaction.Contract(abi);
    byte[] callData = contract1.getByName("multipleHomicide").encode();
    Assert.assertNull(contract1.getConstructor());
    Assert.assertNotNull(contract1.parseInvocation(callData));
    Assert.assertNotNull(contract1.parseInvocation(callData).toString());
    try {
        contract1.parseInvocation(new byte[32]);
        Assert.fail();
    } catch (RuntimeException ex) {
    }
    try {
        contract1.parseInvocation(new byte[2]);
        Assert.fail();
    } catch (RuntimeException ex) {
    }
    Transaction tx1 = createTx(sender, contractAddress, callData, repository);
    ProgramResult programResult = executeTransaction(blockchain, blockStore, tx1, repository, blockTxSignatureCache).getResult();
    // suicide of a single account should be counted only once
    Assert.assertEquals(24000, programResult.getFutureRefund());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ProgramResult(org.ethereum.vm.program.ProgramResult) ECKey(org.ethereum.crypto.ECKey) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) MutableTrieImpl(co.rsk.db.MutableTrieImpl) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 19 with TrieStoreImpl

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

the class CommonConfig method repository.

@Bean
public Repository repository(RskSystemProperties config) {
    String databaseDir = config.databaseDir();
    if (config.databaseReset()) {
        FileUtil.recursiveDelete(databaseDir);
        logger.info("Database reset done");
    }
    KeyValueDataSource ds = makeDataSource(config, "state");
    KeyValueDataSource detailsDS = makeDataSource(config, "details");
    return new RepositoryImpl(config, new TrieStoreImpl(ds), detailsDS);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) RepositoryImpl(co.rsk.db.RepositoryImpl) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Bean(org.springframework.context.annotation.Bean)

Example 20 with TrieStoreImpl

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

the class ContractDetailsImplTest method syncStorageWithExternalStorage.

@Test
public void syncStorageWithExternalStorage() {
    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();
    int ssize = details.getStorageSize();
    details = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertEquals(ssize, details.getStorageSize());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details.get(new DataWord(k)));
    ContractDetailsImpl clone = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertNotNull(clone);
    Assert.assertTrue(clone.hasExternalStorage());
    Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(clone.get(new DataWord(k)));
    for (int k = 1; k <= nkeys + 1; k++) clone.put(new DataWord(k), new DataWord(k * 3));
    Assert.assertTrue(clone.hasExternalStorage());
    Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
    ContractDetailsImpl snapshot = (ContractDetailsImpl) clone.getSnapshotTo(clone.getStorageHash());
    Assert.assertTrue(snapshot.hasExternalStorage());
    Assert.assertEquals(clone.getStorageSize(), snapshot.getStorageSize());
}
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)

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