|
23 | 23 | import org.tron.common.utils.StringUtil; |
24 | 24 | import org.tron.core.Wallet; |
25 | 25 | import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException; |
| 26 | +import org.tron.core.exception.jsonrpc.JsonRpcPrunedHistoryException; |
26 | 27 | import org.tron.protos.Protocol.Block; |
27 | 28 | import org.tron.protos.Protocol.Transaction; |
28 | 29 | import org.tron.protos.Protocol.Transaction.Contract.ContractType; |
@@ -61,6 +62,7 @@ public class JsonRpcApiUtil { |
61 | 62 | public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported"; |
62 | 63 | public static final String TAG_SAFE_SUPPORT_ERROR = "TAG safe not supported"; |
63 | 64 | public static final String BLOCK_NUM_ERROR = "invalid block number"; |
| 65 | + public static final String PRUNED_HISTORY_ERROR = "Pruned history unavailable"; |
64 | 66 | public static final String TX_INDEX_ERROR = "invalid index value"; |
65 | 67 |
|
66 | 68 | private static final SecureRandom random = new SecureRandom(); |
@@ -633,7 +635,7 @@ public static long parseBlockTag(String tag, Wallet wallet) |
633 | 635 | return wallet.getHeadBlockNum(); |
634 | 636 | } |
635 | 637 | if (EARLIEST_STR.equalsIgnoreCase(tag)) { |
636 | | - return 0; |
| 638 | + return resolveEarliestBlockNum(wallet); |
637 | 639 | } |
638 | 640 | if (FINALIZED_STR.equalsIgnoreCase(tag)) { |
639 | 641 | return wallet.getSolidBlockNum(); |
@@ -697,6 +699,47 @@ public static long parseBlockNumber(String blockNumOrTag, Wallet wallet) |
697 | 699 | return parseBlockNumber(blockNumOrTag); |
698 | 700 | } |
699 | 701 |
|
| 702 | + /** |
| 703 | + * The lowest block for which everything the node persists is available; what "earliest" |
| 704 | + * resolves to. On a LiteNode this is the |
| 705 | + * lowest receipt block when receipts are persisted, otherwise the lowest block (receipt endpoints |
| 706 | + * answer 4444 on such a node regardless of this value). On a FullNode it is genesis. |
| 707 | + */ |
| 708 | + public static long resolveEarliestBlockNum(Wallet wallet) { |
| 709 | + if (!wallet.isLiteNode()) { |
| 710 | + return 0; |
| 711 | + } |
| 712 | + long lowestReceiptBlockNum = wallet.getLowestBlockNumOfReceiptStore(); |
| 713 | + return lowestReceiptBlockNum == Long.MAX_VALUE |
| 714 | + ? wallet.getLowestBlockNum() : lowestReceiptBlockNum; |
| 715 | + } |
| 716 | + |
| 717 | + /** |
| 718 | + * Rejects a query for a block below the LiteNode pruning cutoff with error code 4444. |
| 719 | + * Raw primitive — no genesis exemption; callers own that semantics. |
| 720 | + */ |
| 721 | + public static void checkPrunedBlockHistory(long blockNum, Wallet wallet) |
| 722 | + throws JsonRpcPrunedHistoryException { |
| 723 | + if (wallet.isLiteNode() && blockNum < wallet.getLowestBlockNum()) { |
| 724 | + throw new JsonRpcPrunedHistoryException(PRUNED_HISTORY_ERROR); |
| 725 | + } |
| 726 | + } |
| 727 | + |
| 728 | + /** |
| 729 | + * Receipt form of {@link #checkPrunedBlockHistory(long, Wallet)} for endpoints that read |
| 730 | + * receipts or logs; their lower bound is the lowest receipt block. Same raw-primitive |
| 731 | + * contract. Receipt persistence is a per-node switch independent of node type, so a node |
| 732 | + * that never persists receipts is rejected before the LiteNode gate. |
| 733 | + */ |
| 734 | + public static void checkPrunedReceiptHistory(long blockNum, Wallet wallet) |
| 735 | + throws JsonRpcPrunedHistoryException { |
| 736 | + long lowestReceiptBlockNum = wallet.getLowestBlockNumOfReceiptStore(); |
| 737 | + if (lowestReceiptBlockNum == Long.MAX_VALUE |
| 738 | + || (wallet.isLiteNode() && blockNum < lowestReceiptBlockNum)) { |
| 739 | + throw new JsonRpcPrunedHistoryException(PRUNED_HISTORY_ERROR); |
| 740 | + } |
| 741 | + } |
| 742 | + |
700 | 743 | /** |
701 | 744 | * Max hex digits of a 32-bit int (0x7FFFFFFF). A transaction index fits a signed int, so the |
702 | 745 | * longest valid input is "0x" + 8 hex digits; the +2 in the guard covers the prefix. |
|
0 commit comments