use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class BlockExecutorTest method executeBlockWithTxThatMakesBlockInvalidSenderHasNoBalance.
@Test
public void executeBlockWithTxThatMakesBlockInvalidSenderHasNoBalance() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Repository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
Repository track = repository.startTracking();
Account account = createAccount("acctest1", track, Coin.valueOf(30000));
Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
Account account3 = createAccount("acctest3", track, Coin.ZERO);
track.commit();
Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
BlockExecutor executor = buildBlockExecutor(trieStore);
Transaction tx3 = 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();
tx3.sign(account.getEcKey().getPrivKeyBytes());
Transaction tx = tx3;
Transaction tx1 = Transaction.builder().nonce(repository.getNonce(account3.getAddress())).gasPrice(BigInteger.ONE).gasLimit(BigInteger.valueOf(21000)).destination(account2.getAddress()).chainId(CONFIG.getNetworkConstants().getChainId()).value(BigInteger.TEN).build();
tx1.sign(account3.getEcKey().getPrivKeyBytes());
Transaction tx2 = tx1;
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
txs.add(tx2);
List<BlockHeader> uncles = new ArrayList<>();
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
genesis.setStateRoot(repository.getRoot());
Block block = blockGenerator.createChildBlock(genesis, txs, uncles, 1, null);
BlockResult result = executor.execute(block, genesis.getHeader(), false);
Assert.assertSame(BlockResult.INTERRUPTED_EXECUTION_BLOCK_RESULT, result);
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class BlockExecutorTest method executeBlockWithOneStrangeTransaction.
private void executeBlockWithOneStrangeTransaction(boolean mustFailValidation, boolean mustFailExecution, TestObjects objects) {
Block parent = objects.getParent();
Block block = objects.getBlock();
TrieStore trieStore = objects.getTrieStore();
BlockExecutor executor = buildBlockExecutor(trieStore);
Repository repository = new MutableRepository(trieStore, trieStore.retrieve(objects.getParent().getStateRoot()).get());
Transaction tx = objects.getTransaction();
Account account = objects.getAccount();
BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
// Only adding one rule
validatorBuilder.addBlockTxsFieldsValidationRule();
BlockValidatorImpl validator = validatorBuilder.build();
Assert.assertEquals(validator.isValid(block), !mustFailValidation);
if (mustFailValidation) {
// If it fails validation, is it important if it fails or not execution? I don't think so.
return;
}
BlockResult result = executor.execute(block, parent.getHeader(), false);
Assert.assertNotNull(result);
if (mustFailExecution) {
Assert.assertEquals(result, BlockResult.INTERRUPTED_EXECUTION_BLOCK_RESULT);
return;
}
Assert.assertNotNull(result.getTransactionReceipts());
Assert.assertFalse(result.getTransactionReceipts().isEmpty());
Assert.assertEquals(1, result.getTransactionReceipts().size());
TransactionReceipt receipt = result.getTransactionReceipts().get(0);
Assert.assertEquals(tx, receipt.getTransaction());
Assert.assertEquals(21000, new BigInteger(1, receipt.getGasUsed()).longValue());
Assert.assertEquals(21000, new BigInteger(1, receipt.getCumulativeGas()).longValue());
Assert.assertEquals(21000, result.getGasUsed());
Assert.assertEquals(Coin.valueOf(21000), result.getPaidFees());
Assert.assertFalse(Arrays.equals(repository.getRoot(), result.getFinalState().getHash().getBytes()));
byte[] calculatedLogsBloom = BlockExecutor.calculateLogsBloom(result.getTransactionReceipts());
Assert.assertEquals(256, calculatedLogsBloom.length);
Assert.assertArrayEquals(new byte[256], calculatedLogsBloom);
AccountState accountState = repository.getAccountState(account.getAddress());
Assert.assertNotNull(accountState);
Assert.assertEquals(BigInteger.valueOf(30000), accountState.getBalance().asBigInteger());
Repository finalRepository = new MutableRepository(trieStore, trieStore.retrieve(result.getFinalState().getHash().getBytes()).get());
accountState = finalRepository.getAccountState(account.getAddress());
Assert.assertNotNull(accountState);
Assert.assertEquals(BigInteger.valueOf(30000 - 21000 - 10), accountState.getBalance().asBigInteger());
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class BlockExecutorTest method executeBlockWithOneTransaction.
@Test
public void executeBlockWithOneTransaction() {
executor.setRegisterProgramResults(false);
// this changes the best block
Block block = getBlockWithOneTransaction();
Block parent = blockchain.getBestBlock();
Transaction tx = block.getTransactionsList().get(0);
RskAddress account = tx.getSender();
BlockResult result = executor.execute(block, parent.getHeader(), false);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getTransactionReceipts());
Assert.assertFalse(result.getTransactionReceipts().isEmpty());
Assert.assertEquals(1, result.getTransactionReceipts().size());
Assert.assertNull(executor.getProgramResult(tx.getHash()));
TransactionReceipt receipt = result.getTransactionReceipts().get(0);
Assert.assertEquals(tx, receipt.getTransaction());
Assert.assertEquals(21000, new BigInteger(1, receipt.getGasUsed()).longValue());
Assert.assertEquals(21000, new BigInteger(1, receipt.getCumulativeGas()).longValue());
Assert.assertTrue(receipt.hasTxStatus() && receipt.isTxStatusOK() && receipt.isSuccessful());
Assert.assertEquals(21000, result.getGasUsed());
Assert.assertEquals(21000, result.getPaidFees().asBigInteger().intValueExact());
Assert.assertFalse(Arrays.equals(repository.getRoot(), result.getFinalState().getHash().getBytes()));
byte[] calculatedLogsBloom = BlockExecutor.calculateLogsBloom(result.getTransactionReceipts());
Assert.assertEquals(256, calculatedLogsBloom.length);
Assert.assertArrayEquals(new byte[256], calculatedLogsBloom);
AccountState accountState = repository.getAccountState(account);
Assert.assertNotNull(accountState);
Assert.assertEquals(BigInteger.valueOf(30000), accountState.getBalance().asBigInteger());
Repository finalRepository = new MutableRepository(trieStore, trieStore.retrieve(result.getFinalState().getHash().getBytes()).get());
accountState = finalRepository.getAccountState(account);
Assert.assertNotNull(accountState);
Assert.assertEquals(BigInteger.valueOf(30000 - 21000 - 10), accountState.getBalance().asBigInteger());
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class BlockExecutorTest method executeBlockWithOneTransactionAndCollectingProgramResults.
@Test
public void executeBlockWithOneTransactionAndCollectingProgramResults() {
executor.setRegisterProgramResults(true);
// this changes the best block
Block block = getBlockWithOneTransaction();
Block parent = blockchain.getBestBlock();
Transaction tx = block.getTransactionsList().get(0);
RskAddress account = tx.getSender();
BlockResult result = executor.execute(block, parent.getHeader(), false);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getTransactionReceipts());
Assert.assertFalse(result.getTransactionReceipts().isEmpty());
Assert.assertEquals(1, result.getTransactionReceipts().size());
Assert.assertNotNull(executor.getProgramResult(tx.getHash()));
executor.setRegisterProgramResults(false);
TransactionReceipt receipt = result.getTransactionReceipts().get(0);
Assert.assertEquals(tx, receipt.getTransaction());
Assert.assertEquals(21000, new BigInteger(1, receipt.getGasUsed()).longValue());
Assert.assertEquals(21000, new BigInteger(1, receipt.getCumulativeGas()).longValue());
Assert.assertTrue(receipt.hasTxStatus() && receipt.isTxStatusOK() && receipt.isSuccessful());
Assert.assertEquals(21000, result.getGasUsed());
Assert.assertEquals(21000, result.getPaidFees().asBigInteger().intValueExact());
Assert.assertFalse(Arrays.equals(repository.getRoot(), result.getFinalState().getHash().getBytes()));
byte[] calculatedLogsBloom = BlockExecutor.calculateLogsBloom(result.getTransactionReceipts());
Assert.assertEquals(256, calculatedLogsBloom.length);
Assert.assertArrayEquals(new byte[256], calculatedLogsBloom);
AccountState accountState = repository.getAccountState(account);
Assert.assertNotNull(accountState);
Assert.assertEquals(BigInteger.valueOf(30000), accountState.getBalance().asBigInteger());
Repository finalRepository = new MutableRepository(trieStore, trieStore.retrieve(result.getFinalState().getHash().getBytes()).get());
accountState = finalRepository.getAccountState(account);
Assert.assertNotNull(accountState);
Assert.assertEquals(BigInteger.valueOf(30000 - 21000 - 10), accountState.getBalance().asBigInteger());
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class BlockExecutorTest method executeAndFillBlockWithTxToExcludeBecauseSenderHasNoBalance.
@Test
public void executeAndFillBlockWithTxToExcludeBecauseSenderHasNoBalance() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Repository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
Repository track = repository.startTracking();
Account account = createAccount("acctest1", track, Coin.valueOf(30000));
Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
Account account3 = createAccount("acctest3", track, Coin.ZERO);
track.commit();
Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
BlockExecutor executor = buildBlockExecutor(trieStore);
Transaction tx3 = 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();
tx3.sign(account.getEcKey().getPrivKeyBytes());
Transaction tx = tx3;
Transaction tx1 = Transaction.builder().nonce(repository.getNonce(account3.getAddress())).gasPrice(BigInteger.ONE).gasLimit(BigInteger.valueOf(21000)).destination(account2.getAddress()).chainId(CONFIG.getNetworkConstants().getChainId()).value(BigInteger.TEN).build();
tx1.sign(account3.getEcKey().getPrivKeyBytes());
Transaction tx2 = tx1;
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
txs.add(tx2);
List<BlockHeader> uncles = new ArrayList<>();
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
genesis.setStateRoot(repository.getRoot());
Block block = blockGenerator.createChildBlock(genesis, txs, uncles, 1, null);
executor.executeAndFill(block, genesis.getHeader());
// Check tx2 was excluded
Assert.assertEquals(1, block.getTransactionsList().size());
Assert.assertEquals(tx, block.getTransactionsList().get(0));
Assert.assertArrayEquals(calculateTxTrieRoot(Collections.singletonList(tx), block.getNumber()), block.getTxTrieRoot());
Assert.assertEquals(3141592, new BigInteger(1, block.getGasLimit()).longValue());
}
Aggregations