Search in sources :

Example 1 with Address

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();
            }
        }
    }
}
Also used : Address(org.aion.base.type.Address) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger)

Example 2 with Address

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;
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) AccountState(org.aion.txpool.common.AccountState) Entry(java.util.Map.Entry) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger)

Example 3 with Address

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;
}
Also used : Address(org.aion.base.type.Address) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger) AccountState(org.aion.txpool.common.AccountState)

Example 4 with Address

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)));
        }
    }
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 5 with Address

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)));
        }
    }
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Aggregations

Address (org.aion.base.type.Address)47 BigInteger (java.math.BigInteger)28 Test (org.junit.Test)24 AionTransaction (org.aion.zero.types.AionTransaction)20 ITransaction (org.aion.base.type.ITransaction)14 ECKey (org.aion.crypto.ECKey)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)9 DataWord (org.aion.mcf.vm.types.DataWord)8 HashMap (java.util.HashMap)6 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)5 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)5 ImportResult (org.aion.mcf.core.ImportResult)5 AionBlock (org.aion.zero.impl.types.AionBlock)5 AccountState (org.aion.mcf.core.AccountState)4 ArrayList (java.util.ArrayList)3 IRepository (org.aion.base.db.IRepository)3 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)3 java.util (java.util)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 Entry (java.util.Map.Entry)2