p2p

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: GPL-3.0 Imports: 45 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeENR added in v0.2.0

func DecodeENR(enrStr string) (enr.Record, error)

DecodeENR returns a enr record decoded from the string. See reference github.com/ethereum/go-ethereum@v1.10.10/p2p/dnsdisc/tree.go:378.

func EncodeENR added in v0.2.0

func EncodeENR(record enr.Record) (string, error)

EncodeENR returns an encoded string format of the enr record.

func IsRelayAddr added in v0.13.0

func IsRelayAddr(a ma.Multiaddr) bool

IsRelayAddr returns true if the address is a relayed address. Copied from github.com/libp2p/go-libp2p@v0.22.0/p2p/protocol/circuitv2/relay/relay.go:593.

func IsRelayError added in v0.13.0

func IsRelayError(err error) bool

IsRelayError returns true if the error is due to temporary relay circuit recycling.

func KeyPath added in v0.3.0

func KeyPath(datadir string) string

KeyPath returns the charon-enr-private-key path relative to the data dir.

func LoadPrivKey added in v0.3.0

func LoadPrivKey(dataDir string) (*ecdsa.PrivateKey, error)

LoadPrivKey returns the ecdsa k1 key saved in the directory.

func NamedAddr added in v0.10.0

func NamedAddr(addr ma.Multiaddr) string

NamedAddr returns the multiaddr as a string with peer names instead of peer IDs.

func NewDiscoveryRouter added in v0.10.0

func NewDiscoveryRouter(tcpNode host.Host, udpNode *MutableUDPNode, peers []Peer) lifecycle.HookFuncCtx

NewDiscoveryRouter returns a life cycle hook that links discv5 to libp2p by continuously polling discv5 for latest peer ENRs and adding them to the libp2p peer store.

func NewEventCollector added in v0.10.0

func NewEventCollector(tcpNode host.Host) lifecycle.HookFuncCtx

NewEventCollector returns a lifecycle hook that instruments libp2p events.

func NewLocalEnode added in v0.1.1

func NewLocalEnode(config Config, key *ecdsa.PrivateKey) (*enode.LocalNode, *enode.DB, error)

NewLocalEnode returns a local enode and a peer DB or an error.

func NewPingService added in v0.2.0

func NewPingService(h host.Host, peers []peer.ID, conf TestPingConfig) lifecycle.HookFuncCtx

NewPingService returns a start function of a p2p ping service that pings all peers every second and collects metrics.

func NewRelayReserver added in v0.4.0

func NewRelayReserver(tcpNode host.Host, relay *MutablePeer) lifecycle.HookFunc

NewRelayReserver returns a life cycle hook function that continuously reserves a relay circuit until the context is closed.

func NewRelayRouter added in v0.10.0

func NewRelayRouter(tcpNode host.Host, peers []Peer, relays []*MutablePeer) lifecycle.HookFuncCtx

NewRelayRouter returns a life cycle hook that routes peers via relays in libp2p by continuously adding peer relay addresses to libp2p peer store.

func NewSavedPrivKey added in v0.3.0

func NewSavedPrivKey(datadir string) (*ecdsa.PrivateKey, error)

NewSavedPrivKey generates a new ecdsa k1 key and saves it to the directory.

func NewTCPNode added in v0.1.1

func NewTCPNode(ctx context.Context, cfg Config, key *ecdsa.PrivateKey, connGater ConnGater, opts ...libp2p.Option,
) (host.Host, error)

NewTCPNode returns a started tcp-based libp2p host.

func PeerIDFromKey added in v0.10.1

func PeerIDFromKey(pubkey *ecdsa.PublicKey) (peer.ID, error)

PeerIDFromKey returns the peer ID of the public key.

func PeerIDToKey added in v0.12.0

func PeerIDToKey(p peer.ID) (*ecdsa.PublicKey, error)

PeerIDToKey returns the public key of the peer ID.

func PeerName added in v0.6.0

func PeerName(id peer.ID) string

PeerName returns a deterministic pseudo random human friendly name for the peer ID.

func ProtocolSupported added in v0.13.0

