use of org.aion.base.type.ITransaction in project aion by aionnetwork.
the class TxPoolA0 method remove.
@Override
public synchronized List<TX> remove(Map<Address, BigInteger> accNonce) {
List<ByteArrayWrapper> bwList = new ArrayList<>();
for (Map.Entry<Address, BigInteger> en1 : accNonce.entrySet()) {
AccountState as = this.getAccView(en1.getKey());
Iterator<Map.Entry<BigInteger, AbstractMap.SimpleEntry<ByteArrayWrapper, BigInteger>>> it = as.getMap().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<BigInteger, AbstractMap.SimpleEntry<ByteArrayWrapper, BigInteger>> en = it.next();
if (en1.getValue().compareTo(en.getKey()) > 0) {
bwList.add(en.getValue().getKey());
it.remove();
} else {
break;
}
}
Set<BigInteger> fee = Collections.synchronizedSet(new HashSet<>());
if (this.getPoolStateView(en1.getKey()) != null) {
this.getPoolStateView(en1.getKey()).parallelStream().forEach(ps -> fee.add(ps.getFee()));
}
fee.parallelStream().forEach(bi -> {
if (this.getFeeView().get(bi) != null) {
this.getFeeView().get(bi).entrySet().removeIf(byteArrayWrapperTxDependListEntry -> byteArrayWrapperTxDependListEntry.getValue().getAddress().equals(en1.getKey()));
if (this.getFeeView().get(bi).isEmpty()) {
this.getFeeView().remove(bi);
}
}
});
as.setDirty();
}
List<TX> removedTxl = Collections.synchronizedList(new ArrayList<>());
bwList.parallelStream().forEach(bw -> {
if (this.getMainMap().get(bw) != null) {
ITransaction tx = this.getMainMap().get(bw).getTx().clone();
removedTxl.add((TX) tx);
long timestamp = tx.getTimeStampBI().longValue() / multiplyM;
synchronized (this.getTimeView().get(timestamp)) {
if (this.getTimeView().get(timestamp) == null) {
LOG.error("Txpool.remove can't find the timestamp in the map [{}]", tx.toString());
return;
}
this.getTimeView().get(timestamp).remove(bw);
if (this.getTimeView().get(timestamp).isEmpty()) {
this.getTimeView().remove(timestamp);
}
}
this.getMainMap().remove(bw);
}
});
this.updateAccPoolState();
this.updateFeeMap();
if (LOG.isInfoEnabled()) {
LOG.info("TxPoolA0.remove {} TX", removedTxl.size());
}
return removedTxl;
}
use of org.aion.base.type.ITransaction in project aion by aionnetwork.
the class TxnPoolTest method TxnfeeCombineTest.
@Test
public void TxnfeeCombineTest() {
Properties config = new Properties();
config.put("txn-timeout", "100");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 10;
for (int i = 0; i < cnt; i++) {
byte[] nonce = new byte[Long.BYTES];
nonce[Long.BYTES - 1] = (byte) (i + 1);
ITransaction txn = genTransaction(nonce);
((AionTransaction) txn).sign(key.get(0));
txn.setNrgConsume(i + 1);
txnl.add(txn);
}
tp.add(txnl);
assertTrue(tp.size() == cnt);
// sort the inserted txs
tp.snapshot();
List<BigInteger> nl = tp.getFeeList();
assertTrue(nl.size() == 1);
assertTrue(nl.get(0).compareTo(BigInteger.valueOf(55 / 10)) == 0);
}
use of org.aion.base.type.ITransaction in project aion by aionnetwork.
the class TxnPoolTest method TxnfeeCombineTest2.
@Test
public void TxnfeeCombineTest2() {
Properties config = new Properties();
config.put("txn-timeout", "100");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 26;
for (int i = 0; i < cnt; i++) {
byte[] nonce = new byte[Long.BYTES];
nonce[Long.BYTES - 1] = (byte) (i + 1);
ITransaction txn = genTransaction(nonce);
((AionTransaction) txn).sign(key.get(0));
txn.setNrgConsume(i + 1);
txnl.add(txn);
}
tp.add(txnl);
assertTrue(tp.size() == cnt);
// sort the inserted txs
tp.snapshot();
List<BigInteger> nl = tp.getFeeList();
assertTrue(nl.size() == 2);
assertTrue(nl.get(0).compareTo(BigInteger.valueOf(26)) == 0);
assertTrue(nl.get(1).compareTo(BigInteger.valueOf(325 / 25)) == 0);
}
use of org.aion.base.type.ITransaction in project aion by aionnetwork.
the class TxnPoolTest method snapshot4.
@Test
public void snapshot4() {
Properties config = new Properties();
config.put("txn-timeout", "100");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 26;
for (int i = 0; i < cnt; i++) {
byte[] nonce = new byte[Long.BYTES];
nonce[Long.BYTES - 1] = (byte) i;
ITransaction txn = genTransaction(nonce);
((AionTransaction) txn).sign(key.get(0));
txn.setNrgConsume(50 - i);
txnl.add(txn);
}
tp.add(txnl);
assertTrue(tp.size() == cnt);
// sort the inserted txs
List<ITransaction> txl = tp.snapshot();
long nonce = 0;
for (ITransaction tx : txl) {
assertTrue((new BigInteger(tx.getNonce())).longValue() == nonce++);
}
}
use of org.aion.base.type.ITransaction in project aion by aionnetwork.
the class TxnPoolTest method addRepeatedTxn2.
@Test
public void addRepeatedTxn2() {
Properties config = new Properties();
config.put("txn-timeout", "10");
ITxPool<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 10;
for (int i = 0; i < cnt; i++) {
byte[] nonce = new byte[Long.BYTES];
nonce[Long.BYTES - 1] = (byte) i;
ITransaction txn = genTransaction(nonce);
((AionTransaction) txn).sign(key.get(0));
txn.setNrgConsume(50);
txnl.add(txn);
}
tp.add(txnl);
assertTrue(tp.size() == cnt);
byte[] nonce = new byte[Long.BYTES];
nonce[Long.BYTES - 1] = (byte) 5;
ITransaction txn = genTransaction(nonce);
((AionTransaction) txn).sign(key.get(0));
txn.setNrgConsume(500);
tp.add(txn);
List<ITransaction> snapshot = tp.snapshot();
assertTrue(snapshot.size() == cnt);
assertTrue(snapshot.get(5).equals(txn));
}
Aggregations