99import static org .tron .core .services .jsonrpc .JsonRpcApiUtil .getTransactionIndex ;
1010import static org .tron .core .services .jsonrpc .JsonRpcApiUtil .getTxID ;
1111import static org .tron .core .services .jsonrpc .JsonRpcApiUtil .triggerCallContract ;
12+ import static org .tron .core .services .jsonrpc .JsonRpcApiUtil .validateBlockNumOrHashOrTag ;
1213
1314import com .alibaba .fastjson .JSON ;
1415import 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