use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class BlockExecutorTest method generateBlockWithOneTransaction.
private static TestObjects generateBlockWithOneTransaction() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Repository repository = new MutableRepository(trieStore, new Trie(trieStore));
Repository track = repository.startTracking();
Account account = createAccount("acctest1", track, Coin.valueOf(30000));
Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
track.commit();
Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
BlockExecutor executor = buildBlockExecutor(trieStore);
Transaction tx1 = Transaction.builder().nonce(repository.getNonce(account.getAddress())).gasPrice(BigInteger.ONE).gasLimit(BigInteger.valueOf(21000)).destination(account2.getAddress()).chainId(CONFIG.getNetworkConstants().getChainId()).value(BigInteger.TEN).build();
tx1.sign(account.getEcKey().getPrivKeyBytes());
Transaction tx = tx1;
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
List<BlockHeader> uncles = new ArrayList<>();
// getGenesisBlock() modifies the repository, adding some pre-mined accounts
// Not nice for a getter, but it is what it is :(
Block genesis = BlockChainImplTest.getGenesisBlock(trieStore);
genesis.setStateRoot(repository.getRoot());
// Returns the root state prior block execution but after loading
// some sample accounts (account/account2) and the premined accounts
// in genesis.
byte[] rootPriorExecution = repository.getRoot();
Block block = new BlockGenerator().createChildBlock(genesis, txs, uncles, 1, null);
executor.executeAndFill(block, genesis.getHeader());
repository.save();
return new TestObjects(trieStore, block, genesis, tx, account, rootPriorExecution);
}
use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class BlockExecutorTest method validateStateRootWithRskip126DisabledAndValidStateRoot.
@Test
public void validateStateRootWithRskip126DisabledAndValidStateRoot() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Trie trie = new Trie(trieStore);
Block block = new BlockGenerator().getBlock(1);
block.setStateRoot(trie.getHash().getBytes());
BlockResult blockResult = new BlockResult(block, Collections.emptyList(), Collections.emptyList(), 0, Coin.ZERO, trie);
RskSystemProperties cfg = spy(CONFIG);
ActivationConfig activationConfig = spy(cfg.getActivationConfig());
doReturn(false).when(activationConfig).isActive(eq(RSKIP126), anyLong());
doReturn(activationConfig).when(cfg).getActivationConfig();
BlockExecutor executor = buildBlockExecutor(trieStore, cfg);
Assert.assertTrue(executor.validateStateRoot(block.getHeader(), blockResult));
}
use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class BlockExecutorTest method validateStateRootWithRskip126DisabledAndInvalidStateRoot.
@Test
public void validateStateRootWithRskip126DisabledAndInvalidStateRoot() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Trie trie = new Trie(trieStore);
Block block = new BlockGenerator().getBlock(1);
block.setStateRoot(new byte[] { 1, 2, 3, 4 });
BlockResult blockResult = new BlockResult(block, Collections.emptyList(), Collections.emptyList(), 0, Coin.ZERO, trie);
RskSystemProperties cfg = spy(CONFIG);
ActivationConfig activationConfig = spy(cfg.getActivationConfig());
doReturn(false).when(activationConfig).isActive(eq(RSKIP126), anyLong());
doReturn(activationConfig).when(cfg).getActivationConfig();
BlockExecutor executor = buildBlockExecutor(trieStore, cfg);
Assert.assertTrue(executor.validateStateRoot(block.getHeader(), blockResult));
}
use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class RepositoryImplOriginalTest method testMultiThread.
// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
MutableTrieImpl mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
final Repository repository = new MutableRepository(mutableTrie);
final DataWord cowKey1 = DataWord.valueFromHex("c1");
final DataWord cowKey2 = DataWord.valueFromHex("c2");
final DataWord cowVal0 = DataWord.valueFromHex("c0a0");
Repository track2 = repository.startTracking();
track2.addStorageRow(COW, cowKey2, cowVal0);
track2.commit();
// Changes commited to repository
assertThat(repository.getStorageValue(COW, cowKey2), is(cowVal0));
final CountDownLatch failSema = new CountDownLatch(1);
// First create the 10 snapshots. The snapshots should not be created while the
// repository is being changed.
Repository[] snaps = new Repository[10];
for (int i = 0; i < 10; ++i) {
snaps[i] = new MutableRepository(trieStore, trieStore.retrieve(repository.getRoot()).get());
}
for (int i = 0; i < 10; ++i) {
int finalI = i;
new Thread(() -> {
try {
int cnt = 1;
while (running) {
Repository snap = snaps[finalI].startTracking();
snap.addBalance(COW, Coin.valueOf(10L));
snap.addStorageRow(COW, cowKey1, DataWord.valueOf(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 = DataWord.valueOf(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, DataWord.valueOf(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 co.rsk.trie.TrieStoreImpl 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);
}
Aggregations