Skip to content

Commit 8570eea

Browse files
committed
feat:support query eth_getBlockReceipts by blockHash
1 parent 1980f0b commit 8570eea

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, Stri
151151
@JsonRpcError(exception = JsonRpcInvalidParamsException.class, code = -32602, data = "{}"),
152152
@JsonRpcError(exception = JsonRpcInternalException.class, code = -32000, data = "{}")
153153
})
154-
List<TransactionReceipt> getBlockReceipts(String blockNumOrTag)
154+
List<TransactionReceipt> getBlockReceipts(String blockNumOrHashOrTag)
155155
throws JsonRpcInvalidParamsException, JsonRpcInternalException;
156156

157157
@JsonRpcMethod("eth_call")

framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -839,16 +839,25 @@ private TransactionContext findTransactionContext(TransactionInfoList infoList,
839839

840840
/**
841841
* Get all transaction receipts for a specific block
842-
* @param blockNumOrTag the block number or tag (latest, earliest, pending, finalized)
842+
* @param blockNumOrHashOrTag blockNumber or blockHash or tag,
843+
* tag includes: latest, earliest, pending, finalized
843844
* @return List of TransactionReceipt objects for all transactions in the block,
844845
* null if block not found
845846
* @throws JsonRpcInvalidParamsException if the parameter format is invalid
846847
* @throws JsonRpcInternalException if there's an internal error
847848
*/
848849
@Override
849-
public List<TransactionReceipt> getBlockReceipts(String blockNumOrTag)
850+
public List<TransactionReceipt> getBlockReceipts(String blockNumOrHashOrTag)
850851
throws JsonRpcInvalidParamsException, JsonRpcInternalException {
851-
Block block = wallet.getByJsonBlockId(blockNumOrTag);
852+
853+
Block block = null;
854+
855+
if (Pattern.matches(HASH_REGEX, blockNumOrHashOrTag)) {
856+
block = getBlockByJsonHash(blockNumOrHashOrTag);
857+
} else {
858+
block = wallet.getByJsonBlockId(blockNumOrHashOrTag);
859+
}
860+
852861

853862
// block receipts not available: block is genesis, not produced yet, or pruned in light node
854863
if (block == null || block.getBlockHeader().getRawData().getNumber() == 0) {

framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,20 @@ public void testGetBlockReceipts() {
10731073
throw new RuntimeException(e);
10741074
}
10751075

1076+
try {
1077+
String blockHash = blockCapsule1.getBlockId().toString();
1078+
List<TransactionReceipt> transactionReceiptList
1079+
= tronJsonRpc.getBlockReceipts(blockHash);
1080+
List<TransactionReceipt> transactionReceiptList2
1081+
= tronJsonRpc.getBlockReceipts("0x" + blockHash);
1082+
1083+
Assert.assertFalse(transactionReceiptList.isEmpty());
1084+
Assert.assertEquals(JSON.toJSONString(transactionReceiptList),
1085+
JSON.toJSONString(transactionReceiptList2));
1086+
} catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) {
1087+
throw new RuntimeException(e);
1088+
}
1089+
10761090
}
10771091

10781092
@Test

0 commit comments

Comments
 (0)