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