Search in sources :

Example 21 with TrieStore

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());
}
Also used : BlockStore(org.ethereum.db.BlockStore) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) RepositorySnapshot(co.rsk.db.RepositorySnapshot) Test(org.junit.Test)

Example 22 with TrieStore

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());
}
Also used : BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 23 with TrieStore

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);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 24 with TrieStore

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());
}
Also used : TrieStore(co.rsk.trie.TrieStore) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger)

Example 25 with TrieStore

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());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Aggregations

TrieStore (co.rsk.trie.TrieStore)43 Test (org.junit.Test)29 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)28 HashMapDB (org.ethereum.datasource.HashMapDB)26 Trie (co.rsk.trie.Trie)18 MutableRepository (org.ethereum.db.MutableRepository)12 BlockStore (org.ethereum.db.BlockStore)11 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)9 RepositoryLocator (co.rsk.db.RepositoryLocator)6 MultiTrieStore (co.rsk.trie.MultiTrieStore)6 Path (java.nio.file.Path)6 Repository (org.ethereum.core.Repository)6 DataWord (org.ethereum.vm.DataWord)6 MutableTrieImpl (co.rsk.db.MutableTrieImpl)5 TransactionGateway (co.rsk.net.TransactionGateway)5 World (co.rsk.test.World)5 BigInteger (java.math.BigInteger)5 ArrayList (java.util.ArrayList)5 ECKey (org.ethereum.crypto.ECKey)5 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)5