libp2p

package
v0.15.4 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2023 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Overview

Package libp2p provides webmesh integration with libp2p.

Package libp2p provides webmesh integration with libp2p.

Index

Constants

View Source
const (
	// BootstrapProtocol is the protocol used for bootstrapping a mesh.
	BootstrapProtocol = protocol.ID("/webmesh/bootstrap/0.0.1")
	// RPCProtocol is the protocol used for executing RPCs against a mesh.
	// The method should be appended to the end of the protocol.
	RPCProtocol = protocol.ID("/webmesh/rpc/0.0.1")
	// RaftProtocol is the protocol used for webmesh raft.
	// This is not used yet.
	RaftProtocol = protocol.ID("/webmesh/raft/0.0.1")
	// UDPRelayProtocol is the protocol used for relaying UDP packets.
	// The destination node should be appended to the end of the protocol.
	UDPRelayProtocol = protocol.ID("/webmesh/udp-relay/0.0.1")
)
View Source
const MaxBuffer = 2500000

MaxBuffer is the maximum buffer size for libp2p.

Variables

This section is empty.

Functions

func NewAnnouncer

func NewAnnouncer[REQ, RESP any](ctx context.Context, opts AnnounceOptions, rt transport.UnaryServer[REQ, RESP]) (io.Closer, error)

NewAnnouncer creates a generic announcer for the given method, request, and response objects.

func NewConnFromStream added in v0.15.0

func NewConnFromStream(stream network.Stream) net.Conn

NewConnFromStream creates a new net.Conn from a libp2p stream.

func NewDHT

func NewDHT(ctx context.Context, host host.Host, bootstrapPeers []multiaddr.Multiaddr, connectTimeout time.Duration) (*dht.IpfsDHT, error)

NewDHT returns a DHT for given host. If bootstrap peers is empty, the default bootstrap peers will be used.

func NewDiscoveryJoinRoundTripper added in v0.15.0

func NewDiscoveryJoinRoundTripper(ctx context.Context, opts RoundTripOptions) (transport.JoinRoundTripper, error)

NewDiscoveryJoinRoundTripper returns a round tripper that uses the libp2p kademlia DHT to join a cluster. The created host is closed when the round tripper is closed.

func NewDiscoveryRoundTripper added in v0.15.0

func NewDiscoveryRoundTripper[REQ, RESP any](ctx context.Context, opts RoundTripOptions) (transport.RoundTripper[REQ, RESP], error)

NewDiscoveryRoundTripper returns a round tripper that uses the libp2p kademlia DHT. The created host is closed when the round tripper is closed.

func NewDiscoveryTransport added in v0.15.0

func NewDiscoveryTransport(ctx context.Context, opts TransportOptions) (transport.RPCTransport, error)

NewDiscoveryTransport returns a new RPC transport over libp2p using the IPFS DHT for discovery.

func NewJoinAnnouncer

func NewJoinAnnouncer(ctx context.Context, opts AnnounceOptions, join transport.JoinServer) (io.Closer, error)

NewJoinAnnouncer creates a new announcer on the kadmilia DHT and executes received join requests against the given join Server.

func NewJoinRoundTripper

func NewJoinRoundTripper(ctx context.Context, opts RoundTripOptions) (transport.JoinRoundTripper, error)

NewJoinRoundTripper returns a round tripper that dials the given multiaddrs directly using an uncertified peerstore.

func NewRoundTripper

func NewRoundTripper[REQ, RESP any](ctx context.Context, opts RoundTripOptions) (transport.RoundTripper[REQ, RESP], error)

NewRoundTripper returns a round tripper that dials the given multiaddrs directly using an uncertified peerstore.

func NewTransport added in v0.15.0

func NewTransport(host Host, credentials ...grpc.DialOption) transport.RPCTransport

NewTransport returns a new transport using the underlying host. The passed addresses to Dial are parsed as a multiaddrs. It assumes the host's peerstore has been populated with the addresses before calls to Dial.

func NewUncertifiedPeerstore added in v0.15.0

func NewUncertifiedPeerstore() (peerstore.Peerstore, error)

NewUncertifiedPeerstore creates a new uncertified peerstore.

func RPCProtocolFor

func RPCProtocolFor(method string) protocol.ID

RPCProtocolFor returns the RPCProtocol for the given method.

func SetMaxSystemBuffers

func SetMaxSystemBuffers()

SetMaxSystemBuffers sets the system buffers to the maximum size for libp2p.

func SetSystemBuffers

func SetSystemBuffers(size int)

