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