Search in sources :

Example 16 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class BlockChainImplTest method getBlockWithOneTransaction.

private Block getBlockWithOneTransaction() {
    Block bestBlock = blockChain.getBestBlock();
    RepositorySnapshot repository = objects.getRepositoryLocator().snapshotAt(bestBlock.getHeader());
    String toAddress = ByteUtil.toHexString(catKey.getAddress());
    BigInteger nonce = repository.getNonce(new RskAddress(cowKey.getAddress()));
    Transaction tx = Transaction.builder().nonce(nonce).gasPrice(BigInteger.ONE).gasLimit(BigInteger.valueOf(21000)).destination(Hex.decode(toAddress)).chainId(config.getNetworkConstants().getChainId()).value(BigInteger.TEN).build();
    tx.sign(cowKey.getPrivKeyBytes());
    List<Transaction> txs = java.util.Arrays.asList(tx, new RemascTransaction(bestBlock.getNumber() + 1));
    List<BlockHeader> uncles = new ArrayList<>();
    Block block = new BlockGenerator().createChildBlock(bestBlock, txs, uncles, 1, bestBlock.getMinimumGasPrice().asBigInteger());
    blockExecutor.executeAndFill(block, bestBlock.getHeader());
    return block;
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) RemascTransaction(co.rsk.remasc.RemascTransaction) RemascTransaction(co.rsk.remasc.RemascTransaction) RskAddress(co.rsk.core.RskAddress) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator)

Example 17 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class ContractRunner method createAndRunContract.

public ProgramResult createAndRunContract(byte[] bytecode, byte[] encodedCall, BigInteger value, boolean localCall) {
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    createContract(bytecode, repository);
    Transaction creationTx = contractCreateTx(bytecode, repository);
    executeTransaction(creationTx, repository);
    return runContract(creationTx.getContractAddress().getBytes(), encodedCall, value, localCall, repository);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot)

Example 18 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class TransactionModuleTest method testGasEstimation.

@Test
public void testGasEstimation() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    BlockStore blockStore = world.getBlockStore();
    BlockTxSignatureCache blockTxSignatureCache = world.getBlockTxSignatureCache();
    ReceivedTxSignatureCache receivedTxSignatureCache = world.getReceivedTxSignatureCache();
    TransactionExecutorFactory transactionExecutorFactory = buildTransactionExecutionFactoryWithProgramInvokeFactory(blockStore, null, blockTxSignatureCache);
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, transactionExecutorFactory, receivedTxSignatureCache, 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironmentGasExactimation(blockchain, trieStore, transactionPool, blockStore, transactionGateway, transactionExecutorFactory);
    RskAddress srcAddr = new RskAddress(ECKey.fromPrivate(Keccak256Helper.keccak256("cow".getBytes())).getAddress());
    // Create the transaction that creates the destination contract
    sendContractCreationTransaction(srcAddr, web3, repository);
    // Compute contract destination address
    BigInteger nonce = repository.getAccountState(srcAddr).getNonce();
    RskAddress contractAddress = new RskAddress(HashUtil.calcNewAddr(srcAddr.getBytes(), nonce.toByteArray()));
    // start with 5M
    int gasLimit = 5000000;
    int consumed = checkEstimateGas(callCallWithValue, 33472, gasLimit, srcAddr, contractAddress, web3, repository);
    // Now that I know the estimation, call again using the estimated value
    // it should not fail. We set the gasLimit to the expected value plus 1 to
    // differentiate between OOG and success.
    int consumed2 = checkEstimateGas(callCallWithValue, 33472, consumed + 1, srcAddr, contractAddress, web3, repository);
    Assert.assertEquals(consumed, consumed2);
    consumed = checkEstimateGas(callUnfill, 46942, gasLimit, srcAddr, contractAddress, web3, repository);
    consumed2 = checkEstimateGas(callUnfill, 46942, consumed + 1, srcAddr, contractAddress, web3, repository);
    Assert.assertEquals(consumed, consumed2);
}
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) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 19 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class TransactionModuleTest method sendTransactionMustNotBeMined.

@Test
public void sendTransactionMustNotBeMined() {
    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, false, world.getBlockTxSignatureCache(), transactionGateway);
    String tx = sendTransaction(web3, repository);
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, tx);
    // Transaction tx must not be in block
    Assert.assertNull(txInBlock);
}
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 20 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class TransactionPoolImpl method internalAddTransaction.

private TransactionPoolAddResult internalAddTransaction(final Transaction tx) {
    if (pendingTransactions.hasTransaction(tx)) {
        return TransactionPoolAddResult.withError("pending transaction with same hash already exists");
    }
    if (queuedTransactions.hasTransaction(tx)) {
        return TransactionPoolAddResult.withError("queued transaction with same hash already exists");
    }
    RepositorySnapshot currentRepository = getCurrentRepository();
    TransactionValidationResult validationResult = shouldAcceptTx(tx, currentRepository);
    if (!validationResult.transactionIsValid()) {
        return TransactionPoolAddResult.withError(validationResult.getErrorMessage());
    }
    Keccak256 hash = tx.getHash();
    logger.trace("add transaction {} {}", toBI(tx.getNonce()), tx.getHash());
    Long bnumber = Long.valueOf(getCurrentBestBlockNumber());
    if (!isBumpingGasPriceForSameNonceTx(tx)) {
        return TransactionPoolAddResult.withError("gas price not enough to bump transaction");
    }
    transactionBlocks.put(hash, bnumber);
    final long timestampSeconds = this.getCurrentTimeInSeconds();
    transactionTimes.put(hash, timestampSeconds);
    BigInteger currentNonce = getPendingState(currentRepository).getNonce(tx.getSender());
    BigInteger txNonce = tx.getNonceAsInteger();
    if (txNonce.compareTo(currentNonce) > 0) {
        this.addQueuedTransaction(tx);
        signatureCache.storeSender(tx);
        return TransactionPoolAddResult.okQueuedTransaction(tx);
    }
    if (!senderCanPayPendingTransactionsAndNewTx(tx, currentRepository)) {
        // discard this tx to prevent spam
        return TransactionPoolAddResult.withError("insufficient funds to pay for pending and new transaction");
    }
    pendingTransactions.addTransaction(tx);
    signatureCache.storeSender(tx);
    return TransactionPoolAddResult.okPendingTransaction(tx);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) TransactionValidationResult(co.rsk.net.TransactionValidationResult) BigInteger(java.math.BigInteger) Keccak256(co.rsk.crypto.Keccak256)

Aggregations

RepositorySnapshot (co.rsk.db.RepositorySnapshot)37 Test (org.junit.Test)25 RepositoryLocator (co.rsk.db.RepositoryLocator)18 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)13 RskAddress (co.rsk.core.RskAddress)12 BlockStore (org.ethereum.db.BlockStore)11 BigInteger (java.math.BigInteger)10 Coin (co.rsk.core.Coin)9 BlockExecutor (co.rsk.core.bc.BlockExecutor)8 World (co.rsk.test.World)7 TestSystemProperties (co.rsk.config.TestSystemProperties)6 RskSystemProperties (co.rsk.config.RskSystemProperties)5 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)5 Account (org.ethereum.core.Account)5 AccountInformationProvider (co.rsk.core.bc.AccountInformationProvider)3 TransactionGateway (co.rsk.net.TransactionGateway)3 DslParser (co.rsk.test.dsl.DslParser)3 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)3 TrieStore (co.rsk.trie.TrieStore)3 Block (org.ethereum.core.Block)3