p2p

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MPL-2.0 Imports: 43 Imported by: 3

Documentation

Index

Constants

View Source
const (
	DesiredRelays  = 2
	RelayBootDelay = 20 * time.Second

	DHTProtocolPrefix protocol.ID = "/awl"
)

Variables

This section is empty.

Functions

func DefaultListenAddrs

func DefaultListenAddrs() []multiaddr.Multiaddr

func UnicastListenAddrs

func UnicastListenAddrs() []multiaddr.Multiaddr

Types

type BootstrapPeerDebugInfo

type BootstrapPeerDebugInfo struct {
	Error       string   `json:",omitempty"`
	Connections []string `json:",omitempty"`
	Ping        Duration `json:",omitempty" swaggertype:"string"`
	// PeerstoreAddrs are the addresses a dial to the peer goes through
	PeerstoreAddrs []string `json:",omitempty"`
	// Reconnect is set while the peer cannot be reached
	Reconnect *ReconnectDebugInfo `json:",omitempty"`
}

type ConnectionInfo

type ConnectionInfo struct {
	Multiaddr    string
	ThroughRelay bool
	RelayPeerID  string
	Address      string
	Protocol     string
	Direction    string
	Opened       time.Time
	Transient    bool
}

type Duration added in v0.13.0

type Duration time.Duration

func (Duration) MarshalJSON added in v0.13.0

func (d Duration) MarshalJSON() ([]byte, error)

func (*Duration) UnmarshalJSON added in v0.13.0

func (d *Duration) UnmarshalJSON(data []byte) error

type HostConfig

type HostConfig struct {
	PrivKeyBytes             []byte
	ListenAddrs              []multiaddr.Multiaddr
	UserAgent                string
	BootstrapPeers           []peer.AddrInfo
	AllowEmptyBootstrapPeers bool
	EnableAutoRelay          bool

	// SocketControlFunc is set when gateway mode is enabled to mark sockets
	// (e.g., SO_MARK on Linux) so they bypass the VPN TUN interface.
	SocketControlFunc func(network, address string, c syscall.RawConn) error

	Libp2pOpts  []libp2p.Option
	ConnManager struct {
		LowWater    int
		HighWater   int
		GracePeriod time.Duration
	}
	Peerstore    peerstore.Peerstore
	DHTDatastore ds.Batching
	DHTOpts      []dht.Option
}

type IDService

type IDService interface {
	// IdentifyConn synchronously triggers an identify request on the connection and
	// waits for it to complete. If the connection is being identified by another
	// caller, this call will wait. If the connection has already been identified,
	// it will return immediately.
	IdentifyConn(network.Conn)
	// IdentifyWait triggers an identify (if the connection has not already been
	// identified) and returns a channel that is closed when the identify protocol
	// completes.
	IdentifyWait(network.Conn) <-chan struct{}
}

type KnownPeer added in v0.20.0

type KnownPeer struct {
	ID peer.ID
	// Eager peers are tried every round with no backoff, however long they have
	// been unreachable.
	Eager bool
}

KnownPeer is a peer MaintainBackgroundConnections keeps a connection to.

type P2p

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

func NewP2p

func NewP2p(ctx context.Context) *P2p

func (*P2p) AnnouncedAs

func (p *P2p) AnnouncedAs() []multiaddr.Multiaddr

func (*P2p) Bootstrap

func (p *P2p) Bootstrap()

func (*P2p) BootstrapPeersStats

func (p *P2p) BootstrapPeersStats() (int, int)

BootstrapPeersStats returns total peers count and connected count.

func (*P2p) BootstrapPeersStatsDetailed

func (p *P2p) BootstrapPeersStatsDetailed() map[string]BootstrapPeerDebugInfo

func (*P2p) ClearBackoff

func (p *P2p) ClearBackoff(peerID peer.ID)

func (*P2p) Close

func (p *P2p) Close() error

func (*P2p) ConfirmedAddrs added in v0.14.0

func (p *P2p) ConfirmedAddrs() (reachable []multiaddr.Multiaddr, unreachable []multiaddr.Multiaddr, unknown []multiaddr.Multiaddr)

func (*P2p) ConnectPeer

func (p *P2p) ConnectPeer(ctx context.Context, peerID peer.ID) error

func (*P2p) ConnectedPeersCount

func (p *P2p) ConnectedPeersCount() int

func (*P2p) ConnectionsLastTrimAgo

func (p *P2p) ConnectionsLastTrimAgo() time.Duration

func (*P2p) FindPeer

func (p *P2p) FindPeer(ctx context.Context, id peer.ID) (peer.AddrInfo, error)

func (*P2p) GetPeerLatency added in v0.14.0

func (p *P2p) GetPeerLatency(id peer.ID) time.Duration

func (*P2p) HasDirectConnection added in v0.18.0

func (p *P2p) HasDirectConnection(peerID peer.ID) bool

HasDirectConnection reports whether at least one current connection to the given peer is direct (i.e. not via a libp2p circuit relay). Returns false if there are no connections at all.

func (*P2p) Host

func (p *P2p) Host() host.Host

func (*P2p) IDService

func (p *P2p) IDService() IDService

func (*P2p) InitHost

func (p *P2p) InitHost(hostConfig HostConfig) (host.Host, error)

func (*P2p) IsConnected

func (p *P2p) IsConnected(peerID peer.ID) bool

func (*P2p) MaintainBackgroundConnections

func (p *P2p) MaintainBackgroundConnections(ctx context.Context, interval time.Duration, knownPeersFunc func() []KnownPeer)

MaintainBackgroundConnections keeps the node connected to its known and bootstrap peers. A peer that cannot be reached is retried less and less often, see retrySchedule; interval is both the pace of the rounds and the first pause of that backoff.

