Search in sources :

Example 71 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class ApiAion method getTransactionByBlockHashAndIndex.

public AionTransaction getTransactionByBlockHashAndIndex(byte[] hash, long index) {
    AionBlock pBlk = this.getBlockByHash(hash);
    if (pBlk == null) {
        if (LOG.isErrorEnabled()) {
            LOG.error("ApiAion.getTransactionByBlockHashAndIndex - can't find the block by the block hash");
        }
        return null;
    }
    List<AionTransaction> txList = pBlk.getTransactionsList();
    AionTransaction tx = txList.get((int) index);
    if (tx == null) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Can't find the transaction!");
        }
        return null;
    }
    TxRecpt receipt = this.getTransactionReceipt(tx.getHash());
    // TODO
    if (receipt == null) {
        throw new NullPointerException();
    }
    tx.setBlockNumber(pBlk.getNumber());
    tx.setBlockHash(pBlk.getHash());
    tx.setTxIndexInBlock(index);
    tx.setNrgConsume(receipt.nrgUsed);
    return tx;
}
Also used : TxRecpt(org.aion.api.server.types.TxRecpt) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 72 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class PendingTxCache method addCacheTx.

List<AionTransaction> addCacheTx(AionTransaction tx) {
    if (tx == null) {
        throw new NullPointerException();
    }
    int txSize = tx.getEncoded().length;
    if (isCacheMax(txSize)) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("PendingTx reached the max Memory settings");
        }
        if (cacheTxMap.get(tx.getFrom()) == null) {
            // no tx belong to the account, return directly
            return Collections.singletonList(tx);
        } else {
            // calculate replaced nonce tx size
            BigInteger nonce = tx.getNonceBI();
            List<BigInteger> removeTx;
            boolean findPosition = false;
            removeTx = new ArrayList<>();
            int tempCacheSize = currentSize.get();
            if (cacheTxMap.get(tx.getFrom()).get(nonce) != null) {
                // case 1: found tx has same nonce in the cachemap
                removeTx.add(nonce);
                int oldTxSize = cacheTxMap.get(tx.getFrom()).get(nonce).getEncoded().length;
                tempCacheSize -= oldTxSize;
                if (!isCacheMax(txSize - oldTxSize)) {
                    // case 1a: replace nonce within the cachelimit, replace it
                    findPosition = true;
                } else {
                    // case 1b: replace nonce still over the cachelimit, replace it and find the best remove list
                    for (Map.Entry<BigInteger, AionTransaction> e : cacheTxMap.get(tx.getFrom()).descendingMap().entrySet()) {
                        if (e.getKey().compareTo(nonce) > 0) {
                            removeTx.add(e.getKey());
                            tempCacheSize -= e.getValue().getEncoded().length;
                            if (tempCacheSize + txSize < CacheMax) {
                                findPosition = true;
                                break;
                            }
                        }
                    }
                }
            } else {
                // case 2: backward iterate the cache to remove bigger nonce tx until find the enough cache size
                for (Map.Entry<BigInteger, AionTransaction> e : cacheTxMap.get(tx.getFrom()).descendingMap().entrySet()) {
                    if (e.getKey().compareTo(nonce) > 0) {
                        removeTx.add(e.getKey());
                        tempCacheSize -= e.getValue().getEncoded().length;
                        if (tempCacheSize + txSize < CacheMax) {
                            findPosition = true;
                            break;
                        }
                    }
                }
            }
            if (findPosition) {
                for (BigInteger bi : removeTx) {
                    cacheTxMap.get(tx.getFrom()).remove(bi);
                }
                cacheTxMap.get(tx.getFrom()).put(nonce, tx);
                currentSize.set(tempCacheSize + txSize);
            }
        }
    } else {
        if (cacheTxMap.size() == cacheAccountLimit) {
            // remove firstAccount in pendingTxCache
            Iterator<Map.Entry<Address, TreeMap<BigInteger, AionTransaction>>> it = cacheTxMap.entrySet().iterator();
            if (it.hasNext()) {
                currentSize.addAndGet(-getAccountSize(it.next().getValue()));
                it.remove();
            }
        }
        cacheTxMap.computeIfAbsent(tx.getFrom(), k -> new TreeMap<>());
        cacheTxMap.get(tx.getFrom()).put(tx.getNonceBI(), tx);
        currentSize.addAndGet(txSize);
    }
    return new ArrayList<>(cacheTxMap.get(tx.getFrom()).values());
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) BigInteger(java.math.BigInteger) LRUMap(org.apache.commons.collections4.map.LRUMap)

