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{}
}
// 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
// while itself dealing with peers management (handling connects/disconnects).
func (s *Server) run() {

View file

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