Removed redundant lock. Simplified proto topology

This commit is contained in:
Milos Gajdos 2019-09-11 23:19:29 +01:00
parent d6be91e8af
commit fa4ff8921e
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F
3 changed files with 10 additions and 25 deletions

View File

@ -276,10 +276,7 @@ func (n *network) processNetChan(client transport.Client, listener tunnel.Listen
}
n.Unlock()
// get all the node peers down to MaxDepth encoded in protobuf
msg, err := n.node.getProtoTopology(MaxDepth)
if err != nil {
log.Debugf("Network unable to retrieve node topology: %s", err)
}
msg := n.node.getProtoTopology(MaxDepth)
// advertise yourself to the network
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
log.Debugf("Network failed to advertise peers: %v", err)
@ -396,12 +393,7 @@ func (n *network) announce(client transport.Client) {
case <-n.closed:
return
case <-announce.C:
msg, err := n.node.getProtoTopology(MaxDepth)
if err != nil {
log.Debugf("Network unable to retrieve node topology: %s", err)
continue
}
n.node.RUnlock()
msg := n.node.getProtoTopology(MaxDepth)
// advertise yourself to the network
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
log.Debugf("Network failed to advertise peers: %v", err)

View File

@ -129,7 +129,7 @@ func (n *node) Peers() []Node {
}
// getProtoTopology returns node topology down to the given depth encoded in protobuf
func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
func (n *node) getProtoTopology(depth uint) *pb.Peer {
n.RLock()
defer n.RUnlock()
@ -145,7 +145,7 @@ func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
// return if have either reached the depth or have no more peers
if depth == 0 || len(n.peers) == 0 {
return pbPeers, nil
return pbPeers
}
// decrement the depth
@ -155,10 +155,7 @@ func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
for _, peer := range n.peers {
// get peers of the node peers
// NOTE: this is [not] a recursive call
pbPeerPeer, err := peer.getProtoTopology(depth)
if err != nil {
return nil, err
}
pbPeerPeer := peer.getProtoTopology(depth)
// add current peer to explored peers
peers = append(peers, pbPeerPeer)
}
@ -166,7 +163,7 @@ func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
// add peers to the parent topology
pbPeers.Peers = peers
return pbPeers, nil
return pbPeers
}
// unpackPeer unpacks pb.Peer into node topology of given depth

View File

@ -266,10 +266,8 @@ func TestGetProtoTopology(t *testing.T) {
}
topCount := 0
protoTop, err := single.getProtoTopology(10)
if err != nil {
t.Errorf("Error getting proto topology: %s", err)
}
protoTop := single.getProtoTopology(10)
if len(protoTop.Peers) != topCount {
t.Errorf("Expected to find %d nodes, found: %d", topCount, len(protoTop.Peers))
}
@ -284,10 +282,8 @@ func TestGetProtoTopology(t *testing.T) {
peerIds[id] = true
}
// depth 1 should give us immmediate neighbours only
protoTop, err = node.getProtoTopology(1)
if err != nil {
t.Errorf("Error getting proto topology: %s", err)
}
protoTop = node.getProtoTopology(1)
if len(protoTop.Peers) != topCount {
t.Errorf("Expected to find %d nodes, found: %d", topCount, len(protoTop.Peers))
}