p2p

package
v0.0.0-...-ff2c966 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: AGPL-3.0 Imports: 25 Imported by: 0

README

P2P

This package provides an interface for peer-to-peer networking features, such as peer identification, addressing, connectivity, messaging, custom protocol streams, and peer discovery. These features are implemented using libp2p, and this package and its sub-packages serve as the main integration point with libp2p.

The code in this package is part of an ongoing initiative to research and implement an alternative networking stack that supports decentralized network topologies, while benefiting from the history and experience of libp2p's protocol development.

The current status of this initiative is purely experimental, and is not enabled by default or recommended for regular use. The components implemented here offer no guarantee of security, reliability, or performance. Updates will be provided on the progress and readiness of these components for standard usage in the future.

Background

Decentralized networks depend on mechanisms for discovering other peers and reliably broadcasting messages to other interested peers. Libp2p provides pluggable transports and multiplexing to allow multiple stream-based protocols to share the same connection. Libp2p also provides an implementation of a message-based gossip protocol, GossipSub.

Algorand's current network protocol sends messages between peers over bidirectional WebSocket connections. Nodes that are configured to enable message-forwarding (including nodes currently called "relays") validate incoming messages, then selectively forward messages to other connected peers. This network implementation (WebsocketNetwork) sits behind the GossipNode interface in the network package.

Current P2P implementation details

This package supports a P2PNetwork implementation of the GossipNode interface, providing the same peer connection management and message broadcast functions, but via peer connections managed by libp2p. The P2PNetwork implementation uses libp2p's multiaddress scheme and peer IDs to establish connections and identify peers.

Currently transactions (protocol tag TX) are distributed using the GossipSub protocol, while all other messages are forwarded over a custom message protocol /algorand-ws/1.0.0 that uses the same message serialization as the existing WebsocketNetwork implementation. These two protocols are multiplexed over a single connection using libp2p streams.

graph LR
    subgraph "P2P Implementation"
        P2P[P2PNetwork]
        TX[Transactions]
        AW[Other messages]
    end
    P2P --> TX
    P2P --> AW

    subgraph "libp2p"
        G["GossipSub 1.1<br/>/meshsub/1.1.0"]
        WS["Legacy message serialization<br/>/algorand-ws/1.0.0"]
        S["Multiplexer<br/>/yamux/1.0.0"]
        T["TCP Transport"]
    end
    G --> S
    WS --> S
    TX --> G
    AW --> WS
    S --> T

Documentation

Index

Constants

View Source
const AlgorandWsProtocol = "/algorand-ws/1.0.0"

AlgorandWsProtocol defines a libp2p protocol name for algorand's websockets messages

View Source
const DefaultPrivKeyPath = "peerIDPrivKey.pem"

DefaultPrivKeyPath is the default path inside the node's root directory at which the private key for p2p identity is found and persisted to when a new one is generated.

View Source
const TXTopicName = "/algo/tx/0.1.0"

TXTopicName defines a pubsub topic for TX messages

Variables

This section is empty.

Functions

func GetPrivKey

func GetPrivKey(cfg config.Local, dataDir string) (crypto.PrivKey, error)

GetPrivKey manages loading and creation of private keys for network PeerIDs It prioritizes, in this order:

  1. user supplied path to privKey
  2. default path to privKey,
  3. generating a new privKey.

If a new privKey is generated it will be saved to default path if cfg.P2PPersistPeerID.

func MakeService

func MakeService(ctx context.Context, log logging.Logger, cfg config.Local, datadir string, pstore peerstore.Peerstore, wsStreamHandler StreamHandler) (*serviceImpl, error)

MakeService creates a P2P service instance

Types

type PeerID

type PeerID string

PeerID is a string representation of a peer's public key, primarily used to avoid importing libp2p into packages that shouldn't need it

func PeerIDFromPublicKey

func PeerIDFromPublicKey(pubKey crypto.PubKey) (PeerID, error)

PeerIDFromPublicKey returns a PeerID from a public key, thin wrapper over libp2p function doing the same

type Service

type Service interface {
	Close() error
	ID() peer.ID             // return peer.ID for self
	AddrInfo() peer.AddrInfo // return addrInfo for self

	DialNode(context.Context, *peer.AddrInfo) error
	DialPeersUntilTargetCount(targetConnCount int)
	ClosePeer(peer.ID) error

	Conns() []network.Conn
	ListPeersForTopic(topic string) []peer.ID
	Subscribe(topic string, val pubsub.ValidatorEx) (*pubsub.Subscription, error)
	Publish(ctx context.Context, topic string, data []byte) error
}

Service defines the interface used by the network integrating with underlying p2p implementation

type StreamHandler

type StreamHandler func(ctx context.Context, pid peer.ID, s network.Stream, incoming bool)

StreamHandler is called when a new bidirectional stream for a given protocol and peer is opened.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL