Search in sources :

Example 31 with DataWord

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

the class ContractDetailsImplTest method externalStorageTransition.

@Test
public void externalStorageTransition() {
    byte[] address = randomAddress();
    byte[] code = randomBytes(512);
    Map<DataWord, DataWord> elements = new HashMap<>();
    HashMapDB externalStorage = new HashMapDB();
    ContractDetailsImpl original = new ContractDetailsImpl(config, address, new TrieImpl(new TrieStoreImpl(externalStorage), true), code);
    for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT - 1; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    ContractDetails deserialized = new ContractDetailsImpl(config, original.getEncoded());
    // adds keys for in-memory storage limit overflow
    for (int i = 0; i < 10; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        deserialized.put(key, value);
    }
    deserialized.syncStorage();
    deserialized = new ContractDetailsImpl(config, deserialized.getEncoded());
    Map<DataWord, DataWord> storage = deserialized.getStorage();
    Assert.assertEquals(elements.size(), storage.size());
    for (DataWord key : elements.keySet()) {
        Assert.assertEquals(elements.get(key), storage.get(key));
    }
}
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) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 32 with DataWord

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

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

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

Example 35 with DataWord

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

the class RepositoryImplOriginalTest method test16_4.

@Test
public void test16_4() {
    Repository repository = new RepositoryImpl(config);
    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.addStorageRow(COW, new DataWord(cowKey1), new DataWord(cowValue1));
    track.commit();
    // changes level_1
    Repository track1 = repository.startTracking();
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addStorageRow(COW, new DataWord(cowKey2), new DataWord(cowValue2));
    track2.commit();
    // leaving level_2
    track1.commit();
    // leaving level_1
    assertEquals(new DataWord(cowValue1), track1.getStorageValue(COW, new DataWord(cowKey1)));
    assertEquals(new DataWord(cowValue2), track1.getStorageValue(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