Example 73 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class PendingTxCache method flush.

public List<AionTransaction> flush(Map<Address, BigInteger> nonceMap) {
    if (nonceMap == null) {
        throw new NullPointerException();
    }
    List<AionTransaction> processableTx = new ArrayList<>();
    for (Address addr : nonceMap.keySet()) {
        BigInteger bn = nonceMap.get(addr);
        if (LOG.isDebugEnabled()) {
            LOG.debug("cacheTx.flush addr[{}] bn[{}] size[{}], cache_size[{}]", addr.toString(), bn.toString(), cacheTxMap.get(addr).size(), currentSize.get());
        }
        if (cacheTxMap.get(addr) != null) {
            currentSize.addAndGet(-getAccountSize(cacheTxMap.get(addr)));
            cacheTxMap.get(addr).headMap(bn).clear();
            currentSize.addAndGet(getAccountSize(cacheTxMap.get(addr)));
            if (LOG.isDebugEnabled()) {
                LOG.debug("cacheTx.flush after addr[{}] size[{}], cache_size[{}]", addr.toString(), cacheTxMap.get(addr).size(), currentSize.get());
            }
            if (cacheTxMap.get(addr).get(bn) != null) {
                processableTx.add(cacheTxMap.get(addr).get(bn));
            }
        }
    }
    return processableTx;
}
Also used : Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) AionTransaction(org.aion.zero.types.AionTransaction)

Example 74 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class AionPoW method createNewBlockTemplate.

/**
 * Creates a new block template.
 */
protected synchronized void createNewBlockTemplate() {
    if (!shutDown.get()) {
        if (!config.getConsensus().getMining()) {
            return;
        }
        // it be used in DDOS?
        if (this.syncMgr.getNetworkBestBlockNumber() - blockchain.getBestBlock().getNumber() > syncLimit) {
            return;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Creating a new block template");
        }
        AionBlock bestBlock = blockchain.getBlockByNumber(blockchain.getBestBlock().getNumber());
        List<AionTransaction> txs = pendingState.getPendingTransactions();
        AionBlock newBlock = blockchain.createNewBlock(bestBlock, txs, false);
        EventConsensus ev = new EventConsensus(EventConsensus.CALLBACK.ON_BLOCK_TEMPLATE);
        ev.setFuncArgs(Collections.singletonList(newBlock));
        eventMgr.newEvent(ev);
        // update last timestamp
        lastUpdate.set(System.currentTimeMillis());
    }
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 75 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class AionTransactionTest method testClone.

@Test
public void testClone() {
    byte[] nonce = RandomUtils.nextBytes(16);
    Address to = Address.wrap(RandomUtils.nextBytes(32));
    byte[] value = RandomUtils.nextBytes(16);
    byte[] data = RandomUtils.nextBytes(64);
    long nrg = RandomUtils.nextLong(0, Long.MAX_VALUE);
    long nrgPrice = RandomUtils.nextLong(0, Long.MAX_VALUE);
    byte type = 1;
    AionTransaction tx = new AionTransaction(nonce, to, value, data, nrg, nrgPrice, type);
    tx.sign(ECKeyFac.inst().create());
    AionTransaction tx2 = tx.clone();
    assertTransactionEquals(tx, tx2);
}
Also used : Address(org.aion.base.type.Address) AionTransaction(org.aion.zero.types.AionTransaction) Test(org.junit.Test)

Aggregations

AionTransaction (org.aion.zero.types.AionTransaction)75 Test (org.junit.Test)44 BigInteger (java.math.BigInteger)30 ITransaction (org.aion.base.type.ITransaction)26 Address (org.aion.base.type.Address)23 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)21 AionBlock (org.aion.zero.impl.types.AionBlock)17 ArrayList (java.util.ArrayList)16 ECKey (org.aion.crypto.ECKey)12 AionTxReceipt (org.aion.zero.types.AionTxReceipt)7 TxRecpt (org.aion.api.server.types.TxRecpt)4 ImportResult (org.aion.mcf.core.ImportResult)4 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)4 ByteString (com.google.protobuf.ByteString)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ByteBuffer (java.nio.ByteBuffer)2 java.util (java.util)2 Entry (java.util.Map.Entry)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Collectors (java.util.stream.Collectors)2