Search in sources :

Example 36 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class TransactionStore method put.

public boolean put(INFO tx) {
    byte[] txHash = tx.getReceipt().getTransaction().getHash();
    List<INFO> existingInfos = null;
    synchronized (lastSavedTxHash) {
        if (lastSavedTxHash.put(new ByteArrayWrapper(txHash), object) != null || !lastSavedTxHash.isFull()) {
            existingInfos = get(txHash);
        }
    }
    if (existingInfos == null) {
        existingInfos = new ArrayList<>();
    } else {
        for (AbstractTxInfo<TXR, TX> info : existingInfos) {
            if (FastByteComparisons.equal(info.getBlockHash(), tx.getBlockHash())) {
                return false;
            }
        }
    }
    existingInfos.add(tx);
    put(txHash, existingInfos);
    return true;
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Example 37 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class AbstractDatabaseWithCache method put.

@Override
public void put(byte[] k, byte[] v) {
    AbstractDB.check(k);
    // acquire write lock
    lock.writeLock().lock();
    try {
        check();
        ByteArrayWrapper key = ByteArrayWrapper.wrap(k);
        this.loadingCache.put(key, Optional.ofNullable(v));
        // keeping track of dirty data
        this.dirtyEntries.put(key, v);
        if (enableAutoCommit) {
            flushInternal();
        }
    } finally {
        // releasing write lock
        lock.writeLock().unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper)

Example 38 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class ApiWeb3Aion method eth_getTransactionReceipt.

public Object eth_getTransactionReceipt(String _txHash) {
    byte[] txHash = TypeConverter.StringHexToByteArray(_txHash);
    TxRecpt r = getTransactionReceipt(txHash);
    // if we can't find the receipt on the mainchain, try looking for it in pending receipts cache
    if (r == null) {
        AionTxReceipt pendingReceipt = pendingReceipts.get(new ByteArrayWrapper(txHash));
        r = new TxRecpt(pendingReceipt, null, null, null, true);
    }
    if (r == null)
        return null;
    return r.toJson();
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) AionTxReceipt(org.aion.zero.types.AionTxReceipt)

Example 39 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class ApiAion0 method pendingTxUpdate.

protected void pendingTxUpdate(ITxReceipt _txRcpt, EventTx.STATE _state) {
    ByteArrayWrapper txHashW = new ByteArrayWrapper(((AionTxReceipt) _txRcpt).getTransaction().getHash());
    if (LOG.isDebugEnabled()) {
        LOG.debug("ApiAionA0.onPendingTransactionUpdate - txHash: [{}], state: [{}]", txHashW.toString(), _state.getValue());
    }
    if (getMsgIdMapping().get(txHashW) != null) {
        if (pendingStatus.remainingCapacity() == 0) {
            pendingStatus.poll();
            LOG.warn("ApiAionA0.onPendingTransactionUpdate - txPend ingStatus queue full, drop the first message.");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("ApiAionA0.onPendingTransactionUpdate - the pending Tx state : [{}]", _state.getValue());
        }
        pendingStatus.add(new TxPendingStatus(txHashW, getMsgIdMapping().get(txHashW).getValue(), getMsgIdMapping().get(txHashW).getKey(), _state.getValue(), ByteArrayWrapper.wrap(((AionTxReceipt) _txRcpt).getExecutionResult() == null ? ByteUtil.EMPTY_BYTE_ARRAY : ((AionTxReceipt) _txRcpt).getExecutionResult())));
        if (_state.isPending()) {
            pendingReceipts.put(txHashW, ((AionTxReceipt) _txRcpt));
        } else {
            pendingReceipts.remove(txHashW);
            getMsgIdMapping().remove(txHashW);
        }
    } else {
        if (txWait.remainingCapacity() == 0) {
            txWait.poll();
            if (LOG.isDebugEnabled()) {
                LOG.debug("ApiAionA0.onPendingTransactionUpdate - txWait queue full, drop the first message.");
            }
        }
        // waiting origin Api call status been callback
        try {
            txWait.put(new TxWaitingMappingUpdate(txHashW, _state.getValue(), ((AionTxReceipt) _txRcpt)));
        } catch (InterruptedException e) {
            LOG.error("ApiAionA0.onPendingTransactionUpdate txWait.put exception", e.getMessage());
        }
    }
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) AionTxReceipt(org.aion.zero.types.AionTxReceipt)

Example 40 with ByteArrayWrapper

use of org.aion.base.util.ByteArrayWrapper in project aion by aionnetwork.

the class AbstractTxPool method updateAccPoolState.

protected void updateAccPoolState() {
    // iterate tx by account
    List<Address> clearAddr = new ArrayList<>();
    for (Entry<Address, AccountState> e : this.accountView.entrySet()) {
        AccountState as = e.getValue();
        if (as.isDirty()) {
            if (as.getMap().isEmpty()) {
                this.poolStateView.remove(e.getKey());
                clearAddr.add(e.getKey());
            } else {
                // checking AccountState given by account
                List<PoolState> psl = this.poolStateView.get(e.getKey());
                if (psl == null) {
                    psl = new LinkedList<>();
                }
                List<PoolState> newPoolState = new LinkedList<>();
                // Checking new tx has been include into old pools.
                BigInteger txNonceStart = as.getFirstNonce();
                if (txNonceStart != null) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("AbstractTxPool.updateAccPoolState fn [{}]", txNonceStart.toString());
                    }
                    for (PoolState ps : psl) {
                        // check the previous txn status in the old PoolState
                        if (isClean(ps, as) && ps.firstNonce.equals(txNonceStart) && ps.combo == SEQUENTAILTXNCOUNT_MAX) {
                            ps.resetInFeePool();
                            newPoolState.add(ps);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("AbstractTxPool.updateAccPoolState add fn [{}]", ps.firstNonce.toString());
                            }
                            txNonceStart = txNonceStart.add(BigInteger.valueOf(SEQUENTAILTXNCOUNT_MAX));
                        } else {
                            // remove old poolState in the feeMap
                            Map<ByteArrayWrapper, TxDependList<ByteArrayWrapper>> txDp = this.feeView.get(ps.getFee());
                            if (txDp != null) {
                                if (e.getValue().getMap().get(ps.firstNonce) != null) {
                                    txDp.remove(e.getValue().getMap().get(ps.firstNonce).getKey());
                                }
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("AbstractTxPool.updateAccPoolState remove fn [{}]", ps.firstNonce.toString());
                                }
                                if (txDp.isEmpty()) {
                                    this.feeView.remove(ps.getFee());
                                }
                            }
                        }
                    }
                }
                int cnt = 0;
                BigInteger fee = BigInteger.ZERO;
                BigInteger totalFee = BigInteger.ZERO;
                for (Entry<BigInteger, SimpleEntry<ByteArrayWrapper, BigInteger>> en : as.getMap().entrySet()) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("AbstractTxPool.updateAccPoolState mapsize[{}] nonce:[{}] cnt[{}] txNonceStart[{}]", as.getMap().size(), en.getKey().toString(), cnt, txNonceStart.toString());
                    }
                    if (en.getKey().equals(txNonceStart != null ? txNonceStart.add(BigInteger.valueOf(cnt)) : null)) {
                        if (en.getValue().getValue().compareTo(fee) > -1) {
                            fee = en.getValue().getValue();
                            totalFee = totalFee.add(fee);
                            if (++cnt == SEQUENTAILTXNCOUNT_MAX) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("AbstractTxPool.updateAccPoolState case1 - nonce:[{}] totalFee:[{}] cnt:[{}]", txNonceStart, totalFee.toString(), cnt);
                                }
                                newPoolState.add(new PoolState(txNonceStart, totalFee.divide(BigInteger.valueOf(cnt)), cnt));
                                txNonceStart = en.getKey().add(BigInteger.ONE);
                                totalFee = BigInteger.ZERO;
                                fee = BigInteger.ZERO;
                                cnt = 0;
                            }
                        } else {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("AbstractTxPool.updateAccPoolState case2 - nonce:[{}] totalFee:[{}] cnt:[{}]", txNonceStart, totalFee.toString(), cnt);
                            }
                            newPoolState.add(new PoolState(txNonceStart, totalFee.divide(BigInteger.valueOf(cnt)), cnt));
                            // next PoolState
                            txNonceStart = en.getKey();
                            fee = en.getValue().getValue();
                            totalFee = fee;
                            cnt = 1;
                        }
                    }
                }
                if (totalFee.signum() == 1) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("AbstractTxPool.updateAccPoolState case3 - nonce:[{}] totalFee:[{}] cnt:[{}] bw:[{}]", txNonceStart, totalFee.toString(), cnt, e.getKey().toString());
                    }
                    newPoolState.add(new PoolState(txNonceStart, totalFee.divide(BigInteger.valueOf(cnt)), cnt));
                }
                this.poolStateView.put(e.getKey(), newPoolState);
                if (LOG.isTraceEnabled()) {
                    this.poolStateView.forEach((k, v) -> v.forEach(l -> {
                        LOG.trace("AbstractTxPool.updateAccPoolState - the first nonce of the poolState list:[{}]", l.firstNonce);
                    }));
                }
                as.sorted();
            }
        }
    }
    if (!clearAddr.isEmpty()) {
        clearAddr.forEach(addr -> {
            this.accountView.remove(addr);
            this.bestNonce.remove(addr);
        });
    }
}
Also used : AionLoggerFactory(org.aion.log.AionLoggerFactory) java.util(java.util) Logger(org.slf4j.Logger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEnum(org.aion.log.LogEnum) Entry(java.util.Map.Entry) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger) ByteUtils(org.spongycastle.pqc.math.linearalgebra.ByteUtils) SimpleEntry(java.util.AbstractMap.SimpleEntry) Address(org.aion.base.type.Address) SimpleEntry(java.util.AbstractMap.SimpleEntry) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) BigInteger(java.math.BigInteger)

Aggregations

ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)45 BigInteger (java.math.BigInteger)10 Address (org.aion.base.type.Address)5 ITransaction (org.aion.base.type.ITransaction)4 Entry (java.util.Map.Entry)3 DataWord (org.aion.mcf.vm.types.DataWord)3 AionBlock (org.aion.zero.impl.types.AionBlock)3 Test (org.junit.Test)3 java.util (java.util)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ImportResult (org.aion.mcf.core.ImportResult)2 Value (org.aion.rlp.Value)2 AccountState (org.aion.txpool.common.AccountState)2 TxDependList (org.aion.txpool.common.TxDependList)2 AionTxReceipt (org.aion.zero.types.AionTxReceipt)2