func ProtocolSupported(tcpNode host.Host, peerID peer.ID, protocolID protocol.ID) (supported bool, known bool)

ProtocolSupported returns whether the peer supports the protocol or whether this is unknown.

func RegisterConnectionLogger added in v0.11.0

func RegisterConnectionLogger(tcpNode host.Host, peerIDs []peer.ID)

RegisterConnectionLogger registers a connection logger with the host. This is pretty weird and hacky, but that is because libp2p uses the network.Notifiee interface as a map key, so the implementation can only contain fields that are hashable. So we use a channel and do the logic externally. :(.

func RegisterHandler added in v0.11.0

func RegisterHandler(logTopic string, tcpNode host.Host, protocol protocol.ID,
	zeroReq func() proto.Message, handlerFunc HandlerFunc,
)

RegisterHandler registers a canonical proto request and response handler for the provided protocol. - The zeroReq function returns a zero request to unmarshal. - The handlerFunc is called with the unmarshalled request and returns either a response or false or an error. - The marshalled response is sent back if present. - The stream is always closed before returning.

func Send added in v0.7.0

func Send(ctx context.Context, tcpNode host.Host, protoID protocol.ID, peerID peer.ID, msg proto.Message) error

Send sends a libp2p message synchronously. It implements SendFunc.

func SendReceive added in v0.11.0

func SendReceive(ctx context.Context, tcpNode host.Host, peerID peer.ID,
	req, resp proto.Message, pID protocol.ID, opts ...func(*sendRecvOpts),
) error

SendReceive sends and receives a libp2p request and response message pair synchronously and then closes the stream. The provided response proto will be populated if err is nil. It implements SendReceiveFunc.

func VerifyP2PKey added in v0.10.1

func VerifyP2PKey(peers []Peer, key *ecdsa.PrivateKey) error

VerifyP2PKey returns an error if the p2pkey doesn't match any lock operator ENR.

func WithSendReceiveProtocols added in v0.13.0

func WithSendReceiveProtocols(pids ...protocol.ID) func(*sendRecvOpts)

WithSendReceiveProtocols returns an option for SendReceive that sets the protocols to use. Note this overrides the protocol provided in the SendReceive.

func WithSendReceiveRTT added in v0.13.0

func WithSendReceiveRTT(callback func(time.Duration)) func(*sendRecvOpts)

WithSendReceiveRTT returns an option for SendReceive that sets a callback for the RTT.

Types

type Config

type Config struct {
	// UDPBootnodes defines the discv5 boot node URLs.
	UDPBootnodes []string // TODO(corver): Rename to Relays once discv5 removed.
	// UDPBootLock enables using cluster-lock ENRs as discv5 boot nodes.
	UDPBootLock bool
	// UDPAddr defines the discv5 udp listen address.
	UDPAddr string
	// ExternalIP is the IP advertised by libp2p.
	ExternalIP string
	// ExternalHost is the DNS hostname advertised by libp2p.
	ExternalHost string
	// TCPAddrs defines the lib-p2p tcp listen addresses.
	TCPAddrs []string
	// Allowlist defines csv CIDR blocks for lib-p2p allowed connections.
	Allowlist string
	// Allowlist defines csv CIDR blocks for lib-p2p denied connections.
	Denylist string
	// BootnodeRelay enables circuit relay via bootnodes if direct connections fail.
	// Only applicable to charon nodes not bootnodes.
	BootnodeRelay bool
	// DisableReuseport disables TCP port reuse for libp2p.
	DisableReuseport bool
}

func (Config) Discv5Discovery added in v0.13.0

func (c Config) Discv5Discovery() bool

Discv5Discovery returns true if discv5 discovery is enabled.

func (Config) Multiaddrs

func (c Config) Multiaddrs() ([]ma.Multiaddr, error)

Multiaddrs returns the configured addresses as libp2p multiaddrs.

func (Config) ParseTCPAddrs added in v0.1.1

func (c Config) ParseTCPAddrs() ([]*net.TCPAddr, error)

ParseTCPAddrs returns the configured tcp addresses as typed net tcp addresses.

func (Config) RelayDiscovery added in v0.13.0

func (c Config) RelayDiscovery() bool

RelayDiscovery returns true if relay discovery is enabled.

type ConnGater

type ConnGater struct {
	// contains filtered or unexported fields
}

ConnGater filters incoming connections by the cluster peers.

func NewConnGater added in v0.1.1

func NewConnGater(peers []peer.ID, relays []*MutablePeer) (ConnGater, error)

NewConnGater return a new connection gater that limits access to the cluster peers and relays.

func NewOpenGater added in v0.4.0

func NewOpenGater() ConnGater

NewOpenGater returns a connection gater that is open, not gating any connections.

func (ConnGater) InterceptAccept

func (ConnGater) InterceptAccept(_ network.ConnMultiaddrs) (allow bool)

func (ConnGater) InterceptAddrDial

func (ConnGater) InterceptAddrDial(_ peer.ID, _ multiaddr.Multiaddr) (allow bool)

func (ConnGater) InterceptPeerDial

func (ConnGater) InterceptPeerDial(_ peer.ID) (allow bool)

InterceptPeerDial does nothing.

func (ConnGater) InterceptSecured

func (c ConnGater) InterceptSecured(_ network.Direction, id peer.ID, _ network.ConnMultiaddrs) bool

InterceptSecured rejects nodes with a peer ID that isn't part of any known DV.

func (ConnGater) InterceptUpgraded

func (ConnGater) InterceptUpgraded(_ network.Conn) (bool, control.DisconnectReason)

InterceptUpgraded does nothing.

type HandlerFunc added in v0.11.0

type HandlerFunc func(ctx context.Context, peerID peer.ID, req proto.Message) (proto.Message, bool, error)

HandlerFunc abstracts the handler logic that processes a p2p received proto message and returns a response or false or an error.

type MutablePeer added in v0.10.0

type MutablePeer struct {
	// contains filtered or unexported fields
}

MutablePeer defines a mutable peer used mostly for stateless bootnodes/relays that change ID on restart but have a consistent URL to resolve them. The zero value is a valid empty MutablePeer.

func NewMutablePeer added in v0.10.0

func NewMutablePeer(p Peer) *MutablePeer

NewMutablePeer returns a new non-empty mutable peer.

func NewRelays added in v0.4.0

func NewRelays(conf Config, bootnodes []*MutablePeer) []*MutablePeer

NewRelays returns the libp2p circuit relays from bootnodes if enabled.

func NewUDPBootnodes added in v0.4.0

func NewUDPBootnodes(ctx context.Context, config Config, peers []Peer,
	localEnode enode.ID, lockHashHex string,
) ([]*MutablePeer, error)

NewUDPBootnodes returns the discv5 udp bootnodes from the config.

func (*MutablePeer) Peer added in v0.10.0

func (p *MutablePeer) Peer() (Peer, bool)

Peer returns the current peer or false if not available.

func (*MutablePeer) Set added in v0.10.0

func (p *MutablePeer) Set(peer Peer)

Set updates the mutable enode and calls all subscribers.

func (*MutablePeer) Subscribe added in v0.10.0

func (p *MutablePeer) Subscribe(sub func(Peer))

Subscribe registers a function that is called when the peer is updated.

type MutableUDPNode added in v0.10.0

type MutableUDPNode struct {
	// contains filtered or unexported fields
}

MutableUDPNode wraps a discv5 udp node providing support to recreate it if bootnodes change.

func NewUDPNode added in v0.1.1

func NewUDPNode(ctx context.Context, config Config, ln *enode.LocalNode,
	key *ecdsa.PrivateKey, bootnodes []*MutablePeer,
) (*MutableUDPNode, error)

NewUDPNode starts and returns a discv5 UDP provider.

func (*MutableUDPNode) AllNodes added in v0.10.0

func (n *MutableUDPNode) AllNodes() []*enode.Node

func (*MutableUDPNode) Close added in v0.10.0

func (n *MutableUDPNode) Close()

func (*MutableUDPNode) Resolve added in v0.10.0

func (n *MutableUDPNode) Resolve(node *enode.Node) *enode.Node

func (*MutableUDPNode) Set added in v0.10.0

func (n *MutableUDPNode) Set(node *discover.UDPv5)

type Peer added in v0.2.0

type Peer struct {
	// ENR defines the networking information of the peer.
	ENR enr.Record

	// Enode represents the networking host of the peer.
	Enode enode.Node

	// ID is a libp2p peer identity. It is inferred from the ENR.
	ID peer.ID

	// Index is the order of this node in the cluster.
	// This is only applicable to charon nodes, not relays.
	Index int

	// Name represents a human friendly name for the peer.
	Name string
}

Peer represents a peer in the libp2p network, either a charon node or a relay.

func NewPeer added in v0.2.0

func NewPeer(record enr.Record, index int) (Peer, error)

NewPeer returns a new charon peer.

func (Peer) AddrInfo added in v0.12.0

func (p Peer) AddrInfo() (peer.AddrInfo, error)

AddrInfo returns the libp2p peer addr info (peer ID and multiaddr) from the peer ENR.

func (Peer) MultiAddr added in v0.12.0

func (p Peer) MultiAddr() (ma.Multiaddr, error)

MultiAddr returns the libp2p multiaddr (tcp ip and port) from the peer ENR.

func (Peer) PublicKey added in v0.12.0

func (p Peer) PublicKey() (*ecdsa.PublicKey, error)

PublicKey returns peer public key.

func (Peer) ShareIdx added in v0.10.0

func (p Peer) ShareIdx() int

ShareIdx returns share index of this Peer. ShareIdx is 1-indexed while peerIdx is 0-indexed.

func (Peer) TCPAddr added in v0.12.0

func (p Peer) TCPAddr() (*net.TCPAddr, error)

TCPAddr returns the tcp address (ip and port) from the peer ENR.

type RegisterHandlerFunc added in v0.11.0

type RegisterHandlerFunc func(logTopic string, tcpNode host.Host, protocol protocol.ID,
	zeroReq func() proto.Message, handlerFunc HandlerFunc,
)

RegisterHandlerFunc abstracts a function that registers a libp2p stream handler that reads a single protobuf request and returns an optional response.

type SendFunc added in v0.7.0

SendFunc is an abstract function responsible for sending libp2p messages.

type SendReceiveFunc added in v0.11.0

type SendReceiveFunc func(ctx context.Context, tcpNode host.Host, peerID peer.ID,
	req, resp proto.Message, protocol protocol.ID, opts ...func(*sendRecvOpts)) error

SendReceiveFunc is an abstract function responsible for sending a libp2p request and returning (populating) a libp2p response.

type Sender added in v0.7.0

type Sender struct {
	// contains filtered or unexported fields
}

Sender provides an API for sending libp2p messages, both synchronous and asynchronous. It also provides log filtering for async sending, mitigating error storms when peers are down.

func (*Sender) SendAsync added in v0.7.0

func (s *Sender) SendAsync(parent context.Context, tcpNode host.Host, protoID protocol.ID, peerID peer.ID, msg proto.Message) error

SendAsync returns nil and sends a libp2p message asynchronously. It logs results on state change (success to/from failure). It implements SendFunc.

func (*Sender) SendReceive added in v0.11.0

func (s *Sender) SendReceive(ctx context.Context, tcpNode host.Host, peerID peer.ID, req, resp proto.Message,
	protocol protocol.ID, opts ...func(*sendRecvOpts),
) error

SendReceive sends and receives a libp2p request and response message pair synchronously and then closes the stream. The provided response proto will be populated if err is nil. It logs results on state change (success to/from failure). It implements SendReceiveFunc.

type TestPingConfig added in v0.10.0

type TestPingConfig struct {
	// Disable disables pinging.
	Disable bool
	// Callback is called on successful pings.
	Callback func(peer.ID, host.Host)
	// MaxBackoff overrides default max backoff.
	MaxBackoff time.Duration
}

TestPingConfig overrides ping config for testing.

Jump to

Keyboard shortcuts

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