Skip to content

feat(api): improve request handling and validation - #7022

Merged
lvs0075 merged 10 commits into
tronprotocol:release_v4.8.3from
halibobo1205:feat/api-resource-limits
Oct 20, 2026
Merged

lvs0075 merged 10 commits into
tronprotocol:release_v4.8.3from
halibobo1205:feat/api-resource-limits

Conversation

@halibobo1205

@halibobo1205 halibobo1205 commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Improve input handling and validation across HTTP and gRPC APIs.
  • Reject unsupported requests earlier in processing.
  • Reduce logging for invalid client input.
  • Disable numeric JSON field aliases by default (node.allowNumericFieldAlias = false).

Compatibility

Some previously accepted inputs may now be rejected. Earlier validation may change error precedence and returned fields, using endpoint-specific error responses. A transaction that cannot be parsed now returns the parse error instead of internal server error, and single-digit JSON keys are no longer resolved to protobuf field numbers unless node.allowNumericFieldAlias is enabled.

Block processing, P2P, and consensus behavior remain unchanged apart from address-validation logging.

TODO

  • Document the client-visible compatibility changes in the release notes.

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SHOULD] The Compatibility section is still generic. Please list the concrete client-visible changes so operators and SDK maintainers can check their integrations:

  • JsonFormat enforces a 100,000-token budget per parsed document, including the ABI strings parsed by deploycontract and JSON-RPC buildTransaction.
  • Form-encoded bodies have a fixed 1,000-entry limit, including repeated keys.
  • Multi-contract requests are rejected earlier. Some HTTP arrays containing a valid contract and an unparseable or untyped contract previously had the latter silently dropped. Rejected multi-contract sign-weight/approval replies omit transaction. When rejection happens in packTransaction, HTTP broadcasttransaction returns an empty txid.
  • gRPC createCommonTransaction now returns CONTRACT_VALIDATE_ERROR for zero or multiple contracts, instead of failing on an empty list or using only the first contract.
  • Wallet.broadcastTransaction checks the contract count before the expiration check and before Manager's signature/permission validation. Its existing signature-length check still runs first.

