github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/connector.go (about) 1 package p2p 2 3 import ( 4 "context" 5 6 "github.com/libp2p/go-libp2p/core/host" 7 "github.com/libp2p/go-libp2p/core/network" 8 "github.com/libp2p/go-libp2p/core/peer" 9 ) 10 11 // PeerUpdater connects to the given peer.IDs. It also disconnects from any other peers with which it may have 12 // previously established connection. 13 type PeerUpdater interface { 14 // UpdatePeers connects to the given peer.IDs. It also disconnects from any other peers with which it may have 15 // previously established connection. 16 // UpdatePeers implementation should be idempotent such that multiple calls to connect to the same peer should not 17 // create multiple connections 18 UpdatePeers(ctx context.Context, peerIDs peer.IDSlice) 19 } 20 21 // Connector is an interface that allows connecting to a peer.ID. 22 type Connector interface { 23 // Connect connects to the given peer.ID. 24 // Note that connection may be established asynchronously. Any error encountered while connecting to the peer.ID 25 // is benign and should not be returned. Also, Connect implementation should not cause any blocking or crash. 26 // Args: 27 // ctx: context.Context to be used for the connection 28 // peerChan: channel to which the peer.AddrInfo of the connected peer.ID is sent. 29 // Returns: 30 // none. 31 Connect(ctx context.Context, peerChan <-chan peer.AddrInfo) 32 } 33 34 // ConnectorFactory is a factory function to create a new Connector. 35 type ConnectorFactory func(host host.Host) (Connector, error) 36 37 type PeerFilter func(peer.ID) error 38 39 // AllowAllPeerFilter returns a peer filter that does not do any filtering. 40 func AllowAllPeerFilter() PeerFilter { 41 return func(p peer.ID) error { 42 return nil 43 } 44 } 45 46 // ConnectorHost is a wrapper around the libp2p host.Host interface to provide the required functionality for the 47 // Connector interface. 48 type ConnectorHost interface { 49 // Connections returns all the connections of the underlying host. 50 Connections() []network.Conn 51 52 // IsConnectedTo returns true if the given peer.ID is connected to the underlying host. 53 // Args: 54 // peerID: peer.ID for which the connection status is requested 55 // Returns: 56 // true if the given peer.ID is connected to the underlying host. 57 IsConnectedTo(peerId peer.ID) bool 58 59 // PeerInfo returns the peer.AddrInfo for the given peer.ID. 60 // Args: 61 // id: peer.ID for which the peer.AddrInfo is requested 62 // Returns: 63 // peer.AddrInfo for the given peer.ID 64 PeerInfo(peerId peer.ID) peer.AddrInfo 65 66 // IsProtected returns true if the given peer.ID is protected from pruning. 67 // Args: 68 // id: peer.ID for which the protection status is requested 69 // Returns: 70 // true if the given peer.ID is protected from pruning 71 IsProtected(peerId peer.ID) bool 72 73 // ClosePeer closes the connection to the given peer.ID. 74 // Args: 75 // id: peer.ID for which the connection is to be closed 76 // Returns: 77 // error if there is any error while closing the connection to the given peer.ID. All errors are benign. 78 ClosePeer(peerId peer.ID) error 79 80 // ID returns the peer.ID of the underlying host. 81 // Returns: 82 // peer.ID of the underlying host. 83 ID() peer.ID 84 }