Search in sources :

Example 21 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class HashMapDBTest method putKeyValue.

@Test
public void putKeyValue() {
    KeyValueDataSource ds = new HashMapDB();
    byte[] key = new byte[] { 0x01, 0x02 };
    byte[] value = new byte[] { 0x03, 0x03 };
    byte[] result = ds.put(key, value);
    Assert.assertNull(result);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 22 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class HashMapDBTest method putAndDeleteKeyValue.

@Test
public void putAndDeleteKeyValue() {
    KeyValueDataSource ds = new HashMapDB();
    byte[] key = new byte[] { 0x01, 0x02 };
    byte[] value = new byte[] { 0x03, 0x03 };
    ds.put(key, value);
    ds.delete(key);
    byte[] result = ds.get(key);
    Assert.assertNull(result);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 23 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class ContractDetailsImplTest method usingSameExternalStorage.

@Test
public void usingSameExternalStorage() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Trie trie = new TrieImpl(store, false);
    byte[] accountAddress = randomAddress();
    ContractDetailsImpl details = new ContractDetailsImpl(config, accountAddress, trie, null);
    int nkeys = IN_MEMORY_STORAGE_LIMIT;
    for (int k = 1; k <= nkeys + 1; k++) details.put(new DataWord(k), new DataWord(k * 2));
    Assert.assertTrue(details.hasExternalStorage());
    details.syncStorage();
    ContractDetailsImpl details1 = new ContractDetailsImpl(config, details.getEncoded());
    ContractDetailsImpl details2 = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertTrue(details1.hasExternalStorage());
    Assert.assertTrue(details2.hasExternalStorage());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details1.get(new DataWord(k)));
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details2.get(new DataWord(k)));
    details1.syncStorage();
    details2.syncStorage();
}
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) TrieStore(co.rsk.trie.TrieStore) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 24 with HashMapDB

use of org.ethereum.datasource.HashMapDB 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 25 with HashMapDB

use of org.ethereum.datasource.HashMapDB 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)

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)141 Test (org.junit.Test)130 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)28 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)23 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)22 SimpleBlock (co.rsk.peg.simples.SimpleBlock)18 ArrayList (java.util.ArrayList)18 RepositoryImpl (co.rsk.db.RepositoryImpl)15 DataWord (org.ethereum.vm.DataWord)14 Repository (org.ethereum.core.Repository)12 RskAddress (co.rsk.core.RskAddress)11 TrieStore (co.rsk.trie.TrieStore)10 BlockBuilder (co.rsk.test.builders.BlockBuilder)9 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)8 ReceiptStore (org.ethereum.db.ReceiptStore)8 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)8 Keccak256 (co.rsk.crypto.Keccak256)7 World (co.rsk.test.World)7 TrieImpl (co.rsk.trie.TrieImpl)6 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6