tronprotocol / tronprotocol/libp2p
Discard unknown protobuf fields during message parsing
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 55
- Forks
- 35
- Avg merge
- 6d 16h
- Merged PRs (30d)
- 2
Description
Summary
- libp2p control and node-discovery messages currently retain unknown protobuf fields during parsing. These fields are not used by existing handlers but remain attached to the parsed message objects.
- Discarding unknown fields during parsing would reduce unnecessary allocation and retained memory, making message-processing costs more predictable.
Problem
Motivation
The current handlers do not use protobuf unknown fields. Retaining them increases the size of parsed message objects and may add unnecessary serialization or processing overhead.
A shared parsing policy would keep libp2p message objects lightweight and provide consistent behavior across message types.
Current State
The following libp2p messages are parsed using generated protobuf parseFrom(byte[]) methods:
-
Connection messages:
HelloMessagePingMessagePongMessageStatusMessageP2pDisconnectMessage
-
Node-discovery messages:
PingMessagePongMessageFindNodeMessageNeighborsMessage
By default, protobuf stores unrecognized fields in each message's UnknownFieldSet. These fields are not consumed by the current libp2p handlers.
Application-layer messages forwarded through P2pEventHandler.onMessage, including java-tron transaction, block, inventory, and synchronization messages, are outside the scope of this change.
Limitations and Risks
- Unused fields increase the memory footprint of parsed messages.
- Large or numerous unknown fields may cause unnecessary allocations.
- Retained fields may add overhead if parsed messages are subsequently serialized.
- Parsing behavior is currently repeated across individual message constructors.
Proposed Solution
Discard protobuf unknown fields when parsing libp2p-owned connection and node-discovery messages.
Proposed Design
- Introduce a shared protobuf parsing helper that configures
CodedInputStreamto discard unknown fields. - Use the helper in the applicable connection and discovery message constructors.
- Continue validating and processing all known fields normally.
- Keep application payloads delivered through
P2pEventHandler.onMessageunchanged.
The parser must still read and skip unknown fields to validate the protobuf wire format, but it should not retain them in the resulting message object.
Key Changes
-
Add a shared unknown-field-discarding protobuf parser.
-
Apply it to libp2p control and discovery messages.
-
Add tests verifying that:
- known fields are preserved;
- unknown fields are absent after parsing;
- malformed protobuf input is still rejected;
- application-layer payload delivery remains unchanged.
Impact
- Memory usage: Reduces unnecessary data retained by parsed messages.
- Performance: Avoids allocating and storing unused unknown fields.
- Predictability: Bounds the retained parsed representation more closely to the defined protocol fields.
- Maintainability: Centralizes protobuf parsing behavior across libp2p message types.
Compatibility
- Breaking change: No change to currently defined fields or their interpretation.
- Default behavior change: Yes. Unknown fields in libp2p-owned protobuf messages will be discarded instead of retained.
- Migration required: No.
- Application payloads: Unchanged.
Older nodes will continue to accept messages containing newer fields, but they will no longer preserve those fields in parsed libp2p message objects.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the constructors and parseFrom(byte[]) calls for the listed HelloMessage, PingMessage, PongMessage, StatusMessage, P2pDisconnectMessage, FindNodeMessage, and NeighborsMessage classes. Start by tracing their current protobuf parsing and the P2pEventHandler.onMessage path. Done means known fields still parse, unknown fields are discarded, malformed input is rejected, and application-layer payload delivery remains unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- networking
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100