Skip to content

fix(net): precheck BLOCK/TRXS and reject unsupported TRX - #7115

Merged
kuny0707 merged 7 commits into
tronprotocol:release_v4.8.3from
317787106:hotfix/block_message_precheck
Nov 23, 2026
Merged

kuny0707 merged 7 commits into
tronprotocol:release_v4.8.3from
317787106:hotfix/block_message_precheck

Conversation

@317787106

@317787106 317787106 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Add read-only wire-format prechecks to inbound BLOCK and TRXS messages, and reject unsupported P2P TRX messages before transaction parsing:

  • BLOCK: reject protobuf payloads larger than BLOCK_SIZE + 1000 (2,001,000 bytes), or more than MAX_BLOCK_SIZE / MIN_TRANSACTION_SIZE top-level transaction entries (currently 25,012), before BlockCapsule construction.
  • TRXS: reject more than MAX_TRX_FETCH_PER_PEER top-level transaction entries (currently 1,000), before Protocol.Transactions.parseFrom().
  • TRX: remove the unsupported decoding branch from TronMessageFactory. The private dispatch now rejects it through default with NO_SUCH_MESSAGE, before TransactionMessage construction or transaction parsing.
  • Count transaction entries by field tag and wire type, skip their contents without constructing transaction or unknown-field objects, and perform the top-level EOF check during precheck.
  • Share the existing block-validation and fetch-batch limits.

Why are these changes required?

BLOCK and TRXS messages are parsed before handler-level request and size checks. BLOCK also creates a capsule for every transaction before those checks. Inputs that are ultimately rejected can therefore incur substantial allocation and parsing work.

P2P TRX messages were already unsupported by the event dispatcher, but the factory still parsed them first. Removing that factory branch rejects them before transaction decoding while preserving the existing unsupported-message disconnect reason.

The BLOCK count bound follows the existing 80-byte minimum enforced by validBlock() on advertisement and sync paths. Header and repeated-field encoding overhead make this a conservative upper bound. The TRXS count bound follows our existing maximum of 1,000 entries per TRX fetch request; responses may split that request into smaller batches.

This PR has been tested by:

Local validation on macOS ARM64 / JDK 17:

  • 57 focused tests passed on the final implementation: TronMessageFactoryTest (5), BlockMessageTest (11), TransactionsMessageTest (9), TransactionsMsgHandlerTest (30), and MessageHandlerTest (2).
  • Coverage includes size/count boundaries, rejection before parsing or capsule construction, both protobuf-filter settings, opaque bytes, unknown groups, wrong wire types, malformed lengths, END_GROUP handling, unsupported TRX rejection and its P2P disconnect reason, direct API/internal TransactionMessage construction, and supported message decoding.
  • Build without tests, the CI module-scoped Checkstyle gate, and the Math usage check passed.

The full regression suite was not completed.

Follow up

Further TRXS hardening is deferred. The current early checks address outer message/list amplification while retaining the existing transport frame-size limit; nested transaction and unknown-field contents can still incur parsing costs.

Deeper content prechecks would add CPU work to normal message processing, and tighter admission limits would require separate compatibility validation. The incremental benefit of additional restrictions under the current limits has not yet been established. Revisit request-state gating, tighter size limits, or per-entry structure budgets when profiling or operational evidence justifies them.

Extra details

The BLOCK size check applies to the decompressed protobuf payload, excluding the message-type byte and transport framing. The existing handler checks parsed getSerializedSize(); the new raw-size limit also rejects oversized noncanonical encodings that normalize to a smaller parsed message. This stricter admission behavior is covered by a regression test.

TRXS receives only a top-level transaction-count precheck and retains the existing transport frame-size limit. These checks address outer transaction-list amplification; nested transaction and unknown-field structure budgets are outside this scope.

Unsupported P2P TRX messages are rejected in the private factory dispatch after the existing payload slice, before transaction decoding. Direct TransactionMessage construction for API and internal callers remains available.

Different peer fetch limits

Nodes do not need identical MAX_TRX_FETCH_PER_PEER values. Under the current per-request response flow, different values alone do not cause a disconnect: each requester limits its own fetch batch and accepts up to that same local limit, while the responder returns only transactions from that request, possibly split into smaller TRXS messages. The responder's own fetch limit does not cap its outgoing responses.

