Skip to content

Commit bdb59dc

Browse files
committed
refactor(jsonrpc): use getHeadBlockNum for parseBlockTag latest resolution, also use longValueExact in eth_getProof block number parsing and clarify the getBlockByNumOrTag comment
1 parent 0b375d5 commit bdb59dc

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,17 +526,17 @@ public static boolean isBlockTag(String tag) {
526526
|| SAFE_STR.equalsIgnoreCase(tag);
527527
}
528528

529+
/**
530+
* Parse a block tag (latest, earliest, finalized) to block number.
531+
*
532+
* <p>Note: for "latest", the returned block number may not yet be available in
533+
* blockStore or blockIndexStore due to write ordering. Callers that need the
534+
* actual block must handle the not-found case.</p>
535+
*/
529536
public static long parseBlockTag(String tag, Wallet wallet)
530537
throws JsonRpcInvalidParamsException {
531538
if (LATEST_STR.equalsIgnoreCase(tag)) {
532-
// Use getNowBlock() to fetch the latest persisted block directly from blockStore,
533-
// avoiding a race with updateDynamicProperties that updates latestBlockHeaderNumber
534-
// before the block is written to blockStore/blockIndexStore.
535-
Block block = wallet.getNowBlock();
536-
if (block == null) {
537-
return 0;
538-
}
539-
return block.getBlockHeader().getRawData().getNumber();
539+
return wallet.getHeadBlockNum();
540540
}
541541
if (EARLIEST_STR.equalsIgnoreCase(tag)) {
542542
return 0;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ private Block getBlockByNumOrTag(String blockNumOrTag) throws JsonRpcInvalidPara
371371
long blockNum;
372372
if (JsonRpcApiUtil.isBlockTag(blockNumOrTag)) {
373373
if (LATEST_STR.equalsIgnoreCase(blockNumOrTag)) {
374-
// Read the head block directly from blockStore to avoid the window where
375-
// getNowBlock() already sees the new head but getBlockByNum() still misses it.
374+
// Return the head block directly from blockStore, bypassing blockIndexStore
375+
// which may not yet be written when latestBlockHeaderNumber is already updated.
376376
return wallet.getNowBlock();
377377
}
378378
return wallet.getBlockByNum(JsonRpcApiUtil.parseBlockTag(blockNumOrTag, wallet));
@@ -971,7 +971,7 @@ public String getCall(CallArguments transactionCall, Object blockParamObj)
971971

972972
long blockNumber;
973973
try {
974-
blockNumber = ByteArray.hexToBigInteger(blockNumOrTag).longValue();
974+
blockNumber = ByteArray.hexToBigInteger(blockNumOrTag).longValueExact();
975975
} catch (Exception e) {
976976
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
977977
}

0 commit comments

Comments
 (0)