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);
}
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;
}
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);
}
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));
}
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();
}
Aggregations