Search in sources :

Example 41 with DataWord

use of org.ethereum.vm.DataWord 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 42 with DataWord

use of org.ethereum.vm.DataWord 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 43 with DataWord

use of org.ethereum.vm.DataWord 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 44 with DataWord

use of org.ethereum.vm.DataWord 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)

Example 45 with DataWord

use of org.ethereum.vm.DataWord in project rskj by rsksmart.

the class RepositoryTest method test16_4.

@Test
public void test16_4() {
    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();
    Repository track = repository.startTracking();
    track.addStorageBytes(COW, new DataWord(cowKey1), cowValue1);
    track.commit();
    // changes level_1
    Repository track1 = repository.startTracking();
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
    track2.commit();
    // leaving level_2
    track1.commit();
    // leaving level_1
    assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Aggregations

DataWord (org.ethereum.vm.DataWord)133 Test (org.junit.Test)88 Repository (org.ethereum.core.Repository)41 RskAddress (co.rsk.core.RskAddress)25 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)22 BigInteger (java.math.BigInteger)19 LogInfo (org.ethereum.vm.LogInfo)17 HashMapDB (org.ethereum.datasource.HashMapDB)14 Program (org.ethereum.vm.program.Program)13 Stack (org.ethereum.vm.program.Stack)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 RepositoryImpl (co.rsk.db.RepositoryImpl)9 ContractDetails (org.ethereum.db.ContractDetails)9 Trie (co.rsk.trie.Trie)8 CallTransaction (org.ethereum.core.CallTransaction)8 TrieImpl (co.rsk.trie.TrieImpl)7 TrieStore (co.rsk.trie.TrieStore)7 Coin (co.rsk.core.Coin)5