|
1 | 1 | package org.tron.core.services.jsonrpc; |
2 | 2 |
|
3 | | -import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.EARLIEST_STR; |
4 | | -import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.FINALIZED_STR; |
5 | | -import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.LATEST_STR; |
6 | | -import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.PENDING_STR; |
7 | | -import static org.tron.core.services.jsonrpc.TronJsonRpcImpl.TAG_PENDING_SUPPORT_ERROR; |
8 | | - |
9 | 3 | import com.google.common.base.Throwables; |
10 | 4 | import com.google.common.primitives.Longs; |
11 | 5 | import com.google.protobuf.Any; |
|
57 | 51 | @Slf4j(topic = "API") |
58 | 52 | public class JsonRpcApiUtil { |
59 | 53 |
|
| 54 | + public static final String EARLIEST_STR = "earliest"; |
| 55 | + public static final String PENDING_STR = "pending"; |
| 56 | + public static final String LATEST_STR = "latest"; |
| 57 | + public static final String FINALIZED_STR = "finalized"; |
| 58 | + public static final String SAFE_STR = "safe"; |
| 59 | + public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported"; |
| 60 | + public static final String TAG_SAFE_SUPPORT_ERROR = "TAG safe not supported"; |
| 61 | + public static final String BLOCK_NUM_ERROR = "invalid block number"; |
| 62 | + |
60 | 63 | public static byte[] convertToTronAddress(byte[] address) { |
61 | 64 | byte[] newAddress = new byte[21]; |
62 | 65 | byte[] temp = new byte[] {Wallet.getAddressPreFixByte()}; |
@@ -515,20 +518,48 @@ public static long parseEnergyFee(long timestamp, String energyPriceHistory) { |
515 | 518 | return -1; |
516 | 519 | } |
517 | 520 |
|
518 | | - public static long getByJsonBlockId(String blockNumOrTag, Wallet wallet) |
| 521 | + public static boolean isBlockTag(String tag) { |
| 522 | + return LATEST_STR.equalsIgnoreCase(tag) |
| 523 | + || EARLIEST_STR.equalsIgnoreCase(tag) |
| 524 | + || FINALIZED_STR.equalsIgnoreCase(tag) |
| 525 | + || PENDING_STR.equalsIgnoreCase(tag) |
| 526 | + || SAFE_STR.equalsIgnoreCase(tag); |
| 527 | + } |
| 528 | + |
| 529 | + public static long parseBlockTag(String tag, Wallet wallet) |
519 | 530 | throws JsonRpcInvalidParamsException { |
520 | | - if (PENDING_STR.equalsIgnoreCase(blockNumOrTag)) { |
521 | | - throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR); |
| 531 | + 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 | + return wallet.getNowBlock().getBlockHeader().getRawData().getNumber(); |
522 | 536 | } |
523 | | - if (StringUtils.isEmpty(blockNumOrTag) || LATEST_STR.equalsIgnoreCase(blockNumOrTag)) { |
524 | | - return -1; |
525 | | - } else if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)) { |
| 537 | + if (EARLIEST_STR.equalsIgnoreCase(tag)) { |
526 | 538 | return 0; |
527 | | - } else if (FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) { |
| 539 | + } |
| 540 | + if (FINALIZED_STR.equalsIgnoreCase(tag)) { |
528 | 541 | return wallet.getSolidBlockNum(); |
529 | | - } else { |
530 | | - return ByteArray.jsonHexToLong(blockNumOrTag); |
531 | 542 | } |
| 543 | + if (PENDING_STR.equalsIgnoreCase(tag)) { |
| 544 | + throw new JsonRpcInvalidParamsException(TAG_PENDING_SUPPORT_ERROR); |
| 545 | + } |
| 546 | + if (SAFE_STR.equalsIgnoreCase(tag)) { |
| 547 | + throw new JsonRpcInvalidParamsException(TAG_SAFE_SUPPORT_ERROR); |
| 548 | + } |
| 549 | + throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR); |
| 550 | + } |
| 551 | + |
| 552 | + /** |
| 553 | + * Parse a block tag or hex number. Uses strict jsonHexToLong (requires 0x prefix) for hex. |
| 554 | + * Callers needing flexible hex parsing (0x -> hex, bare number -> decimal) should use |
| 555 | + * isBlockTag/parseBlockTag and handle hex separately with hexToBigInteger. |
| 556 | + */ |
| 557 | + public static long parseBlockNumber(String blockNumOrTag, Wallet wallet) |
| 558 | + throws JsonRpcInvalidParamsException { |
| 559 | + if (isBlockTag(blockNumOrTag)) { |
| 560 | + return parseBlockTag(blockNumOrTag, wallet); |
| 561 | + } |
| 562 | + return ByteArray.jsonHexToLong(blockNumOrTag); |
532 | 563 | } |
533 | 564 |
|
534 | 565 | public static String generateFilterId() { |
|
0 commit comments