Convert Figma logo to code with AI

libp2p logogo-libp2p

libp2p implementation in Go

5,973
1,050
5,973
252

Top Related Projects

16,006

An IPFS implementation in Go

Go implementation of the Ethereum protocol

The Rust Implementation of the libp2p networking stack.

Dendrite is a second-generation Matrix homeserver written in Go!

63,682

Open Source Continuous File Synchronization

æternity blockchain - scalable blockchain for the people - smart contracts, state channels, names, tokens

Quick Overview

go-libp2p is a modular peer-to-peer networking stack implemented in Go. It provides a set of protocols, specifications, and libraries to build decentralized applications and systems. go-libp2p is part of the larger libp2p project, which aims to create a universal networking layer for distributed systems.

Pros

  • Modular and extensible architecture, allowing developers to customize and combine different components
  • Cross-platform compatibility, supporting various operating systems and network environments
  • Strong focus on security, with built-in encryption and authentication mechanisms
  • Active development and community support, with regular updates and improvements

Cons

  • Steep learning curve for developers new to peer-to-peer networking concepts
  • Documentation can be fragmented or outdated in some areas
  • Performance overhead compared to more specialized networking solutions
  • Complexity in managing peer connections and network topologies in large-scale deployments

Code Examples

  1. Creating a basic libp2p host:
import (
    "context"
    "fmt"
    "github.com/libp2p/go-libp2p"
)

func main() {
    ctx := context.Background()
    host, err := libp2p.New(ctx)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Host ID: %s\n", host.ID())
    fmt.Printf("Host Addrs: %s\n", host.Addrs())
}
  1. Implementing a simple echo protocol:
import (
    "bufio"
    "fmt"
    "github.com/libp2p/go-libp2p/core/network"
)

func echoHandler(stream network.Stream) {
    buf := bufio.NewReader(stream)
    str, err := buf.ReadString('\n')
    if err != nil {
        stream.Reset()
        return
    }
    fmt.Fprintf(stream, "Echo: %s", str)
    stream.Close()
}

// In main function:
host.SetStreamHandler("/echo/1.0.0", echoHandler)
  1. Connecting to a peer and sending data:
import (
    "context"
    "fmt"
    "github.com/libp2p/go-libp2p/core/peer"
    "github.com/multiformats/go-multiaddr"
)

func connectAndSend(host libp2p.Host, peerAddr string, data string) error {
    ctx := context.Background()
    maddr, err := multiaddr.NewMultiaddr(peerAddr)
    if err != nil {
        return err
    }
    peerInfo, err := peer.AddrInfoFromP2pAddr(maddr)
    if err != nil {
        return err
    }
    if err := host.Connect(ctx, *peerInfo); err != nil {
        return err
    }
    stream, err := host.NewStream(ctx, peerInfo.ID, "/echo/1.0.0")
    if err != nil {
        return err
    }
    _, err = fmt.Fprintf(stream, "%s\n", data)
    return err
}

Getting Started

To start using go-libp2p in your Go project:

  1. Install Go (version 1.16 or later)
  2. Initialize a new Go module:
    go mod init myproject
    
  3. Add go-libp2p as a dependency:
    go get github.com/libp2p/go-libp2p
    
  4. Import and use go-libp2p in your code:
    import "github.com/libp2p/go-libp2p"
    
  5. Run your project:
    go run .
    

For more detailed examples and documentation, refer to the official go-libp2p GitHub repository and the libp2p website.

Competitor Comparisons

16,006

An IPFS implementation in Go

Pros of Kubo

  • Full IPFS implementation with extensive features and functionality
  • Includes a complete command-line interface for interacting with IPFS
  • Offers a more user-friendly experience for those wanting to run an IPFS node

Cons of Kubo

  • Larger codebase and potentially more complex to contribute to or modify
  • May include unnecessary components for projects only needing libp2p functionality
  • Potentially higher resource usage due to full IPFS implementation

Code Comparison

Kubo (IPFS node initialization):

cfg, err := config.Init(os.Stdout, 2048)
if err != nil {
    return err
}
node, err := core.NewNode(ctx, &core.BuildCfg{
    Online: true,
    Repo:   repo,
})

go-libp2p (libp2p node creation):

host, err := libp2p.New(
    libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"),
    libp2p.Identity(priv),
)
if err != nil {
    panic(err)
}

The code snippets demonstrate that Kubo focuses on IPFS node creation with more configuration options, while go-libp2p provides a more streamlined approach for creating a libp2p host.

