use of org.aion.base.type.Address in project aion by aionnetwork.
the class AbstractTxPool method updateFeeMap.
protected void updateFeeMap() {
for (Entry<Address, List<PoolState>> e : this.poolStateView.entrySet()) {
ByteArrayWrapper dependTx = null;
for (PoolState ps : e.getValue()) {
if (LOG.isTraceEnabled()) {
LOG.trace("updateFeeMap addr[{}] inFp[{}] fn[{}] cb[{}] fee[{}]", e.getKey().toString(), ps.isInFeePool(), ps.getFirstNonce().toString(), ps.getCombo(), ps.getFee().toString());
}
if (ps.isInFeePool()) {
dependTx = this.accountView.get(e.getKey()).getMap().get(ps.getFirstNonce()).getKey();
if (LOG.isTraceEnabled()) {
LOG.trace("updateFeeMap isInFeePool [{}]", dependTx.toString());
}
} else {
TxDependList<ByteArrayWrapper> txl = new TxDependList<>();
for (BigInteger i = ps.firstNonce; i.compareTo(ps.firstNonce.add(BigInteger.valueOf(ps.combo))) < 0; i = i.add(BigInteger.ONE)) {
txl.addTx(this.accountView.get(e.getKey()).getMap().get(i).getKey());
}
if (!txl.isEmpty()) {
txl.setDependTx(dependTx);
dependTx = txl.getTxList().get(0);
txl.setAddress(e.getKey());
}
if (this.feeView.get(ps.fee) == null) {
Map<ByteArrayWrapper, TxDependList<ByteArrayWrapper>> set = new LinkedHashMap<>();
set.put(txl.getTxList().get(0), txl);
if (LOG.isTraceEnabled()) {
LOG.trace("updateFeeMap new feeView put fee[{}]", ps.fee);
}
this.feeView.put(ps.fee, set);
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("updateFeeMap update feeView put fee[{}]", ps.fee);
}
Map<ByteArrayWrapper, TxDependList<ByteArrayWrapper>> preset = this.feeView.get(ps.fee);
preset.put(txl.getTxList().get(0), txl);
this.feeView.put(ps.fee, preset);
}
ps.setInFeePool();
}
}
}
}
use of org.aion.base.type.Address 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.Address in project aion by aionnetwork.
the class TxPoolA0 method remove.
@Override
@Deprecated
public synchronized List<TX> remove(List<TX> txs) {
List<TX> removedTxl = Collections.synchronizedList(new ArrayList<>());
Set<Address> checkedAddress = Collections.synchronizedSet(new HashSet<>());
for (TX tx : txs) {
ByteArrayWrapper bw = ByteArrayWrapper.wrap(tx.getHash());
if (this.getMainMap().remove(bw) == null) {
continue;
}
// noinspection unchecked
removedTxl.add((TX) tx.clone());
if (LOG.isTraceEnabled()) {
LOG.trace("TxPoolA0.remove:[{}] nonce:[{}]", ByteUtils.toHexString(tx.getHash()), tx.getNonceBI().toString());
}
long timestamp = tx.getTimeStampBI().longValue() / multiplyM;
if (this.getTimeView().get(timestamp).remove(bw)) {
if (this.getTimeView().get(timestamp).isEmpty()) {
this.getTimeView().remove(timestamp);
}
}
// remove the all transactions belong to the given address in the feeView
Address address = tx.getFrom();
Set<BigInteger> fee = Collections.synchronizedSet(new HashSet<>());
if (!checkedAddress.contains(address)) {
this.getPoolStateView(tx.getFrom()).parallelStream().forEach(ps -> fee.add(ps.getFee()));
fee.parallelStream().forEach(bi -> {
this.getFeeView().get(bi).entrySet().removeIf(byteArrayWrapperTxDependListEntry -> byteArrayWrapperTxDependListEntry.getValue().getAddress().equals(address));
if (this.getFeeView().get(bi).isEmpty()) {
this.getFeeView().remove(bi);
}
});
checkedAddress.add(address);
}
AccountState as = this.getAccView(tx.getFrom());
as.getMap().remove(tx.getNonceBI());
as.setDirty();
}
this.updateAccPoolState();
this.updateFeeMap();
if (LOG.isInfoEnabled()) {
LOG.info("TxPoolA0.remove TX remove [{}] removed [{}]", txs.size(), removedTxl.size());
}
return removedTxl;
}
use of org.aion.base.type.Address in project aion by aionnetwork.
the class TxnPoolTest method benchmarkSnapshot2.
@Test
public /* 100K new transactions in pool around 650ms (cold-call)
1K new transactions insert to the pool later around 70ms to snap (including sort)
*/
void benchmarkSnapshot2() {
Properties config = new Properties();
config.put("txn-timeout", "100");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 10000;
for (ECKey aKey2 : key) {
Address acc = Address.wrap(aKey2.getAddress());
for (int i = 0; i < cnt; i++) {
ITransaction txn = new AionTransaction(BigInteger.valueOf(i).toByteArray(), acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
((AionTransaction) txn).sign(aKey2);
txn.setNrgConsume(100L);
txnl.add(txn);
}
}
tp.add(txnl);
assertTrue(tp.size() == cnt * key.size());
// sort the inserted txs
long start = System.currentTimeMillis();
tp.snapshot();
System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
int cnt2 = 100;
txnl.clear();
for (ECKey aKey1 : key) {
for (int i = 0; i < cnt2; i++) {
ITransaction txn = new AionTransaction(BigInteger.valueOf(cnt + i).toByteArray(), Address.wrap(aKey1.getAddress()), Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
((AionTransaction) txn).sign(aKey1);
txn.setNrgConsume(100L);
txnl.add(txn);
}
}
tp.add(txnl);
assertTrue(tp.size() == (cnt + cnt2) * key.size());
start = System.currentTimeMillis();
tp.snapshot();
System.out.println("2nd time spent: " + (System.currentTimeMillis() - start) + " ms.");
for (ECKey aKey : key) {
List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey.getAddress()));
for (int i = 0; i < cnt + cnt2; i++) {
assertTrue(nl.get(i).equals(BigInteger.valueOf(i)));
}
}
}
use of org.aion.base.type.Address in project aion by aionnetwork.
the class TxnPoolTest method noncebyAccountTest2.
@Test
public void noncebyAccountTest2() {
Properties config = new Properties();
config.put("txn-timeout", "10");
TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
List<ITransaction> txnl = new ArrayList<>();
int cnt = 100;
for (ECKey aKey1 : key) {
Address acc = Address.wrap(aKey1.getAddress());
for (int i = 0; i < cnt; i++) {
byte[] nonce = new byte[Long.BYTES];
nonce[Long.BYTES - 1] = (byte) (i + 1);
ITransaction txn = new AionTransaction(nonce, acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
((AionTransaction) txn).sign(aKey1);
txn.setNrgConsume(100L);
txnl.add(txn);
}
}
tp.add(txnl);
assertTrue(tp.size() == cnt * key.size());
// sort the inserted txs
tp.snapshot();
for (ECKey aKey : key) {
List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey.getAddress()));
for (int i = 0; i < cnt; i++) {
assertTrue(nl.get(i).equals(BigInteger.valueOf(i + 1)));
}
}
}
Aggregations