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/peer.go | |
parent | 05f74077fb1bc23937f3b25fd4e826dcf5789212 (diff) | |
download | go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.gz go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.bz2 go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.lz go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.xz go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.tar.zst go-tangerine-e46ab3bdcde7236c8fe54d6c83655e50bd19fe31.zip |
eth, p2p, rpc/api: polish protocol info gathering
Diffstat (limited to 'p2p/peer.go')
-rw-r--r-- | p2p/peer.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/p2p/peer.go b/p2p/peer.go index 1b3b19c79..72ed4069c 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -359,3 +359,49 @@ func (rw *protoRW) ReadMsg() (Msg, error) { return Msg{}, io.EOF } } + +// PeerInfo represents a short summary of the information known about a connected +// peer. Sub-protocol independent fields are contained and initialized here, with +// protocol specifics delegated to all connected sub-protocols. +type PeerInfo 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 + Caps []string `json:"caps"` // Sum-protocols advertised by this particular peer + Network struct { + LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection + RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection + } `json:"network"` + Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields +} + +// Info gathers and returns a collection of metadata known about a peer. +func (p *Peer) Info() *PeerInfo { + // Gather the protocol capabilities + var caps []string + for _, cap := range p.Caps() { + caps = append(caps, cap.String()) + } + // Assemble the generic peer metadata + info := &PeerInfo{ + ID: p.ID().String(), + Name: p.Name(), + Caps: caps, + Protocols: make(map[string]interface{}), + } + info.Network.LocalAddress = p.LocalAddr().String() + info.Network.RemoteAddress = p.RemoteAddr().String() + + // Gather all the running protocol infos + for _, proto := range p.running { + protoInfo := interface{}("unknown") + if query := proto.Protocol.PeerInfo; query != nil { + if metadata := query(p.ID()); metadata != nil { + protoInfo = metadata + } else { + protoInfo = "handshake" + } + } + info.Protocols[proto.Name] = protoInfo + } + return info +} |