Go implementation of the Ethereum protocol

Pros of go-ethereum

  • Comprehensive implementation of the Ethereum protocol
  • Extensive documentation and community support
  • Built-in command-line interface (geth) for easy interaction

Cons of go-ethereum

  • Larger codebase, potentially more complex to understand and contribute to
  • Focused specifically on Ethereum, less versatile for other blockchain projects
  • Heavier resource requirements for running a full node

Code Comparison

go-ethereum (consensus-related code):

func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
    header := block.Header()
    // Sealing the genesis block is not supported
    number := header.Number.Uint64()
    if number == 0 {
        return errUnknownBlock
    }
    // ...
}

go-libp2p (network-related code):

func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
    h.Mux().AddHandler(string(pid), func(p string, rwc io.ReadWriteCloser) error {
        return handler(rwc)
    })
    h.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
        Added: []protocol.ID{pid},
    })
}

The Rust Implementation of the libp2p networking stack.

Pros of rust-libp2p

  • Memory safety and performance benefits inherent to Rust
  • More modular architecture, allowing easier customization
  • Better support for async/await patterns

Cons of rust-libp2p

  • Smaller ecosystem and community compared to Go
  • Less mature, with potentially fewer features implemented
  • Steeper learning curve for developers new to Rust

Code Comparison

go-libp2p:

host, err := libp2p.New(
    libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"),
    libp2p.Identity(priv),
)

rust-libp2p:

let transport = libp2p::development_transport(local_key).await?;
let behaviour = MyBehaviour::default();
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);

The Go version provides a more straightforward setup with a single function call, while the Rust version offers more granular control over the transport and behavior components. The Rust implementation also leverages async/await syntax for better handling of asynchronous operations.

Both libraries aim to provide a modular and extensible networking stack for peer-to-peer applications. go-libp2p benefits from Go's simplicity and extensive standard library, while rust-libp2p leverages Rust's performance and safety features. The choice between them often depends on the project's requirements, the team's expertise, and the broader ecosystem compatibility.

Dendrite is a second-generation Matrix homeserver written in Go!

Pros of Dendrite

  • Specifically designed for Matrix protocol, offering tailored features for Matrix homeservers
  • Written in Go, providing good performance and concurrency support
  • Modular architecture allows for easier scaling and maintenance

Cons of Dendrite

  • More limited in scope compared to go-libp2p's general-purpose networking capabilities
  • Smaller community and ecosystem compared to libp2p's broader adoption
  • Less flexible for non-Matrix use cases

Code Comparison

Dendrite (Matrix-specific routing):

func (r *InternalAPIRouter) RouteMatrixMessage(ctx context.Context, req *api.PerformMatrixRequest) (*api.PerformMatrixResponse, error) {
    // Matrix-specific routing logic
}

go-libp2p (General-purpose networking):

func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
    h.Mux().AddHandler(pid, handler)
}

The code snippets highlight the difference in focus between the two projects. Dendrite's code is tailored for Matrix protocol operations, while go-libp2p provides more general-purpose networking primitives that can be used for various decentralized applications.

63,682

Open Source Continuous File Synchronization

Pros of Syncthing

  • Complete file synchronization solution out-of-the-box
  • User-friendly interface for easy setup and management
  • Strong focus on privacy and security with end-to-end encryption

Cons of Syncthing

  • Less flexible for building custom peer-to-peer applications
  • Limited to file synchronization use cases
  • Potentially higher resource usage for large-scale deployments

Code Comparison

Syncthing (Go):

func (m *Model) Index(deviceID protocol.DeviceID, folder string, files []protocol.FileInfo, flags uint32, options []protocol.Option) {
    // Implementation for indexing files
}

go-libp2p (Go):

func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler network.StreamHandler) {
    // Implementation for setting stream handlers
}

Syncthing is a complete file synchronization solution with a user-friendly interface, while go-libp2p is a modular networking stack for building peer-to-peer applications. Syncthing offers an out-of-the-box solution for file syncing, whereas go-libp2p provides more flexibility for custom p2p implementations. The code examples show Syncthing's focus on file operations and go-libp2p's emphasis on network communication primitives.

æternity blockchain - scalable blockchain for the people - smart contracts, state channels, names, tokens

Pros of Aeternity

  • Comprehensive blockchain platform with smart contracts and state channels
  • Built-in oracle system for integrating real-world data
  • Focuses on scalability and efficiency for decentralized applications

