diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-11-04 18:59:31 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-11-04 18:59:31 +0800 |
commit | e165c2d23cf29fdec51c59f42f74735be2a8e1ff (patch) | |
tree | 375a6b3870c6b4f791c79128a4d7b29015b64851 /p2p/server.go | |
parent | dda3bf3ce7ef109541adb6f0020f8d2dd28e5e51 (diff) | |
parent | e46ab3bdcde7236c8fe54d6c83655e50bd19fe31 (diff) | |
download | dexon-e165c2d23cf29fdec51c59f42f74735be2a8e1ff.tar.gz dexon-e165c2d23cf29fdec51c59f42f74735be2a8e1ff.tar.zst dexon-e165c2d23cf29fdec51c59f42f74735be2a8e1ff.zip |
Merge pull request #1934 from karalabe/polish-protocol-infos
eth, p2p, rpc/api: polish protocol info gathering
Diffstat (limited to 'p2p/server.go')
-rw-r--r-- | p2p/server.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/p2p/server.go b/p2p/server.go index 6060adc71..ee670b10e 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -689,3 +689,66 @@ func (srv *Server) runPeer(p *Peer) { NumConnections: srv.PeerCount(), }) } + +// NodeInfo represents a short summary of the information known about the host. +type NodeInfo struct { + ID string `json:"id"` // Unique node identifier (also the encryption key) + Name string `json:"name"` // Name of the node, including client type, version, OS, custom data + Enode string `json:"enode"` // Enode URL for adding this peer from remote peers + IP string `json:"ip"` // IP address of the node + Ports struct { + Discovery int `json:"discovery"` // UDP listening port for discovery protocol + Listener int `json:"listener"` // TCP listening port for RLPx + } `json:"ports"` + ListenAddr string `json:"listenAddr"` + Protocols map[string]interface{} `json:"protocols"` +} + +// Info gathers and returns a collection of metadata known about the host. +func (srv *Server) NodeInfo() *NodeInfo { + node := srv.Self() + + // Gather and assemble the generic node infos + info := &NodeInfo{ + Name: srv.Name, + Enode: node.String(), + ID: node.ID.String(), + IP: node.IP.String(), + ListenAddr: srv.ListenAddr, + Protocols: make(map[string]interface{}), + } + info.Ports.Discovery = int(node.UDP) + info.Ports.Listener = int(node.TCP) + + // Gather all the running protocol infos (only once per protocol type) + for _, proto := range srv.Protocols { + if _, ok := info.Protocols[proto.Name]; !ok { + nodeInfo := interface{}("unknown") + if query := proto.NodeInfo; query != nil { + nodeInfo = proto.NodeInfo() + } + info.Protocols[proto.Name] = nodeInfo + } + } + return info +} + +// PeersInfo returns an array of metadata objects describing connected peers. +func (srv *Server) PeersInfo() []*PeerInfo { + // Gather all the generic and sub-protocol specific infos + infos := make([]*PeerInfo, 0, srv.PeerCount()) + for _, peer := range srv.Peers() { + if peer != nil { + infos = append(infos, peer.Info()) + } + } + // Sort the result array alphabetically by node identifier + for i := 0; i < len(infos); i++ { + for j := i + 1; j < len(infos); j++ { + if infos[i].ID > infos[j].ID { + infos[i], infos[j] = infos[j], infos[i] + } + } + } + return infos +} |