Search in sources :

Example 11 with TrieStoreImpl

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

the class BlockExecutorTest method generateBlockWithOneTransaction.

private static TestObjects generateBlockWithOneTransaction() {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Repository repository = new MutableRepository(trieStore, new Trie(trieStore));
    Repository track = repository.startTracking();
    Account account = createAccount("acctest1", track, Coin.valueOf(30000));
    Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
    track.commit();
    Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
    BlockExecutor executor = buildBlockExecutor(trieStore);
    Transaction tx1 = Transaction.builder().nonce(repository.getNonce(account.getAddress())).gasPrice(BigInteger.ONE).gasLimit(BigInteger.valueOf(21000)).destination(account2.getAddress()).chainId(CONFIG.getNetworkConstants().getChainId()).value(BigInteger.TEN).build();
    tx1.sign(account.getEcKey().getPrivKeyBytes());
    Transaction tx = tx1;
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    List<BlockHeader> uncles = new ArrayList<>();
    // getGenesisBlock() modifies the repository, adding some pre-mined accounts
    // Not nice for a getter, but it is what it is :(
    Block genesis = BlockChainImplTest.getGenesisBlock(trieStore);
    genesis.setStateRoot(repository.getRoot());
    // Returns the root state prior block execution but after loading
    // some sample accounts (account/account2) and the premined accounts
    // in genesis.
    byte[] rootPriorExecution = repository.getRoot();
    Block block = new BlockGenerator().createChildBlock(genesis, txs, uncles, 1, null);
    executor.executeAndFill(block, genesis.getHeader());
    repository.save();
    return new TestObjects(trieStore, block, genesis, tx, account, rootPriorExecution);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie)

Example 12 with TrieStoreImpl

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

the class BlockExecutorTest method validateStateRootWithRskip126DisabledAndValidStateRoot.

@Test
public void validateStateRootWithRskip126DisabledAndValidStateRoot() {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Trie trie = new Trie(trieStore);
    Block block = new BlockGenerator().getBlock(1);
    block.setStateRoot(trie.getHash().getBytes());
    BlockResult blockResult = new BlockResult(block, Collections.emptyList(), Collections.emptyList(), 0, Coin.ZERO, trie);
    RskSystemProperties cfg = spy(CONFIG);
    ActivationConfig activationConfig = spy(cfg.getActivationConfig());
    doReturn(false).when(activationConfig).isActive(eq(RSKIP126), anyLong());
    doReturn(activationConfig).when(cfg).getActivationConfig();
    BlockExecutor executor = buildBlockExecutor(trieStore, cfg);
    Assert.assertTrue(executor.validateStateRoot(block.getHeader(), blockResult));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) Trie(co.rsk.trie.Trie) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Example 13 with TrieStoreImpl

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

the class BlockExecutorTest method validateStateRootWithRskip126DisabledAndInvalidStateRoot.

@Test
public void validateStateRootWithRskip126DisabledAndInvalidStateRoot() {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Trie trie = new Trie(trieStore);
    Block block = new BlockGenerator().getBlock(1);
    block.setStateRoot(new byte[] { 1, 2, 3, 4 });
    BlockResult blockResult = new BlockResult(block, Collections.emptyList(), Collections.emptyList(), 0, Coin.ZERO, trie);
    RskSystemProperties cfg = spy(CONFIG);
    ActivationConfig activationConfig = spy(cfg.getActivationConfig());
    doReturn(false).when(activationConfig).isActive(eq(RSKIP126), anyLong());
    doReturn(activationConfig).when(cfg).getActivationConfig();
    BlockExecutor executor = buildBlockExecutor(trieStore, cfg);
    Assert.assertTrue(executor.validateStateRoot(block.getHeader(), blockResult));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) Trie(co.rsk.trie.Trie) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Example 14 with TrieStoreImpl

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

the class RepositoryImplOriginalTest method testMultiThread.

// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    MutableTrieImpl mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
    final Repository repository = new MutableRepository(mutableTrie);
    final DataWord cowKey1 = DataWord.valueFromHex("c1");
    final DataWord cowKey2 = DataWord.valueFromHex("c2");
    final DataWord cowVal0 = DataWord.valueFromHex("c0a0");
    Repository track2 = repository.startTracking();
    track2.addStorageRow(COW, cowKey2, cowVal0);
    track2.commit();
    // Changes commited to repository
    assertThat(repository.getStorageValue(COW, cowKey2), is(cowVal0));
    final CountDownLatch failSema = new CountDownLatch(1);
    // First create the 10 snapshots. The snapshots should not be created while the
    // repository is being changed.
    Repository[] snaps = new Repository[10];
    for (int i = 0; i < 10; ++i) {
        snaps[i] = new MutableRepository(trieStore, trieStore.retrieve(repository.getRoot()).get());
    }
    for (int i = 0; i < 10; ++i) {
        int finalI = i;
        new Thread(() -> {
            try {
                int cnt = 1;
                while (running) {
                    Repository snap = snaps[finalI].startTracking();
                    snap.addBalance(COW, Coin.valueOf(10L));
                    snap.addStorageRow(COW, cowKey1, DataWord.valueOf(cnt));
                    snap.rollback();
                    cnt++;
                }
            } catch (Throwable e) {
                e.printStackTrace();
                failSema.countDown();
            }
        }).start();
    }
    new Thread(() -> {
        int cnt = 1;
        try {
            while (running) {
                Repository track21 = repository.startTracking();
                DataWord cVal = DataWord.valueOf(cnt);
                track21.addStorageRow(COW, cowKey1, cVal);
                track21.addBalance(COW, Coin.valueOf(1L));
                track21.commit();
                assertEquals(BigInteger.valueOf(cnt), repository.getBalance(COW).asBigInteger());
                assertEquals(cVal, repository.getStorageValue(COW, cowKey1));
                assertEquals(cowVal0, repository.getStorageValue(COW, cowKey2));
                cnt++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            try {
                repository.addStorageRow(COW, cowKey1, DataWord.valueOf(123));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            failSema.countDown();
        }
    }).start();
    failSema.await(10, TimeUnit.SECONDS);
    running = false;
    if (failSema.getCount() == 0) {
        throw new RuntimeException("Test failed.");
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) CountDownLatch(java.util.concurrent.CountDownLatch) TrieStore(co.rsk.trie.TrieStore) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 15 with TrieStoreImpl

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

the class RepositoryTest method setUp.

@Before
public void setUp() {
    trieStore = new TrieStoreImpl(new HashMapDB());
    mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
    repository = new MutableRepository(mutableTrie);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) MutableRepository(org.ethereum.db.MutableRepository) HashMapDB(org.ethereum.datasource.HashMapDB) Trie(co.rsk.trie.Trie) Before(org.junit.Before)

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