p2pnode

package
v0.36.8 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: AGPL-3.0 Imports: 30 Imported by: 1

Documentation

Overview

Package p2pnode encapsulates the libp2p library

Index

Constants

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

)

Variables

This section is empty.

Functions

func NewGossipSubAdapter

func NewGossipSubAdapter(ctx context.Context,
	logger zerolog.Logger,
	h host.Host,
	cfg p2p.PubSubAdapterConfig,
	clusterChangeConsumer p2p.CollectionClusterChangesConsumer) (p2p.PubSubAdapter, error)

Types

type GossipSubAdapter

type GossipSubAdapter struct {
	component.Component
	// contains filtered or unexported fields
}

GossipSubAdapter is a wrapper around the libp2p GossipSub implementation that implements the PubSubAdapter interface for the Flow network.

func (*GossipSubAdapter) ActiveClustersChanged

func (g *GossipSubAdapter) ActiveClustersChanged(lst flow.ChainIDList)

ActiveClustersChanged is called when the active clusters of collection nodes changes. GossipSubAdapter implements this method to forward the call to the clusterChangeConsumer (rpc inspector), which will then update the cluster state of the rpc inspector. Args: - lst: the list of active clusters Returns: - void

func (*GossipSubAdapter) GetLocalMeshPeers

func (g *GossipSubAdapter) GetLocalMeshPeers(topic channels.Topic) []peer.ID

GetLocalMeshPeers returns the list of peers in the local mesh for the given topic. Args: - topic: the topic. Returns: - []peer.ID: the list of peers in the local mesh for the given topic.

func (*GossipSubAdapter) GetTopics

func (g *GossipSubAdapter) GetTopics() []string

func (*GossipSubAdapter) Join

func (g *GossipSubAdapter) Join(topic string) (p2p.Topic, error)

func (*GossipSubAdapter) ListPeers

func (g *GossipSubAdapter) ListPeers(topic string) []peer.ID

func (*GossipSubAdapter) PeerScoreExposer

func (g *GossipSubAdapter) PeerScoreExposer() p2p.PeerScoreExposer

PeerScoreExposer returns the peer score exposer for the gossipsub adapter. The exposer is a read-only interface for querying peer scores and returns the local scoring table of the underlying gossipsub node. The exposer is only available if the gossipsub adapter was configured with a score tracer. If the gossipsub adapter was not configured with a score tracer, the exposer will be nil. Args:

None.

Returns:

The peer score exposer for the gossipsub adapter.

func (*GossipSubAdapter) RegisterTopicValidator

func (g *GossipSubAdapter) RegisterTopicValidator(topic string, topicValidator p2p.TopicValidatorFunc) error

func (*GossipSubAdapter) UnregisterTopicValidator

func (g *GossipSubAdapter) UnregisterTopicValidator(topic string) error

type GossipSubAdapterConfig

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

GossipSubAdapterConfig is a wrapper around libp2p pubsub options that implements the PubSubAdapterConfig interface for the Flow network.

func NewGossipSubAdapterConfig

func NewGossipSubAdapterConfig(base *p2p.BasePubSubAdapterConfig) *GossipSubAdapterConfig

NewGossipSubAdapterConfig creates a new GossipSubAdapterConfig with the default options. Args:

  • base: the base pubsub adapter config

Returns:

  • a new GossipSubAdapterConfig

func (*GossipSubAdapterConfig) Build

func (g *GossipSubAdapterConfig) Build() []pubsub.Option

Build returns the libp2p pubsub options. Args:

  • None

Returns:

  • []pubsub.Option: the libp2p pubsub options.

Build is idempotent.

func (*GossipSubAdapterConfig) PubSubTracer

func (g *GossipSubAdapterConfig) PubSubTracer() p2p.PubSubTracer

PubSubTracer returns the tracer for the pubsub. Args: - None Returns: - p2p.PubSubTracer: the tracer for the pubsub.

func (*GossipSubAdapterConfig) RpcInspectorComponent added in v0.33.30

func (g *GossipSubAdapterConfig) RpcInspectorComponent() component.Component

RpcInspectorComponent returns the component that manages the lifecycle of the inspector suite. This is used to start and stop the inspector suite by the PubSubAdapter. Args:

  • None

Returns:

  • component.Component: the component that manages the lifecycle of the inspector suite.

func (*GossipSubAdapterConfig) ScoreTracer

func (g *GossipSubAdapterConfig) ScoreTracer() p2p.PeerScoreTracer

ScoreTracer returns the tracer for the peer score. Args:

  • None

Returns: - p2p.PeerScoreTracer: the tracer for the peer score.

func (*GossipSubAdapterConfig) ScoringComponent

func (g *GossipSubAdapterConfig) ScoringComponent() component.Component

func (*GossipSubAdapterConfig) TopicScoreParamFunc

func (g *GossipSubAdapterConfig) TopicScoreParamFunc() (func(topic *pubsub.Topic) *pubsub.TopicScoreParams, bool)

TopicScoreParamFunc returns the topic score param function. This function is used to get the topic score params for a topic. The topic score params are used to set the topic parameters in GossipSub at the time of joining the topic. Args:

  • None

