Search in sources :

Example 21 with TrieStoreImpl

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

the class ContractDetailsImplTest method testExternalStorageSerialization.

@Test
public void testExternalStorageSerialization() {
    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 + 10; i++) {
        DataWord key = randomDataWord();
        DataWord value = randomDataWord();
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    byte[] rlp = original.getEncoded();
    ContractDetailsImpl deserialized = new ContractDetailsImpl(config, rlp);
    Assert.assertEquals(toHexString(address), toHexString(deserialized.getAddress()));
    Assert.assertEquals(toHexString(code), toHexString(deserialized.getCode()));
    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));
    }
    DataWord deletedKey = elements.keySet().iterator().next();
    deserialized.put(deletedKey, DataWord.ZERO);
    deserialized.put(randomDataWord(), DataWord.ZERO);
}
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) Test(org.junit.Test)

Example 22 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl 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 23 with TrieStoreImpl

use of co.rsk.trie.TrieStoreImpl 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 24 with TrieStoreImpl

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

the class RepositoryImplOriginalTest method test20.

// testing for snapshot
@Test
public void test20() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    Repository repository = new RepositoryImpl(config, store);
    byte[] root = repository.getRoot();
    DataWord cowKey1 = new DataWord("c1");
    DataWord cowKey2 = new DataWord("c2");
    DataWord cowVal1 = new DataWord("c0a1");
    DataWord cowVal0 = new DataWord("c0a0");
    DataWord horseKey1 = new DataWord("e1");
    DataWord horseKey2 = new DataWord("e2");
    DataWord horseVal1 = new DataWord("c0a1");
    DataWord horseVal0 = new DataWord("c0a0");
    // track
    Repository track2 = repository.startTracking();
    track2.addStorageRow(COW, cowKey1, cowVal1);
    track2.addStorageRow(HORSE, horseKey1, horseVal1);
    track2.commit();
    byte[] root2 = repository.getRoot();
    // track
    track2 = repository.startTracking();
    track2.addStorageRow(COW, cowKey2, cowVal0);
    track2.addStorageRow(HORSE, horseKey2, horseVal0);
    track2.commit();
    byte[] root3 = repository.getRoot();
    Repository snapshot = repository.getSnapshotTo(root);
    ContractDetails cowDetails = snapshot.getContractDetails(COW);
    ContractDetails horseDetails = snapshot.getContractDetails(HORSE);
    assertEquals(null, cowDetails.get(cowKey1));
    assertEquals(null, cowDetails.get(cowKey2));
    assertEquals(null, horseDetails.get(horseKey1));
    assertEquals(null, horseDetails.get(horseKey2));
    snapshot = repository.getSnapshotTo(root2);
    cowDetails = snapshot.getContractDetails(COW);
    horseDetails = snapshot.getContractDetails(HORSE);
    assertEquals(cowVal1, cowDetails.get(cowKey1));
    assertEquals(null, cowDetails.get(cowKey2));
    assertEquals(horseVal1, horseDetails.get(horseKey1));
    assertEquals(null, horseDetails.get(horseKey2));
    snapshot = repository.getSnapshotTo(root3);
    cowDetails = snapshot.getContractDetails(COW);
    horseDetails = snapshot.getContractDetails(HORSE);
    assertEquals(cowVal1, cowDetails.get(cowKey1));
    assertEquals(cowVal0, cowDetails.get(cowKey2));
    assertEquals(horseVal1, horseDetails.get(horseKey1));
    assertEquals(horseVal0, horseDetails.get(horseKey2));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 25 with TrieStoreImpl

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

the class RepositoryImplTest method syncToRootAfterCreatingAnAccount.

@Test
public void syncToRootAfterCreatingAnAccount() {
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    RepositoryImpl repository = new RepositoryImpl(config, store);
    repository.flush();
    RskAddress accAddress = randomAccountAddress();
    byte[] initialRoot = repository.getRoot();
    repository.createAccount(accAddress);
    repository.flush();
    byte[] newRoot = repository.getRoot();
    Assert.assertTrue(repository.isExist(accAddress));
    repository.syncToRoot(initialRoot);
    Assert.assertFalse(repository.isExist(accAddress));
    repository.syncToRoot(newRoot);
    Assert.assertTrue(repository.isExist(accAddress));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) RskAddress(co.rsk.core.RskAddress) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TrieImplHashTest(co.rsk.trie.TrieImplHashTest) Test(org.junit.Test)

Aggregations

TrieStoreImpl (co.rsk.trie.TrieStoreImpl)38 HashMapDB (org.ethereum.datasource.HashMapDB)34 TrieStore (co.rsk.trie.TrieStore)25 Trie (co.rsk.trie.Trie)21 Test (org.junit.Test)20 MutableRepository (org.ethereum.db.MutableRepository)12 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 RskAddress (co.rsk.core.RskAddress)8 DataWord (org.ethereum.vm.DataWord)8 TrieImpl (co.rsk.trie.TrieImpl)7 Repository (org.ethereum.core.Repository)7 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)7 RepositoryImpl (co.rsk.db.RepositoryImpl)6 ArrayList (java.util.ArrayList)6 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)6 MutableTrieImpl (co.rsk.db.MutableTrieImpl)5 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)5 RskSystemProperties (co.rsk.config.RskSystemProperties)4 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)4 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)4