Search in sources :

Example 36 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolV1 method getPoolTx.

/**
 * @implNote get pool transaction by given the transaction sender address and the transaction nonce.
 * @param sender the transaction sender address.
 * @param nonce the transaction nonce.
 * @return the pooledTransaction when the arguments matched. Otherwise, return null.
 */
public PooledTransaction getPoolTx(AionAddress sender, BigInteger nonce) {
    Objects.requireNonNull(sender);
    Objects.requireNonNull(nonce);
    lock.lock();
    try {
        Map<BigInteger, ByteArrayWrapper> accountInfo = accountView.get(sender);
        if (accountInfo == null) {
            return null;
        }
        ByteArrayWrapper txHash = accountInfo.get(nonce);
        if (txHash == null) {
            return null;
        }
        return poolTransactions.get(txHash);
    } finally {
        lock.unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) BigInteger(java.math.BigInteger)

Example 37 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolV1 method updatePoolTransaction.

/**
 * @implNote Update pool transaction for updating the correct energy consumption for repay transaction.
 * @param pooledTransaction update the pooedTransaction
 */
public void updatePoolTransaction(PooledTransaction pooledTransaction) {
    Objects.requireNonNull(pooledTransaction);
    lock.lock();
    try {
        ByteArrayWrapper txHash = ByteArrayWrapper.wrap(pooledTransaction.tx.getTransactionHash());
        if (poolTransactions.containsKey(txHash)) {
            poolTransactions.put(txHash, pooledTransaction);
        }
    } finally {
        lock.unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 38 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolV1 method remove.

/**
 * @implNote remove transactions into the pool.
 * @param tx pool transactions
 * @return the transactions has been removed from the pool.
 */
public List<PooledTransaction> remove(List<PooledTransaction> tx) {
    Objects.requireNonNull(tx);
    lock.lock();
    try {
        List<PooledTransaction> removedTx = new ArrayList<>();
        for (PooledTransaction pTx : tx) {
            ByteArrayWrapper txHash = ByteArrayWrapper.wrap(pTx.tx.getTransactionHash());
            PooledTransaction removedPoolTx = poolRemove(txHash);
            if (removedPoolTx != null) {
                removedTx.add(removedPoolTx);
            }
        }
        return removedTx;
    } finally {
        lock.unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) PooledTransaction(org.aion.base.PooledTransaction)

Example 39 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolV1 method add.

/**
 * @implNote add transactions into the pool. If the transaction has the same account nonce and
 *     the new transaction has higher energy price. The pool will remove the old transaction and
 *     update to the new one.
 * @param list pool transactions
 * @return the transactions has been added into the pool.
 */
public List<PooledTransaction> add(List<PooledTransaction> list) {
    Objects.requireNonNull(list);
    if (list.isEmpty()) {
        return Collections.emptyList();
    }
    lock.lock();
    try {
        List<PooledTransaction> addedTransactions = new ArrayList<>();
        for (PooledTransaction poolTx : list) {
            if (poolTransactions.size() == maxPoolSize) {
                LOG_TXPOOL.warn("txPool is full. No transaction has been added!");
                return addedTransactions;
            }
            ByteArrayWrapper repayOldTx = checkRepayTransaction(poolTx.tx);
            ByteArrayWrapper poolTxHash = ByteArrayWrapper.wrap(poolTx.tx.getTransactionHash());
            if (repayOldTx != null) {
                if (repayOldTx.equals(poolTxHash)) {
                    LOG_TXPOOL.debug("skip adding the tx [{}] because it's not a valid repay transaction.", poolTx.tx);
                    continue;
                } else {
                    LOG_TXPOOL.debug("repay tx found! Remove original tx");
                    droppedPoolTx = poolRemove(repayOldTx);
                }
            }
            poolAdd(poolTxHash, poolTx);
            addedTransactions.add(poolTx);
        }
        return addedTransactions;
    } finally {
        lock.unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) PooledTransaction(org.aion.base.PooledTransaction)

Example 40 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TxPoolV1Test method snapshotWithSameTransactionTimestamp.

@Test
public void snapshotWithSameTransactionTimestamp() {
    Properties config = new Properties();
    config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");
    TxPoolV1 tp = new TxPoolV1(config);
    List<PooledTransaction> txnl = new ArrayList<>();
    Map<ByteArrayWrapper, PooledTransaction> txMap = new HashMap<>();
    byte[] timeStamp = ByteUtil.longToBytes(TimeInstant.now().toEpochMicro());
    final int cnt = 16;
    for (int i = 0; i < cnt; i++) {
        byte[] nonce = new byte[Long.BYTES];
        nonce[Long.BYTES - 1] = (byte) i;
        PooledTransaction pooledTx = genTransactionWithTimestamp(nonce, key.get(0), timeStamp);
        txnl.add(pooledTx);
        txMap.put(ByteArrayWrapper.wrap(pooledTx.tx.getTransactionHash()), pooledTx);
    }
    timeStamp = ByteUtil.longToBytes(TimeInstant.now().toEpochMicro());
    for (int i = 0; i < cnt; i++) {
        byte[] nonce = new byte[Long.BYTES];
        nonce[Long.BYTES - 1] = (byte) i;
        PooledTransaction pooledTx = genTransactionWithTimestamp(nonce, key.get(1), timeStamp);
        txnl.add(pooledTx);
        txMap.put(ByteArrayWrapper.wrap(pooledTx.tx.getTransactionHash()), pooledTx);
    }
    timeStamp = ByteUtil.longToBytes(TimeInstant.now().toEpochMicro());
    for (int i = cnt; i < 2 * cnt; i++) {
        byte[] nonce = new byte[Long.BYTES];
        nonce[Long.BYTES - 1] = (byte) i;
        PooledTransaction pooledTx = genTransactionWithTimestamp(nonce, key.get(0), timeStamp);
        txnl.add(pooledTx);
        txMap.put(ByteArrayWrapper.wrap(pooledTx.tx.getTransactionHash()), pooledTx);
    }
    timeStamp = ByteUtil.longToBytes(TimeInstant.now().toEpochMicro());
    for (int i = cnt; i < 2 * cnt; i++) {
        byte[] nonce = new byte[Long.BYTES];
        nonce[Long.BYTES - 1] = (byte) i;
        PooledTransaction pooledTx = genTransactionWithTimestamp(nonce, key.get(1), timeStamp);
        txnl.add(pooledTx);
        txMap.put(ByteArrayWrapper.wrap(pooledTx.tx.getTransactionHash()), pooledTx);
    }
    tp.add(txnl);
    Assert.assertEquals(tp.size(), cnt * 4);
    // sort the inserted txs
    List<AionTransaction> txl = tp.snapshot();
    Assert.assertEquals(tp.size(), txl.size());
    Assert.assertEquals(tp.snapshotAll().size(), txl.size());
    for (AionTransaction tx : txl) {
        assertTrue(txMap.containsKey(ByteArrayWrapper.wrap(tx.getTransactionHash())));
        Assert.assertEquals(txMap.get(ByteArrayWrapper.wrap(tx.getTransactionHash())).tx, tx);
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PooledTransaction(org.aion.base.PooledTransaction) AionTransaction(org.aion.base.AionTransaction) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5