Search in sources :

Example 51 with PooledTransaction

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();
}
Also used : AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) PooledTransaction(org.aion.base.PooledTransaction) AionTransaction(org.aion.base.AionTransaction) Properties(java.util.Properties) Test(org.junit.Test)

Example 52 with PooledTransaction

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));
    }
}
Also used : AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionTransaction(org.aion.base.AionTransaction) PooledTransaction(org.aion.base.PooledTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 53 with PooledTransaction

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());
            }
        }
    }
}
Also used : AionTransaction(org.aion.base.AionTransaction) PooledTransaction(org.aion.base.PooledTransaction)

Example 54 with PooledTransaction

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()));
        }
    }
}
Also used : Block(org.aion.zero.impl.types.Block) List(java.util.List) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) PooledTransaction(org.aion.base.PooledTransaction) Stack(java.util.Stack)

Example 55 with PooledTransaction

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;
            }
        }
    }
}
Also used : AionTxExecSummary(org.aion.base.AionTxExecSummary) BigInteger(java.math.BigInteger) PooledTransaction(org.aion.base.PooledTransaction)

Aggregations

PooledTransaction (org.aion.base.PooledTransaction)55 ArrayList (java.util.ArrayList)40 Properties (java.util.Properties)40 AionTransaction (org.aion.base.AionTransaction)40 Test (org.junit.Test)40 BigInteger (java.math.BigInteger)16 AionAddress (org.aion.types.AionAddress)16 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)11 HashMap (java.util.HashMap)5 ECKey (org.aion.crypto.ECKey)5 AionTxExecSummary (org.aion.base.AionTxExecSummary)3 List (java.util.List)2 AionTxReceipt (org.aion.base.AionTxReceipt)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 Stack (java.util.Stack)1 TreeMap (java.util.TreeMap)1 Block (org.aion.zero.impl.types.Block)1