use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryImplOriginalTest method test17.
@Test
public void test17() {
Repository repository = new RepositoryImpl(config);
byte[] cowKey1 = "key-c-1".getBytes();
byte[] cowValue1 = "val-c-1".getBytes();
// changes level_1
Repository track1 = repository.startTracking();
// changes level_2
Repository track2 = track1.startTracking();
track2.addStorageRow(COW, new DataWord(cowKey1), new DataWord(cowValue1));
assertEquals(new DataWord(cowValue1), track2.getStorageValue(COW, new DataWord(cowKey1)));
track2.rollback();
// leaving level_2
track1.commit();
// leaving level_1
Assert.assertEquals(Hex.toHexString(HashUtil.EMPTY_TRIE_HASH), Hex.toHexString(repository.getRoot()));
repository.close();
}
use of org.ethereum.core.Repository 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.");
}
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryImplOriginalTest method test8.
@Test
public void test8() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
track.addBalance(COW, Coin.valueOf(10L));
track.addBalance(HORSE, Coin.valueOf(1L));
assertEquals(BigInteger.TEN, track.getBalance(COW).asBigInteger());
assertEquals(BigInteger.ONE, track.getBalance(HORSE).asBigInteger());
track.rollback();
assertEquals(BigInteger.ZERO, repository.getBalance(COW).asBigInteger());
assertEquals(BigInteger.ZERO, repository.getBalance(HORSE).asBigInteger());
repository.close();
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryImplOriginalTest method test15.
@Test
public void test15() {
Repository repository = new RepositoryImpl(config);
final BigInteger ELEVEN = BigInteger.TEN.add(BigInteger.ONE);
// changes level_1
Repository track1 = repository.startTracking();
track1.addBalance(COW, Coin.valueOf(10L));
track1.addBalance(HORSE, Coin.valueOf(1L));
assertEquals(BigInteger.TEN, track1.getBalance(COW).asBigInteger());
assertEquals(BigInteger.ONE, track1.getBalance(HORSE).asBigInteger());
// changes level_2
Repository track2 = track1.startTracking();
track2.addBalance(COW, Coin.valueOf(1L));
track2.addBalance(HORSE, Coin.valueOf(10L));
assertEquals(ELEVEN, track2.getBalance(COW).asBigInteger());
assertEquals(ELEVEN, track2.getBalance(HORSE).asBigInteger());
track2.rollback();
track1.commit();
assertEquals(BigInteger.TEN, repository.getBalance(COW).asBigInteger());
assertEquals(BigInteger.ONE, repository.getBalance(HORSE).asBigInteger());
repository.close();
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryImplOriginalTest method test19.
@Test
public void test19() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
DataWord cowKey1 = new DataWord("c1");
DataWord cowVal1 = new DataWord("c0a1");
DataWord cowVal0 = new DataWord("c0a0");
DataWord horseKey1 = new DataWord("e1");
DataWord horseVal1 = new DataWord("c0a1");
DataWord horseVal0 = new DataWord("c0a0");
track.addStorageRow(COW, cowKey1, cowVal0);
track.addStorageRow(HORSE, horseKey1, horseVal0);
track.commit();
// track
Repository track2 = repository.startTracking();
track2.addStorageRow(HORSE, horseKey1, horseVal0);
Repository track3 = track2.startTracking();
ContractDetails cowDetails = track3.getContractDetails(COW);
cowDetails.put(cowKey1, cowVal1);
ContractDetails horseDetails = track3.getContractDetails(HORSE);
horseDetails.put(horseKey1, horseVal1);
track3.commit();
track2.rollback();
ContractDetails cowDetailsOrigin = repository.getContractDetails(COW);
DataWord cowValOrin = cowDetailsOrigin.get(cowKey1);
ContractDetails horseDetailsOrigin = repository.getContractDetails(HORSE);
DataWord horseValOrin = horseDetailsOrigin.get(horseKey1);
assertEquals(cowVal0, cowValOrin);
assertEquals(horseVal0, horseValOrin);
}
Aggregations