use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryTest method test4.
@Test
public void test4() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
byte[] cowKey = Hex.decode("A1A2A3");
byte[] cowValue = Hex.decode("A4A5A6");
byte[] horseKey = Hex.decode("B1B2B3");
byte[] horseValue = Hex.decode("B4B5B6");
track.addStorageBytes(COW, new DataWord(cowKey), cowValue);
track.addStorageBytes(HORSE, new DataWord(horseKey), horseValue);
track.commit();
assertArrayEquals(cowValue, repository.getStorageBytes(COW, new DataWord(cowKey)));
assertArrayEquals(horseValue, repository.getStorageBytes(HORSE, new DataWord(horseKey)));
repository.close();
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryTest 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 byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
final DataWord cowKey1 = new DataWord("c1");
final DataWord cowKey2 = new DataWord("c2");
final byte[] cowVal0 = Hex.decode("c0a0");
// track
Repository track2 = repository.startTracking();
track2.addStorageBytes(COW, cowKey2, cowVal0);
track2.commit();
repository.flush();
ContractDetails cowDetails = repository.getContractDetails(COW);
assertArrayEquals(cowVal0, cowDetails.getBytes(cowKey2));
final CountDownLatch failSema = new CountDownLatch(1);
new Thread(() -> {
try {
int cnt = 1;
while (true) {
// To Review, not needed?
repository.flush();
Repository snap = repository.getSnapshotTo(repository.getRoot()).startTracking();
byte[] vcnr = new byte[1];
vcnr[0] = (byte) (cnt % 128);
snap.addStorageBytes(COW, cowKey1, vcnr);
cnt++;
}
} catch (Throwable e) {
e.printStackTrace();
failSema.countDown();
}
}).start();
new Thread(() -> {
int cnt = 1;
try {
while (true) {
// track
Repository track21 = repository.startTracking();
byte[] cVal = new byte[1];
cVal[0] = (byte) (cnt % 128);
track21.addStorageBytes(COW, cowKey1, cVal);
track21.commit();
repository.flush();
assertArrayEquals(cVal, repository.getStorageBytes(COW, cowKey1));
assertArrayEquals(cowVal0, repository.getStorageBytes(COW, cowKey2));
cnt++;
}
} catch (Throwable e) {
e.printStackTrace();
failSema.countDown();
}
}).start();
failSema.await(10, TimeUnit.SECONDS);
if (failSema.getCount() == 0) {
throw new RuntimeException("Test failed.");
}
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryTest method test16_5.
@Test
public void test16_5() {
Repository repository = new RepositoryImpl(config);
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
byte[] cowKey1 = "key-c-1".getBytes();
byte[] cowValue1 = "val-c-1".getBytes();
byte[] horseKey1 = "key-h-1".getBytes();
byte[] horseValue1 = "val-h-1".getBytes();
byte[] cowKey2 = "key-c-2".getBytes();
byte[] cowValue2 = "val-c-2".getBytes();
byte[] horseKey2 = "key-h-2".getBytes();
byte[] horseValue2 = "val-h-2".getBytes();
// changes level_1
Repository track1 = repository.startTracking();
track1.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
// changes level_2
Repository track2 = track1.startTracking();
assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
assertNull(track1.getStorageBytes(COW, new DataWord(cowKey1)));
track2.commit();
// leaving level_2
track1.commit();
// leaving level_1
assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
assertNull(track1.getStorageBytes(COW, new DataWord(cowKey1)));
repository.close();
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryTest method test16.
@Test
public void test16() {
Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
byte[] cowKey1 = "key-c-1".getBytes();
byte[] cowValue1 = "val-c-1".getBytes();
byte[] horseKey1 = "key-h-1".getBytes();
byte[] horseValue1 = "val-h-1".getBytes();
byte[] cowKey2 = "key-c-2".getBytes();
byte[] cowValue2 = "val-c-2".getBytes();
byte[] horseKey2 = "key-h-2".getBytes();
byte[] horseValue2 = "val-h-2".getBytes();
// changes level_1
Repository track1 = repository.startTracking();
track1.addStorageBytes(COW, new DataWord(cowKey1), cowValue1);
track1.addStorageBytes(HORSE, new DataWord(horseKey1), horseValue1);
assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
assertArrayEquals(horseValue1, track1.getStorageBytes(HORSE, new DataWord(horseKey1)));
// changes level_2
Repository track2 = track1.startTracking();
track2.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
track2.addStorageBytes(HORSE, new DataWord(horseKey2), horseValue2);
assertArrayEquals(cowValue1, track2.getStorageBytes(COW, new DataWord(cowKey1)));
assertArrayEquals(horseValue1, track2.getStorageBytes(HORSE, new DataWord(horseKey1)));
assertArrayEquals(cowValue2, track2.getStorageBytes(COW, new DataWord(cowKey2)));
assertArrayEquals(horseValue2, track2.getStorageBytes(HORSE, new DataWord(horseKey2)));
track2.commit();
// leaving level_2
assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
assertArrayEquals(horseValue1, track1.getStorageBytes(HORSE, new DataWord(horseKey1)));
assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
assertArrayEquals(horseValue2, track1.getStorageBytes(HORSE, new DataWord(horseKey2)));
track1.commit();
// leaving level_1
assertArrayEquals(cowValue1, repository.getStorageBytes(COW, new DataWord(cowKey1)));
assertArrayEquals(horseValue1, repository.getStorageBytes(HORSE, new DataWord(horseKey1)));
assertArrayEquals(cowValue2, repository.getStorageBytes(COW, new DataWord(cowKey2)));
assertArrayEquals(horseValue2, repository.getStorageBytes(HORSE, new DataWord(horseKey2)));
repository.close();
}
use of org.ethereum.core.Repository in project rskj by rsksmart.
the class RepositoryTest method test10.
@Test
public void test10() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
DataWord cowKey = new DataWord(Hex.decode("A1A2A3"));
byte[] cowValue = Hex.decode("A4A5A6");
DataWord horseKey = new DataWord(Hex.decode("B1B2B3"));
byte[] horseValue = Hex.decode("B4B5B6");
track.addStorageBytes(COW, cowKey, cowValue);
track.addStorageBytes(HORSE, horseKey, horseValue);
assertArrayEquals(cowValue, track.getStorageBytes(COW, cowKey));
assertArrayEquals(horseValue, track.getStorageBytes(HORSE, horseKey));
track.rollback();
assertEquals(null, repository.getStorageBytes(COW, cowKey));
assertEquals(null, repository.getStorageBytes(HORSE, horseKey));
repository.close();
}
Aggregations