Search in sources :

Example 16 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class ApiAion method getTransactionByBlockNumberAndIndex.

protected TransactionWithBlockInfo getTransactionByBlockNumberAndIndex(long blkNr, long index) {
    Block 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.getTransactionHash());
    // The receipt shouldn't be null!
    if (receipt == null) {
        throw new NullPointerException();
    }
    return new TransactionWithBlockInfo(tx, pBlk.getHash(), pBlk.getNumber(), index, receipt.nrgUsed);
}
Also used : TxRecpt(org.aion.api.server.types.TxRecpt) 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 17 with Block

use of org.aion.zero.impl.types.Block 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 18 with Block

use of org.aion.zero.impl.types.Block 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 19 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class ContractIntegTest method tellFvmContractCallWithinDeployingBlock.

@Test
public void tellFvmContractCallWithinDeployingBlock() throws IOException {
    if (txType == TransactionTypes.AVM_CREATE_CODE) {
        return;
    }
    String contractName = "InternalCallContract";
    byte[] deployCode = getDeployCode(contractName);
    long nrg = Constants.NRG_TRANSACTION_MAX;
    long nrgPrice = energyPrice;
    BigInteger value = BigInteger.ZERO;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
    RepositoryCache repo = blockchain.getRepository().startTracking();
    nonce = nonce.add(BigInteger.ONE);
    assertTrue(tx.isContractCreationTransaction());
    assertEquals(deployerBalance, repo.getBalance(deployer));
    assertEquals(deployerNonce, repo.getNonce(deployer));
    List<AionTransaction> ls = new ArrayList<>();
    ls.add(tx);
    byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
    AionTransaction tx2 = AionTransaction.create(deployerKey, nonce.toByteArray(), TxUtil.calculateContractAddress(tx), BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
    assertFalse(tx2.isContractCreationTransaction());
    ls.add(tx2);
    BigInteger senderBalance = repo.getBalance(deployer);
    Block parent = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parent, ls, false, parent.getTimestamp());
    Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
    AionBlockSummary summary = result.getRight();
    assertTrue(result.getLeft().isSuccessful());
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contractAddress));
    assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getReceipts().get(0).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))).subtract(BigInteger.valueOf(summary.getReceipts().get(1).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
    repo = blockchain.getRepository().startTracking();
    assertEquals(BigInteger.TWO, repo.getNonce(deployer));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) AionRepositoryCache(org.aion.zero.impl.db.AionRepositoryCache) RepositoryCache(org.aion.base.db.RepositoryCache) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 20 with Block

use of org.aion.zero.impl.types.Block in project aion by aionnetwork.

the class AlternatingVmBlockTest method getAvmContractDeploymentCost.

private long getAvmContractDeploymentCost(AvmVersion version, BigInteger nonce) {
    AionTransaction avmDeploy = makeAvmContractCreateTransaction(version, deployerKey, nonce);
    Block parentBlock = blockchain.getBestBlock();
    MiningBlock block = blockchain.createBlock(parentBlock, Collections.singletonList(avmDeploy), false, parentBlock.getTimestamp());
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
    return connectResult.getRight().getReceipts().get(0).getEnergyUsed();
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock)

Aggregations

Block (org.aion.zero.impl.types.Block)283 MiningBlock (org.aion.zero.impl.types.MiningBlock)155 Test (org.junit.Test)148 AionTransaction (org.aion.base.AionTransaction)106 ImportResult (org.aion.zero.impl.core.ImportResult)86 ArrayList (java.util.ArrayList)63 AionAddress (org.aion.types.AionAddress)61 StakingBlock (org.aion.zero.impl.types.StakingBlock)58 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)57 BigInteger (java.math.BigInteger)55 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)34 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)30 AionTxReceipt (org.aion.base.AionTxReceipt)29 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)28 AccountState (org.aion.base.AccountState)26 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)26 JSONArray (org.json.JSONArray)26 JSONObject (org.json.JSONObject)26 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 AionTxExecSummary (org.aion.base.AionTxExecSummary)20