Search in sources :

Example 1 with ITransaction

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;
}
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 2 with ITransaction

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

Example 3 with ITransaction

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

Example 4 with ITransaction

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

Example 5 with ITransaction

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

Aggregations

ITransaction (org.aion.base.type.ITransaction)29 AionTransaction (org.aion.zero.types.AionTransaction)26 Test (org.junit.Test)26 BigInteger (java.math.BigInteger)22 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)19 Address (org.aion.base.type.Address)13 ArrayList (java.util.ArrayList)7 ECKey (org.aion.crypto.ECKey)5 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)3 HashMap (java.util.HashMap)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AccountState (org.aion.txpool.common.AccountState)1 TxDependList (org.aion.txpool.common.TxDependList)1