Search in sources :

Example 36 with Repository

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

the class RepositoryTest method test4.

@Test
public void test4() {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    byte[] cowKey = Hex.decode("A1A2A3");
    byte[] cowValue = Hex.decode("A4A5A6");
    byte[] horseKey = Hex.decode("B1B2B3");
    byte[] horseValue = Hex.decode("B4B5B6");
    track.addStorageBytes(COW, new DataWord(cowKey), cowValue);
    track.addStorageBytes(HORSE, new DataWord(horseKey), horseValue);
    track.commit();
    assertArrayEquals(cowValue, repository.getStorageBytes(COW, new DataWord(cowKey)));
    assertArrayEquals(horseValue, repository.getStorageBytes(HORSE, new DataWord(horseKey)));
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 37 with Repository

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

the class RepositoryTest 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 byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    final DataWord cowKey1 = new DataWord("c1");
    final DataWord cowKey2 = new DataWord("c2");
    final byte[] cowVal0 = Hex.decode("c0a0");
    // track
    Repository track2 = repository.startTracking();
    track2.addStorageBytes(COW, cowKey2, cowVal0);
    track2.commit();
    repository.flush();
    ContractDetails cowDetails = repository.getContractDetails(COW);
    assertArrayEquals(cowVal0, cowDetails.getBytes(cowKey2));
    final CountDownLatch failSema = new CountDownLatch(1);
    new Thread(() -> {
        try {
            int cnt = 1;
            while (true) {
                // To Review, not needed?
                repository.flush();
                Repository snap = repository.getSnapshotTo(repository.getRoot()).startTracking();
                byte[] vcnr = new byte[1];
                vcnr[0] = (byte) (cnt % 128);
                snap.addStorageBytes(COW, cowKey1, vcnr);
                cnt++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            failSema.countDown();
        }
    }).start();
    new Thread(() -> {
        int cnt = 1;
        try {
            while (true) {
                // track
                Repository track21 = repository.startTracking();
                byte[] cVal = new byte[1];
                cVal[0] = (byte) (cnt % 128);
                track21.addStorageBytes(COW, cowKey1, cVal);
                track21.commit();
                repository.flush();
                assertArrayEquals(cVal, repository.getStorageBytes(COW, cowKey1));
                assertArrayEquals(cowVal0, repository.getStorageBytes(COW, cowKey2));
                cnt++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            failSema.countDown();
        }
    }).start();
    failSema.await(10, TimeUnit.SECONDS);
    if (failSema.getCount() == 0) {
        throw new RuntimeException("Test failed.");
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) 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) Test(org.junit.Test)

Example 38 with Repository

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

the class RepositoryTest method test16_5.

@Test
public void test16_5() {
    Repository repository = new RepositoryImpl(config);
    byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
    byte[] cowKey1 = "key-c-1".getBytes();
    byte[] cowValue1 = "val-c-1".getBytes();
    byte[] horseKey1 = "key-h-1".getBytes();
    byte[] horseValue1 = "val-h-1".getBytes();
    byte[] cowKey2 = "key-c-2".getBytes();
    byte[] cowValue2 = "val-c-2".getBytes();
    byte[] horseKey2 = "key-h-2".getBytes();
    byte[] horseValue2 = "val-h-2".getBytes();
    // changes level_1
    Repository track1 = repository.startTracking();
    track1.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
    // changes level_2
    Repository track2 = track1.startTracking();
    assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
    assertNull(track1.getStorageBytes(COW, new DataWord(cowKey1)));
    track2.commit();
    // leaving level_2
    track1.commit();
    // leaving level_1
    assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
    assertNull(track1.getStorageBytes(COW, new DataWord(cowKey1)));
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 39 with Repository

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

the class RepositoryTest method test16.

@Test
public void test16() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
    byte[] cowKey1 = "key-c-1".getBytes();
    byte[] cowValue1 = "val-c-1".getBytes();
    byte[] horseKey1 = "key-h-1".getBytes();
    byte[] horseValue1 = "val-h-1".getBytes();
    byte[] cowKey2 = "key-c-2".getBytes();
    byte[] cowValue2 = "val-c-2".getBytes();
    byte[] horseKey2 = "key-h-2".getBytes();
    byte[] horseValue2 = "val-h-2".getBytes();
    // changes level_1
    Repository track1 = repository.startTracking();
    track1.addStorageBytes(COW, new DataWord(cowKey1), cowValue1);
    track1.addStorageBytes(HORSE, new DataWord(horseKey1), horseValue1);
    assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, track1.getStorageBytes(HORSE, new DataWord(horseKey1)));
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
    track2.addStorageBytes(HORSE, new DataWord(horseKey2), horseValue2);
    assertArrayEquals(cowValue1, track2.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, track2.getStorageBytes(HORSE, new DataWord(horseKey1)));
    assertArrayEquals(cowValue2, track2.getStorageBytes(COW, new DataWord(cowKey2)));
    assertArrayEquals(horseValue2, track2.getStorageBytes(HORSE, new DataWord(horseKey2)));
    track2.commit();
    // leaving level_2
    assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, track1.getStorageBytes(HORSE, new DataWord(horseKey1)));
    assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
    assertArrayEquals(horseValue2, track1.getStorageBytes(HORSE, new DataWord(horseKey2)));
    track1.commit();
    // leaving level_1
    assertArrayEquals(cowValue1, repository.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(horseValue1, repository.getStorageBytes(HORSE, new DataWord(horseKey1)));
    assertArrayEquals(cowValue2, repository.getStorageBytes(COW, new DataWord(cowKey2)));
    assertArrayEquals(horseValue2, repository.getStorageBytes(HORSE, new DataWord(horseKey2)));
    repository.close();
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 40 with Repository

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

the class RepositoryTest method test10.

@Test
public void test10() {
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
    DataWord cowKey = new DataWord(Hex.decode("A1A2A3"));
    byte[] cowValue = Hex.decode("A4A5A6");
    DataWord horseKey = new DataWord(Hex.decode("B1B2B3"));
    byte[] horseValue = Hex.decode("B4B5B6");
    track.addStorageBytes(COW, cowKey, cowValue);
    track.addStorageBytes(HORSE, horseKey, horseValue);
    assertArrayEquals(cowValue, track.getStorageBytes(COW, cowKey));
    assertArrayEquals(horseValue, track.getStorageBytes(HORSE, horseKey));
    track.rollback();
    assertEquals(null, repository.getStorageBytes(COW, cowKey));
    assertEquals(null, repository.getStorageBytes(HORSE, horseKey));
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) 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