github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/connection/connector_host.go (about) 1 package connection 2 3 import ( 4 "github.com/libp2p/go-libp2p/core/host" 5 "github.com/libp2p/go-libp2p/core/network" 6 "github.com/libp2p/go-libp2p/core/peer" 7 8 "github.com/onflow/flow-go/network/p2p" 9 ) 10 11 // ConnectorHost is a wrapper around the libp2p host.Host interface to provide the required functionality for the 12 // Connector interface. 13 type ConnectorHost struct { 14 h host.Host 15 } 16 17 var _ p2p.ConnectorHost = (*ConnectorHost)(nil) 18 19 func NewConnectorHost(h host.Host) *ConnectorHost { 20 return &ConnectorHost{ 21 h: h, 22 } 23 } 24 25 // Connections returns all the connections of the underlying host. 26 func (c *ConnectorHost) Connections() []network.Conn { 27 return c.h.Network().Conns() 28 } 29 30 // IsConnectedTo returns true if the given peer.ID is connected to the underlying host. 31 // Args: 32 // 33 // peerID: peer.ID for which the connection status is requested 34 // 35 // Returns: 36 // 37 // true if the given peer.ID is connected to the underlying host. 38 func (c *ConnectorHost) IsConnectedTo(peerID peer.ID) bool { 39 return c.h.Network().Connectedness(peerID) == network.Connected && len(c.h.Network().ConnsToPeer(peerID)) > 0 40 } 41 42 // PeerInfo returns the peer.AddrInfo for the given peer.ID. 43 // Args: 44 // 45 // id: peer.ID for which the peer.AddrInfo is requested 46 // 47 // Returns: 48 // 49 // peer.AddrInfo for the given peer.ID 50 func (c *ConnectorHost) PeerInfo(id peer.ID) peer.AddrInfo { 51 return c.h.Peerstore().PeerInfo(id) 52 } 53 54 // IsProtected returns true if the given peer.ID is protected from pruning. 55 // Args: 56 // 57 // id: peer.ID for which the protection status is requested 58 // 59 // Returns: 60 // 61 // true if the given peer.ID is protected from pruning 62 func (c *ConnectorHost) IsProtected(id peer.ID) bool { 63 return c.h.ConnManager().IsProtected(id, "") 64 } 65 66 // ClosePeer closes the connection to the given peer.ID. 67 // Args: 68 // 69 // id: peer.ID for which the connection is to be closed 70 // 71 // Returns: 72 // 73 // error if there is any error while closing the connection to the given peer.ID. All errors are benign. 74 func (c *ConnectorHost) ClosePeer(id peer.ID) error { 75 return c.h.Network().ClosePeer(id) 76 } 77 78 // ID returns the peer.ID of the underlying host. 79 // Returns: 80 // 81 // peer.ID of the underlying host. 82 // 83 // ID returns the peer.ID of the underlying host. 84 func (c *ConnectorHost) ID() peer.ID { 85 return c.h.ID() 86 }