canopy

P2P Package

The P2P package provides a robust peer-to-peer networking layer for the Canopy Network. It implements a secure, multiplexed, and encrypted communication system between network nodes.

Overview

The P2P package is designed to handle:

Core Components

P2P

The main entry point for the P2P system. It manages the overall P2P network functionality, including:

PeerSet

Manages the active set of connected peers. It provides:

PeerBook

Maintains a persistent list of potential peers. It provides:

MultiConn

Represents a multiplexed connection to a peer. It provides:

EncryptedConn

Handles the encrypted communication with peers. It implements:

Stream

Represents a single communication channel within a MultiConn. It provides:

Sequence Diagram

The following sequence diagram illustrates the core interactions in the P2P package:

sequenceDiagram
    participant App as Application
    participant P2P as P2P
    participant PS as PeerSet
    participant PB as PeerBook
    participant Peer as Remote Peer

    %% Connection flow
    Note over P2P,Peer: New peer connects
    Peer->>P2P: Connection request
    P2P->>P2P: Security handshake
    P2P->>PS: Add to active peers
    
    %% Message flow
    App->>P2P: Send message
    P2P->>PS: Forward to peer
    PS->>Peer: Send encrypted message
    
    Peer->>P2P: Receive message
    P2P->>App: Deliver to application
    
    %% Gossip protocol
    App->>P2P: Broadcast message
    P2P->>PS: Send to all peers except sender
    PS->>Peer: Forward message
    
    %% Peer discovery
    P2P->>P2P: Request peer list
    P2P->>PS: Send request to peers
    PS->>Peer: Forward request
    
    Peer->>P2P: Send peer list
    P2P->>PB: Update peer book

Technical Details

Multiplexing

The P2P system uses multiplexing to allow multiple independent communication channels over a single TCP connection. This is achieved by:

This approach is similar to how a single phone line can carry multiple conversations using different frequencies. By multiplexing, the system can efficiently handle different types of messages without needing separate connections for each type, reducing overhead and improving performance.

It’s important to note that this multiplexing is implemented entirely in our application code logic, not relying on transport-level multiplexing like HTTP/2. This gives us complete control over the multiplexing behavior and allows us to optimize it specifically for our blockchain network requirements.

Encryption Protocol

The P2P system uses a robust encryption protocol to secure all communications:

  1. Key Exchange: When two peers connect, they use the X25519 elliptic curve Diffie-Hellman (ECDH) algorithm to establish a shared secret. This allows them to generate the same encryption keys without ever transmitting the keys themselves.

  2. Key Derivation: The shared secret is then processed using HMAC-based Key Derivation Function (HKDF) to derive:
    • An encryption key for sending messages
    • An encryption key for receiving messages
    • A nonce (number used once) for ensuring message uniqueness
  3. Message Encryption: All messages are encrypted using the ChaCha20-Poly1305 authenticated encryption algorithm, which:
    • Provides confidentiality (messages can’t be read by others)
    • Ensures authenticity (messages haven’t been tampered with)
    • Verifies integrity (messages arrive exactly as sent)

This encryption approach provides end-to-end security, meaning that even if someone intercepts the traffic between two nodes, they cannot read or modify the messages without knowing the encryption keys.

Component Interactions

1. Getting Started: P2P Initialization

When a node starts up, the P2P system initializes with these steps:

Think of this like setting up a phone system - you need a phone book (PeerBook), a list of active calls (PeerSet), and different lines for different types of conversations (channels).

2. Making Connections: Peer Connection Flow

When nodes connect to each other, several layers of security and functionality are added:

This is similar to how a phone call works - you dial a number (or receive a call), verify who’s on the other end, and then can have a secure conversation.

3. Sending Messages: Message Flow

When a node wants to send information to another node:

This is like sending a letter - you write your message, put it in an envelope (packet), seal it (encrypt it), and send it through the mail (TCP connection). The recipient opens the envelope and reads the message.

4. Discovering Peers: Peer Discovery and Management

The P2P system uses several techniques to discover and manage peers:

The gossip protocol is particularly interesting - it’s like how rumors spread in a social network. If you hear something interesting, you tell all your friends except the one who told you, and they tell all their friends except you, and soon many people know about it. This is an efficient way to disseminate information in a decentralized network without a central authority, ensuring that important updates reach all nodes quickly.

5. Security Measures

The P2P system implements several security techniques:

These measures work together to create a secure and resilient network that can operate even when some participants are malicious.

Security Features

Usage

To use the P2P package:

  1. Create a new P2P instance with appropriate configuration
  2. Start the P2P service
  3. Send and receive messages through the appropriate channels
  4. Stop the P2P service when done
// Example usage
p2p := p2p.New(privateKey, maxMembersPerCommittee, metrics, config, logger)
p2p.Start()
// Use p2p.Inbox(topic) to receive messages
// Use p2p.PeerSet.SendTo() to send messages
p2p.Stop()