use of org.aion.base.PooledTransaction in project aion by aionnetwork.
the class TxPoolV1Test method isContainedTest.
@Test
public void isContainedTest() {
List<PooledTransaction> txs = new ArrayList<>();
AionTransaction tx = AionTransaction.create(key.get(0), BigInteger.valueOf(0).toByteArray(), AddressUtils.wrapAddress("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), Constant.MIN_ENERGY_CONSUME, 1L, TransactionTypes.DEFAULT, null);
PooledTransaction pooledTx = new PooledTransaction(tx, Constant.MIN_ENERGY_CONSUME);
txs.add(pooledTx);
Properties config = new Properties();
config.put(TXPOOL_PROPERTY.PROP_POOL_SIZE_MAX, String.valueOf(Constant.TXPOOL_SIZE_MIN));
TxPoolV1 tp = new TxPoolV1(config);
assertThat(tp.isContained(new AionAddress(key.get(0).getAddress()), BigInteger.ZERO)).isFalse();
tp.add(txs);
assertThat(tp.isContained(new AionAddress(key.get(0).getAddress()), BigInteger.ZERO)).isTrue();
assertThat(tp.isContained(new AionAddress(key.get(0).getAddress()), BigInteger.ONE)).isFalse();
}
use of org.aion.base.PooledTransaction in project aion by aionnetwork.
the class AionPendingStateImpl method updateCachedTxToTxPool.
private void updateCachedTxToTxPool(Map<AionAddress, BigInteger> nonceMap) {
List<AionTransaction> newPendingTransactions = pendingTxCache.getNewPendingTransactions(nonceMap);
LOGGER_TX.debug("flushCachedTx - newPendingTxs#[{}]", newPendingTransactions.size());
Set<AionAddress> updatedAddress = new HashSet<>();
for (AionTransaction tx : newPendingTransactions) {
if (txPool.isFull()) {
LOGGER_TX.debug("flushCachedTx txPool is full, cannot add new pending transactions from the cachedPool");
break;
}
LOGGER_TX.debug("flushCachedTx - loop: {}", tx);
AionTxExecSummary txSum = executeTx(tx);
AionTxReceipt receipt = txSum.getReceipt();
receipt.setTransaction(tx);
if (txSum.isRejected()) {
LOGGER_TX.debug("Invalid transaction in cachedPool: {}", tx);
fireTxUpdate(receipt, PendingTransactionState.DROPPED, currentBestBlock.get());
pendingTxCache.removeTransaction(tx.getSenderAddress(), tx.getNonceBI());
} else {
PooledTransaction pTx = txPool.add(new PooledTransaction(tx, receipt.getEnergyUsed()));
if (pTx != null) {
fireTxUpdate(receipt, PendingTransactionState.PENDING, currentBestBlock.get());
updatedAddress.add(tx.getSenderAddress());
pendingTxCache.removeTransaction(tx.getSenderAddress(), tx.getNonceBI());
}
}
}
for (AionAddress addr : updatedAddress) {
nonceMap.put(addr, bestPendingStateNonce(addr));
}
}
use of org.aion.base.PooledTransaction in project aion by aionnetwork.
the class AionPendingStateImpl method addRepayTxToTxPool.
private void addRepayTxToTxPool() {
for (AionTransaction tx : repayTransaction) {
// Add the energy limit value because it will get rerun soon after it is added
PooledTransaction ptx = txPool.add(new PooledTransaction(tx, tx.getEnergyLimit()));
if (ptx != null && ptx.tx.equals(tx)) {
addPendingTxToBackupDatabase(tx);
} else {
// lowPriceTransaction been dropped!
PooledTransaction droppedPtx = txPool.getDroppedPoolTx();
if (droppedPtx != null) {
removeBackupDBPendingTx(droppedPtx.tx.getTransactionHash());
fireDroppedTx(droppedPtx.tx, TxResponse.DROPPED.getMessage());
}
}
}
}
use of org.aion.base.PooledTransaction in project aion by aionnetwork.
the class AionPendingStateImpl method processRollbackTransactions.
private void processRollbackTransactions(Block commonAncestor) {
// first return back the transactions from forked blocks
Block rollback = currentBestBlock.get();
Stack<List<AionTransaction>> stack = new Stack<>();
while (!rollback.isEqual(commonAncestor)) {
LOGGER_TX.debug("Rollback: {}", rollback.getShortDescr());
stack.push(rollback.getTransactionsList());
rollback = blockchain.getBlockByHash(rollback.getParentHash());
}
while (!stack.isEmpty()) {
List<AionTransaction> transactions = stack.pop();
for (AionTransaction tx : transactions) {
/* We can add the Tx directly to the pool with the energy value
because all txs in the pool are going to be re-run in rerunTxsInPool(best.get()) */
txPool.add(new PooledTransaction(tx, tx.getEnergyLimit()));
}
}
}
use of org.aion.base.PooledTransaction in project aion by aionnetwork.
the class AionPendingStateImpl method addPendingTransactionInner.
/**
* Executes pending tx on the latest best block Fires pending state update
*
* @param tx transaction come from API or P2P
* @return SUCCESS if transaction gets NEW_PENDING state, else appropriate message such as
* DROPPED, INVALID_TX, etc.
*/
private TxResponse addPendingTransactionInner(final AionTransaction tx) {
if (txPool.isContained(tx.getSenderAddress(), tx.getNonceBI())) {
// check energy usage
PooledTransaction poolTx = txPool.getPoolTx(tx.getSenderAddress(), tx.getNonceBI());
// Use BigInteger to avoid the overflow
BigInteger repayValidPrice = BigInteger.valueOf(poolTx.tx.getEnergyPrice()).multiply(BigInteger.TWO);
if (BigInteger.valueOf(tx.getEnergyPrice()).compareTo(repayValidPrice) >= 0) {
if (repayTransaction.size() < (txPool.maxPoolSize / 4)) {
repayTransaction.add(tx);
return TxResponse.REPAID;
} else {
fireDroppedTx(tx, TxResponse.REPAYTX_BUFFER_FULL.getMessage());
return TxResponse.DROPPED;
}
} else {
fireDroppedTx(tx, TxResponse.REPAYTX_LOWPRICE.getMessage());
return TxResponse.REPAYTX_LOWPRICE;
}
} else {
AionTxExecSummary txSum = executeTx(tx);
if (txSum.isRejected()) {
LOGGER_TX.debug("addPendingTransactionImpl tx: {} is rejected due to: {}", Hex.toHexString(tx.getTransactionHash()), txSum.getReceipt().getError());
fireTxUpdate(txSum.getReceipt(), PendingTransactionState.DROPPED, currentBestBlock.get());
return TxResponse.DROPPED;
} else {
PooledTransaction pendingTx = new PooledTransaction(tx, txSum.getReceipt().getEnergyUsed());
LOGGER_TX.debug("addPendingTransactionImpl validTx: {}", tx);
PooledTransaction rtn = this.txPool.add(pendingTx);
if (rtn == null || rtn != pendingTx) {
// Replay tx case should not happen in this check.
throw new IllegalStateException("The pool data has broken, missing the tx: " + pendingTx);
} else {
fireTxUpdate(txSum.getReceipt(), PendingTransactionState.NEW_PENDING, currentBestBlock.get());
return TxResponse.SUCCESS;
}
}
}
}
Aggregations