Returns: - func(topic *pubsub.Topic) *pubsub.TopicScoreParams: the topic score param function if set, nil otherwise. - bool: true if the topic score param function is set, false otherwise.

func (*GossipSubAdapterConfig) WithMessageIdFunction

func (g *GossipSubAdapterConfig) WithMessageIdFunction(f func([]byte) string)

WithMessageIdFunction adds a message ID function option to the config. Args: - f: the message ID function to use Returns: -None

func (*GossipSubAdapterConfig) WithRoutingDiscovery

func (g *GossipSubAdapterConfig) WithRoutingDiscovery(routing routing.ContentRouting)

WithRoutingDiscovery adds a routing discovery option to the config. Args:

  • routing: the routing discovery to use

Returns: -None

func (*GossipSubAdapterConfig) WithRpcInspector added in v0.33.30

func (g *GossipSubAdapterConfig) WithRpcInspector(inspector p2p.GossipSubRPCInspector)

WithInspectorSuite adds an inspector suite option to the config. Args: - suite: the inspector suite to use Returns: -None

func (*GossipSubAdapterConfig) WithScoreOption

func (g *GossipSubAdapterConfig) WithScoreOption(option p2p.ScoreOptionBuilder)

WithScoreOption adds a score option to the config. Args: - option: the score option to use Returns: -None

func (*GossipSubAdapterConfig) WithScoreTracer

func (g *GossipSubAdapterConfig) WithScoreTracer(tracer p2p.PeerScoreTracer)

WithScoreTracer sets the tracer for the peer score. Args:

  • tracer: the tracer for the peer score.

Returns:

  • None

func (*GossipSubAdapterConfig) WithSubscriptionFilter

func (g *GossipSubAdapterConfig) WithSubscriptionFilter(filter p2p.SubscriptionFilter)

WithSubscriptionFilter adds a subscription filter option to the config. Args:

  • filter: the subscription filter to use

Returns: -None

func (*GossipSubAdapterConfig) WithTracer

func (g *GossipSubAdapterConfig) WithTracer(tracer p2p.PubSubTracer)

WithTracer adds a tracer option to the config. Args: - tracer: the tracer to use Returns: -None

type GossipSubTopic

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

GossipSubTopic is a wrapper around libp2p pubsub topics that implements the PubSubTopic interface for the Flow network.

func NewGossipSubTopic

func NewGossipSubTopic(topic *pubsub.Topic) *GossipSubTopic

func (*GossipSubTopic) Close

func (g *GossipSubTopic) Close() error

func (*GossipSubTopic) Publish

func (g *GossipSubTopic) Publish(ctx context.Context, bytes []byte) error

func (*GossipSubTopic) String

func (g *GossipSubTopic) String() string

func (*GossipSubTopic) Subscribe

func (g *GossipSubTopic) Subscribe() (p2p.Subscription, error)

type Node

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

Node is a wrapper around the LibP2P host.

func NewNode

func NewNode(cfg *p2p.NodeConfig) (*Node, error)

NewNode creates a new libp2p node and sets its parameters. Args:

  • cfg: The configuration for the libp2p node.

Returns:

  • *Node: The created libp2p node.

- error: An error, if any occurred during the process. This includes failure in creating the node. The returned error is irrecoverable, and the node cannot be used.

func (*Node) ActiveClustersChanged

func (n *Node) ActiveClustersChanged(list flow.ChainIDList)

ActiveClustersChanged is called when the active clusters list of the collection clusters has changed. The LibP2PNode implementation directly calls the ActiveClustersChanged method of the pubsub implementation, as the pubsub implementation is responsible for the actual handling of the event. Args: - list: the new active clusters list. Returns: - none

func (*Node) ConnectToPeer

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

ConnectToPeerAddrInfo adds a peer to this node by adding it to this node's peerstore and connecting to it. All errors returned from this function can be considered benign.

func (*Node) GetIPPort

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

GetIPPort returns the IP and Port the libp2p node is listening on. All errors returned from this function can be considered benign.

func (*Node) GetLocalMeshPeers

func (n *Node) GetLocalMeshPeers(topic channels.Topic) []peer.ID

GetLocalMeshPeers returns the list of peers in the local mesh for the given topic. Args: - topic: the topic. Returns: - []peer.ID: the list of peers in the local mesh for the given topic.

func (*Node) GetPeersForProtocol

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

GetPeersForProtocol returns slice peer IDs for the specified protocol ID.

func (*Node) HasSubscription

func (n *Node) HasSubscription(topic channels.Topic) bool

HasSubscription returns true if the node currently has an active subscription to the topic.

func (*Node) Host

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

Host returns pointer to host object of node.

func (*Node) ID

func (n *Node) ID() peer.ID

ID returns the peer.ID of the node, which is the unique identifier of the node at the libp2p level. For other libp2p nodes, the current node is identified by this ID.

func (*Node) IsConnected

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

