Search in sources :

Example 6 with TrieStore

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

use of co.rsk.trie.TrieStore 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 8 with TrieStore

use of co.rsk.trie.TrieStore 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 9 with TrieStore

use of co.rsk.trie.TrieStore 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)

Example 10 with TrieStore

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

the class RepositoryImplTest method getAccountsKeysOnSnapshot.

@Test
public void getAccountsKeysOnSnapshot() {
    RskAddress accAddress1 = randomAccountAddress();
    RskAddress accAddress2 = randomAccountAddress();
    TrieStore store = new TrieStoreImpl(new HashMapDB());
    RepositoryImpl repository = new RepositoryImpl(config, store);
    repository.createAccount(accAddress1);
    repository.flush();
    byte[] root = repository.getRoot();
    repository.createAccount(accAddress2);
    repository.syncToRoot(root);
    Set<RskAddress> keys = repository.getAccountsKeys();
    Assert.assertNotNull(keys);
    Assert.assertFalse(keys.isEmpty());
    Assert.assertEquals(1, keys.size());
}
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

TrieStore (co.rsk.trie.TrieStore)11 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)11 HashMapDB (org.ethereum.datasource.HashMapDB)10 Test (org.junit.Test)10 DataWord (org.ethereum.vm.DataWord)7 TrieImpl (co.rsk.trie.TrieImpl)5 RskAddress (co.rsk.core.RskAddress)4 Trie (co.rsk.trie.Trie)4 TestUtils.randomDataWord (org.ethereum.TestUtils.randomDataWord)4 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)3 Repository (org.ethereum.core.Repository)3 ContractDetails (org.ethereum.db.ContractDetails)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)1