Skip to content

feat(p2p): add connection logging behaviour and global peer context#202

Merged
varex83 merged 62 commits into
mainfrom
bohdan/add-connection-logger
Feb 25, 2026
Merged

feat(p2p): add connection logging behaviour and global peer context#202
varex83 merged 62 commits into
mainfrom
bohdan/add-connection-logger

Conversation

@varex83
Copy link
Copy Markdown
Collaborator

@varex83 varex83 commented Feb 3, 2026

This PR implements connection logging behaviour

Summary

This PR introduces connection logging functionality to the P2P module, porting the connection logging behaviour from Charon Go. The implementation tracks and logs libp2p connection lifecycle events while exposing connection metrics.

Key Changes

  • Connection Logger Behaviour (conn_logger.rs): A new NetworkBehaviour implementation that:

    • Tracks inbound/outbound connections per peer with connection type (direct/relay) and protocol (TCP/QUIC) granularity
    • Emits structured tracing logs for connection establishment and closure events
    • Exposes metrics via a pluggable ConnectionLoggerMetrics trait with default and test implementations
    • Includes comprehensive unit tests for connection counting logic
    • Add ping latencies and other metrics.
  • Global Context (global_context.rs): Introduces a shared GlobalContext with a thread-safe PeerStore for tracking active/inactive peers across the P2P stack

  • Optional Behaviour Wrapper (optional.rs): A generic OptionalBehaviour<B> wrapper that allows behaviours to be conditionally enabled, simplifying behaviour composition

  • Pluto Behaviour Refactoring:

    • Simplified PlutoBehaviour to use OptionalBehaviour for the inner behaviour
    • Removed custom dummy connection handler in favour of libp2p's built-in implementation
    • Added GlobalContext integration to the behaviour builder
  • Node API Improvements:

    • Node now implements Stream for ergonomic event handling
    • Added GlobalContext propagation through node creation
    • Closure signatures updated to receive GlobalContext for inner behaviour construction
  • Utility Additions: Helper functions for detecting relay addresses (is_relay_addr) and QUIC addresses (is_quic_addr), plus connection type/protocol classification

Test Plan

  • Verify connection logger metrics are correctly incremented/decremented during peer connections
  • Run the p2p.rs example with multiple nodes to validate mDNS discovery and relay connections
  • Confirm structured logs appear with correct peer names and connection metadata
  • Review metrics output via the metrics endpoint
image

Base automatically changed from bohdan/finish-p2p to main February 15, 2026 15:44
@varex83 varex83 marked this pull request as draft February 16, 2026 19:01
@varex83 varex83 mentioned this pull request Feb 18, 2026
@varex83 varex83 changed the title feat: add connection logger feat(p2p): add connection logging behaviour and global peer context Feb 19, 2026
@varex83 varex83 marked this pull request as ready for review February 19, 2026 15:18
Comment thread crates/p2p/src/global_context.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/utils.rs
Copy link
Copy Markdown
Collaborator

@emlautarom1 emlautarom1 left a comment

Choose a reason for hiding this comment

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

There are multiple points in this PR:

  • The move to Stream for p2p behaviors.
  • The refactoring of how we construct nodes and relay servers
  • Connection logging

Overall looks good, but it would have been better to have it as stacked PRs.

The only issue that I see is on the refactoring where the code for constructing nodes and relays is quite convoluted and could use some improvements.

Some tests seem excessive (most likely AI generating for the sake of it) and I expect that we can trim them down with some minor changes.

Comment thread crates/p2p/src/utils.rs Outdated
Comment thread crates/p2p/src/global_context.rs Outdated
Comment thread crates/p2p/src/global_context.rs Outdated
Comment thread crates/p2p/src/p2p.rs
Comment thread crates/p2p/src/p2p.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/global_context.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs
Comment thread crates/p2p/src/conn_logger.rs Outdated
Comment thread crates/p2p/src/conn_logger.rs Outdated
Copy link
Copy Markdown
Collaborator

@emlautarom1 emlautarom1 left a comment

Choose a reason for hiding this comment

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

LGTM. We'll revisit the constructor code later, for now no need to worry about it.

Copy link
Copy Markdown
Collaborator

@iamquang95 iamquang95 left a comment

Choose a reason for hiding this comment

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

LGTM!

@varex83 varex83 merged commit 9940cbc into main Feb 25, 2026
5 checks passed
@varex83 varex83 deleted the bohdan/add-connection-logger branch February 25, 2026 10:39
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