use of org.aion.api.server.types.TxRecpt in project aion by aionnetwork.
the class ApiWeb3Aion method ops_getTransactionReceiptListByBlockHash.
public RpcMsg ops_getTransactionReceiptListByBlockHash(Object _params) {
String _blockHash;
if (_params instanceof JSONArray) {
_blockHash = ((JSONArray) _params).get(0) + "";
} else if (_params instanceof JSONObject) {
_blockHash = ((JSONObject) _params).get("blockHash") + "";
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
byte[] blockHash = StringUtils.StringHexToByteArray(_blockHash);
if (blockHash.length != 32) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
// ok to getUnchecked() since the load() implementation does not throw checked exceptions
Block b;
try {
b = blockCache.get(ByteArrayWrapper.wrap(blockHash));
} catch (Exception e) {
// Catch errors if send an incorrect tx hash
return new RpcMsg(null, RpcError.INVALID_REQUEST, "Invalid Request " + e + " " + e.getMessage() != null ? e.getMessage() : "");
}
// cast will cause issues after the PoW refactor goes in
AionBlockchainImpl chain = (AionBlockchainImpl) this.ac.getAionHub().getBlockchain();
Function<AionTransaction, JSONObject> extractTxReceipt = t -> {
AionTxInfo info = chain.getTransactionInfoLite(t.getTransactionHash(), b.getHash());
info.setTransaction(t);
return ((new TxRecpt(b, info, 0L, true)).toJson());
};
List<JSONObject> receipts;
// use the fork-join pool to parallelize receipt retrieval if necessary
int PARALLELIZE_RECEIPT_COUNT = 20;
if (b.getTransactionsList().size() > PARALLELIZE_RECEIPT_COUNT) {
receipts = b.getTransactionsList().parallelStream().map(extractTxReceipt).collect(toList());
} else {
receipts = b.getTransactionsList().stream().map(extractTxReceipt).collect(toList());
}
return new RpcMsg(new JSONArray(receipts));
}
use of org.aion.api.server.types.TxRecpt in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getTransactionReceipt.
public RpcMsg eth_getTransactionReceipt(Object _params) {
String _hash;
if (_params instanceof JSONArray) {
_hash = ((JSONArray) _params).get(0) + "";
} else if (_params instanceof JSONObject) {
_hash = ((JSONObject) _params).get("hash") + "";
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
byte[] txHash = StringUtils.StringHexToByteArray(_hash);
TxRecpt r = getTransactionReceipt(txHash);
if (r == null) {
return new RpcMsg(// json rpc spec: 'or null when no receipt was found'
JSONObject.NULL);
}
return new RpcMsg(r.toJson());
}
use of org.aion.api.server.types.TxRecpt in project aion by aionnetwork.
the class ApiWeb3Aion method ops_getTransactionReceiptByTransactionHash.
/**
* This function runs in ~ 30ms Is an order of magnitude slower than
* ops_getTransactionReceiptByTransactionAndBlockHash
*/
public RpcMsg ops_getTransactionReceiptByTransactionHash(Object _params) {
String _transactionHash;
if (_params instanceof JSONArray) {
_transactionHash = ((JSONArray) _params).get(0) + "";
} else if (_params instanceof JSONObject) {
_transactionHash = ((JSONObject) _params).get("transactionHash") + "";
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
byte[] transactionHash = StringUtils.StringHexToByteArray(_transactionHash);
AionTxInfo info = this.ac.getAionHub().getBlockchain().getTransactionInfo(transactionHash);
if (info == null) {
return new RpcMsg(JSONObject.NULL);
}
Block block = blockCache.get(ByteArrayWrapper.wrap(info.getBlockHash()));
return new RpcMsg((new TxRecpt(block, info, 0L, true)).toJson());
}
use of org.aion.api.server.types.TxRecpt in project aion by aionnetwork.
the class ApiAion method getTransactionByBlockHashAndIndex.
protected TransactionWithBlockInfo getTransactionByBlockHashAndIndex(byte[] hash, long index) {
Block pBlk = this.getBlockByHash(hash);
if (pBlk == null) {
if (LOG.isErrorEnabled()) {
LOG.error("ApiAion.getTransactionByBlockHashAndIndex - can't find the block by the block hash");
}
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!");
}
return null;
}
TxRecpt receipt = this.getTransactionReceipt(tx.getTransactionHash());
// TODO
if (receipt == null) {
throw new NullPointerException();
}
return new TransactionWithBlockInfo(tx, pBlk.getHash(), pBlk.getNumber(), index, receipt.nrgUsed);
}
Aggregations