Search in sources :

Example 21 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class RepositoryImplOriginalTest method test17.

@Test
public void test17() {
    Repository repository = new RepositoryImpl(config);
    byte[] cowKey1 = "key-c-1".getBytes();
    byte[] cowValue1 = "val-c-1".getBytes();
    // changes level_1
    Repository track1 = repository.startTracking();
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addStorageRow(COW, new DataWord(cowKey1), new DataWord(cowValue1));
    assertEquals(new DataWord(cowValue1), track2.getStorageValue(COW, new DataWord(cowKey1)));
    track2.rollback();
    // leaving level_2
    track1.commit();
    // leaving level_1
    Assert.assertEquals(Hex.toHexString(HashUtil.EMPTY_TRIE_HASH), Hex.toHexString(repository.getRoot()));
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 22 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class RepositoryImplOriginalTest method testMultiThread.

// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    final Repository repository = new RepositoryImpl(config, store);
    final DataWord cowKey1 = new DataWord("c1");
    final DataWord cowKey2 = new DataWord("c2");
    final DataWord cowVal0 = new DataWord("c0a0");
    Repository track2 = repository.startTracking();
    track2.addStorageRow(COW, cowKey2, cowVal0);
    track2.commit();
    ContractDetails cowDetails = repository.getContractDetails(COW);
    assertEquals(cowVal0, cowDetails.get(cowKey2));
    final CountDownLatch failSema = new CountDownLatch(1);
    for (int i = 0; i < 10; ++i) {
        new Thread(() -> {
            try {
                int cnt = 1;
                while (running) {
                    Repository snap = repository.getSnapshotTo(repository.getRoot()).startTracking();
                    snap.addBalance(COW, Coin.valueOf(10L));
                    snap.addStorageRow(COW, cowKey1, new DataWord(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 = new DataWord(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, new DataWord(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) ContractDetails(org.ethereum.db.ContractDetails) Repository(org.ethereum.core.Repository) Test(org.junit.Test)

Example 23 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class RepositoryImplOriginalTest method test8.

@Test
public void test8() {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    track.addBalance(COW, Coin.valueOf(10L));
    track.addBalance(HORSE, Coin.valueOf(1L));
    assertEquals(BigInteger.TEN, track.getBalance(COW).asBigInteger());
    assertEquals(BigInteger.ONE, track.getBalance(HORSE).asBigInteger());
    track.rollback();
    assertEquals(BigInteger.ZERO, repository.getBalance(COW).asBigInteger());
    assertEquals(BigInteger.ZERO, repository.getBalance(HORSE).asBigInteger());
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) Test(org.junit.Test)

Example 24 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class RepositoryImplOriginalTest method test15.

@Test
public void test15() {
    Repository repository = new RepositoryImpl(config);
    final BigInteger ELEVEN = BigInteger.TEN.add(BigInteger.ONE);
    // changes level_1
    Repository track1 = repository.startTracking();
    track1.addBalance(COW, Coin.valueOf(10L));
    track1.addBalance(HORSE, Coin.valueOf(1L));
    assertEquals(BigInteger.TEN, track1.getBalance(COW).asBigInteger());
    assertEquals(BigInteger.ONE, track1.getBalance(HORSE).asBigInteger());
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addBalance(COW, Coin.valueOf(1L));
    track2.addBalance(HORSE, Coin.valueOf(10L));
    assertEquals(ELEVEN, track2.getBalance(COW).asBigInteger());
    assertEquals(ELEVEN, track2.getBalance(HORSE).asBigInteger());
    track2.rollback();
    track1.commit();
    assertEquals(BigInteger.TEN, repository.getBalance(COW).asBigInteger());
    assertEquals(BigInteger.ONE, repository.getBalance(HORSE).asBigInteger());
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 25 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class RepositoryImplOriginalTest method test19.

@Test
public void test19() {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    DataWord cowKey1 = new DataWord("c1");
    DataWord cowVal1 = new DataWord("c0a1");
    DataWord cowVal0 = new DataWord("c0a0");
    DataWord horseKey1 = new DataWord("e1");
    DataWord horseVal1 = new DataWord("c0a1");
    DataWord horseVal0 = new DataWord("c0a0");
    track.addStorageRow(COW, cowKey1, cowVal0);
    track.addStorageRow(HORSE, horseKey1, horseVal0);
    track.commit();
    // track
    Repository track2 = repository.startTracking();
    track2.addStorageRow(HORSE, horseKey1, horseVal0);
    Repository track3 = track2.startTracking();
    ContractDetails cowDetails = track3.getContractDetails(COW);
    cowDetails.put(cowKey1, cowVal1);
    ContractDetails horseDetails = track3.getContractDetails(HORSE);
    horseDetails.put(horseKey1, horseVal1);
    track3.commit();
    track2.rollback();
    ContractDetails cowDetailsOrigin = repository.getContractDetails(COW);
    DataWord cowValOrin = cowDetailsOrigin.get(cowKey1);
    ContractDetails horseDetailsOrigin = repository.getContractDetails(HORSE);
    DataWord horseValOrin = horseDetailsOrigin.get(horseKey1);
    assertEquals(cowVal0, cowValOrin);
    assertEquals(horseVal0, horseValOrin);
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Aggregations

Repository (org.ethereum.core.Repository)136 Test (org.junit.Test)109 RskAddress (co.rsk.core.RskAddress)59 DataWord (org.ethereum.vm.DataWord)43 BigInteger (java.math.BigInteger)31 RepositoryImpl (co.rsk.db.RepositoryImpl)25 Coin (co.rsk.core.Coin)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 HashMapDB (org.ethereum.datasource.HashMapDB)12 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)11 Transaction (org.ethereum.core.Transaction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)10 ArrayList (java.util.ArrayList)10 Program (org.ethereum.vm.program.Program)10 Block (org.ethereum.core.Block)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)9 RskSystemProperties (co.rsk.config.RskSystemProperties)8 IOException (java.io.IOException)8 List (java.util.List)8