use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class RepositoryTest method setUp.
@Before
public void setUp() {
trieStore = new TrieStoreImpl(new HashMapDB());
mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
repository = new MutableRepository(mutableTrie);
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class RepositoryTest method testCode.
@Test
public void testCode() {
Repository track = repository.startTracking();
byte[] codeLongerThan32bytes = "this-is-code-because-I-say-it-man".getBytes();
assertTrue(codeLongerThan32bytes.length > 32);
track.saveCode(COW, codeLongerThan32bytes);
byte[] returnedCode = track.getCode(COW);
assertArrayEquals(codeLongerThan32bytes, returnedCode);
track.commit();
// Now try to get the size
int codeSize = repository.getCodeLength(COW);
assertEquals(codeLongerThan32bytes.length, codeSize);
byte[] returnedCode2 = repository.getCode(COW);
assertArrayEquals(codeLongerThan32bytes, returnedCode2);
repository.save();
byte[] prevRoot = repository.getRoot();
// Use the same store
// Now we'll create a new repository based on the same store and force
// this new repository to read all nodes from the store. The results must
// be the same: lazy evaluation of the value must work.
Repository repository2 = new MutableRepository(trieStore, trieStore.retrieve(prevRoot).get());
// Now try to get the size
codeSize = repository2.getCodeLength(COW);
assertEquals(codeLongerThan32bytes.length, codeSize);
returnedCode2 = repository2.getCode(COW);
assertArrayEquals(codeLongerThan32bytes, returnedCode2);
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class RepositoryTest method testMultiThread.
// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
final DataWord cowKey1 = DataWord.valueFromHex("c1");
final DataWord cowKey2 = DataWord.valueFromHex("c2");
final byte[] cowVal0 = Hex.decode("c0a0");
// track
Repository track2 = repository.startTracking();
track2.addStorageBytes(COW, cowKey2, cowVal0);
track2.commit();
trieStore.flush();
assertArrayEquals(cowVal0, repository.getStorageBytes(COW, cowKey2));
final CountDownLatch failSema = new CountDownLatch(2);
Repository snap = new MutableRepository(trieStore, trieStore.retrieve(repository.getRoot()).get());
new Thread(() -> {
try {
int cnt = 1;
while (true) {
Repository snapTrack = snap.startTracking();
byte[] vcnr = new byte[1];
vcnr[0] = (byte) (cnt % 128);
snapTrack.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();
trieStore.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() < 2) {
throw new RuntimeException("Test failed.");
}
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class RepositoryUpdateTest method putNullValueAsDeleteValue.
@Test
public void putNullValueAsDeleteValue() {
ContractDetailsImpl details = buildContractDetails();
details.putBytes(DataWord.ONE, new byte[] { 0x01, 0x02, 0x03 });
details.putBytes(DataWord.ONE, null);
Repository repo = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(null, new Trie())));
updateContractDetails(repo, address, details);
repo.commit();
byte[] value = repo.getTrie().get(DataWord.ONE.getData());
Assert.assertNull(value);
Assert.assertEquals(0, details.getStorageSize());
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class RepositoryUpdateTest method putDataWordWithoutLeadingZeroes.
@Test
public void putDataWordWithoutLeadingZeroes() {
ContractDetailsImpl details = buildContractDetails();
details.put(DataWord.ONE, DataWord.valueOf(42));
Repository repo = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(null, new Trie())));
updateContractDetails(repo, address, details);
byte[] value = repo.getStorageBytes(address, DataWord.ONE);
Assert.assertNotNull(value);
Assert.assertEquals(1, value.length);
Assert.assertEquals(42, value[0]);
Assert.assertEquals(1, details.getStorageSize());
}
Aggregations