Skip to content

Commit b526ede

Browse files
committed
update getByJsonBlockId
1 parent 1fda1ed commit b526ede

2 files changed

Lines changed: 14 additions & 22 deletions

File tree

common/src/main/java/org/tron/common/utils/ByteArray.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ public static BigInteger hexToBigInteger(String input) {
156156
}
157157

158158
public static long jsonHexToLong(String x) throws JsonRpcInvalidParamsException {
159-
// Constants for input length validation to prevent DDoS attacks
160-
int MAX_HEX_LONG_LENGTH = 20; // For 64-bit long values (18 chars for 0x7FFFFFFFFFFFFFFF) + safety bufferty buffer
159+
// Constants for input length validation to prevent DDoS attacks
160+
int MAX_HEX_LONG_LENGTH = 20; // For 64-bit long values (18 chars for 0x7FFFFFFFFFFFFFFF) + safety buffer
161161
if (x == null || x.length() > MAX_HEX_LONG_LENGTH) {
162-
throw new IllegalArgumentException("Incorrect string length");
162+
throw new JsonRpcInvalidParamsException("Input cannot be null or too long");
163163
}
164164

165165
if (!x.startsWith("0x")) {

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getTransactionIndex;
1010
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.getTxID;
1111
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.triggerCallContract;
12+
import static org.tron.core.services.jsonrpc.JsonRpcApiUtil.validateBlockNumOrHashOrTag;
1213

1314
import com.alibaba.fastjson.JSON;
1415
import com.google.common.cache.Cache;
@@ -115,7 +116,6 @@ public enum RequestSource {
115116
private static final String FILTER_NOT_FOUND = "filter not found";
116117
public static final int EXPIRE_SECONDS = 5 * 60;
117118
private static final int maxBlockFilterNum = Args.getInstance().getJsonRpcMaxBlockFilterNum();
118-
119119
private static final Cache<LogFilterElement, LogFilterElement> logElementCache =
120120
CacheBuilder.newBuilder()
121121
.maximumSize(300_000L) // 300s * tps(1000) * 1 log/tx ≈ 300_000
@@ -415,12 +415,6 @@ public String getTrxBalance(String address, String blockNumOrTag)
415415
}
416416
return ByteArray.toJsonHex(balance);
417417
} else {
418-
try {
419-
ByteArray.hexToBigInteger(blockNumOrTag);
420-
} catch (Exception e) {
421-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
422-
}
423-
424418
throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR);
425419
}
426420
}
@@ -541,6 +535,9 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va
541535
@Override
542536
public String getStorageAt(String address, String storageIdx, String blockNumOrTag)
543537
throws JsonRpcInvalidParamsException {
538+
// Add length check to prevent DDoS attacks
539+
JsonRpcApiUtil.validateBlockNumOrHashOrTag(blockNumOrTag);
540+
544541
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
545542
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
546543
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
@@ -564,19 +561,16 @@ public String getStorageAt(String address, String storageIdx, String blockNumOrT
564561
DataWord value = storage.getValue(new DataWord(ByteArray.fromHexString(storageIdx)));
565562
return ByteArray.toJsonHex(value == null ? new byte[32] : value.getData());
566563
} else {
567-
try {
568-
ByteArray.hexToBigInteger(blockNumOrTag);
569-
} catch (Exception e) {
570-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
571-
}
572-
573564
throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR);
574565
}
575566
}
576567

577568
@Override
578569
public String getABIOfSmartContract(String contractAddress, String blockNumOrTag)
579570
throws JsonRpcInvalidParamsException {
571+
// Add length check to prevent DDoS attacks
572+
JsonRpcApiUtil.validateBlockNumOrHashOrTag(blockNumOrTag);
573+
580574
if (EARLIEST_STR.equalsIgnoreCase(blockNumOrTag)
581575
|| PENDING_STR.equalsIgnoreCase(blockNumOrTag)
582576
|| FINALIZED_STR.equalsIgnoreCase(blockNumOrTag)) {
@@ -595,12 +589,6 @@ public String getABIOfSmartContract(String contractAddress, String blockNumOrTag
595589
}
596590

597591
} else {
598-
try {
599-
ByteArray.hexToBigInteger(blockNumOrTag);
600-
} catch (Exception e) {
601-
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
602-
}
603-
604592
throw new JsonRpcInvalidParamsException(QUANTITY_NOT_SUPPORT_ERROR);
605593
}
606594
}
@@ -984,6 +972,8 @@ public String getCall(CallArguments transactionCall, Object blockParamObj)
984972

985973
long blockNumber;
986974
try {
975+
// Add length check to prevent DDoS attacks
976+
validateBlockNumOrHashOrTag(blockNumOrTag);
987977
blockNumber = ByteArray.hexToBigInteger(blockNumOrTag).longValue();
988978
} catch (Exception e) {
989979
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);
@@ -1026,6 +1016,8 @@ public String getCall(CallArguments transactionCall, Object blockParamObj)
10261016
ByteArray.fromHexString(transactionCall.getData()));
10271017
} else {
10281018
try {
1019+
// Add length check to prevent DDoS attacks
1020+
JsonRpcApiUtil.validateBlockNumOrHashOrTag(blockNumOrTag);
10291021
ByteArray.hexToBigInteger(blockNumOrTag);
10301022
} catch (Exception e) {
10311023
throw new JsonRpcInvalidParamsException(BLOCK_NUM_ERROR);

0 commit comments

Comments
 (0)