Help us shape the future of Go! Take the 2026 Go Developer Survey

p2pnode

package
v0.28.13 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: AGPL-3.0 Imports: 26 Imported by: 0

Documentation

Overview

Package p2pnode encapsulates the libp2p library

Index

Constants

View Source
const (
	// MaxConnectAttempt is the maximum number of attempts to be made to connect to a remote node for 1-1 direct communication
	MaxConnectAttempt = 3

	// DefaultMaxPubSubMsgSize defines the maximum message size in publish and multicast modes
	DefaultMaxPubSubMsgSize = 5 * mb // 5 mb

)

Variables

This section is empty.

Functions

This section is empty.

Types

type Node

type Node struct {
	component.Component
	sync.Mutex
	// contains filtered or unexported fields
}

Node is a wrapper around the LibP2P host.

func NewNode

func NewNode(
	logger zerolog.Logger,
	host host.Host,
	pCache *ProtocolPeerCache,
	uniMgr *unicast.Manager,
	peerManager *connection.PeerManager,
) *Node

NewNode creates a new libp2p node and sets its parameters.

func (*Node) AddPeer

func (n *Node) AddPeer(ctx context.Context, peerInfo peer.AddrInfo) error

AddPeer adds a peer to this node by adding it to this node's peerstore and connecting to it

func (*Node) CreateStream

func (n *Node) CreateStream(ctx context.Context, peerID peer.ID) (libp2pnet.Stream, error)

CreateStream returns an existing stream connected to the peer if it exists, or creates a new stream with it.

func (*Node) GetIPPort

func (n *Node) GetIPPort() (string, string, error)

GetIPPort returns the IP and Port the libp2p node is listening on.

func (*Node) GetPeersForProtocol

func (n *Node) GetPeersForProtocol(pid protocol.ID) peer.IDSlice

func (*Node) Host

func (n *Node) Host() host.Host

Host returns pointer to host object of node.

func (*Node) IsConnected

func (n *Node) IsConnected(peerID peer.ID) (bool, error)

IsConnected returns true is address is a direct peer of this node else false

func (*Node) ListPeers

func (n *Node) ListPeers(topic string) []peer.ID

func (*Node) PeerManagerComponent

func (n *Node) PeerManagerComponent() component.Component

PeerManagerComponent returns the component interface of the peer manager.

func (*Node) Publish

func (n *Node) Publish(ctx context.Context, topic channels.Topic, data []byte) error

Publish publishes the given payload on the topic

func (*Node) RemovePeer

func (n *Node) RemovePeer(peerID peer.ID) error

RemovePeer closes the connection with the peer.

func (*Node) RequestPeerUpdate

func (n *Node) RequestPeerUpdate()

RequestPeerUpdate requests an update to the peer connections of this node using the peer manager.

func (*Node) Routing

func (n *Node) Routing() routing.Routing

Routing returns the node's routing implementation.

func (*Node) RoutingTable

func (n *Node) RoutingTable() *kbucket.RoutingTable

func (*Node) SetPubSub

func (n *Node) SetPubSub(ps *pubsub.PubSub)

SetPubSub sets the node's pubsub implementation. SetPubSub may be called at most once.

func (*Node) SetRouting

func (n *Node) SetRouting(r routing.Routing)

SetRouting sets the node's routing implementation. SetRouting may be called at most once.

func (*Node) Stop

func (n *Node) Stop() error

Stop terminates the libp2p node.

func (*Node) Subscribe

func (n *Node) Subscribe(topic channels.Topic, codec flownet.Codec, peerFilter p2p.PeerFilter, slashingViolationsConsumer slashing.ViolationsConsumer, validators ...validator.PubSubMessageValidator) (*pubsub.Subscription, error)

Subscribe subscribes the node to the given topic and returns the subscription Currently only one subscriber is allowed per topic. NOTE: A node will receive its own published messages.

func (*Node) UnSubscribe

func (n *Node) UnSubscribe(topic channels.Topic) error

UnSubscribe cancels the subscriber and closes the topic.

func (*Node) WithDefaultUnicastProtocol

func (n *Node) WithDefaultUnicastProtocol(defaultHandler libp2pnet.StreamHandler, preferred []unicast.ProtocolName) error

func (*Node) WithPeersProvider

func (n *Node) WithPeersProvider(peersProvider p2p.PeersProvider)

WithPeersProvider sets the PeersProvider for the peer manager. If a peer manager factory is set, this method will set the peer manager's PeersProvider.

type ProtocolPeerCache

type ProtocolPeerCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ProtocolPeerCache store a mapping from protocol ID to peers who support that protocol

func NewProtocolPeerCache

func NewProtocolPeerCache(logger zerolog.Logger, h host.Host) (*ProtocolPeerCache, error)

func (*ProtocolPeerCache) AddProtocols

func (p *ProtocolPeerCache) AddProtocols(peerID peer.ID, protocols []protocol.ID)

func (*ProtocolPeerCache) GetPeers

func (p *ProtocolPeerCache) GetPeers(pid protocol.ID) map[peer.ID]struct{}

func (*ProtocolPeerCache) RemovePeer

func (p *ProtocolPeerCache) RemovePeer(peerID peer.ID)

func (*ProtocolPeerCache) RemoveProtocols

func (p *ProtocolPeerCache) RemoveProtocols(peerID peer.ID, protocols []protocol.ID)

Jump to

Keyboard shortcuts

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