I tested a local merge of f17a200 with release_v4.8.3 at 4c9b5f4 on JDK 17: no conflicts, and all 537 selected tests (the PR's test classes plus the JSON-RPC test packages) passed. The PR's listed CI checks also pass. Consensus validation rules and Manager's existing error message are unchanged, so no hard fork is required.

I recommend fixing the form-counting boundary before merging; the rest looks good to me.

[NIT] DecodeUtil.java:21 reports 42 as the expected byte length; please log ADDRESS_SIZE / 2 (21 bytes). If the shielded servlet re-wrap stays, a short comment explaining that clientMessage matches the exact exception class would help.

@halibobo1205
halibobo1205 force-pushed the feat/api-resource-limits branch from f17a200 to 6913441 Compare October 13, 2026 08:27
@halibobo1205

Copy link
Copy Markdown
Collaborator Author

@waynercheung
Thanks for the review.

  • Compatibility: the client-visible changes will be documented in the release notes and tracked as a TODO in the PR description.
  • Form counting: kept as is; see the inline reply.
  • null → internal server error: kept for compatibility; see the inline reply.
  • NITs addressed: DecodeUtil now logs ADDRESS_SIZE / 2 (7ed8af2), and the shielded servlet re-wrap has a comment explaining the exact-class match in clientMessage (6913441).

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. The address-length log fix and the re-wrap comment look good. I accept the intentional raw-segment cap, and the remaining documentation and regression-test suggestions are non-blocking.

I tested a local merge of 6913441 with release_v4.8.3 at a901f85 on JDK 17: all 537 selected tests (the PR's test classes plus the JSON-RPC test packages) and framework Checkstyle pass.

@SeriousCoding789 SeriousCoding789 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small simplification ideas:

  • The inner try/catch blocks seem to exist because Util.clientMessage matches exact classes, so ContractSizeNotEqualToOneException would otherwise become internal server error. Adding it to the clientMessage allow-list would let these servlets drop the inner try/catch and rely on the existing Util.processError(e, response).
  • The same contract-count check and message formatting now appear in about six places (packTransaction, broadcastTransaction, getTransactionApprovedList, getTransactionSignWeight, getShieldTransactionHash, createCommonTransaction). A shared helper, e.g. TransactionUtil.checkSingleContract(Transaction) plus a factory on ContractSizeNotEqualToOneException for the message, could keep one definition of the rule; Manager can stay as is.

@halibobo1205

Copy link
Copy Markdown
Collaborator Author

Two small simplification ideas:

  • The inner try/catch blocks seem to exist because Util.clientMessage matches exact classes, so ContractSizeNotEqualToOneException would otherwise become internal server error. Adding it to the clientMessage allow-list would let these servlets drop the inner try/catch and rely on the existing Util.processError(e, response).
  • The same contract-count check and message formatting now appear in about six places (packTransaction, broadcastTransaction, getTransactionApprovedList, getTransactionSignWeight, getShieldTransactionHash, createCommonTransaction). A shared helper, e.g. TransactionUtil.checkSingleContract(Transaction) plus a factory on ContractSizeNotEqualToOneException for the message, could keep one definition of the rule; Manager can stay as is.

Thanks, I looked at both and would keep the current form:

  • Allow-list: the inner catch in BroadcastServlet, GetTransactionSignWeightServlet and GetTransactionApprovedListServlet keeps their typed response; it isn't only a workaround for clientMessage. If the exception were allow-listed and sent to processError, they would answer {"Error": "..."} instead of code/message/txid or result (see my reply on those servlets). Only GetShieldTransactionHashServlet reports its validation errors through processError, and it is not mounted (@HttpApiExcluded).
  • Shared helper: the message already has one definition, ActuatorConstant.CONTRACT_SIZE_NOT_EQUAL_TO_ONE; what repeats is the > 1 comparison. Each site handles zero contracts differently and at a different point (No contract!, Contract is null, or Invalid transaction: no valid contract after the signature-count check), so a shared checkSingleContract could only cover the > 1 branch. A factory on ContractSizeNotEqualToOneException would also need the message moved into common, which can't see ActuatorConstant.

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@halibobo1205
halibobo1205 force-pushed the feat/api-resource-limits branch from 1609c2d to 3fc7c35 Compare October 14, 2026 09:58
@halibobo1205

Copy link
Copy Markdown
Collaborator Author

Refactor only: the pack-and-catch for ContractSizeNotEqualToOneException is now one helper, Util.withPackedTransaction, so BroadcastServlet, GetTransactionSignWeightServlet and GetTransactionApprovedListServlet no longer need an inner try/catch. ContractSizeNotEqualToOneException is added to the clientMessage allow-list (exact class), so GetShieldTransactionHashServlet is back to its original form. Responses are unchanged.

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@halibobo1205
halibobo1205 force-pushed the feat/api-resource-limits branch from fd035ff to 81b51b2 Compare October 16, 2026 07:13
public static final String TX_RESULT_NULL = "TransactionResultCapsule is null";
public static final String CONTRACT_NOT_EXIST = "No contract!";
public static final String CONTRACT_SIZE_NOT_EQUAL_TO_ONE =
"contract size should be exactly 1, this is extend feature ,actual :%d";

@SeriousCoding789 SeriousCoding789 Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format error, modify it to contract size should be exactly 1. This is extend feature, actual :%d

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep it as before in Manager.processTransaction

@SeriousCoding789

Copy link
Copy Markdown
Contributor

New commits since the last review:

  • 6f62c1c: extract the contract count in Manager.processTransaction (no behavior change).
  • f5391c0: packTransaction now throws the ParseException instead of returning null, so broadcasttransaction, getsignweight and gettransactionapprovedlist return the parse error instead of internal server error when a transaction cannot be parsed.
  • 6506222: node.allowNumericFieldAlias now defaults to false; single-digit JSON keys are only resolved to protobuf field numbers when an operator enables it.

On node.allowNumericFieldAlias defaulting to false (81b51b2):

This is a breaking change for HTTP clients. Up to v4.8.2.3 there was no switch and single-digit keys were always resolved, and #7028 added the switch a day earlier with true as the default specifically to stay "Backward-compatible by default". There may be legitimate clients that only use numeric keys. After upgrading, their requests are not rejected: the key falls through to handleMissingField, its value is silently dropped, and the request still succeeds (testOffNamedFieldParsedWhileInnerNumericAliasIgnored asserts this). Sometimes the missing field makes actuator validation fail, but some cases produce a valid transaction with different semantics. For example, freezebalancev2 with {"owner_address": "...", "frozen_balance": 1000000, "3": "ENERGY"} builds an ENERGY freeze on v4.8.2.3, while under the new default resource is dropped and the node builds a BANDWIDTH freeze without any error. A client that signs the returned transaction without checking it would freeze for the wrong resource.

I understand the concern behind the change: a numeric key can override a value that the named-field checks would have filtered, as the packTransaction Javadoc notes for raw_data. That seems better handled by running those checks on the parsed message, where a numeric key cannot get around them, than by turning off a behavior existing clients may depend on.

So I wouldn't recommend flipping the default directly in this PR, and would keep true as in #7028. If the alias is to be retired, a deprecation path (usage logging or a metric, an announcement in the release notes, then false in a later release) gives clients time to migrate. When it does become false, rejecting a single-digit key that resolves to an existing field with a ParseException would make the change visible instead of silent. If false stays here, it would be worth calling it out in the release notes as a breaking change, with node.allowNumericFieldAlias = true as the way to restore the old behavior.

@SeriousCoding789
SeriousCoding789 self-requested a review October 16, 2026 09:43
@halibobo1205

Copy link
Copy Markdown
Collaborator Author

@SeriousCoding789
Thanks for the detailed write-up. Your example is accurate: a body that mixes field names with a single-digit key such as "3" builds a different transaction once the alias is off. I'd still keep off as the default for 4.8.3:

  • Numeric keys are not part of the protobuf JSON mapping. protobuf-java-util's JsonFormat rejects {"frozen_balance":1000000,"3":"ENERGY"} with Cannot find field: 3, and drops the key under ignoringUnknownFields(). The lookup in our JsonFormat was inherited from protobuf-java-format, where it only served to read back unknown fields printed by the same library; java-tron hasn't printed unknown fields since 4.4.5, only field numbers 1–9 ever matched, and message fields take hex-encoded protobuf there instead of a JSON object.
  • No documented request or common client produces them. The HTTP API reference uses field names only, TronWeb and tronpy send field names, trident and wallet-cli use gRPC, and no java-tron response contains a numeric key a client could echo back. A body that mixes field names with "3" only exists if someone writes it that way on purpose.
  • With the switch off, a numeric key is treated like any other unrecognized key (a misspelled field name is skipped the same way), which also matches protobuf-java-util with ignoringUnknownFields().
  • The intended default for this switch was off. feat(http): add node.allowNumericFieldAlias switch for JSON aliases #7028 landed with true, but 4.8.3 hasn't shipped, so no release has carried that default.

Agreed on the release notes: 4.8.3 will list this as a behavior change for HTTP clients, with node.allowNumericFieldAlias = true to restore the previous parsing.

TransactionSignWeight.Builder tswBuilder = TransactionSignWeight.newBuilder();
Result.Builder resultBuilder = Result.newBuilder();
int contractCount = trx.getRawData().getContractCount();
if (contractCount > 1) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NIT] Why not change to contractCount != 1 ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> 1 is on purpose: zero contracts keep the existing handling further down (Invalid transaction: no valid contract, with transaction in the reply, after the signature-count check), so this PR only adds the rejection for more than one contract and leaves the zero-contract response unchanged. A zero count also usually means packTransaction dropped contracts it couldn't parse, where "no valid contract" is the more accurate message. Wallet.getTransactionApprovedList follows the same split.

@0xbigapple 0xbigapple left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@waynercheung waynercheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lvs0075
lvs0075 merged commit 039d186 into tronprotocol:release_v4.8.3 Oct 20, 2026
10 checks passed
@halibobo1205

Copy link
Copy Markdown
Collaborator Author

Follow-up (P2P side): this PR adds up-front contract count checks on the API paths and, as noted under Compatibility, leaves P2P unchanged. On P2P, TransactionsMsgHandler.check() only rejects an empty contract list; a transaction with more than one contract is rejected later, in Manager.processTransaction. A follow-up PR will:

  • reject a TRXS message in check() with BAD_TRX if any of its transactions does not carry exactly one contract, using the same CONTRACT_SIZE_NOT_EQUAL_TO_ONE message;
  • stop admitting transactions that set raw_data.auths, a field the node does not use: they are skipped at P2P admission and rejected at the start of Manager.pushTransaction, so broadcasttransaction, broadcasthex and gRPC BroadcastTransaction return CONTRACT_VALIDATE_ERROR with Contract validate error : Transaction auths are not supported.

Admission only: block validation and consensus are unchanged. The API-visible change will be listed in the 4.8.3 release notes together with the compatibility changes of this PR.

@halibobo1205

Copy link
Copy Markdown
Collaborator Author

The P2P-side follow-up described above is open in #7064 . Once it is merged, TRXS admission checks the contract count up front, as the API paths here do, and transactions that set raw_data.auths are not admitted over P2P or the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants