Search in sources :

Example 1 with TrieImpl

use of co.rsk.trie.TrieImpl 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 2 with TrieImpl

use of co.rsk.trie.TrieImpl 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 3 with TrieImpl

use of co.rsk.trie.TrieImpl in project rskj by rsksmart.

the class SecureTrieImplKeyValueTest method oneKeyWhenTwoKeysHasSharedPathAndOneIsPrefixOfTheOther.

@Test
public void oneKeyWhenTwoKeysHasSharedPathAndOneIsPrefixOfTheOther() {
    byte[] zeroKey = "0".getBytes();
    byte[] oneKey = "012345678910".getBytes();
    Trie trie = new TrieImpl(true).put(zeroKey, "So, first of all, let me assert my firm belief that".getBytes()).put(oneKey, "the only thing we have to fear is... fear itself ".getBytes());
    trie = trie.delete(oneKey);
    Assert.assertTrue(Arrays.equals(trie.get(zeroKey), "So, first of all, let me assert my firm belief that".getBytes()));
    Assert.assertNull(trie.get(oneKey));
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 4 with TrieImpl

use of co.rsk.trie.TrieImpl in project rskj by rsksmart.

the class SecureTrieImplKeyValueTest method oneKeyWhenTwoKeysHasNoSharedPath.

@Test
public void oneKeyWhenTwoKeysHasNoSharedPath() {
    byte[] zeroKey = "0".getBytes();
    byte[] oneKey = "1".getBytes();
    Trie trie = new TrieImpl(true).put(zeroKey, "So, first of all, let me assert my firm belief that".getBytes()).put(oneKey, "the only thing we have to fear is... fear itself ".getBytes());
    trie = trie.delete(zeroKey);
    Assert.assertTrue(Arrays.equals(trie.get(oneKey), "the only thing we have to fear is... fear itself ".getBytes()));
    Assert.assertNull(trie.get(zeroKey));
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 5 with TrieImpl

use of co.rsk.trie.TrieImpl in project rskj by rsksmart.

the class TrieImplHashTest method sonWithNoSiblingsAndTwoSonsShouldBringSameHashRecursionCase.

@Test
public void sonWithNoSiblingsAndTwoSonsShouldBringSameHashRecursionCase() {
    Trie trie1 = new TrieImpl().put("ro", "4".getBytes()).put("roose", "42".getBytes()).put("roosevalt", "4243".getBytes()).put("roosevalt0oosevalt", "424344".getBytes()).put("roosevalt1oosevalt", "42434445".getBytes()).delete("roosevalt");
    Trie trie2 = new TrieImpl().put("ro", "4".getBytes()).put("roose", "42".getBytes()).put("roosevalt0oosevalt", "424344".getBytes()).put("roosevalt1oosevalt", "42434445".getBytes());
    Assert.assertTrue(Arrays.equals(trie1.get("ro"), "4".getBytes()));
    Assert.assertTrue(Arrays.equals(trie1.get("roose"), "42".getBytes()));
    Assert.assertTrue(Arrays.equals(trie1.get("roosevalt0oosevalt"), "424344".getBytes()));
    Assert.assertTrue(Arrays.equals(trie1.get("roosevalt1oosevalt"), "42434445".getBytes()));
    Assert.assertNull(trie1.get("roosevalt"));
    Assert.assertEquals(trie1.getHash(), trie2.getHash());
}
Also used : TrieImpl(co.rsk.trie.TrieImpl) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Aggregations

TrieImpl (co.rsk.trie.TrieImpl)25 Trie (co.rsk.trie.Trie)22 Test (org.junit.Test)20 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)7 DataWord (org.ethereum.vm.DataWord)7 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6 HashMapDB (org.ethereum.datasource.HashMapDB)6 TrieStore (co.rsk.trie.TrieStore)5 RskAddress (co.rsk.core.RskAddress)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 Coin (co.rsk.core.Coin)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1 BigInteger (java.math.BigInteger)1 Set (java.util.Set)1 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)1 Transaction (org.ethereum.core.Transaction)1 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)1 ContractDetails (org.ethereum.db.ContractDetails)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1