use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class HashMapDBTest method putKeyValue.
@Test
public void putKeyValue() {
KeyValueDataSource ds = new HashMapDB();
byte[] key = new byte[] { 0x01, 0x02 };
byte[] value = new byte[] { 0x03, 0x03 };
byte[] result = ds.put(key, value);
Assert.assertNull(result);
}
use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class HashMapDBTest method putAndDeleteKeyValue.
@Test
public void putAndDeleteKeyValue() {
KeyValueDataSource ds = new HashMapDB();
byte[] key = new byte[] { 0x01, 0x02 };
byte[] value = new byte[] { 0x03, 0x03 };
ds.put(key, value);
ds.delete(key);
byte[] result = ds.get(key);
Assert.assertNull(result);
}
use of org.ethereum.datasource.HashMapDB 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 org.ethereum.datasource.HashMapDB 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 org.ethereum.datasource.HashMapDB in project rskj by rsksmart.
the class RepositoryImplOriginalTest method testMultiThread.
// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
TrieStore store = new TrieStoreImpl(new HashMapDB());
final Repository repository = new RepositoryImpl(config, store);
final DataWord cowKey1 = new DataWord("c1");
final DataWord cowKey2 = new DataWord("c2");
final DataWord cowVal0 = new DataWord("c0a0");
Repository track2 = repository.startTracking();
track2.addStorageRow(COW, cowKey2, cowVal0);
track2.commit();
ContractDetails cowDetails = repository.getContractDetails(COW);
assertEquals(cowVal0, cowDetails.get(cowKey2));
final CountDownLatch failSema = new CountDownLatch(1);
for (int i = 0; i < 10; ++i) {
new Thread(() -> {
try {
int cnt = 1;
while (running) {
Repository snap = repository.getSnapshotTo(repository.getRoot()).startTracking();
snap.addBalance(COW, Coin.valueOf(10L));
snap.addStorageRow(COW, cowKey1, new DataWord(cnt));
snap.rollback();
cnt++;
}
} catch (Throwable e) {
e.printStackTrace();
failSema.countDown();
}
}).start();
}
new Thread(() -> {
int cnt = 1;
try {
while (running) {
Repository track21 = repository.startTracking();
DataWord cVal = new DataWord(cnt);
track21.addStorageRow(COW, cowKey1, cVal);
track21.addBalance(COW, Coin.valueOf(1L));
track21.commit();
assertEquals(BigInteger.valueOf(cnt), repository.getBalance(COW).asBigInteger());
assertEquals(cVal, repository.getStorageValue(COW, cowKey1));
assertEquals(cowVal0, repository.getStorageValue(COW, cowKey2));
cnt++;
}
} catch (Throwable e) {
e.printStackTrace();
try {
repository.addStorageRow(COW, cowKey1, new DataWord(123));
} catch (Exception e1) {
e1.printStackTrace();
}
failSema.countDown();
}
}).start();
failSema.await(10, TimeUnit.SECONDS);
running = false;
if (failSema.getCount() == 0) {
throw new RuntimeException("Test failed.");
}
}
Aggregations