diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-10-27 21:10:30 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-10-28 18:44:15 +0800 |
commit | e46ab3bdcde7236c8fe54d6c83655e50bd19fe31 (patch) | |
tree | 4f30c5f9757ebfbde6501e1d53484ff51fd2fad9 /p2p/server.go | |
parent | 05f74077fb1bc23937f3b25fd4e826dcf5789212 (diff) | |
download | dexon-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.gz dexon-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.zst dexon-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.zip |
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 +} |