Search in sources :

Example 21 with MutableRepository

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);
}
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 22 with MutableRepository

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

Example 23 with MutableRepository

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

Example 24 with MutableRepository

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

Example 25 with MutableRepository

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());
}
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

MutableRepository (org.ethereum.db.MutableRepository)40 Test (org.junit.Test)31 Trie (co.rsk.trie.Trie)26 RskAddress (co.rsk.core.RskAddress)21 Repository (org.ethereum.core.Repository)15 TrieStore (co.rsk.trie.TrieStore)12 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)12 HashMapDB (org.ethereum.datasource.HashMapDB)12 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 BigInteger (java.math.BigInteger)8 Coin (co.rsk.core.Coin)7 DataWord (org.ethereum.vm.DataWord)7 MutableTrieImpl (co.rsk.db.MutableTrieImpl)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)5 MutableTrie (co.rsk.trie.MutableTrie)4 ArrayList (java.util.ArrayList)4 Block (org.ethereum.core.Block)4 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)2 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)2 CountDownLatch (java.util.concurrent.CountDownLatch)2