Search in sources :

Example 26 with AionTransaction

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

the class ApiAion method getTransactionByBlockNumberAndIndex.

public AionTransaction getTransactionByBlockNumberAndIndex(long blkNr, long index) {
    AionBlock pBlk = this.getBlock(blkNr);
    if (pBlk == null) {
        if (LOG.isErrorEnabled()) {
            LOG.error("ApiAion.getTransactionByBlockNumberAndIndex - can't find the block by the block number");
        }
        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 by the txIndex");
        }
        return null;
    }
    TxRecpt receipt = this.getTransactionReceipt(tx.getHash());
    // The receipt shouldn't be null!
    if (receipt == null) {
        throw new NullPointerException();
    }
    tx.rlpParse();
    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 27 with AionTransaction

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

the class ApiAion method estimateGas.

public long estimateGas(ArgTxCall params) {
    AionTransaction tx = new AionTransaction(params.getNonce().toByteArray(), params.getTo(), params.getValue().toByteArray(), params.getData(), params.getNrg(), params.getNrgPrice());
    AionTxReceipt receipt = this.ac.callConstant(tx, this.ac.getAionHub().getBlockchain().getBestBlock());
    return receipt.getEnergyUsed();
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionTxReceipt(org.aion.zero.types.AionTxReceipt)

Example 28 with AionTransaction

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

the class AionBlockLoader method loadBlocks.

public void loadBlocks() {
    exec1 = new ExecutorPipeline<AionBlock, AionBlock>(8, 1000, true, new Functional.Function<AionBlock, AionBlock>() {

        @Override
        public AionBlock apply(AionBlock b) {
            for (AionTransaction tx : b.getTransactionsList()) {
                tx.getFrom();
            }
            return b;
        }
    }, new Functional.Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) {
            logger.error("Unhandled exception: ", throwable);
        }
    });
    exec2 = exec1.add(1, 1000, new Functional.Consumer<AionBlock>() {

        @Override
        public void accept(AionBlock block) {
            try {
                blockWork(block);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    try {
        exec1.join();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    blockchain.flush();
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 29 with AionTransaction

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

the class PendingTxCache method findSeqTx.

private List<AionTransaction> findSeqTx(BigInteger bn, Address addr) {
    List<AionTransaction> rtn = new ArrayList<>();
    rtn.add(cacheTxMap.get(addr).get(bn));
    boolean foundNext = true;
    while (foundNext) {
        bn = bn.add(BigInteger.ONE);
        AionTransaction nextTx = cacheTxMap.get(addr).get(bn);
        if (nextTx == null) {
            foundNext = false;
        } else {
            rtn.add(cacheTxMap.get(addr).get(bn));
        }
    }
    return rtn;
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction)

Example 30 with AionTransaction

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

the class ApiAion method getTransactionReceipt.

/* NOTE: only use this if you need receipts for one or small number transactions in a block.
     * (since there is n^2 work happening here to compute cumulative nrg)
     * For use cases where you need all the transaction receipts in a block, please use a different
     * strategy,
     */
public TxRecpt getTransactionReceipt(byte[] txHash) {
    if (txHash == null) {
        if (LOG.isErrorEnabled()) {
            LOG.error("<get-transaction-receipt msg=tx-hash-null>");
        }
        return null;
    }
    AionTxInfo txInfo = this.ac.getAionHub().getBlockchain().getTransactionInfo(txHash);
    if (txInfo == null) {
        if (LOG.isErrorEnabled()) {
            LOG.error("<get-transaction-receipt msg=tx-info-null>");
        }
        return null;
    }
    AionBlock block = this.ac.getAionHub().getBlockchain().getBlockByHash(txInfo.getBlockHash());
    if (block == null) {
        if (LOG.isErrorEnabled()) {
            LOG.error("<get-transaction-receipt msg=block-null>");
        }
        return null;
    }
    // need to return txes only from main chain
    AionBlock mainBlock = this.ac.getAionHub().getBlockchain().getBlockByNumber(block.getNumber());
    if (!Arrays.equals(block.getHash(), mainBlock.getHash())) {
        LOG.debug("<get-transaction-receipt msg=hash-not-match>");
        return null;
    }
    // @Jay
    // TODO : think the good way to calculate the cumulated nrg use
    long cumulateNrg = 0L;
    for (AionTransaction atx : block.getTransactionsList()) {
        // @Jay: This should not happen!
        byte[] hash = atx.getHash();
        if (hash == null) {
            throw new NullPointerException();
        }
        AionTxInfo info = this.ac.getAionHub().getBlockchain().getTransactionInfo(hash);
        // @Jay: This should not happen!
        if (info == null) {
            throw new NullPointerException();
        }
        cumulateNrg += info.getReceipt().getEnergyUsed();
        if (Arrays.equals(txHash, hash)) {
            break;
        }
    }
    return new TxRecpt(block, txInfo, cumulateNrg, true);
}
Also used : TxRecpt(org.aion.api.server.types.TxRecpt) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

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