Search in sources :

Example 36 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class ApiAion method getTransactionCount.

protected long getTransactionCount(AionAddress addr, long blkNr) {
    Block pBlk = this.getBlock(blkNr);
    if (pBlk == null) {
        LOG.error("ApiAion.getTransactionByBlockNumberAndIndex - can't find the block by the block number");
        return -1;
    }
    long cnt = 0;
    List<AionTransaction> txList = pBlk.getTransactionsList();
    for (AionTransaction tx : txList) {
        if (addr.equals(tx.getSenderAddress())) {
            cnt++;
        }
    }
    return cnt;
}
Also used : EventBlock(org.aion.evtmgr.impl.evt.EventBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction)

Example 37 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class ApiAion method createContract.

protected ApiTxResponse createContract(ArgTxCall _params) {
    if (_params == null) {
        return (new ApiTxResponse(TxResponse.INVALID_TX));
    }
    AionAddress from = _params.getFrom();
    if (from == null) {
        LOG.error("<create-contract msg=invalid-from-address>");
        return (new ApiTxResponse(TxResponse.INVALID_FROM));
    }
    ECKey key = this.getAccountKey(from.toString());
    if (key == null) {
        LOG.debug("ApiAion.createContract - null key");
        return (new ApiTxResponse(TxResponse.INVALID_ACCOUNT));
    }
    try {
        synchronized (pendingState) {
            byte[] nonce = !(_params.getNonce().equals(BigInteger.ZERO)) ? _params.getNonce().toByteArray() : pendingState.bestPendingStateNonce(new AionAddress(key.getAddress())).toByteArray();
            AionTransaction tx = AionTransaction.create(key, nonce, null, _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice(), _params.getType(), null);
            TxResponse rsp = pendingState.addTransactionFromApiServer(tx);
            AionAddress address = TxUtil.calculateContractAddress(tx);
            return new ApiTxResponse(rsp, tx.getTransactionHash(), address);
        }
    } catch (Exception ex) {
        LOG.error("ApiAion.createContract - exception: [{}]", ex.getMessage());
        return new ApiTxResponse(TxResponse.EXCEPTION, ex);
    }
}
Also used : AionAddress(org.aion.types.AionAddress) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) TxResponse(org.aion.zero.impl.types.TxResponse)

Example 38 with AionTransaction

use of org.aion.base.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,
     */
protected 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;
    }
    Block 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
    Block 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.getTransactionHash();
        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) EventBlock(org.aion.evtmgr.impl.evt.EventBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction)

Example 39 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class ApiAion method estimateNrg.

protected long estimateNrg(ArgTxCall params) {
    AionAddress fromAddr = (params.getFrom() == null) ? AddressUtils.ZERO_ADDRESS : params.getFrom();
    AionTransaction tx = AionTransaction.createWithoutKey(params.getNonce().toByteArray(), fromAddr, params.getTo(), params.getValue().toByteArray(), params.getData(), params.getNrg(), params.getNrgPrice(), params.getType(), null);
    AionTxReceipt receipt = this.ac.callConstant(tx, this.ac.getAionHub().getBlockchain().getBestBlock());
    return receipt.getEnergyUsed();
}
Also used : AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt)

Example 40 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class Benchmark method createDummyBlock.

private static MiningBlock createDummyBlock() {
    byte[] parentHash = new byte[32];
    byte[] coinbase = RandomUtils.nextBytes(AionAddress.LENGTH);
    byte[] logsBloom = new byte[0];
    byte[] difficulty = new DataWord(0x1000000L).getData();
    long number = 1;
    long timestamp = System.currentTimeMillis() / 1000;
    byte[] extraData = new byte[0];
    byte[] nonce = new byte[32];
    byte[] receiptsRoot = new byte[32];
    byte[] transactionsRoot = new byte[32];
    byte[] stateRoot = new byte[32];
    List<AionTransaction> transactionsList = Collections.emptyList();
    byte[] solutions = new byte[0];
    // TODO: set a dummy limit of 5000000 for now
    return new MiningBlock(parentHash, new AionAddress(coinbase), logsBloom, difficulty, number, timestamp, extraData, nonce, receiptsRoot, transactionsRoot, stateRoot, transactionsList, solutions, 0, 5000000);
}
Also used : AionAddress(org.aion.types.AionAddress) DataWord(org.aion.util.types.DataWord) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Aggregations

AionTransaction (org.aion.base.AionTransaction)437 Test (org.junit.Test)308 AionAddress (org.aion.types.AionAddress)273 BigInteger (java.math.BigInteger)174 MiningBlock (org.aion.zero.impl.types.MiningBlock)149 ArrayList (java.util.ArrayList)127 ImportResult (org.aion.zero.impl.core.ImportResult)115 AionTxExecSummary (org.aion.base.AionTxExecSummary)103 Block (org.aion.zero.impl.types.Block)102 RepositoryCache (org.aion.base.db.RepositoryCache)89 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)87 AionTxReceipt (org.aion.base.AionTxReceipt)75 ECKey (org.aion.crypto.ECKey)52 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)46 BlockContext (org.aion.zero.impl.types.BlockContext)43 PooledTransaction (org.aion.base.PooledTransaction)40 AccountState (org.aion.base.AccountState)39 Properties (java.util.Properties)35 HashMap (java.util.HashMap)33 DataWord (org.aion.util.types.DataWord)29