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 ;
@@ -296,6 +297,7 @@ public String web3Sha3(String data) throws JsonRpcInvalidParamsException {
296297 @ Override
297298 public String ethGetBlockTransactionCountByHash (String blockHash )
298299 throws JsonRpcInvalidParamsException {
300+ validateBlockNumOrHashOrTag (blockHash );
299301 Block b = getBlockByJsonHash (blockHash );
300302 if (b == null ) {
301303 return null ;
@@ -308,6 +310,7 @@ public String ethGetBlockTransactionCountByHash(String blockHash)
308310 @ Override
309311 public String ethGetBlockTransactionCountByNumber (String blockNumOrTag )
310312 throws JsonRpcInvalidParamsException {
313+ validateBlockNumOrHashOrTag (blockNumOrTag );
311314 List <Transaction > list = wallet .getTransactionsByJsonBlockId (blockNumOrTag );
312315 if (list == null ) {
313316 return null ;
@@ -327,6 +330,7 @@ public BlockResult ethGetBlockByHash(String blockHash, Boolean fullTransactionOb
327330 @ Override
328331 public BlockResult ethGetBlockByNumber (String blockNumOrTag , Boolean fullTransactionObjects )
329332 throws JsonRpcInvalidParamsException {
333+ validateBlockNumOrHashOrTag (blockNumOrTag );
330334 final Block b = wallet .getByJsonBlockId (blockNumOrTag );
331335 return (b == null ? null : getBlockResult (b , fullTransactionObjects ));
332336 }
@@ -393,6 +397,9 @@ public String getLatestBlockNum() {
393397 @ Override
394398 public String getTrxBalance (String address , String blockNumOrTag )
395399 throws JsonRpcInvalidParamsException {
400+ // Add length check and validate hex format to prevent DDoS attacks
401+ validateBlockNumOrHashOrTag (blockNumOrTag );
402+
396403 if (EARLIEST_STR .equalsIgnoreCase (blockNumOrTag )
397404 || PENDING_STR .equalsIgnoreCase (blockNumOrTag )
398405 || FINALIZED_STR .equalsIgnoreCase (blockNumOrTag )) {
@@ -409,12 +416,6 @@ public String getTrxBalance(String address, String blockNumOrTag)
409416 }
410417 return ByteArray .toJsonHex (balance );
411418 } else {
412- try {
413- ByteArray .hexToBigInteger (blockNumOrTag );
414- } catch (Exception e ) {
415- throw new JsonRpcInvalidParamsException (BLOCK_NUM_ERROR );
416- }
417-
418419 throw new JsonRpcInvalidParamsException (QUANTITY_NOT_SUPPORT_ERROR );
419420 }
420421 }
@@ -535,6 +536,7 @@ private String call(byte[] ownerAddressByte, byte[] contractAddressByte, long va
535536 @ Override
536537 public String getStorageAt (String address , String storageIdx , String blockNumOrTag )
537538 throws JsonRpcInvalidParamsException {
539+ validateBlockNumOrHashOrTag (blockNumOrTag );
538540 if (EARLIEST_STR .equalsIgnoreCase (blockNumOrTag )
539541 || PENDING_STR .equalsIgnoreCase (blockNumOrTag )
540542 || FINALIZED_STR .equalsIgnoreCase (blockNumOrTag )) {
@@ -558,19 +560,14 @@ public String getStorageAt(String address, String storageIdx, String blockNumOrT
558560 DataWord value = storage .getValue (new DataWord (ByteArray .fromHexString (storageIdx )));
559561 return ByteArray .toJsonHex (value == null ? new byte [32 ] : value .getData ());
560562 } else {
561- try {
562- ByteArray .hexToBigInteger (blockNumOrTag );
563- } catch (Exception e ) {
564- throw new JsonRpcInvalidParamsException (BLOCK_NUM_ERROR );
565- }
566-
567563 throw new JsonRpcInvalidParamsException (QUANTITY_NOT_SUPPORT_ERROR );
568564 }
569565 }
570566
571567 @ Override
572568 public String getABIOfSmartContract (String contractAddress , String blockNumOrTag )
573569 throws JsonRpcInvalidParamsException {
570+ validateBlockNumOrHashOrTag (blockNumOrTag );
574571 if (EARLIEST_STR .equalsIgnoreCase (blockNumOrTag )
575572 || PENDING_STR .equalsIgnoreCase (blockNumOrTag )
576573 || FINALIZED_STR .equalsIgnoreCase (blockNumOrTag )) {
@@ -589,12 +586,6 @@ public String getABIOfSmartContract(String contractAddress, String blockNumOrTag
589586 }
590587
591588 } else {
592- try {
593- ByteArray .hexToBigInteger (blockNumOrTag );
594- } catch (Exception e ) {
595- throw new JsonRpcInvalidParamsException (BLOCK_NUM_ERROR );
596- }
597-
598589 throw new JsonRpcInvalidParamsException (QUANTITY_NOT_SUPPORT_ERROR );
599590 }
600591 }
@@ -791,6 +782,7 @@ private TransactionResult getTransactionByBlockAndIndex(Block block, String inde
791782 @ Override
792783 public TransactionResult getTransactionByBlockHashAndIndex (String blockHash , String index )
793784 throws JsonRpcInvalidParamsException {
785+ validateBlockNumOrHashOrTag (blockHash );
794786 final Block block = getBlockByJsonHash (blockHash );
795787
796788 if (block == null ) {
@@ -803,6 +795,9 @@ public TransactionResult getTransactionByBlockHashAndIndex(String blockHash, Str
803795 @ Override
804796 public TransactionResult getTransactionByBlockNumberAndIndex (String blockNumOrTag , String index )
805797 throws JsonRpcInvalidParamsException {
798+ // Add length check and validate hex format to prevent DDoS attacks
799+ validateBlockNumOrHashOrTag (blockNumOrTag );
800+
806801 Block block = wallet .getByJsonBlockId (blockNumOrTag );
807802 if (block == null ) {
808803 return null ;
@@ -888,6 +883,8 @@ private TransactionContext findTransactionContext(TransactionInfoList infoList,
888883 @ Override
889884 public List <TransactionReceipt > getBlockReceipts (String blockNumOrHashOrTag )
890885 throws JsonRpcInvalidParamsException , JsonRpcInternalException {
886+ // Add length check and validate hex format to prevent DDoS attacks
887+ validateBlockNumOrHashOrTag (blockNumOrHashOrTag );
891888
892889 Block block = null ;
893890
@@ -973,6 +970,8 @@ public String getCall(CallArguments transactionCall, Object blockParamObj)
973970
974971 long blockNumber ;
975972 try {
973+ // Add length check to prevent DDoS attacks
974+ validateBlockNumOrHashOrTag (blockNumOrTag );
976975 blockNumber = ByteArray .hexToBigInteger (blockNumOrTag ).longValue ();
977976 } catch (Exception e ) {
978977 throw new JsonRpcInvalidParamsException (BLOCK_NUM_ERROR );
@@ -1014,12 +1013,6 @@ public String getCall(CallArguments transactionCall, Object blockParamObj)
10141013 return call (addressData , contractAddressData , transactionCall .parseValue (),
10151014 ByteArray .fromHexString (transactionCall .getData ()));
10161015 } else {
1017- try {
1018- ByteArray .hexToBigInteger (blockNumOrTag );
1019- } catch (Exception e ) {
1020- throw new JsonRpcInvalidParamsException (BLOCK_NUM_ERROR );
1021- }
1022-
10231016 throw new JsonRpcInvalidParamsException (QUANTITY_NOT_SUPPORT_ERROR );
10241017 }
10251018 }
0 commit comments