Skip to content

Commit 3b35e94

Browse files
authored
feat(jsonrpc): return code 3 for revert and 4444 for pruned history (#7004)
1 parent abaac41 commit 3b35e94

26 files changed

Lines changed: 1071 additions & 165 deletions

‎chainbase/src/main/java/org/tron/core/ChainBaseManager.java‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import lombok.Setter;
1010
import lombok.extern.slf4j.Slf4j;
1111
import org.apache.commons.collections4.CollectionUtils;
12+
import org.apache.commons.lang3.BooleanUtils;
1213
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.stereotype.Component;
15+
import org.tron.common.parameter.CommonParameter;
1416
import org.tron.common.storage.metric.DbStatService;
1517
import org.tron.common.utils.ForkController;
1618
import org.tron.common.utils.Sha256Hash;
@@ -244,10 +246,38 @@ public class ChainBaseManager {
244246
@Setter
245247
private long lowestBlockNum = -1; // except num = 0.
246248

249+
// first block of transactionRetStore; head + 1 when the store is empty,
250+
// Long.MAX_VALUE when receipts are not persisted
251+
@Getter
252+
private long lowestBlockNumOfReceiptStore = -1;
253+
247254
@Getter
248255
@Setter
249256
private long latestSaveBlockTime;
250257

258+
@PostConstruct
259+
private void init() {
260+
this.lowestBlockNum = this.blockIndexStore.getLimitNumber(1, 1).stream()
261+
.map(BlockId::getNum).findFirst().orElse(0L);
262+
this.nodeType = getLowestBlockNum() > 1 ? NodeType.LITE : NodeType.FULL;
263+
this.latestSaveBlockTime = System.currentTimeMillis();
264+
}
265+
266+
/**
267+
* Reads the lowest receipt block from the store itself, not from snapshot metadata; an empty
268+
* store means receipts begin with the next executed block. With receipt persistence off
269+
* the store never grows, so no lower bound exists. Must run after checkpoint recovery (so the
270+
* last session's tail is visible) and before any session is built ({@code getNext} does
271+
* not merge in-flight layers).
272+
*/
273+
public void initLowestBlockNumOfReceiptStore() {
274+
boolean persistReceipts = BooleanUtils.toBoolean(CommonParameter.getInstance()
275+
.getStorage().getTransactionHistorySwitch());
276+
this.lowestBlockNumOfReceiptStore = persistReceipts
277+
? this.transactionRetStore.getLowestReceiptBlockNum().orElseGet(() -> getHeadBlockNum() + 1)
278+
: Long.MAX_VALUE;
279+
}
280+
251281
// for test only
252282
public List<ByteString> getWitnesses() {
253283
return witnessScheduleStore.getActiveWitnesses();
@@ -389,14 +419,6 @@ public long getNextBlockSlotTime() {
389419
+ slotCount * BLOCK_PRODUCED_INTERVAL;
390420
}
391421

392-
@PostConstruct
393-
private void init() {
394-
this.lowestBlockNum = this.blockIndexStore.getLimitNumber(1, 1).stream()
395-
.map(BlockId::getNum).findFirst().orElse(0L);
396-
this.nodeType = getLowestBlockNum() > 1 ? NodeType.LITE : NodeType.FULL;
397-
this.latestSaveBlockTime = System.currentTimeMillis();
398-
}
399-
400422
public void shutdown() {
401423
dbStatService.shutdown();
402424
}

‎chainbase/src/main/java/org/tron/core/capsule/TransactionResultCapsule.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public void setErrorCode(Result.code code) {
147147
this.transactionResult = this.transactionResult.toBuilder().setRet(code).build();
148148
}
149149

150+
public void setResultCode(contractResult code) {
151+
this.transactionResult = this.transactionResult.toBuilder().setContractRet(code).build();
152+
}
153+
150154
public long getShieldedTransactionFee() {
151155
return transactionResult.getShieldedTransactionFee();
152156
}
@@ -184,4 +188,4 @@ public byte[] getData() {
184188
public Result getInstance() {
185189
return this.transactionResult;
186190
}
187-
}
191+
}

‎chainbase/src/main/java/org/tron/core/store/TransactionRetStore.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.tron.core.store;
22

3+
import com.google.common.primitives.Longs;
34
import com.google.protobuf.ByteString;
45
import java.util.Objects;
6+
import java.util.OptionalLong;
57
import lombok.extern.slf4j.Slf4j;
68
import org.apache.commons.lang3.BooleanUtils;
79
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +39,18 @@ public void put(byte[] key, TransactionRetCapsule item) {
3739
}
3840
}
3941

42+
/**
43+
* Lowest block number that has receipts, or empty when the store has none. On a LiteNode
44+
* this is generally above the lowest block: a snapshot ships block bodies but no receipts.
45+
*
46+
* <p>Startup use only — must run before any session is built. With in-flight snapshot
47+
* layers, {@code getNext} does not merge deletions correctly.
48+
*/
49+
public OptionalLong getLowestReceiptBlockNum() {
50+
return revokingDB.getNext(ByteArray.fromLong(0), 1).keySet().stream()
51+
.mapToLong(Longs::fromByteArray).findFirst();
52+
}
53+
4054
public TransactionInfoCapsule getTransactionInfo(byte[] key) throws BadItemException {
4155
long blockNumber = transactionStore.getBlockNumber(key);
4256
if (blockNumber == -1) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.tron.core.exception.jsonrpc;
2+
3+
public class JsonRpcExecutionRevertedException extends JsonRpcException {
4+
5+
public JsonRpcExecutionRevertedException(String message) {
6+
super(message);
7+
}
8+
9+
public JsonRpcExecutionRevertedException(String message, Object data) {
10+
super(message, data);
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.tron.core.exception.jsonrpc;
2+
3+
/**
4+
* Thrown when a request targets data the node does not have: history pruned on a LiteNode,
5+
* or receipts and logs on a node that does not persist them. Maps to JSON-RPC error code 4444
6+
* "Pruned history unavailable", as standardized by the Ethereum Execution API (EIP-4444).
7+
*/
8+
public class JsonRpcPrunedHistoryException extends JsonRpcException {
9+
10+
public JsonRpcPrunedHistoryException(String message) {
11+
super(message);
12+
}
13+
14+
public JsonRpcPrunedHistoryException(String message, Object data) {
15+
super(message, data);
16+
}
17+
}

‎common/src/main/resources/reference.conf‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ storage {
4747

4848
db.directory = "database" # Database directory under the node --output-directory path.
4949

50-
# Whether to write transaction result in transactionRetStore
50+
# Whether to store transaction receipts (fee, energy usage, logs) in transactionRetStore.
51+
# When "off", the JSON-RPC methods eth_getBlockReceipts, eth_getLogs and eth_newFilter
52+
# return error 4444, including for blocks whose receipts were stored while the switch
53+
# was "on". Queries for the genesis block alone are still answered.
5154
transHistory.switch = "on"
5255

5356
# Per-database LevelDB option overrides.Default: empty. All databases use global LevelDB settings.

‎framework/src/main/java/org/tron/core/Wallet.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
import org.tron.protos.Protocol.Transaction.Contract;
237237
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
238238
import org.tron.protos.Protocol.Transaction.Result.code;
239+
import org.tron.protos.Protocol.Transaction.Result.contractResult;
239240
import org.tron.protos.Protocol.TransactionInfo;
240241
import org.tron.protos.contract.AssetIssueContractOuterClass.AssetIssueContract;
241242
import org.tron.protos.contract.BalanceContract;
@@ -743,6 +744,18 @@ public long getHeadBlockNum() {
743744
return chainBaseManager.getHeadBlockNum();
744745
}
745746

747+
public boolean isLiteNode() {
748+
return chainBaseManager.isLiteNode();
749+
}
750+
751+
public long getLowestBlockNum() {
752+
return chainBaseManager.getLowestBlockNum();
753+
}
754+
755+
public long getLowestBlockNumOfReceiptStore() {
756+
return chainBaseManager.getLowestBlockNumOfReceiptStore();
757+
}
758+
746759
public BlockCapsule getBlockCapsuleByNum(long blockNum) {
747760
try {
748761
return chainBaseManager.getBlockByNum(blockNum);
@@ -3208,6 +3221,7 @@ public Transaction callConstantContract(TransactionCapsule trxCap,
32083221
}
32093222
if (result.isRevert()) {
32103223
ret.setStatus(0, code.FAILED);
3224+
ret.setResultCode(contractResult.REVERT);
32113225
retBuilder.setMessage(ByteString.copyFromUtf8("REVERT opcode executed"))
32123226
.build();
32133227
}

‎framework/src/main/java/org/tron/core/db/Manager.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ public void init() {
461461
trieService.setChainBaseManager(chainBaseManager);
462462
revokingStore.disable();
463463
revokingStore.check();
464+
chainBaseManager.initLowestBlockNumOfReceiptStore();
464465
transactionCache.initCache();
465466
rewardViCalService.init();
466467
this.setProposalController(ProposalController.createInstance(this));

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.tron.common.utils.StringUtil;
2424
import org.tron.core.Wallet;
2525
import org.tron.core.exception.jsonrpc.JsonRpcInvalidParamsException;
26+
import org.tron.core.exception.jsonrpc.JsonRpcPrunedHistoryException;
2627
import org.tron.protos.Protocol.Block;
2728
import org.tron.protos.Protocol.Transaction;
2829
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
@@ -61,6 +62,7 @@ public class JsonRpcApiUtil {
6162
public static final String TAG_PENDING_SUPPORT_ERROR = "TAG pending not supported";
6263
public static final String TAG_SAFE_SUPPORT_ERROR = "TAG safe not supported";
6364
public static final String BLOCK_NUM_ERROR = "invalid block number";
65+
public static final String PRUNED_HISTORY_ERROR = "Pruned history unavailable";
6466
public static final String TX_INDEX_ERROR = "invalid index value";
6567

6668
private static final SecureRandom random = new SecureRandom();
@@ -633,7 +635,7 @@ public static long parseBlockTag(String tag, Wallet wallet)
633635
return wallet.getHeadBlockNum();
634636
}
635637
if (EARLIEST_STR.equalsIgnoreCase(tag)) {
636-
return 0;
638+
return resolveEarliestBlockNum(wallet);
637639
}
638640
if (FINALIZED_STR.equalsIgnoreCase(tag)) {
639641
return wallet.getSolidBlockNum();
@@ -697,6 +699,47 @@ public static long parseBlockNumber(String blockNumOrTag, Wallet wallet)
697699
return parseBlockNumber(blockNumOrTag);
698700
}
699701

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+
700743
/**
701744
* Max hex digits of a 32-bit int (0x7FFFFFFF). A transaction index fits a signed int, so the
702745
* longest valid input is "0x" + 8 hex digits; the +2 in the guard covers the prefix.

0 commit comments

Comments
 (0)