use of co.rsk.trie.TrieImpl in project rskj by rsksmart.
the class ContractDetailsCacheImpl method getStorageHash.
@Override
public byte[] getStorageHash() {
// todo: unsupported
Trie storageTrie = new TrieImpl(null, true);
for (DataWord key : storage.keySet()) {
DataWord value = storage.get(key);
storageTrie = storageTrie.put(key.getData(), RLP.encodeElement(value.getNoLeadZeroesData()));
}
for (DataWord key : bytesStorage.keySet()) {
byte[] value = bytesStorage.get(key);
storageTrie = storageTrie.put(key.getData(), RLP.encodeElement(value));
}
return storageTrie.getHash().getBytes();
}
use of co.rsk.trie.TrieImpl 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());
}
use of co.rsk.trie.TrieImpl 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);
}
use of co.rsk.trie.TrieImpl 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());
}
use of co.rsk.trie.TrieImpl 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)));
}
Aggregations