SetSystemBuffers sets the system buffers to use for libp2p.

func ToMultiaddrs added in v0.15.0

func ToMultiaddrs(addrs []string) []multiaddr.Multiaddr

ToMultiaddrs returns the given strings as multiaddrs. It silently ignores any invalid multiaddrs.

func UDPRelayProtocolFor

func UDPRelayProtocolFor(pubkey crypto.PublicKey) protocol.ID

UDPRelayProtocolFor returns the UDPRelayProtocol for accepting connections from the given public key.

Types

type AnnounceOptions

type AnnounceOptions struct {
	// Rendezvous is the pre-shared key to use as a rendezvous point for the DHT.
	Rendezvous string
	// AnnounceTTL is the TTL to use for the discovery service.
	AnnounceTTL time.Duration
	// HostOptions are options for configuring the host. These can be left
	// empty if using a pre-created host.
	HostOptions HostOptions
	// Method is the method to announce.
	Method string
	// Host is a pre-started host to use for announcing.
	Host host.Host
}

AnnounceOptions are options for announcing the host or discovering peers on the libp2p kademlia DHT.

func (AnnounceOptions) MarshalJSON added in v0.11.2

func (opts AnnounceOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type Announcer

type Announcer interface {
	// AnnounceToDHT should announce the join protocol to the DHT,
	// such that it can be used by a libp2p transport.JoinRoundTripper.
	AnnounceToDHT(ctx context.Context, opts AnnounceOptions) error
	// LeaveDHT should remove the join protocol from the DHT for the
	// given rendezvous string.
	LeaveDHT(ctx context.Context, rendezvous string) error
}

Announcer is an interface for nodes that can announce themselves to the network.

type DiscoveryHost

type DiscoveryHost interface {
	Host

	// DHT is the underlying libp2p DHT.
	DHT() *dht.IpfsDHT
	// Announce announces the host to the DHT for the given rendezvous string.
	Announce(ctx context.Context, rendezvous string, ttl time.Duration)
}

DiscoveryHost is an interface that provides facilities for discovering and connecting to peers over libp2p. It can be used to avoid the need for re-creating a libp2p host and bootstrapping the DHT for each new connection.

func NewDiscoveryHost

func NewDiscoveryHost(ctx context.Context, opts HostOptions) (DiscoveryHost, error)

NewDiscoveryHost creates a new libp2p host connected to the DHT with the given options.

func WrapHostWithDiscovery

func WrapHostWithDiscovery(ctx context.Context, host Host, bootstrapPeers []multiaddr.Multiaddr, connectTimeout time.Duration) (DiscoveryHost, error)

WrapHostWithDiscovery will wrap a native libp2p Host, bootstrap a DHT alongside it and return a DiscoveryHost.

type Host added in v0.15.0

type Host interface {
	// ID returns the peer ID of the host as a raw string.
	ID() string
	// Host is the underlying libp2p host.
	Host() host.Host
	// AddAddrs adds the given addresses to the host's peerstore.
	AddAddrs(addrs []multiaddr.Multiaddr, id peer.ID, ttl time.Duration) error
	// SignAddrs creates an envelope for this host's peer ID and addresses.
	SignAddrs(seq uint64) (*record.Envelope, error)
	// ConsumePeerRecord consumes a peer record and adds it to the peerstore.
	ConsumePeerRecord(rec *record.Envelope, ttl time.Duration) error
	// RPCListener creates and returns a new net.Listener listening for RPC connections.
	// This should only ever be called once per host. The host will be closed when the
	// listener is closed.
	RPCListener() net.Listener
	// Close closes the host and its DHT.
	Close() error
}

Host is an interface that provides facilities for connecting to peers over libp2p.

func NewHost

func NewHost(ctx context.Context, opts HostOptions) (Host, error)

NewHost creates a new libp2p host with the given options.

type HostOptions

type HostOptions struct {
	// Key is the key to use for identification. If left empty, an ephemeral
	// key is generated.
	Key crypto.PrivateKey
	// BootstrapPeers is a list of bootstrap peers to use for the DHT when
	// creating a discovery host. If empty or nil, the default bootstrap
	// peers will be used.
	BootstrapPeers []multiaddr.Multiaddr
	// Options are options for configuring the libp2p host.
	Options []config.Option
	// LocalAddrs is a list of local addresses to announce the host with.
	// If empty or nil, the default local addresses will be used.
	LocalAddrs []multiaddr.Multiaddr
	// ConnectTimeout is the timeout for connecting to peers when bootstrapping.
	ConnectTimeout time.Duration
	// UncertifiedPeerstore uses an uncertified peerstore for the host.
	// This is useful for testing or when using the host to dial pre-trusted
	// peers.
	UncertifiedPeerstore bool
	// NoFallbackDefaults disables the use of fallback defaults when creating
	// the host. This is useful for testing.
	NoFallbackDefaults bool
}

HostOptions are options for creating a new libp2p host.

func (HostOptions) MarshalJSON added in v0.11.2

func (o HostOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

type RoundTripOptions

type RoundTripOptions struct {
	// Multiaddrs are the multiaddrs to dial. These are mutually exclusive with
	// Rendezvous.
	Multiaddrs []multiaddr.Multiaddr
	// Rendezvous is a rendezvous point on the DHT.
	Rendezvous string
	// HostOptions are options for configuring the host. These can be left
	// empty if using a pre-created host.
	HostOptions HostOptions
	// Method is the method to try to execute.
	Method string
	// Host is a pre-started host to use for the round trip
	Host Host
	// Credentials are gRPC DialOptions to use for the gRPC connection.
	Credentials []grpc.DialOption
}

RoundTripOptions are options for performing a round trip against a discovery node.

type TransportOptions added in v0.15.0

type TransportOptions struct {
	// Rendezvous is the pre-shared string to use as a rendezvous point for the DHT.
	Rendezvous string
	// HostOptions are options for configuring the host. These can be left
	// empty if using a pre-created host.
	HostOptions HostOptions
	// Host is a pre-started host to use for the transport.
	Host Host
	// Credentials are the credentials to use for the transport.
	Credentials []grpc.DialOption
}

TransportOptions are options for configuring an RPC transport over libp2p.

type UDPRelay

type UDPRelay struct {
	UDPRelayOptions
	// contains filtered or unexported fields
}

UDPRelay is a UDP relay.

func NewUDPRelay

func NewUDPRelay(ctx context.Context, opts UDPRelayOptions) (*UDPRelay, error)

NewUDPRelay creates a new UDP relay.

func NewUDPRelayWithHost

func NewUDPRelayWithHost(ctx context.Context, host DiscoveryHost, opts UDPRelayOptions) (*UDPRelay, error)

NewUDPRelay creates a new UDP relay with the given host.

func (*UDPRelay) Close

func (u *UDPRelay) Close() error

Close closes the relay.

func (*UDPRelay) Closed

func (u *UDPRelay) Closed() <-chan struct{}

Closed returns a channel that is closed when the relay is closed.

func (*UDPRelay) Errors

func (u *UDPRelay) Errors() <-chan error

Errors returns a channel that is closed when the relay encounters an error.

func (*UDPRelay) LocalAddr

func (u *UDPRelay) LocalAddr() *net.UDPAddr

LocalAddr returns the local address of the relay.

type UDPRelayOptions

type UDPRelayOptions struct {
	// PrivateKey is the private key to use for the host.
	// This is required.
	PrivateKey crypto.PrivateKey
	// RemotePubKey is the public key of the remote node to negotiate a UDP relay with.
	RemotePubKey crypto.PublicKey
	// Relay are options for the relay
	Relay relay.UDPOptions
	// Host are options for configuring the host
	Host HostOptions
}

UDPRelayOptions are the options for negotiating a UDP relay.

type UncertifiedPeerstore added in v0.15.0

type UncertifiedPeerstore struct {
	peerstore.Peerstore
}

UncertifiedPeerstore is a peerstore that does not verify peer addresses with signatures.

func (*UncertifiedPeerstore) ConsumePeerRecord added in v0.15.0

func (ps *UncertifiedPeerstore) ConsumePeerRecord(s *record.Envelope, ttl time.Duration) (accepted bool, err error)

func (*UncertifiedPeerstore) GetPeerRecord added in v0.15.0

func (ps *UncertifiedPeerstore) GetPeerRecord(p peer.ID) *record.Envelope

Directories

Path Synopsis
embedded
protocol
Package protocol defines the libp2p webmesh protocol.
Package protocol defines the libp2p webmesh protocol.
transport
Package transport defines the libp2p webmesh transport.
Package transport defines the libp2p webmesh transport.
util
Package util provides utility functions for the webmesh libp2p integrations.
Package util provides utility functions for the webmesh libp2p integrations.
wgtransport
Package wgtransport implements a Webmesh WireGuard transport for libp2p.
Package wgtransport implements a Webmesh WireGuard transport for libp2p.

Jump to

Keyboard shortcuts

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