Skip to content

Commit 560200f

Browse files
authored
fix(jsonrpc): support blockHash param and genesis block process for eth_getBlockReceipts (#6433)
* fix: return null for genesis block in eth_getBlockReceipts * feat:support query eth_getBlockReceipts by blockHash * checkStyle
1 parent b1bf6aa commit 560200f

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,25 +839,35 @@ 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-
if (block == null) {
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+
861+
// block receipts not available: block is genesis, not produced yet, or pruned in light node
862+
if (block == null || block.getBlockHeader().getRawData().getNumber() == 0) {
853863
return null;
854864
}
855865

856866
BlockCapsule blockCapsule = new BlockCapsule(block);
857867
long blockNum = blockCapsule.getNum();
858868
TransactionInfoList transactionInfoList = wallet.getTransactionInfoByBlockNum(blockNum);
859869

860-
//energy price at the block timestamp
870+
// energy price at the block timestamp
861871
long energyFee = wallet.getEnergyFee(blockCapsule.getTimeStamp());
862872

863873
// Validate transaction list size consistency

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,6 @@ public void init() {
164164
dbManager.getTransactionStore()
165165
.put(transactionCapsule3.getTransactionId().getBytes(), transactionCapsule3);
166166

167-
blockCapsule0.getTransactions().forEach(tx -> {
168-
TransactionCapsule transactionCapsule = new TransactionCapsule(tx.getInstance());
169-
transactionCapsule.setBlockNum(blockCapsule0.getNum());
170-
dbManager.getTransactionStore()
171-
.put(transactionCapsule.getTransactionId().getBytes(), transactionCapsule);
172-
});
173-
174-
TransactionRetCapsule transactionRetCapsule0 = new TransactionRetCapsule();
175-
blockCapsule0.getTransactions().forEach(tx -> {
176-
TransactionInfoCapsule transactionInfoCapsule = new TransactionInfoCapsule();
177-
transactionInfoCapsule.setId(tx.getTransactionId().getBytes());
178-
transactionInfoCapsule.setBlockNumber(blockCapsule0.getNum());
179-
transactionRetCapsule0.addTransactionInfo(transactionInfoCapsule.getInstance());
180-
});
181-
dbManager.getTransactionRetStore().put(
182-
ByteArray.fromLong(blockCapsule0.getNum()), transactionRetCapsule0);
183-
184167
List<Protocol.TransactionInfo.Log> logs = new ArrayList<>();
185168
logs.add(Protocol.TransactionInfo.Log.newBuilder()
186169
.setAddress(ByteString.copyFrom("address1".getBytes()))
@@ -340,6 +323,8 @@ public void testGetBlockByNumber() {
340323
}
341324
Assert.assertEquals(ByteArray.toJsonHex(0L), blockResult.getNumber());
342325
Assert.assertEquals(ByteArray.toJsonHex(blockCapsule0.getNum()), blockResult.getNumber());
326+
Assert.assertEquals(blockResult.getTransactions().length,
327+
blockCapsule0.getTransactions().size());
343328

344329
// latest
345330
try {
@@ -1048,7 +1033,7 @@ public void testGetBlockReceipts() {
10481033

10491034
try {
10501035
List<TransactionReceipt> transactionReceiptList = tronJsonRpc.getBlockReceipts("earliest");
1051-
Assert.assertFalse(transactionReceiptList.isEmpty());
1036+
Assert.assertNull(transactionReceiptList);
10521037
} catch (JsonRpcInvalidParamsException | JsonRpcInternalException e) {
10531038
throw new RuntimeException(e);
10541039
}
@@ -1088,6 +1073,20 @@ public void testGetBlockReceipts() {
10881073
throw new RuntimeException(e);
10891074
}
10901075

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+
10911090
}
10921091

10931092
@Test

0 commit comments

Comments
 (0)