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();
}
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));
}
}
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));
}
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));
}
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());
}
Aggregations