rpc/network: refactor getpeers logic

Deduplicate and simplify code.
This commit is contained in:
Roman Khimov 2020-01-10 15:13:29 +03:00
parent 336a94456f
commit d92e193e63
3 changed files with 50 additions and 44 deletions

View file

@ -198,6 +198,19 @@ func (s *Server) BadPeers() []string {
return []string{} return []string{}
} }
// ConnectedPeers returns a list of currently connected peers.
func (s *Server) ConnectedPeers() []string {
s.lock.RLock()
defer s.lock.RUnlock()
peers := make([]string, 0, len(s.peers))
for k := range s.peers {
peers = append(peers, k.PeerAddr().String())
}
return peers
}
// run is a goroutine that starts another goroutine to manage protocol specifics // run is a goroutine that starts another goroutine to manage protocol specifics
// while itself dealing with peers management (handling connects/disconnects). // while itself dealing with peers management (handling connects/disconnects).
func (s *Server) run() { func (s *Server) run() {

View file

@ -5,13 +5,16 @@ import (
) )
type ( type (
// Peers payload for outputting peers in `getpeers` RPC call. // GetPeers payload for outputting peers in `getpeers` RPC call.
Peers struct { GetPeers struct {
Unconnected []Peer `json:"unconnected"` Unconnected Peers `json:"unconnected"`
Connected []Peer `json:"connected"` Connected Peers `json:"connected"`
Bad []Peer `json:"bad"` Bad Peers `json:"bad"`
} }
// Peers represent a slice of peers.
Peers []Peer
// Peer represents the peer. // Peer represents the peer.
Peer struct { Peer struct {
Address string `json:"address"` Address string `json:"address"`
@ -19,40 +22,39 @@ type (
} }
) )
// NewPeers creates a new Peers struct. // NewGetPeers creates a new GetPeers structure.
func NewPeers() Peers { func NewGetPeers() GetPeers {
return Peers{ return GetPeers{
Unconnected: []Peer{}, Unconnected: []Peer{},
Connected: []Peer{}, Connected: []Peer{},
Bad: []Peer{}, Bad: []Peer{},
} }
} }
// AddPeer adds a peer to the given peer type slice. // AddUnconnected adds a set of peers to the unconnected peers slice.
func (p *Peers) AddPeer(peerType string, addr string) { func (g *GetPeers) AddUnconnected(addrs []string) {
addressParts := strings.Split(addr, ":") g.Unconnected.addPeers(addrs)
peer := Peer{ }
Address: addressParts[0],
Port: addressParts[1],
}
switch peerType { // AddConnected adds a set of peers to the connected peers slice.
case "unconnected": func (g *GetPeers) AddConnected(addrs []string) {
p.Unconnected = append( g.Connected.addPeers(addrs)
p.Unconnected, }
peer,
)
case "connected": // AddBad adds a set of peers to the bad peers slice.
p.Connected = append( func (g *GetPeers) AddBad(addrs []string) {
p.Connected, g.Bad.addPeers(addrs)
peer, }
)
case "bad": // addPeers adds a set of peers to the given peer slice.
p.Bad = append( func (p *Peers) addPeers(addrs []string) {
p.Bad, for i := range addrs {
peer, addressParts := strings.Split(addrs[i], ":")
) peer := Peer{
Address: addressParts[0],
Port: addressParts[1],
}
*p = append(*p, peer)
} }
} }

View file

@ -192,19 +192,10 @@ Methods:
case "getpeers": case "getpeers":
getpeersCalled.Inc() getpeersCalled.Inc()
peers := result.NewPeers() peers := result.NewGetPeers()
for _, addr := range s.coreServer.UnconnectedPeers() { peers.AddUnconnected(s.coreServer.UnconnectedPeers())
peers.AddPeer("unconnected", addr) peers.AddConnected(s.coreServer.ConnectedPeers())
} peers.AddBad(s.coreServer.BadPeers())
for _, addr := range s.coreServer.BadPeers() {
peers.AddPeer("bad", addr)
}
for addr := range s.coreServer.Peers() {
peers.AddPeer("connected", addr.PeerAddr().String())
}
results = peers results = peers
case "getstorage": case "getstorage":