github.com/evdatsion/aphelion-dpos-bft@v0.32.1/rpc/core/net.go (about) 1 package core 2 3 import ( 4 "fmt" 5 6 "github.com/pkg/errors" 7 8 "github.com/evdatsion/aphelion-dpos-bft/p2p" 9 ctypes "github.com/evdatsion/aphelion-dpos-bft/rpc/core/types" 10 rpctypes "github.com/evdatsion/aphelion-dpos-bft/rpc/lib/types" 11 ) 12 13 // Get network info. 14 // 15 // ```shell 16 // curl 'localhost:26657/net_info' 17 // ``` 18 // 19 // ```go 20 // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket") 21 // err := client.Start() 22 // if err != nil { 23 // // handle error 24 // } 25 // defer client.Stop() 26 // info, err := client.NetInfo() 27 // ``` 28 // 29 // > The above command returns JSON structured like this: 30 // 31 // ```json 32 // { 33 // "jsonrpc": "2.0", 34 // "id": "", 35 // "result": { 36 // "listening": true, 37 // "listeners": [ 38 // "Listener(@)" 39 // ], 40 // "n_peers": "3", 41 // "peers": [ 42 // { 43 // "node_info": { 44 // "protocol_version": { 45 // "p2p": "7", 46 // "block": "8", 47 // "app": "1" 48 // }, 49 // "id": "93529da3435c090d02251a050342b6a488d4ab56", 50 // "listen_addr": "tcp://0.0.0.0:26656", 51 // "network": "chain-RFo6qC", 52 // "version": "0.30.0", 53 // "channels": "4020212223303800", 54 // "moniker": "fc89e4ed23f2", 55 // "other": { 56 // "tx_index": "on", 57 // "rpc_address": "tcp://0.0.0.0:26657" 58 // } 59 // }, 60 // "is_outbound": true, 61 // "connection_status": { 62 // "Duration": "3475230558", 63 // "SendMonitor": { 64 // "Active": true, 65 // "Start": "2019-02-14T12:40:47.52Z", 66 // "Duration": "3480000000", 67 // "Idle": "240000000", 68 // "Bytes": "4512", 69 // "Samples": "9", 70 // "InstRate": "1338", 71 // "CurRate": "2046", 72 // "AvgRate": "1297", 73 // "PeakRate": "6570", 74 // "BytesRem": "0", 75 // "TimeRem": "0", 76 // "Progress": 0 77 // }, 78 // "RecvMonitor": { 79 // "Active": true, 80 // "Start": "2019-02-14T12:40:47.52Z", 81 // "Duration": "3480000000", 82 // "Idle": "280000000", 83 // "Bytes": "4489", 84 // "Samples": "10", 85 // "InstRate": "1821", 86 // "CurRate": "1663", 87 // "AvgRate": "1290", 88 // "PeakRate": "5512", 89 // "BytesRem": "0", 90 // "TimeRem": "0", 91 // "Progress": 0 92 // }, 93 // "Channels": [ 94 // { 95 // "ID": 48, 96 // "SendQueueCapacity": "1", 97 // "SendQueueSize": "0", 98 // "Priority": "5", 99 // "RecentlySent": "0" 100 // }, 101 // { 102 // "ID": 64, 103 // "SendQueueCapacity": "1000", 104 // "SendQueueSize": "0", 105 // "Priority": "10", 106 // "RecentlySent": "14" 107 // }, 108 // { 109 // "ID": 32, 110 // "SendQueueCapacity": "100", 111 // "SendQueueSize": "0", 112 // "Priority": "5", 113 // "RecentlySent": "619" 114 // }, 115 // { 116 // "ID": 33, 117 // "SendQueueCapacity": "100", 118 // "SendQueueSize": "0", 119 // "Priority": "10", 120 // "RecentlySent": "1363" 121 // }, 122 // { 123 // "ID": 34, 124 // "SendQueueCapacity": "100", 125 // "SendQueueSize": "0", 126 // "Priority": "5", 127 // "RecentlySent": "2145" 128 // }, 129 // { 130 // "ID": 35, 131 // "SendQueueCapacity": "2", 132 // "SendQueueSize": "0", 133 // "Priority": "1", 134 // "RecentlySent": "0" 135 // }, 136 // { 137 // "ID": 56, 138 // "SendQueueCapacity": "1", 139 // "SendQueueSize": "0", 140 // "Priority": "5", 141 // "RecentlySent": "0" 142 // }, 143 // { 144 // "ID": 0, 145 // "SendQueueCapacity": "10", 146 // "SendQueueSize": "0", 147 // "Priority": "1", 148 // "RecentlySent": "10" 149 // } 150 // ] 151 // }, 152 // "remote_ip": "192.167.10.3" 153 // }, 154 // ... 155 // } 156 // ``` 157 func NetInfo(ctx *rpctypes.Context) (*ctypes.ResultNetInfo, error) { 158 out, in, _ := p2pPeers.NumPeers() 159 peers := make([]ctypes.Peer, 0, out+in) 160 for _, peer := range p2pPeers.Peers().List() { 161 nodeInfo, ok := peer.NodeInfo().(p2p.DefaultNodeInfo) 162 if !ok { 163 return nil, fmt.Errorf("peer.NodeInfo() is not DefaultNodeInfo") 164 } 165 peers = append(peers, ctypes.Peer{ 166 NodeInfo: nodeInfo, 167 IsOutbound: peer.IsOutbound(), 168 ConnectionStatus: peer.Status(), 169 RemoteIP: peer.RemoteIP().String(), 170 }) 171 } 172 // TODO: Should we include PersistentPeers and Seeds in here? 173 // PRO: useful info 174 // CON: privacy 175 return &ctypes.ResultNetInfo{ 176 Listening: p2pTransport.IsListening(), 177 Listeners: p2pTransport.Listeners(), 178 NPeers: len(peers), 179 Peers: peers, 180 }, nil 181 } 182 183 func UnsafeDialSeeds(ctx *rpctypes.Context, seeds []string) (*ctypes.ResultDialSeeds, error) { 184 if len(seeds) == 0 { 185 return &ctypes.ResultDialSeeds{}, errors.New("No seeds provided") 186 } 187 logger.Info("DialSeeds", "seeds", seeds) 188 if err := p2pPeers.DialPeersAsync(seeds); err != nil { 189 return &ctypes.ResultDialSeeds{}, err 190 } 191 return &ctypes.ResultDialSeeds{Log: "Dialing seeds in progress. See /net_info for details"}, nil 192 } 193 194 func UnsafeDialPeers(ctx *rpctypes.Context, peers []string, persistent bool) (*ctypes.ResultDialPeers, error) { 195 if len(peers) == 0 { 196 return &ctypes.ResultDialPeers{}, errors.New("No peers provided") 197 } 198 logger.Info("DialPeers", "peers", peers, "persistent", persistent) 199 if persistent { 200 if err := p2pPeers.AddPersistentPeers(peers); err != nil { 201 return &ctypes.ResultDialPeers{}, err 202 } 203 } 204 if err := p2pPeers.DialPeersAsync(peers); err != nil { 205 return &ctypes.ResultDialPeers{}, err 206 } 207 return &ctypes.ResultDialPeers{Log: "Dialing peers in progress. See /net_info for details"}, nil 208 } 209 210 // Get genesis file. 211 // 212 // ```shell 213 // curl 'localhost:26657/genesis' 214 // ``` 215 // 216 // ```go 217 // client := client.NewHTTP("tcp://0.0.0.0:26657", "/websocket") 218 // err := client.Start() 219 // if err != nil { 220 // // handle error 221 // } 222 // defer client.Stop() 223 // genesis, err := client.Genesis() 224 // ``` 225 // 226 // > The above command returns JSON structured like this: 227 // 228 // ```json 229 // { 230 // "error": "", 231 // "result": { 232 // "genesis": { 233 // "app_hash": "", 234 // "validators": [ 235 // { 236 // "name": "", 237 // "power": "10", 238 // "pub_key": { 239 // "data": "68DFDA7E50F82946E7E8546BED37944A422CD1B831E70DF66BA3B8430593944D", 240 // "type": "ed25519" 241 // } 242 // } 243 // ], 244 // "chain_id": "test-chain-6UTNIN", 245 // "genesis_time": "2017-05-29T15:05:41.671Z" 246 // } 247 // }, 248 // "id": "", 249 // "jsonrpc": "2.0" 250 // } 251 // ``` 252 func Genesis(ctx *rpctypes.Context) (*ctypes.ResultGenesis, error) { 253 return &ctypes.ResultGenesis{Genesis: genDoc}, nil 254 }