peers

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2025 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProtocolIDDiscover protocol.ID = "/kwil/discovery/1.0.0" // the PEX protocol that all peers support, and is the only real protocol that crawlers support other than the dummy ProtocolIDCrawler
	ProtocolIDCrawler  protocol.ID = "/kwil/crawler/1.0.0"   // dummy to identify a crawler that should not stay connected

	ProtocolIDPrefixChainID protocol.ID = "/kwil/chain/1.0.0/"
)

Variables

View Source
var ErrNoRecordedAddress = errors.New("no address for peer in peerstore")

ErrNoRecordedAddress is returned when a peer is known or even connected, but there are no addresses available for it. This can be case when an incoming connection is established from a peer that does not have a routable IP address on which they can accept incoming connections.

Functions

func ChainConnectionGaters

func ChainConnectionGaters(gaters ...connmgr.ConnectionGater) connmgr.ConnectionGater

func CheckProtocolSupport

func CheckProtocolSupport(_ context.Context, ps peerstore.Peerstore, peerID peer.ID, protoIDs ...protocol.ID) (bool, error)

func CompressDialError

func CompressDialError(err error) error

func ConvertPeersToMultiAddr

func ConvertPeersToMultiAddr(peers []string) ([]string, error)

ConvertPeersToMultiAddr convert a peer from pubkeyHex#keyTypeInt@ip:port to /ip4/ip/tcp/port/p2p/peerID

func NodeIDFromPeerID

func NodeIDFromPeerID(peerID string) (string, error)

Convert from go-libp2p's peer.ID format to Kwil's node ID format.

func NodeIDFromPubKey

func NodeIDFromPubKey(pubkey crypto.PublicKey) string

func NodeIDToPubKey

func NodeIDToPubKey(nodeID string) (crypto.PublicKey, error)

func PeerIDFromPubKey

func PeerIDFromPubKey(pubkey crypto.PublicKey) (peer.ID, error)

PeerIDFromPubKey converts a pubkey to a peer ID string.

func PubKeyFromPeerID

func PubKeyFromPeerID(peerID string) (crypto.PublicKey, error)

PubKeyFromPeerID tries to decode the pubkey from a peer ID string. This will only work if the peer ID is an "identity" multihash.

func RequirePeerProtos

func RequirePeerProtos(ctx context.Context, ps peerstore.Peerstore, peer peer.ID, protoIDs ...protocol.ID) error

func ResolveHost

func ResolveHost(addr string) (ip, ipv string, err error)

Types

type AddrInfo

type AddrInfo struct {
	ID    peer.ID               `json:"id"`
	Addrs []multiaddr.Multiaddr `json:"addrs"`
}

type ChainIDGater

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

func NewChainIDGater

func NewChainIDGater(chainID string, opts ...GateOpt) *ChainIDGater

func (*ChainIDGater) InterceptAccept

func (g *ChainIDGater) InterceptAccept(connAddrs network.ConnMultiaddrs) bool

func (*ChainIDGater) InterceptAddrDial

func (g *ChainIDGater) InterceptAddrDial(p peer.ID, addr multiaddr.Multiaddr) bool

func (*ChainIDGater) InterceptPeerDial

func (g *ChainIDGater) InterceptPeerDial(p peer.ID) bool

func (*ChainIDGater) InterceptSecured

func (g *ChainIDGater) InterceptSecured(dir network.Direction, p peer.ID, conn network.ConnMultiaddrs) bool

func (*ChainIDGater) InterceptUpgraded

func (g *ChainIDGater) InterceptUpgraded(conn network.Conn) (bool, control.DisconnectReason)

type Config

type Config struct {
	PEX      bool
	AddrBook string
	Host     host.Host

	SeedMode          bool
	TargetConnections int
	ChainID           string

	// Optionals
	Logger            log.Logger
	ConnGater         *WhitelistGater
	RequiredProtocols []protocol.ID
}

type Connector

type Connector interface {
	Connect(ctx context.Context, pi peer.AddrInfo) error
}

type GateOpt

type GateOpt func(*gateOpts)

func WithLogger

func WithLogger(logger log.Logger) GateOpt

type OutboundWhitelistGater

type OutboundWhitelistGater struct {
	AllowedOutbound func(peer.ID) bool
}

OutboundWhitelistGater is to prevent dialing out to peers that are not explicitly allowed by an application provided filter function. This exists in part to prevent other modules such as the DHT and gossipsub from dialing out to peers that are not explicitly allowed (e.g. already connected or added by the application).

func (*OutboundWhitelistGater) InterceptAccept

func (g *OutboundWhitelistGater) InterceptAccept(connAddrs network.ConnMultiaddrs) bool

func (*OutboundWhitelistGater) InterceptAddrDial

func (g *OutboundWhitelistGater) InterceptAddrDial(p peer.ID, addr multiaddr.Multiaddr) bool

func (*OutboundWhitelistGater) InterceptPeerDial

func (g *OutboundWhitelistGater) InterceptPeerDial(p peer.ID) bool

func (*OutboundWhitelistGater) InterceptSecured

func (g *OutboundWhitelistGater) InterceptSecured(dir network.Direction, p peer.ID, conn network.ConnMultiaddrs) bool

func (*OutboundWhitelistGater) InterceptUpgraded

func (g *OutboundWhitelistGater) InterceptUpgraded(conn network.Conn) (bool, control.DisconnectReason)

type PeerIDStringer

type PeerIDStringer string

PeerIDStringer provides lazy lazy conversion of a libp2p peer ID into a Kwil node ID, which is a public key with a type suffix.

func (PeerIDStringer) String

