use of co.rsk.trie.TrieStore in project rskj by rsksmart.
the class TransactionModuleTest method sendTransactionMustBeMined.
@Test
public void sendTransactionMustBeMined() {
World world = new World();
BlockChainImpl blockchain = world.getBlockChain();
TrieStore trieStore = world.getTrieStore();
RepositoryLocator repositoryLocator = world.getRepositoryLocator();
RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
BlockStore blockStore = world.getBlockStore();
TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, null, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
Web3Impl web3 = createEnvironment(blockchain, null, trieStore, transactionPool, blockStore, true, world.getBlockTxSignatureCache(), transactionGateway);
String tx = sendTransaction(web3, repository);
Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, tx);
// Transaction tx must be in the block mined.
Assert.assertEquals(tx, txInBlock.getHash().toJsonString());
}
use of co.rsk.trie.TrieStore in project rskj by rsksmart.
the class TransactionModuleTest method sendRawTransactionWithoutAutoMining.
@Test
public void sendRawTransactionWithoutAutoMining() {
ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
World world = new World(receiptStore);
BlockChainImpl blockchain = world.getBlockChain();
TrieStore trieStore = world.getTrieStore();
RepositoryLocator repositoryLocator = world.getRepositoryLocator();
BlockStore blockStore = world.getBlockStore();
TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, receiptStore, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
Web3Impl web3 = createEnvironment(blockchain, receiptStore, trieStore, transactionPool, blockStore, false, world.getBlockTxSignatureCache(), transactionGateway);
String txHash = sendRawTransaction(web3);
Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
Assert.assertEquals(1, transactionPool.getPendingTransactions().size());
Assert.assertEquals(txHash, transactionPool.getPendingTransactions().get(0).getHash().toJsonString());
}
use of co.rsk.trie.TrieStore 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 co.rsk.trie.TrieStore 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 co.rsk.trie.TrieStore 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