|
| 1 | +package debug |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + libp2pPeer "github.com/libp2p/go-libp2p/core/peer" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + libp2pNetwork "github.com/libp2p/go-libp2p/core/network" |
| 10 | + "github.com/pokt-network/pocket/p2p/providers/peerstore_provider" |
| 11 | + typesP2P "github.com/pokt-network/pocket/p2p/types" |
| 12 | + "github.com/pokt-network/pocket/p2p/utils" |
| 13 | + "github.com/pokt-network/pocket/shared/modules" |
| 14 | +) |
| 15 | + |
| 16 | +var printConnectionsHeader = []string{"Peer ID", "Multiaddr", "Opened", "Direction", "NumStreams"} |
| 17 | + |
| 18 | +func PrintPeerConnections(bus modules.Bus, routerType RouterType) error { |
| 19 | + var ( |
| 20 | + connections []libp2pNetwork.Conn |
| 21 | + routerPlurality = "" |
| 22 | + ) |
| 23 | + |
| 24 | + if routerType == AllRouterTypes { |
| 25 | + routerPlurality = "s" |
| 26 | + } |
| 27 | + |
| 28 | + connections, err := getFilteredConnections(bus, routerType) |
| 29 | + if err != nil { |
| 30 | + return fmt.Errorf("getting connecions: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + if err := LogSelfAddress(bus); err != nil { |
| 34 | + return fmt.Errorf("printing self address: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + // NB: Intentionally printing with `fmt` instead of the logger to match |
| 38 | + // `utils.PrintPeerListTable` which does not use the logger due to |
| 39 | + // incompatibilities with the tabwriter. |
| 40 | + // (This doesn't seem to work as expected; i.e. not printing at all in tilt.) |
| 41 | + if _, err := fmt.Fprintf( |
| 42 | + os.Stdout, |
| 43 | + "%s router peerstore%s:\n", |
| 44 | + routerType, |
| 45 | + routerPlurality, |
| 46 | + ); err != nil { |
| 47 | + return fmt.Errorf("printing to stdout: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + if err := PrintConnectionsTable(connections); err != nil { |
| 51 | + return fmt.Errorf("printing peer list: %w", err) |
| 52 | + } |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func PrintConnectionsTable(conns []libp2pNetwork.Conn) error { |
| 57 | + return utils.PrintTable(printConnectionsHeader, peerConnsRowConsumerFactory(conns)) |
| 58 | +} |
| 59 | + |
| 60 | +func getFilteredConnections( |
| 61 | + bus modules.Bus, |
| 62 | + routerType RouterType, |
| 63 | +) ([]libp2pNetwork.Conn, error) { |
| 64 | + var ( |
| 65 | + pstore typesP2P.Peerstore |
| 66 | + idsToInclude map[libp2pPeer.ID]struct{} |
| 67 | + p2pModule = bus.GetP2PModule() |
| 68 | + connections = p2pModule.GetConnections() |
| 69 | + ) |
| 70 | + |
| 71 | + // TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider |
| 72 | + // is retrievable as a proper submodule. |
| 73 | + pstoreProviderModule, err := bus.GetModulesRegistry(). |
| 74 | + GetModule(peerstore_provider.PeerstoreProviderSubmoduleName) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("getting peerstore provider: %w", err) |
| 77 | + } |
| 78 | + pstoreProvider, ok := pstoreProviderModule.(peerstore_provider.PeerstoreProvider) |
| 79 | + if !ok { |
| 80 | + return nil, fmt.Errorf("unknown peerstore provider type: %T", pstoreProviderModule) |
| 81 | + } |
| 82 | + //-- |
| 83 | + |
| 84 | + switch routerType { |
| 85 | + case AllRouterTypes: |
| 86 | + // return early; no need to filter |
| 87 | + return connections, nil |
| 88 | + case StakedRouterType: |
| 89 | + pstore, err = pstoreProvider.GetStakedPeerstoreAtCurrentHeight() |
| 90 | + if err != nil { |
| 91 | + return nil, fmt.Errorf("getting staked peerstore: %w", err) |
| 92 | + } |
| 93 | + case UnstakedRouterType: |
| 94 | + pstore, err = pstoreProvider.GetUnstakedPeerstore() |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("getting unstaked peerstore: %w", err) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + idsToInclude, err = getPeerIDs(pstore.GetPeerList()) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("getting peer IDs: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + for _, conn := range connections { |
| 106 | + if _, ok := idsToInclude[conn.RemotePeer()]; !ok { |
| 107 | + // remote peer ID not in `idsToInclude` set; filter connection out |
| 108 | + connections = append(connections[:], connections[1:]...) |
| 109 | + } |
| 110 | + } |
| 111 | + return connections, nil |
| 112 | +} |
| 113 | + |
| 114 | +func peerConnsRowConsumerFactory(conns []libp2pNetwork.Conn) utils.RowConsumer { |
| 115 | + return func(provideRow utils.RowProvider) error { |
| 116 | + for _, conn := range conns { |
| 117 | + err := provideRow( |
| 118 | + conn.RemotePeer().String(), |
| 119 | + conn.RemoteMultiaddr().String(), |
| 120 | + conn.Stat().Opened.String(), |
| 121 | + conn.Stat().Direction.String(), |
| 122 | + strconv.Itoa(conn.Stat().NumStreams), |
| 123 | + ) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + } |
| 128 | + return nil |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func getPeerIDs(peers []typesP2P.Peer) (ids map[libp2pPeer.ID]struct{}, err error) { |
| 133 | + for _, peer := range peers { |
| 134 | + addrInfo, err := utils.Libp2pAddrInfoFromPeer(peer) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + // ID already in set; continue |
| 140 | + if _, ok := ids[addrInfo.ID]; !ok { |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + // add ID to set |
| 145 | + ids[addrInfo.ID] = struct{}{} |
| 146 | + } |
| 147 | + return ids, nil |
| 148 | +} |
0 commit comments