Cons of Aeternity

  • More complex and specialized compared to go-libp2p's modular approach
  • Potentially steeper learning curve for developers new to blockchain
  • Less flexibility for use in non-blockchain P2P applications

Code Comparison

Aeternity (Erlang):

-module(aec_peers).
-behaviour(gen_server).

-export([start_link/0, add/1, remove/1, get_random/1]).

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

go-libp2p (Go):

package libp2p

import (
    "context"
    "github.com/libp2p/go-libp2p-core/host"
)

func New(ctx context.Context, opts ...Option) (host.Host, error) {
    return NewWithoutDefaults(ctx, append(defaults, opts...)...)
}

The code snippets demonstrate the different languages and approaches used in each project. Aeternity uses Erlang and focuses on blockchain-specific modules, while go-libp2p uses Go and provides a more general-purpose P2P networking framework.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

libp2p hex logo

The Go implementation of the libp2p Networking Stack.

Go Reference

Table of Contents

Background

libp2p is a networking stack and library modularized out of The IPFS Project, and bundled separately for other tools to use.

libp2p is the product of a long, and arduous quest of understanding -- a deep dive into the internet's network stack, and plentiful peer-to-peer protocols from the past. Building large-scale peer-to-peer systems has been complex and difficult in the last 15 years, and libp2p is a way to fix that. It is a "network stack" -- a protocol suite -- that cleanly separates concerns, and enables sophisticated applications to only use the protocols they absolutely need, without giving up interoperability and upgradeability. libp2p grew out of IPFS, but it is built so that lots of people can use it, for lots of different projects.

To learn more, check out the following resources:

Usage

This repository (go-libp2p) serves as the entrypoint to the universe of packages that compose the Go implementation of the libp2p stack.

You can start using go-libp2p in your Go application simply by adding imports from our repos, e.g.:

import "github.com/libp2p/go-libp2p"

Examples

Examples can be found in the examples folder.

Dashboards

We provide prebuilt Grafana dashboards so that applications can better monitor libp2p in production. You can find the dashboard JSON files here.

We also have live Public Dashboards that you can check out to see real time monitoring in action.

Contribute

go-libp2p is MIT-licensed open source software. We welcome contributions big and small! Take a look at the community contributing notes. Please make sure to check the issues. Search the closed ones before reporting things, and help us with the open ones.

Guidelines:

  • read the libp2p spec
  • ask questions or talk about things in our discussion forums, or open an issue for bug reports, or #libp2p-implementers on Filecoin slack.
  • ensure you are able to contribute (no legal issues please -- we use the DCO)
  • get in touch with @libp2p/go-libp2p-maintainers about how best to contribute
  • have fun!

There's a few things you can do right now to help out:

  • Go through the modules below and check out existing issues. This would be especially useful for modules in active development. Some knowledge of IPFS/libp2p may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.
  • Perform code reviews.
  • Add tests. There can never be enough tests.

Supported Go Versions

We test against and support the two most recent major releases of Go. This is informed by Go's own security policy.

Notable Users

Some notable users of go-libp2p are:

  • Kubo - The original Go implementation of IPFS
  • Lotus - An implementation of the Filecoin protocol
  • Drand - A distributed random beacon daemon
  • Prysm - An Ethereum Beacon Chain consensus client built by Prysmatic Labs
  • Berty - An open, secure, offline-first, peer-to-peer and zero trust messaging app.
  • Wasp - A node that runs IOTA Smart Contracts built by the IOTA Foundation
  • Mina - A lightweight, constant-sized blockchain that runs zero-knowledge smart contracts
  • Polygon Edge - A modular, extensible framework for building Ethereum compatible networks
  • Celestia Node - The Go implementation of Celestia's data availability nodes
  • Status go - Status bindings for go-ethereum, built by Status.im
  • Flow - A blockchain built to support games, apps, and digital assets built by Dapper Labs
  • Swarm Bee - A client for connecting to the Swarm network
  • MultiversX Node - The Go implementation of the MultiversX network protocol
  • Sonr - A platform to integrate DID Documents, WebAuthn, and IPFS and manage digital identity and assets.
  • EdgeVPN - A decentralized, immutable, portable VPN and reverse proxy over p2p.
  • Kairos - A Kubernetes-focused, Cloud Native Linux meta-distribution.
  • Oasis Core - The consensus and runtime layers of the Oasis protocol.
  • Spacemesh - The Go implementation of the Spacemesh protocol, a novel layer one blockchain
  • Tau - Open source distributed Platform as a Service (PaaS)

Please open a pull request if you want your project (min. 250 GitHub stars) to be added here.