For example, A with a limit of 2,000 can request 2,000 transactions from B with a limit of 1,000 and accept B's responses. In the reverse direction, B requests at most 1,000 from A and receives at most 1,000 per response. This reasoning relies on the current behavior of replying per request without merging responses across fetch requests.

BLOCK precheck cost

The precheck reads top-level field tags and lengths, counts transaction entries and skips their payloads. It does not parse transaction contents or create per-transaction protobuf/capsule objects. Cost therefore depends mainly on field count, length encoding and cache layout.

In a warmed local microbenchmark on macOS ARM64 / OpenJDK 17.0.19 with three independent JVMs, the slowest tested scenario (8,500 synthetic transactions, approximately 1.90 MB) had a precheck median of approximately 75 µs per block. The highest observed batch-average was approximately 80 µs per block. For the same scenario, parsing plus capsule construction changed from approximately 1.91 ms to 2.19 ms across JVM medians, with per-JVM increases of 14–24%.

These are synthetic microbenchmark observations, not a worst-case latency bound or a production throughput estimate. Measurements exclude protobuf-filter work, networking, signature verification and database operations.

The timing figures above cover BLOCK precheck only.

Reject decompressed BLOCK payloads above the existing block size limit
before parsing. Scan top-level transaction fields without allocating
message objects and bound their count using the existing 80-byte minimum
transaction size to prevent excessive allocations before rejection.

Share the limits with existing validation and cover size/count boundaries,
opaque bytes, unknown groups and malformed wire data in regression tests.
Reject inbound TRXS messages above MAX_TRX_FETCH_PER_PEER transaction
entries before protobuf parsing. Count only top-level transaction fields
and skip their contents without constructing messages.

Keep EOF validation in the precheck and add regression coverage for count
boundaries, rejection before parsing, opaque bytes, unknown groups, wrong
wire types and malformed input.
Reject TRX messages in the P2P message factory before copying payload bytes
or constructing TransactionMessage. Preserve the NO_SUCH_MESSAGE category
already used by the dispatcher for this unsupported message type.

Cover early rejection of canonical, malformed and near-frame-limit inputs,
the P2P disconnect reason, supported message decoding and direct
TransactionMessage construction for API and internal callers.
@github-actions
github-actions Bot requested a review from xxo1shine November 19, 2026 07:25
Let unsupported TRX messages fall through the private factory dispatch
instead of handling them specially in the public entry point.

Continue rejecting before TransactionMessage construction or transaction
parsing, and align regression tests with that guarantee rather than
requiring rejection before the payload copy.
@317787106 317787106 changed the title fix(net): precheck BLOCK and TRXS messages before parsing fix(net): precheck BLOCK/TRXS and reject unsupported TRX Nov 19, 2026
@halibobo1205 halibobo1205 added this to the GreatVoyage-v4.8.3 milestone Nov 19, 2026

@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.

Thanks for this. Overall the change looks correct to me, and I don't see it affecting honest traffic. I also ran the network tests on the PR head locally, and they pass.

  • BLOCK: the size limit is the one the handler already applied, now enforced before parsing, and the transaction-count limit sits above anything a valid block can contain. This removes most of the cost a peer could cause on this path before its block is rejected.
  • TRX: nothing handled it before, so rejecting it before decoding keeps the same outcome without the parse.
  • TRXS: the 1,000-entry limit matches the most a node asks for in one fetch, so honest replies should not be affected, including those from older versions.

My one concern is how much the TRXS check covers. It limits the number of top-level entries but not what each entry carries, so a frame within the limit can still cost about as much to parse as before. The description lists nested budgets as out of scope, while "Follow up: None" reads as if nothing is left on this path. As far as I can see, the remaining cost can be bounded before parsing without touching honest traffic: reject TRXS from a peer the node has no outstanding transaction request to, limit the frame size to what honest replies use, and give each entry a size and structure budget. Whether that goes into this PR or a follow-up is the team's call, but noting it in the description would make the remaining scope clear.

On BLOCK, the size limit now bounds the work, though nested content within that limit is still parsed before the request check.

@317787106

Copy link
Copy Markdown
Collaborator Author

@SeriousCoding789 Thanks for the review. We agree that the top-level TRXS count check does not bound all nested parsing work. The current checks provide early rejection of oversized BLOCK payloads, excessive transaction counts, and unsupported TRX messages, while retaining the existing transport frame-size limit.

