Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Below is the complete implementation and testing details for addressing mimblewimble/grin #2825, along with a Pull Request (PR) description that adheres to the provided PR checklist for the Grin project. The issue, opened by @antiochp on May 14, 2019, concerns the 4x limit on P2P message lengths in
grin/p2p/src/msg.rs
(lines 278–281, commite56cd55
). The current implementation allows messages to be up to four times the defined maximum size (max_msg_size
) to provide flexibility during message format stabilization. However, this has led to uncertainty about the correctness of the base (1x) limits. The proposed solution is to add logging to track message sizes relative to their 1x and 4x limits, enabling data collection before enforcing stricter limits.The solution adds logging to
Message::read
, includes comprehensive tests, and updates documentation, ensuring no consensus-breaking changes or impact on existing functionality. Below, I provide:Full Code to Fix the Issue
The solution modifies
grin/p2p/src/msg.rs
to log message sizes and warn when they exceed the 1x limit, using thelog
crate. It maintains the existing 4x limit and ensures compatibility with Grin’s P2P protocol.1. Update P2P Message Handling
Add logging to the
Message::read
function.File:
p2p/src/msg.rs
2. Ensure Logging Dependency
Verify that logging dependencies are included and initialized.
File:
Cargo.toml
File:
src/main.rs
3. Add Tests for Message Size Logging
Create unit and integration tests to verify logging and message size validation.
File:
p2p/tests/msg_tests.rs
File:
p2p/tests/integration_tests.rs
4. Update Documentation
Document the logging behavior for P2P message sizes.
File:
doc/p2p/p2p_protocol.md
INFO: Received Block message: size=1048676 bytes, 1x limit=1048576 bytes, 4x limit=4194304 bytes
WARN: Message size (1048676 bytes) exceeds 1x limit (1048576 bytes) for type Block
Testing Instructions
1. Unit Tests
Verify logging and error handling for message sizes.
File:
p2p/tests/msg_tests.rs
test_message_size_logging
: Tests that messages within the 4x limit are logged correctly, with warnings for exceeding the 1x limit.test_message_too_large
: Ensures messages exceeding the 4x limit are rejected withError::MsgTooLarge
.Run tests:
cargo test --package grin_p2p --test msg_tests
2. Integration Tests
Test logging during P2P communication.
File:
p2p/tests/integration_tests.rs
test_p2p_message_size_logging
: Simulates a peer sending aBlock
message that exceeds the 1x limit but is within the 4x limit, verifying logging.Run integration tests:
cargo test --package grin_p2p --test integration_tests
3. Manual Testing
Clone and Build:
Cargo.toml
,src/main.rs
,p2p/src/msg.rs
,doc/p2p/p2p_protocol.md
, and test files.Run Grin Node:
grin-server.toml
:Generate P2P Traffic:
Transaction
orBlock
messages:Monitor Logs:
tail -f ~/.grin/floonet/grin.log
Test Edge Cases:
Block
message (>4MB) usingnetcat
:grin.log
.Headers
,TxHashSetArchive
) to ensure consistent logging.Cross-Platform Testing:
ulimit -n 10000
to avoid file descriptor limits), and Windows.Pull Request Description
Title: Add Logging for P2P Message Sizes to Address 4x Limit (#2825)
Labels: enhancement
Assignees: None
Description:
This pull request addresses Issue #2825, opened by @antiochp on May 14, 2019, concerning the 4x limit on P2P message lengths in
grin/p2p/src/msg.rs
(lines 278–281, commite56cd55
). The 4x limit, implemented to provide flexibility during message format stabilization, allows messages to exceed the definedmax_msg_size
by up to four times. This has led to uncertainty about the correctness of the base (1x) limits (e.g., 1MB forBlock
, 100MB forTxHashSetArchive
), as they have not been strictly enforced or updated. The proposed solution adds logging to track message sizes relative to their 1x and 4x limits, enabling data collection to validate limits before introducing stricter constraints.What Problems Does the PR Address?
The 4x limit obscures whether the 1x limits in
max_msg_size
are appropriately set, potentially allowing oversized messages or inefficient validation. This PR adds logging toMessage::read
to report message sizes, including type, actual size, 1x limit, and 4x limit, with warnings when the 1x limit is exceeded. This provides visibility into message size distributions, facilitating future limit adjustments without altering current behavior.Detailed Explanation of Changes
p2p/src/msg.rs
to log message sizes inMessage::read
usinglog::info
for each message andlog::warn
when the 1x limit is exceeded but within the 4x limit.log = "0.4"
andenv_logger = "0.11"
are included inCargo.toml
, with logging initialized insrc/main.rs
.p2p/tests/msg_tests.rs
to verify logging and error handling for oversized messages. Added integration tests inp2p/tests/integration_tests.rs
to confirm logging during P2P communication.doc/p2p/p2p_protocol.md
to document the logging behavior and example log output.