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