func (p PeerIDStringer) String() string

type PeerInfo

type PeerInfo struct {
	AddrInfo
	Protos []protocol.ID `json:"protos"`
}

func (PeerInfo) MarshalJSON

func (p PeerInfo) MarshalJSON() ([]byte, error)

func (*PeerInfo) UnmarshalJSON

func (p *PeerInfo) UnmarshalJSON(data []byte) error

type PeerMan

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

func NewPeerMan

func NewPeerMan(cfg *Config) (*PeerMan, error)

func (*PeerMan) Allow

func (pm *PeerMan) Allow(p peer.ID)

func (*PeerMan) AllowPersistent

func (pm *PeerMan) AllowPersistent(p peer.ID)

func (*PeerMan) Allowed

func (pm *PeerMan) Allowed() []peer.ID

func (*PeerMan) AllowedPersistent

func (pm *PeerMan) AllowedPersistent() []peer.ID

func (*PeerMan) Connect

func (pm *PeerMan) Connect(ctx context.Context, info AddrInfo) error

func (*PeerMan) Connected

func (pm *PeerMan) Connected(net network.Network, conn network.Conn)

Connected is triggered when a peer is connected (inbound or outbound).

func (*PeerMan) ConnectedPeers

func (pm *PeerMan) ConnectedPeers() []PeerInfo

ConnectedPeers returns a list of peer info for all connected peers.

func (*PeerMan) Disallow

func (pm *PeerMan) Disallow(p peer.ID)

func (*PeerMan) Disconnected

func (pm *PeerMan) Disconnected(net network.Network, conn network.Conn)

Disconnected is triggered when a peer disconnects

func (*PeerMan) DiscoveryStreamHandler

func (pm *PeerMan) DiscoveryStreamHandler(s network.Stream)

DiscoveryStreamHandler sends a list of peer addresses on the stream. This implements the receiving side of ProtocolIDDiscover.

func (*PeerMan) FindPeers

func (pm *PeerMan) FindPeers(ctx context.Context, ns string, opts ...discovery.Option) (<-chan peer.AddrInfo, error)

func (*PeerMan) IsAllowed

func (pm *PeerMan) IsAllowed(p peer.ID) bool

func (*PeerMan) KnownPeers

func (pm *PeerMan) KnownPeers() (all, connected, disconnected []PeerInfo)

KnownPeers returns a list of peer info for all known peers (connected or just in peer store).

func (*PeerMan) Listen

func (pm *PeerMan) Listen(network.Network, multiaddr.Multiaddr)

func (*PeerMan) ListenClose

func (pm *PeerMan) ListenClose(network.Network, multiaddr.Multiaddr)

func (*PeerMan) PrintKnownPeers

func (pm *PeerMan) PrintKnownPeers()

func (*PeerMan) RequestPeers

func (pm *PeerMan) RequestPeers(ctx context.Context, peerID peer.ID) ([]PeerInfo, error)

RequestPeers initiates the ProtocolIDDiscover stream.

func (*PeerMan) Start

func (pm *PeerMan) Start(ctx context.Context) error

type PersistentPeerInfo

type PersistentPeerInfo struct {
	NodeID      string                `json:"id"` // "node ID" (pubkeybytes#keytype)
	Addrs       []multiaddr.Multiaddr `json:"addrs"`
	Protos      []protocol.ID         `json:"protos"`
	Whitelisted bool                  `json:"whitelisted"`
}

func (PersistentPeerInfo) MarshalJSON

func (p PersistentPeerInfo) MarshalJSON() ([]byte, error)

func (*PersistentPeerInfo) UnmarshalJSON

func (p *PersistentPeerInfo) UnmarshalJSON(data []byte) error

type RemotePeersFn

type RemotePeersFn func(ctx context.Context, peerID peer.ID) ([]peer.AddrInfo, error)

type WhitelistGater

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

WhitelistGater is a libp2p connmgr.ConnectionGater implementation to enforce a peer whitelist.

func NewWhitelistGater

func NewWhitelistGater(allowed []peer.ID, opts ...GateOpt) *WhitelistGater

func (*WhitelistGater) Allow

func (g *WhitelistGater) Allow(p peer.ID)

Allow adds a peer to the whitelist.

func (*WhitelistGater) Allowed

func (g *WhitelistGater) Allowed() []peer.ID

Allowed returns the list of peers in the whitelist.

func (*WhitelistGater) Disallow

func (g *WhitelistGater) Disallow(p peer.ID) bool

Disallow removes a peer from the whitelist and returns true if the whitelistGater is enabled and the peer was removed.

func (*WhitelistGater) InterceptAccept

func (g *WhitelistGater) InterceptAccept(connAddrs network.ConnMultiaddrs) bool

func (*WhitelistGater) InterceptAddrDial

func (g *WhitelistGater) InterceptAddrDial(p peer.ID, addr multiaddr.Multiaddr) bool

func (*WhitelistGater) InterceptPeerDial

func (g *WhitelistGater) InterceptPeerDial(p peer.ID) bool

func (*WhitelistGater) InterceptSecured

func (g *WhitelistGater) InterceptSecured(dir network.Direction, p peer.ID, conn network.ConnMultiaddrs) bool

func (*WhitelistGater) InterceptUpgraded

func (g *WhitelistGater) InterceptUpgraded(conn network.Conn) (bool, control.DisconnectReason)

func (*WhitelistGater) IsAllowed

func (g *WhitelistGater) IsAllowed(p peer.ID) bool

IsAllowed indicates if a peer is in the whitelist. This is mainly for the connmgr.ConnectionGater methods.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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