libp2p

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: Apache-2.0 Imports: 36 Imported by: 0

Documentation

Overview

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 LineFeed = '\x00'
View Source
const MaxBuffer = 2500000

MaxBuffer is the maximum buffer size for libp2p.

Variables

This section is empty.

Functions

func NewAnnouncer added in v0.4.1

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 NewDHT added in v0.4.0

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 NewHost added in v0.4.1

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

NewHost creates a new libp2p host with the given options.

func NewJoinAnnouncer added in v0.4.0

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 uses the libp2p kademlia DHT to join a cluster. The created host is closed when the round tripper is closed.

func NewRaftTransport added in v0.3.3

func NewRaftTransport(ctx context.Context, opts RaftTransportOptions) (raft.Transport, error)

NewRaftTransport creates a new Raft transport over the Kademlia DHT.

func NewRoundTripper

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

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

func RPCProtocolFor added in v0.4.1

func RPCProtocolFor(method string) protocol.ID

RPCProtocolFor returns the RPCProtocol for the given method.

func SetMaxSystemBuffers added in v0.4.1

func SetMaxSystemBuffers()

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

func SetSystemBuffers added in v0.4.1

func SetSystemBuffers(size int)

SetSystemBuffers sets the system buffers to use for libp2p.

func UDPRelayProtocolFor added in v0.4.1

func UDPRelayProtocolFor(pubkey crypto.PublicKey) protocol.ID

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

Types

type AnnounceOptions added in v0.4.1

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.

type Announcer added in v0.4.1

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 BootstrapOptions added in v0.4.1

type BootstrapOptions struct {
	// Rendezvous is the rendezvous string to use for the transport.
	// This should be the same for all sides of the transport.
	Rendezvous string
	// Signer is provided to sign and verify the UUIDs of the voters.
	Signer crypto.PSK
	// Host are options for configuring a host if one is not provided.
	HostOptions HostOptions
	// Host is a pre-started host to use for the transport.
	Host host.Host
	// ElectionTimeout is the election timeout. The election timeout should
	// be larger than the host's connection timeout. Otherwise, chances
	// of a successful election are low. This does not apply when all sides
	// provide an already bootstrapped host to the transport. All sides of
	// the transport should use the same election timeout.
	ElectionTimeout time.Duration
	// Linger is the time to wait for non-leaders to join before closing the host.
	Linger time.Duration
	// NodeID is the node ID to use for leader election. This should be the
	// local node ID.
	NodeID string
	// NodeIDs are the other node IDs to use for leader election.
	NodeIDs []string
}

BootstrapOptions are options for the bootstrap transport.

type BootstrapTransport added in v0.4.1

type BootstrapTransport interface {
	transport.BootstrapTransport

	// UUID returns the local UUID that was used for voting.
	UUID() uuid.UUID
	// LeaderUUID returns the UUID of the leader that was elected.
	// This is only populated after leader election is complete.
	LeaderUUID() uuid.UUID
}

BootstrapTransport implements bootstrap transport and returns the local UUID that was used for voting.

func NewBootstrapTransport added in v0.4.1

func NewBootstrapTransport(ctx context.Context, announcer Announcer, opts BootstrapOptions) (BootstrapTransport, error)

NewBootstrapTransport creates a new bootstrap transport. The host is closed when leader election is complete.

func NewBootstrapTransportWithHost added in v0.4.1

func NewBootstrapTransportWithHost(host DiscoveryHost, announcer Announcer, opts BootstrapOptions) (BootstrapTransport, error)

NewBootstrapTransportWithHost creates a new bootstrap transport with a host. The host will remain open after leader election is complete.

type DiscoveryHost added in v0.6.0

type DiscoveryHost interface {
	// ID returns the peer ID of the host.
	ID() peer.ID
	// Host is the underlying libp2p host.
	Host() host.Host
	// DHT is the underlying libp2p DHT.
	DHT() *dht.IpfsDHT
	// Close closes the host and its DHT.
	Close(ctx context.Context) error
}

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 added in v0.6.0

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 added in v0.6.0

func WrapHostWithDiscovery(ctx context.Context, host 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 HostOptions added in v0.4.1

type HostOptions struct {
	// BootstrapPeers is a list of bootstrap peers to use for the DHT.
	// 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
}

HostOptions are options for creating a new libp2p host.

type RaftTransportOptions added in v0.3.3

type RaftTransportOptions struct {
	// PSK is the pre-shared key to use as a rendezvous point for the DHT.
	PSK string
	// BootstrapPeers is a list of bootstrap peers to use for the DHT.
	// If empty or nil, the default bootstrap peers will be used.
	BootstrapPeers []multiaddr.Multiaddr
	// LocalAddrs is a list of local addresses to use for the host.
	// If empty or nil, the default local addresses will be used.
	LocalAddrs []multiaddr.Multiaddr
	// Options are options for configuring the libp2p host.
	Options []config.Option
	// DiscoveryTTL is the TTL to use for the discovery service.
	DiscoveryTTL time.Duration
	// ConnectTimeout is the timeout to use when connecting to a peer.
	ConnectTimeout time.Duration
	// LeaderDialer is the function that will be used to dial the leader.
	LeaderDialer transport.LeaderDialer
}

RaftTransportOptions are options for the TCP transport.

type RoundTripOptions

type RoundTripOptions struct {
	// Rendezvous is the pre-shared key 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
	// Method is the method to try to execute.
	Method string
	// Host is a pre-started host to use for the round trip
	Host host.Host
}

RoundTripOptions are options for performing a round trip against a libp2p host.

type UDPRelay added in v0.4.1

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

UDPRelay is a UDP relay.

func NewUDPRelay added in v0.4.1

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

NewUDPRelay creates a new UDP relay.

func NewUDPRelayWithHost added in v0.4.1

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

NewUDPRelay creates a new UDP relay with the given host.

func (*UDPRelay) Close added in v0.4.1

func (u *UDPRelay) Close() error

Close closes the relay.

func (*UDPRelay) Closed added in v0.4.1

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

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

func (*UDPRelay) Errors added in v0.4.1

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

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

func (*UDPRelay) LocalAddr added in v0.4.1

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

LocalAddr returns the local address of the relay.

type UDPRelayOptions added in v0.4.1

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.

Jump to

Keyboard shortcuts

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