Skip to content

Small fixes#164

Merged
herr-seppia merged 4 commits intomainfrom
small-fixes
Mar 5, 2026
Merged

Small fixes#164
herr-seppia merged 4 commits intomainfrom
small-fixes

Conversation

@herr-seppia
Copy link
Member

Fixed

  • Reject messages from invalid peers (P1.10-3)

Changed

  • Limit the number of peers accepted in a single Nodes message (P1.10-4)
  • Increase the POW produced during ID generation (P1.10-2)

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR applies small protocol-safety and robustness fixes in the message handling and routing-table code, and updates the PoW parameters used during local peer ID generation.

Changes:

  • Drop inbound messages whose header doesn’t match the sender IP (invalid peer IDs).
  • Cap how many peers are processed from a single Nodes message.
  • Increase the PoW difficulty used when generating local BinaryIDs; plus minor lifetime-elision cleanups and a test adjustment.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/transport/encoding/raptorq.rs Minor lifetime elision cleanup on ChunkedPayload impl block.
src/lib.rs Raises K_DIFF_PRODUCED_BIT from 8 to 20 (affects local ID generation PoW cost).
src/kbucket/bucket.rs Lifetime elision cleanup on NodeInsertOk impl; adjusts LRU/TTL unit test to pre-generate nodes.
src/handling.rs Rejects invalid peer headers by skipping processing; limits peers processed from Nodes messages.
CHANGELOG.md Documents the fixes/changes under Unreleased.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const K_NONCE_LEN: usize = 4;
const K_DIFF_MIN_BIT: usize = 8;
const K_DIFF_PRODUCED_BIT: usize = 8;
const K_DIFF_PRODUCED_BIT: usize = 20;
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Raising K_DIFF_PRODUCED_BIT to 20 significantly increases the work done in BinaryID::generate (expected ~2^20 hash iterations per PeerNode::generate). This will substantially slow node startup and is also likely to make existing unit tests prohibitively slow (e.g., src/kbucket/key.rs has tests generating hundreds of PeerNodes). If the intent is to increase the required PoW for network participation, this constant alone won’t achieve that because verify_nonce() still checks K_DIFF_MIN_BIT; otherwise consider making the produced difficulty configurable and/or lowering it under cfg(test)/debug builds to keep test and startup times reasonable.

Copilot uses AI. Check for mistakes.
Comment on lines +328 to +345
// Create all the nodes at the beginning to ensure that PoW is not a
// factor in the test timing.
let root = PeerNode::generate("127.0.0.1:666", 0)?;
let node1 = PeerNode::generate("192.168.1.1:8080", 0)?;
let node1_copy = PeerNode::generate("192.168.1.1:8080", 0)?;
let node1_copy2 = PeerNode::generate("192.168.1.1:8080", 0)?;
let node2 = PeerNode::generate("192.168.1.2:8080", 0)?;

let mut additionals = vec![];
for i in 2..21 {
additionals.push(PeerNode::generate(
&format!("192.168.1.{}:8080", i)[..],
0,
)?);
}

let pending = PeerNode::generate("192.168.1.21:8080", 0)?;
let pending_2 = PeerNode::generate("192.168.1.21:8080", 0)?;
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

Pre-generating PeerNodes here sets seen_at at generation time (Node::new uses Instant::now()), so if PeerNode::generate becomes slow (e.g., due to the increased PoW difficulty) nodes may already be close to (or past) node_ttl by the time they’re inserted. That can change the behavior of this TTL-sensitive test and make it flaky. To keep the PoW work out of the timed portion without aging the nodes, consider precomputing the BinaryIDs up front and constructing PeerNodes via PeerNode::from_socket right before insertion (so seen_at is set at insertion time).

Copilot uses AI. Check for mistakes.
Comment on lines 278 to 286
let messages: Vec<_> = peers
.iter()
//filter out my ID to avoid loopback
.filter(|&n| &n.id != self.my_header.binary_id().as_binary())
// Limit the number of peers accepted in a single Nodes message to a
// reasonable bound
.take(K_K * 2)
.filter(|&n| {
let h = self.my_header.binary_id().calculate_distance(&n.id);
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

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

The .take(K_K * 2) is applied before the filter that checks bucket fullness / has_peer(). As a result, many of the first K_K*2 entries could be rejected and you may end up accepting far fewer peers than the intended bound, while also ignoring later entries that would have passed the filter. If the goal is to cap the number of accepted peers from a Nodes message, move the take after the acceptance filter; if the goal is to cap the amount of work per message, consider renaming/commenting accordingly to avoid the semantic mismatch with “accepted”.

Copilot uses AI. Check for mistakes.
@herr-seppia herr-seppia merged commit eb9ac1d into main Mar 5, 2026
9 checks passed
@herr-seppia herr-seppia deleted the small-fixes branch March 5, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants