eth, p2p, rpc/api: polish protocol info gathering
This commit is contained in:
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user