github.com/koko1123/flow-go-1@v0.29.6/network/p2p/libp2pNode.go (about)

     1  package p2p
     2  
     3  import (
     4  	"context"
     5  
     6  	kbucket "github.com/libp2p/go-libp2p-kbucket"
     7  	"github.com/libp2p/go-libp2p/core/host"
     8  	libp2pnet "github.com/libp2p/go-libp2p/core/network"
     9  	"github.com/libp2p/go-libp2p/core/peer"
    10  	"github.com/libp2p/go-libp2p/core/protocol"
    11  	"github.com/libp2p/go-libp2p/core/routing"
    12  
    13  	"github.com/koko1123/flow-go-1/module"
    14  	"github.com/koko1123/flow-go-1/module/component"
    15  	"github.com/koko1123/flow-go-1/module/irrecoverable"
    16  
    17  	"github.com/koko1123/flow-go-1/network/channels"
    18  	"github.com/koko1123/flow-go-1/network/p2p/unicast"
    19  )
    20  
    21  // LibP2PNode represents a flow libp2p node. It provides the network layer with the necessary interface to
    22  // control the underlying libp2p node. It is essentially the flow wrapper around the libp2p node, and allows
    23  // us to define different types of libp2p nodes that can operate in different ways by overriding these methods.
    24  type LibP2PNode interface {
    25  	module.ReadyDoneAware
    26  	// Start the libp2p node.
    27  	Start(ctx irrecoverable.SignalerContext)
    28  	// Stop terminates the libp2p node.
    29  	Stop() error
    30  	// AddPeer adds a peer to this node by adding it to this node's peerstore and connecting to it.
    31  	AddPeer(ctx context.Context, peerInfo peer.AddrInfo) error
    32  	// RemovePeer closes the connection with the peer.
    33  	RemovePeer(peerID peer.ID) error
    34  	// GetPeersForProtocol returns slice peer IDs for the specified protocol ID.
    35  	GetPeersForProtocol(pid protocol.ID) peer.IDSlice
    36  	// CreateStream returns an existing stream connected to the peer if it exists, or creates a new stream with it.
    37  	CreateStream(ctx context.Context, peerID peer.ID) (libp2pnet.Stream, error)
    38  	// GetIPPort returns the IP and Port the libp2p node is listening on.
    39  	GetIPPort() (string, string, error)
    40  	// RoutingTable returns the node routing table
    41  	RoutingTable() *kbucket.RoutingTable
    42  	// ListPeers returns list of peer IDs for peers subscribed to the topic.
    43  	ListPeers(topic string) []peer.ID
    44  	// Subscribe subscribes the node to the given topic and returns the subscription
    45  	Subscribe(topic channels.Topic, topicValidator TopicValidatorFunc) (Subscription, error)
    46  	// UnSubscribe cancels the subscriber and closes the topic.
    47  	UnSubscribe(topic channels.Topic) error
    48  	// Publish publishes the given payload on the topic.
    49  	Publish(ctx context.Context, topic channels.Topic, data []byte) error
    50  	// Host returns pointer to host object of node.
    51  	Host() host.Host
    52  	// WithDefaultUnicastProtocol overrides the default handler of the unicast manager and registers all preferred protocols.
    53  	WithDefaultUnicastProtocol(defaultHandler libp2pnet.StreamHandler, preferred []unicast.ProtocolName) error
    54  	// WithPeersProvider sets the PeersProvider for the peer manager.
    55  	// If a peer manager factory is set, this method will set the peer manager's PeersProvider.
    56  	WithPeersProvider(peersProvider PeersProvider)
    57  	// PeerManagerComponent returns the component interface of the peer manager.
    58  	PeerManagerComponent() component.Component
    59  	// RequestPeerUpdate requests an update to the peer connections of this node using the peer manager.
    60  	RequestPeerUpdate()
    61  	// IsConnected returns true is address is a direct peer of this node else false.
    62  	IsConnected(peerID peer.ID) (bool, error)
    63  	// SetRouting sets the node's routing implementation.
    64  	// SetRouting may be called at most once.
    65  	SetRouting(r routing.Routing)
    66  	// Routing returns node routing object.
    67  	Routing() routing.Routing
    68  	// SetPubSub sets the node's pubsub implementation.
    69  	// SetPubSub may be called at most once.
    70  	SetPubSub(ps PubSubAdapter)
    71  	// SetComponentManager sets the component manager for the node.
    72  	// SetComponentManager may be called at most once.
    73  	SetComponentManager(cm *component.ComponentManager)
    74  	// HasSubscription returns true if the node currently has an active subscription to the topic.
    75  	HasSubscription(topic channels.Topic) bool
    76  }