func (*P2p) NetworkSize added in v0.8.0

func (p *P2p) NetworkSize() (int32, error)

func (*P2p) NetworkStats

func (p *P2p) NetworkStats() metrics.Stats

func (*P2p) NetworkStatsByPeer

func (p *P2p) NetworkStatsByPeer() map[peer.ID]metrics.Stats

func (*P2p) NetworkStatsByProtocol

func (p *P2p) NetworkStatsByProtocol() map[protocol.ID]metrics.Stats

func (*P2p) NetworkStatsForPeer

func (p *P2p) NetworkStatsForPeer(peerID peer.ID) metrics.Stats

func (*P2p) NewStream

func (p *P2p) NewStream(ctx context.Context, id peer.ID, proto protocol.ID) (network.Stream, error)

func (*P2p) NewStreamMulti added in v0.17.0

func (p *P2p) NewStreamMulti(ctx context.Context, id peer.ID, protos ...protocol.ID) (network.Stream, error)

func (*P2p) NewStreamWithDedicatedConn added in v0.11.0

func (p *P2p) NewStreamWithDedicatedConn(ctx context.Context, id peer.ID, proto protocol.ID) (network.Stream, error)

func (*P2p) NotifyNetworkChanged added in v0.20.0

func (p *P2p) NotifyNetworkChanged()

NotifyNetworkChanged tells P2p that the network of the host has changed, for the case when the caller knows better than P2p can find out. Every known and bootstrap peer is then tried right away, with no regard to its backoff. It does not block, and calls that pile up are handled as one.

func (*P2p) OpenConnectionsCount

func (p *P2p) OpenConnectionsCount() int

func (*P2p) OpenStreamStats

func (p *P2p) OpenStreamStats() map[protocol.ID]map[string]int

func (*P2p) OpenStreamsCount

func (p *P2p) OpenStreamsCount() int64

func (*P2p) PeerConnectionsInfo

func (p *P2p) PeerConnectionsInfo(peerID peer.ID) []ConnectionInfo

func (*P2p) PeerID

func (p *P2p) PeerID() peer.ID

func (*P2p) PeerPublicIP added in v0.18.0

func (p *P2p) PeerPublicIP(peerID peer.ID) string

PeerPublicIP returns the verified public IP of the directly-connected peer. It inspects active connections (skipping /p2p-circuit relay connections) and returns the IP from the first non-relay Conn's RemoteMultiaddr — that address is the one libp2p actually completed a handshake against, so it's reliable (unlike Peerstore entries, which include unverified self-reported addrs from identify). Returns "" if the peer is reachable only via a relay or is not connected; in that case the real public IP is not observable to us.

func (*P2p) PeerUserAgent

func (p *P2p) PeerUserAgent(peerID peer.ID) string

func (*P2p) PeersWithAddrsCount

func (p *P2p) PeersWithAddrsCount() int

func (*P2p) PeerstoreAddrs added in v0.20.0

func (p *P2p) PeerstoreAddrs(peerID peer.ID) []string

PeerstoreAddrs returns the addresses we have for the peer, sorted: the ones a dial to it goes through, a /dnsaddr among them is yet to be resolved. They are kept in memory and expire, so there are none for a peer that has been away for a while.

func (*P2p) ProtectPeer

func (p *P2p) ProtectPeer(id peer.ID)

func (*P2p) Reachability

func (p *P2p) Reachability() network.Reachability

func (*P2p) ReconnectInfo added in v0.20.0

func (p *P2p) ReconnectInfo(peerID peer.ID) *ReconnectDebugInfo

ReconnectInfo reports what MaintainBackgroundConnections remembers about a known or a bootstrap peer it could not reach. It is nil for a peer with nothing against it: one that is connected, or was not tried yet.

func (*P2p) RecordPeerLatency added in v0.14.0

func (p *P2p) RecordPeerLatency(id peer.ID, rtt time.Duration)

func (*P2p) RoutingTablePeers

func (p *P2p) RoutingTablePeers() []kbucket.PeerInfo

func (*P2p) RoutingTableSize

func (p *P2p) RoutingTableSize() int

func (*P2p) SubscribeConnectionEvents

func (p *P2p) SubscribeConnectionEvents(onConnected, onDisconnected func(network.Network, network.Conn))

func (*P2p) TCPDialStats added in v0.20.0

func (p *P2p) TCPDialStats() TCPDialStats

TCPDialStats returns the dial counters of the main host. The AutoNAT dial-back hosts are left out: they never reuse a port by design.

func (*P2p) UnprotectPeer

func (p *P2p) UnprotectPeer(id peer.ID)

func (*P2p) Uptime

func (p *P2p) Uptime() time.Duration

type ReconnectDebugInfo added in v0.20.0

type ReconnectDebugInfo struct {
	// Failures in a row
	Failures       int
	LastAttemptAgo Duration `swaggertype:"string"`
	// NextAttemptIn is zero when the peer is to be tried on the next round
	NextAttemptIn Duration `swaggertype:"string"`
	LastError     string
}

ReconnectDebugInfo is what MaintainBackgroundConnections remembers about a peer it could not reach.

type TCPDialStats added in v0.20.0

type TCPDialStats struct {
	// FromListenPort is what hole punching needs. If it stays at zero while
	// FromRandomPort grows, port reuse is silently broken.
	FromListenPort int64
	FromRandomPort int64
}

TCPDialStats counts successful outbound TCP dials of the main libp2p host by the source port they left from. Only maintained when socket marking is on (see reuseportDialer); zero otherwise.

Jump to

Keyboard shortcuts

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