Skip to content

Commit 0b375d5

Browse files
committed
fix(jsonrpc): read latest block selectors from getNowBlock
1 parent 8f6f59a commit 0b375d5

3 files changed

Lines changed: 42 additions & 61 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,11 @@ public static long parseBlockTag(String tag, Wallet wallet)
532532
// Use getNowBlock() to fetch the latest persisted block directly from blockStore,
533533
// avoiding a race with updateDynamicProperties that updates latestBlockHeaderNumber
534534
// before the block is written to blockStore/blockIndexStore.
535-
return wallet.getNowBlock().getBlockHeader().getRawData().getNumber();
535+
Block block = wallet.getNowBlock();
536+
if (block == null) {
537+
return 0;
538+
}
539+
return block.getBlockHeader().getRawData().getNumber();
536540
}
537541
if (EARLIEST_STR.equalsIgnoreCase(tag)) {
538542
return 0;

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

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public enum RequestSource {
159159

160160
private static final String JSON_ERROR = "invalid json request";
161161
private static final String TAG_NOT_SUPPORT_ERROR =
162-
"TAG [earliest | pending | finalized] not supported";
162+
"TAG [earliest | pending | finalized | safe] not supported";
163163
private static final String QUANTITY_NOT_SUPPORT_ERROR =
164164
"QUANTITY not supported, just support TAG as latest";
165165
private static final String NO_BLOCK_HEADER = "header not found";
@@ -305,20 +305,7 @@ public String ethGetBlockTransactionCountByHash(String blockHash)
305305
@Override
306306
public String ethGetBlockTransactionCountByNumber(String blockNumOrTag)
307307
throws JsonRpcInvalidParamsException {
308-
long blockNum;
309-
if (JsonRpcApiUtil.isBlockTag(blockNumOrTag)) {
310-
blockNum = JsonRpcApiUtil.parseBlockTag(blockNumOrTag, wallet);
311-
} else {
312-
try {
313-
// hexToBigInteger: 0x -> hex, bare number -> decimal.
314-
// Preserving original behavior. LogFilterWrapper uses
315-
// strict jsonHexToLong (0x required) via parseBlockNumber.
316-
blockNum = ByteArray.hexToBigInteger(blockNumOrTag).longValue();
317-
} catch (Exception e) {
318-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
319-
}
320-
}
321-
Block block = wallet.getBlockByNum(blockNum);
308+
Block block = getBlockByNumOrTag(blockNumOrTag);
322309
if (block == null) {
323310
return null;
324311
}
@@ -337,17 +324,7 @@ public BlockResult ethGetBlockByHash(String blockHash, Boolean fullTransactionOb
337324
@Override
338325
public BlockResult ethGetBlockByNumber(String blockNumOrTag, Boolean fullTransactionObjects)
339326
throws JsonRpcInvalidParamsException {
340-
long blockNum;
341-
if (JsonRpcApiUtil.isBlockTag(blockNumOrTag)) {
342-
blockNum = JsonRpcApiUtil.parseBlockTag(blockNumOrTag, wallet);
343-
} else {
344-
try {
345-
blockNum = ByteArray.hexToBigInteger(blockNumOrTag).longValue();
346-
} catch (Exception e) {
347-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
348-
}
349-
}
350-
final Block b = wallet.getBlockByNum(blockNum);
327+
final Block b = getBlockByNumOrTag(blockNumOrTag);
351328
return (b == null ? null : getBlockResult(b, fullTransactionObjects));
352329
}
353330

@@ -390,6 +367,24 @@ private Block getBlockByJsonHash(String blockHash) throws JsonRpcInvalidParamsEx
390367
return wallet.getBlockById(ByteString.copyFrom(bHash));
391368
}
392369

370+
private Block getBlockByNumOrTag(String blockNumOrTag) throws JsonRpcInvalidParamsException {
371+
long blockNum;
372+
if (JsonRpcApiUtil.isBlockTag(blockNumOrTag)) {
373+
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.
376+
return wallet.getNowBlock();
377+
}
378+
return wallet.getBlockByNum(JsonRpcApiUtil.parseBlockTag(blockNumOrTag, wallet));
379+
}
380+
try {
381+
blockNum = ByteArray.hexToBigInteger(blockNumOrTag).longValueExact();
382+
} catch (Exception e) {
383+
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
384+
}
385+
return wallet.getBlockByNum(blockNum);
386+
}
387+
393388
private BlockResult getBlockResult(Block block, boolean fullTx) {
394389
if (block == null) {
395390
return null;
@@ -806,17 +801,7 @@ public TransactionResult getTransactionByBlockHashAndIndex(String blockHash, Str
806801
@Override
807802
public TransactionResult getTransactionByBlockNumberAndIndex(String blockNumOrTag, String index)
808803
throws JsonRpcInvalidParamsException {
809-
long blockNum;
810-
if (JsonRpcApiUtil.isBlockTag(blockNumOrTag)) {
811-
blockNum = JsonRpcApiUtil.parseBlockTag(blockNumOrTag, wallet);
812-
} else {
813-
try {
814-
blockNum = ByteArray.hexToBigInteger(blockNumOrTag).longValue();
815-
} catch (Exception e) {
816-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
817-
}
818-
}
819-
Block block = wallet.getBlockByNum(blockNum);
804+
Block block = getBlockByNumOrTag(blockNumOrTag);
820805
if (block == null) {
821806
return null;
822807
}
@@ -907,17 +892,7 @@ public List<TransactionReceipt> getBlockReceipts(String blockNumOrHashOrTag)
907892
if (Pattern.matches(HASH_REGEX, blockNumOrHashOrTag)) {
908893
block = getBlockByJsonHash(blockNumOrHashOrTag);
909894
} else {
910-
long blockNum;
911-
if (JsonRpcApiUtil.isBlockTag(blockNumOrHashOrTag)) {
912-
blockNum = JsonRpcApiUtil.parseBlockTag(blockNumOrHashOrTag, wallet);
913-
} else {
914-
try {
915-
blockNum = ByteArray.hexToBigInteger(blockNumOrHashOrTag).longValue();
916-
} catch (Exception e) {
917-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
918-
}
919-
}
920-
block = wallet.getBlockByNum(blockNum);
895+
block = getBlockByNumOrTag(blockNumOrHashOrTag);
921896
}
922897

923898
// block receipts not available: block is genesis, not produced yet, or pruned in light node

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class JsonrpcServiceTest extends BaseTest {
6565
private static final String OWNER_ADDRESS_ACCOUNT_NAME = "first";
6666
private static final long LATEST_BLOCK_NUM = 10_000L;
6767
private static final long LATEST_SOLIDIFIED_BLOCK_NUM = 4L;
68+
private static final String TAG_NOT_SUPPORT_ERROR =
69+
"TAG [earliest | pending | finalized | safe] not supported";
6870

6971
private static TronJsonRpcImpl tronJsonRpc;
7072
@Resource
@@ -517,23 +519,23 @@ public void testGetTrxBalance() {
517519
tronJsonRpc.getTrxBalance("", "earliest");
518520
Assert.fail("Expected to be thrown");
519521
} catch (Exception e) {
520-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
522+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
521523
e.getMessage());
522524
}
523525

524526
try {
525527
tronJsonRpc.getTrxBalance("", "pending");
526528
Assert.fail("Expected to be thrown");
527529
} catch (Exception e) {
528-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
530+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
529531
e.getMessage());
530532
}
531533

532534
try {
533535
tronJsonRpc.getTrxBalance("", "finalized");
534536
Assert.fail("Expected to be thrown");
535537
} catch (Exception e) {
536-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
538+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
537539
e.getMessage());
538540
}
539541

@@ -552,23 +554,23 @@ public void testGetStorageAt() {
552554
tronJsonRpc.getStorageAt("", "", "earliest");
553555
Assert.fail("Expected to be thrown");
554556
} catch (Exception e) {
555-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
557+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
556558
e.getMessage());
557559
}
558560

559561
try {
560562
tronJsonRpc.getStorageAt("", "", "pending");
561563
Assert.fail("Expected to be thrown");
562564
} catch (Exception e) {
563-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
565+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
564566
e.getMessage());
565567
}
566568

567569
try {
568570
tronJsonRpc.getStorageAt("", "", "finalized");
569571
Assert.fail("Expected to be thrown");
570572
} catch (Exception e) {
571-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
573+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
572574
e.getMessage());
573575
}
574576
}
@@ -579,23 +581,23 @@ public void testGetABIOfSmartContract() {
579581
tronJsonRpc.getABIOfSmartContract("", "earliest");
580582
Assert.fail("Expected to be thrown");
581583
} catch (Exception e) {
582-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
584+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
583585
e.getMessage());
584586
}
585587

