Removed redundant lock. Simplified proto topology
This commit is contained in:
parent
d6be91e8af
commit
fa4ff8921e
@ -276,10 +276,7 @@ func (n *network) processNetChan(client transport.Client, listener tunnel.Listen
|
|||||||
}
|
}
|
||||||
n.Unlock()
|
n.Unlock()
|
||||||
// get all the node peers down to MaxDepth encoded in protobuf
|
// get all the node peers down to MaxDepth encoded in protobuf
|
||||||
msg, err := n.node.getProtoTopology(MaxDepth)
|
msg := n.node.getProtoTopology(MaxDepth)
|
||||||
if err != nil {
|
|
||||||
log.Debugf("Network unable to retrieve node topology: %s", err)
|
|
||||||
}
|
|
||||||
// advertise yourself to the network
|
// advertise yourself to the network
|
||||||
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
|
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
|
||||||
log.Debugf("Network failed to advertise peers: %v", err)
|
log.Debugf("Network failed to advertise peers: %v", err)
|
||||||
@ -396,12 +393,7 @@ func (n *network) announce(client transport.Client) {
|
|||||||
case <-n.closed:
|
case <-n.closed:
|
||||||
return
|
return
|
||||||
case <-announce.C:
|
case <-announce.C:
|
||||||
msg, err := n.node.getProtoTopology(MaxDepth)
|
msg := n.node.getProtoTopology(MaxDepth)
|
||||||
if err != nil {
|
|
||||||
log.Debugf("Network unable to retrieve node topology: %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
n.node.RUnlock()
|
|
||||||
// advertise yourself to the network
|
// advertise yourself to the network
|
||||||
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
|
if err := n.sendMsg("peer", msg, NetworkChannel); err != nil {
|
||||||
log.Debugf("Network failed to advertise peers: %v", err)
|
log.Debugf("Network failed to advertise peers: %v", err)
|
||||||
|
@ -129,7 +129,7 @@ func (n *node) Peers() []Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getProtoTopology returns node topology down to the given depth encoded in protobuf
|
// 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()
|
n.RLock()
|
||||||
defer n.RUnlock()
|
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
|
// return if have either reached the depth or have no more peers
|
||||||
if depth == 0 || len(n.peers) == 0 {
|
if depth == 0 || len(n.peers) == 0 {
|
||||||
return pbPeers, nil
|
return pbPeers
|
||||||
}
|
}
|
||||||
|
|
||||||
// decrement the depth
|
// decrement the depth
|
||||||
@ -155,10 +155,7 @@ func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
|
|||||||
for _, peer := range n.peers {
|
for _, peer := range n.peers {
|
||||||
// get peers of the node peers
|
// get peers of the node peers
|
||||||
// NOTE: this is [not] a recursive call
|
// NOTE: this is [not] a recursive call
|
||||||
pbPeerPeer, err := peer.getProtoTopology(depth)
|
pbPeerPeer := peer.getProtoTopology(depth)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// add current peer to explored peers
|
// add current peer to explored peers
|
||||||
peers = append(peers, pbPeerPeer)
|
peers = append(peers, pbPeerPeer)
|
||||||
}
|
}
|
||||||
@ -166,7 +163,7 @@ func (n *node) getProtoTopology(depth uint) (*pb.Peer, error) {
|
|||||||
// add peers to the parent topology
|
// add peers to the parent topology
|
||||||
pbPeers.Peers = peers
|
pbPeers.Peers = peers
|
||||||
|
|
||||||
return pbPeers, nil
|
return pbPeers
|
||||||
}
|
}
|
||||||
|
|
||||||
// unpackPeer unpacks pb.Peer into node topology of given depth
|
// unpackPeer unpacks pb.Peer into node topology of given depth
|
||||||
|
@ -266,10 +266,8 @@ func TestGetProtoTopology(t *testing.T) {
|
|||||||
}
|
}
|
||||||
topCount := 0
|
topCount := 0
|
||||||
|
|
||||||
protoTop, err := single.getProtoTopology(10)
|
protoTop := single.getProtoTopology(10)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Error getting proto topology: %s", err)
|
|
||||||
}
|
|
||||||
if len(protoTop.Peers) != topCount {
|
if len(protoTop.Peers) != topCount {
|
||||||
t.Errorf("Expected to find %d nodes, found: %d", topCount, len(protoTop.Peers))
|
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
|
peerIds[id] = true
|
||||||
}
|
}
|
||||||
// depth 1 should give us immmediate neighbours only
|
// depth 1 should give us immmediate neighbours only
|
||||||
protoTop, err = node.getProtoTopology(1)
|
protoTop = node.getProtoTopology(1)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Error getting proto topology: %s", err)
|
|
||||||
}
|
|
||||||
if len(protoTop.Peers) != topCount {
|
if len(protoTop.Peers) != topCount {
|
||||||
t.Errorf("Expected to find %d nodes, found: %d", topCount, len(protoTop.Peers))
|
t.Errorf("Expected to find %d nodes, found: %d", topCount, len(protoTop.Peers))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user