Deeper TRXS content inspection would add CPU work to normal message processing, and tighter size or structure limits would require separate compatibility validation. Given the existing constraints, the incremental benefit of those restrictions has not yet been established. We prefer to keep this PR focused and revisit additional hardening when profiling or operational evidence justifies it.

I’ve updated Follow up to make this deferral explicit and acknowledge the remaining nested parsing costs.

@SeriousCoding789

Copy link
Copy Markdown
Contributor

@SeriousCoding789 Thanks for the review. We agree that the top-level TRXS count check does not bound all nested parsing work. The current checks provide early rejection of oversized BLOCK payloads, excessive transaction counts, and unsupported TRX messages, while retaining the existing transport frame-size limit.

Deeper TRXS content inspection would add CPU work to normal message processing, and tighter size or structure limits would require separate compatibility validation. Given the existing constraints, the incremental benefit of those restrictions has not yet been established. We prefer to keep this PR focused and revisit additional hardening when profiling or operational evidence justifies it.

I’ve updated Follow up to make this deferral explicit and acknowledge the remaining nested parsing costs.

Thanks for updating the Follow up. I agree the per-entry structure budget can wait: it is the one check that reads every entry, and its limit needs care so that no valid transaction falls outside it.

The other two checks don't carry those costs, though:

  • Request check. Before parsing, reject a TRXS frame from a peer the node has no outstanding transaction request to. This is a lookup on state the node already keeps, with no content read. It doesn't change what honest peers can send: they send TRXS only in reply to a fetch, and an unrequested transaction is already rejected, with a ban, after the parse. The check moves that existing decision ahead of the parse.
  • Frame size limit. Reject a TRXS payload above a little over 1.5 MB, which is a length comparison. The bound comes from the responder's own batching: it starts a new frame once a batch passes 1,000,000 bytes, and a single transaction is at most 500 KB. That batching code is unchanged from 4.7.0 through the current release branch, so checking compatibility means reading one code path rather than a new round of validation.

On the benefit: in my local runs, a frame that stays under the entry cap can cost more to parse than the frames the cap rejects, so on TRXS the count check alone does not lower the worst case. With the two checks above, only peers the node is fetching from could send TRXS, and the largest frame would drop from 5 MiB to about 1.5 MB.

Both are a few lines on the receive path and leave honest traffic unchanged, so they seem worth including in this PR, with the per-entry budget left for the follow-up.

@317787106

317787106 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

@SeriousCoding789

  1. For the request-state check, we agree that it is inexpensive and could avoid parsing unsolicited TRXS messages. Our concern is where it belongs: this is peer-state-dependent business validation, while TransactionsMessage.precheck() performs stateless wire-format checks.

    There is currently no shared hook for business validation before message construction. Adding a TRXS-specific check directly to the dispatcher would spread request-validation logic across the dispatcher and handler, while the existing handler runs after parsing. We prefer to address this separately with an appropriate placement for the early check, keeping the current PR focused on wire-format limits.

    If needed, wo will fix it in another issue, according with other Messages.

  2. For the frame-size limit, the comparison itself is inexpensive, but we cannot currently establish a safe upper bound. The 1,000,000-byte batching threshold is checked after adding a transaction, so it is not a hard frame limit. A safe cap also depends on the maximum serialized transaction size across cache and stored-block response paths, plus protobuf encoding overhead.

    The proposed ~1.5 MB bound relies on the 500 KiB transaction-size limit. However, in the relevant validation path, that limit is checked after redundant ret entries have been removed. It therefore does not establish the same bound for the transaction bytes carried in a TRXS payload.

    A length-only precheck cannot remove those entries without parsing the transaction contents. Consequently, we cannot safely derive the proposed frame-size cap from these limits and prefer to retain the existing transport frame-size limit in this PR.

    Without that bound, we cannot guarantee that the proposed ~1.5 MB limit preserves legitimate responses. We therefore prefer to retain the existing transport frame-size limit in this PR.

@317787106
317787106 requested a review from 3for November 20, 2026 07:20
@kuny0707
kuny0707 merged commit 2199f45 into tronprotocol:release_v4.8.3 Nov 23, 2026
10 checks passed
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.

6 participants