586588
try {
587589
tronJsonRpc.getABIOfSmartContract("", "pending");
588590
Assert.fail("Expected to be thrown");
589591
} catch (Exception e) {
590-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
592+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
591593
e.getMessage());
592594
}
593595

594596
try {
595597
tronJsonRpc.getABIOfSmartContract("", "finalized");
596598
Assert.fail("Expected to be thrown");
597599
} catch (Exception e) {
598-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
600+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
599601
e.getMessage());
600602
}
601603
}
@@ -606,23 +608,23 @@ public void testGetCall() {
606608
tronJsonRpc.getCall(null, "earliest");
607609
Assert.fail("Expected to be thrown");
608610
} catch (Exception e) {
609-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
611+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
610612
e.getMessage());
611613
}
612614

613615
try {
614616
tronJsonRpc.getCall(null, "pending");
615617
Assert.fail("Expected to be thrown");
616618
} catch (Exception e) {
617-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
619+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
618620
e.getMessage());
619621
}
620622

621623
try {
622624
tronJsonRpc.getCall(null, "finalized");
623625
Assert.fail("Expected to be thrown");
624626
} catch (Exception e) {
625-
Assert.assertEquals("TAG [earliest | pending | finalized] not supported",
627+
Assert.assertEquals(TAG_NOT_SUPPORT_ERROR,
626628
e.getMessage());
627629
}
628630
}

0 commit comments

Comments
 (0)