IsConnected returns true if address is a direct peer of this node else false. Peers are considered not connected if the underlying libp2p host reports the peers as not connected and there are no connections in the connection list. error returns:

  • network.ErrIllegalConnectionState if the underlying libp2p host reports connectedness as NotConnected but the connections list to the peer is not empty. This would normally indicate a bug within libp2p. Although the network.ErrIllegalConnectionState a bug in libp2p there is a small chance that this error will be returned due to a race condition between the time we check Connectedness and ConnsToPeer. There is a chance that a connection could be established after we check Connectedness but right before we check ConnsToPeer.

func (*Node) IsDisallowListed

func (n *Node) IsDisallowListed(peerId peer.ID) ([]flownet.DisallowListedCause, bool)

IsDisallowListed determines whether the given peer is disallow-listed for any reason. Args: - peerID: the peer to check. Returns: - []network.DisallowListedCause: the list of causes for which the given peer is disallow-listed. If the peer is not disallow-listed for any reason, a nil slice is returned. - bool: true if the peer is disallow-listed for any reason, false otherwise.

func (*Node) ListPeers

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

ListPeers returns list of peer IDs for peers subscribed to the topic.

func (*Node) OnAllowListNotification

func (n *Node) OnAllowListNotification(peerId peer.ID, cause flownet.DisallowListedCause)

OnAllowListNotification is called when a new allow list update notification is distributed. Any error on consuming event must handle internally. The implementation must be concurrency safe. Args:

id: peer ID of the peer being allow-listed.
cause: cause of the peer being allow-listed (only this cause is removed from the peer's disallow-listed causes).

Returns:

none

func (*Node) OnDisallowListNotification

func (n *Node) OnDisallowListNotification(peerId peer.ID, cause flownet.DisallowListedCause)

OnDisallowListNotification is called when a new disallow list update notification is distributed. Any error on consuming event must handle internally. The implementation must be concurrency safe. Args:

id: peer ID of the peer being disallow-listed.
cause: cause of the peer being disallow-listed (only this cause is added to the peer's disallow-listed causes).

Returns:

none

func (*Node) OpenAndWriteOnStream

func (n *Node) OpenAndWriteOnStream(ctx context.Context, peerID peer.ID, protectionTag string, writingLogic func(stream libp2pnet.Stream) error) error

OpenAndWriteOnStream opens a new stream to a peer. The stream is opened to the given peerID and writingLogic is executed on the stream. The created stream does not need to be reused and can be inexpensively created for each send. Moreover, the stream creation does not incur a round-trip time as the stream negotiation happens on an existing connection.

Args:

  • ctx: The context used to control the stream's lifecycle.
  • peerID: The ID of the peer to open the stream to.
  • protectionTag: A tag that protects the connection and ensures that the connection manager keeps it alive, and won't prune the connection while the tag is active.
  • writingLogic: A callback function that contains the logic for writing to the stream. It allows an external caller to write to the stream without having to worry about the stream creation and management.

Returns: error: An error, if any occurred during the process. This includes failure in creating the stream, setting the write deadline, executing the writing logic, resetting the stream if the writing logic fails, or closing the stream. All returned errors during this process can be considered benign.

func (*Node) PeerManagerComponent

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

PeerManagerComponent returns the component interface of the peer manager.

func (*Node) PeerScoreExposer

func (n *Node) PeerScoreExposer() p2p.PeerScoreExposer

PeerScoreExposer returns the node's peer score exposer implementation. If the node's peer score exposer has not been set, the second return value will be false.

func (*Node) Publish

func (n *Node) Publish(ctx context.Context, messageScope flownet.OutgoingMessageScope) error

Publish publishes the given payload on the topic. All errors returned from this function can be considered benign.

func (*Node) RemovePeer

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

RemovePeer closes the connection with the peer. All errors returned from this function can be considered benign.

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

RoutingTable returns the node routing table

func (*Node) SetComponentManager

func (n *Node) SetComponentManager(cm *component.ComponentManager)

SetComponentManager sets the component manager for the node. SetComponentManager may be called at most once.

func (*Node) SetPubSub

func (n *Node) SetPubSub(ps p2p.PubSubAdapter)

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

func (*Node) SetRouting

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

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

func (*Node) SetUnicastManager

func (n *Node) SetUnicastManager(uniMgr p2p.UnicastManager)

SetUnicastManager sets the unicast manager for the node. SetUnicastManager may be called at most once.

func (*Node) Start

func (n *Node) Start(ctx irrecoverable.SignalerContext)

func (*Node) Stop

func (n *Node) Stop() error

Stop terminates the libp2p node. All errors returned from this function can be considered benign.

func (*Node) Subscribe

func (n *Node) Subscribe(topic channels.Topic, topicValidator p2p.TopicValidatorFunc) (p2p.Subscription, error)

Subscribe subscribes the node to the given topic and returns the subscription All errors returned from this function can be considered benign.

func (*Node) Unsubscribe

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

Unsubscribe cancels the subscriber and closes the topic. Args: topic: topic to unsubscribe from. Returns: error: error if any, which means unsubscribe failed. All errors returned from this function can be considered benign.

func (*Node) WithDefaultUnicastProtocol

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

WithDefaultUnicastProtocol overrides the default handler of the unicast manager and registers all preferred protocols.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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