Search in sources :

Example 16 with Trie

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

the class ContractDetailsImplTest method syncStorageWithExternalStorage.

@Test
public void syncStorageWithExternalStorage() {
    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();
    int ssize = details.getStorageSize();
    details = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertEquals(ssize, details.getStorageSize());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details.get(new DataWord(k)));
    ContractDetailsImpl clone = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertNotNull(clone);
    Assert.assertTrue(clone.hasExternalStorage());
    Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(clone.get(new DataWord(k)));
    for (int k = 1; k <= nkeys + 1; k++) clone.put(new DataWord(k), new DataWord(k * 3));
    Assert.assertTrue(clone.hasExternalStorage());
    Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
    ContractDetailsImpl snapshot = (ContractDetailsImpl) clone.getSnapshotTo(clone.getStorageHash());
    Assert.assertTrue(snapshot.hasExternalStorage());
    Assert.assertEquals(clone.getStorageSize(), snapshot.getStorageSize());
}
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 17 with Trie

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

the class ContractDetailsImplTest method putDataWordZeroAsDeleteValue.

@Test
public void putDataWordZeroAsDeleteValue() {
    ContractDetailsImpl details = new ContractDetailsImpl(config);
    details.put(DataWord.ONE, new DataWord(42));
    details.put(DataWord.ONE, DataWord.ZERO);
    Trie trie = details.getTrie();
    byte[] value = trie.get(DataWord.ONE.getData());
    Assert.assertNull(value);
    Assert.assertEquals(0, details.getStorageSize());
}
Also used : DataWord(org.ethereum.vm.DataWord) TestUtils.randomDataWord(org.ethereum.TestUtils.randomDataWord) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 18 with Trie

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

the class ContractDetailsImplTest method syncStorageAndGetKeyValues.

@Test
public void syncStorageAndGetKeyValues() {
    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();
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details.get(new DataWord(k)));
    ContractDetailsImpl clone = new ContractDetailsImpl(config, details.getEncoded());
    Assert.assertNotNull(clone);
    Assert.assertTrue(clone.hasExternalStorage());
    Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
    for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(clone.get(new DataWord(k)));
    for (int k = 1; k <= nkeys + 1; k++) clone.put(new DataWord(k), new DataWord(k * 3));
    Assert.assertTrue(clone.hasExternalStorage());
    Assert.assertEquals(details.getStorageSize(), clone.getStorageSize());
    ContractDetailsImpl snapshot = (ContractDetailsImpl) clone.getSnapshotTo(clone.getStorageHash());
    Assert.assertTrue(snapshot.hasExternalStorage());
    Assert.assertEquals(clone.getStorageSize(), snapshot.getStorageSize());
}
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 19 with Trie

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

the class ContractDetailsImplTest method syncStorageInDetailsWithTrieInMemory.

@Test
public void syncStorageInDetailsWithTrieInMemory() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Trie trie = new TrieImpl(store, false);
    byte[] accountAddress = randomAddress();
    ContractDetailsImpl details = new ContractDetailsImpl(config, accountAddress, trie, null);
    details.put(new DataWord(42), DataWord.ONE);
    details.syncStorage();
    Assert.assertNotNull(details.get(new DataWord(42)));
}
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 20 with Trie

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

the class ContractDetailsImplTest method putDataWordWithoutLeadingZeroes.

@Test
public void putDataWordWithoutLeadingZeroes() {
    ContractDetailsImpl details = new ContractDetailsImpl(config);
    details.put(DataWord.ONE, new DataWord(42));
    Trie trie = details.getTrie();
    byte[] value = trie.get(DataWord.ONE.getData());
    Assert.assertNotNull(value);
    Assert.assertEquals(1, value.length);
    Assert.assertEquals(42, value[0]);
    Assert.assertEquals(1, details.getStorageSize());
}
Also used : DataWord(org.ethereum.vm.DataWord) TestUtils.randomDataWord(org.ethereum.TestUtils.randomDataWord) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Aggregations

Trie (co.rsk.trie.Trie)29 Test (org.junit.Test)24 TrieImpl (co.rsk.trie.TrieImpl)22 DataWord (org.ethereum.vm.DataWord)8 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)7 TrieStore (co.rsk.trie.TrieStore)4 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)4 HashMapDB (org.ethereum.datasource.HashMapDB)4 Coin (co.rsk.core.Coin)2 Keccak256 (co.rsk.crypto.Keccak256)2 BigInteger (java.math.BigInteger)2 Ignore (org.junit.Ignore)2 RskSystemProperties (co.rsk.config.RskSystemProperties)1 RskAddress (co.rsk.core.RskAddress)1 RemascTransaction (co.rsk.remasc.RemascTransaction)1 Set (java.util.Set)1 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)1 Transaction (org.ethereum.core.Transaction)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1