From 6eb6d050ed960f4c3703fdad938143f9a465312b Mon Sep 17 00:00:00 2001
From: Milos Gajdos <milosgajdos83@gmail.com>
Date: Tue, 3 Sep 2019 16:39:27 +0100
Subject: [PATCH] Major bug overhaul in how we handle network.Nodes and related
 handler

---
 network/default.go          | 48 ++++++++++++++++++++----------
 network/handler/handler.go  | 42 +++++++++++++--------------
 network/proto/network.pb.go | 58 ++++++++++++++++++-------------------
 network/proto/network.proto |  2 +-
 4 files changed, 84 insertions(+), 66 deletions(-)

diff --git a/network/default.go b/network/default.go
index acac6cdc..d9d8cbfe 100644
--- a/network/default.go
+++ b/network/default.go
@@ -30,6 +30,7 @@ var (
 
 // node is network node
 type node struct {
+	sync.RWMutex
 	// id is node id
 	id string
 	// address is node address
@@ -60,9 +61,18 @@ func (n *node) Network() Network {
 // Neighbourhood returns node neighbourhood
 func (n *node) Neighbourhood() []Node {
 	var nodes []Node
-	for _, node := range n.neighbours {
-		nodes = append(nodes, node)
+	n.RLock()
+	for _, neighbourNode := range n.neighbours {
+		// make a copy of the node
+		n := &node{
+			id:      neighbourNode.id,
+			address: neighbourNode.address,
+			network: neighbourNode.network,
+		}
+		// NOTE: we do not care about neighbour's neighbours
+		nodes = append(nodes, n)
 	}
+	n.RUnlock()
 
 	return nodes
 }
@@ -287,13 +297,19 @@ func (n *network) processNetChan(l tunnel.Listener) {
 				if pbNetConnect.Node.Id == n.options.Id {
 					continue
 				}
-				neighbour := &node{
+				n.Lock()
+				// if the entry already exists skip adding it
+				if _, ok := n.neighbours[pbNetConnect.Node.Id]; ok {
+					n.Unlock()
+					continue
+				}
+				// add a new neighbour;
+				// NOTE: new node does not have any neighbours
+				n.neighbours[pbNetConnect.Node.Id] = &node{
 					id:         pbNetConnect.Node.Id,
 					address:    pbNetConnect.Node.Address,
 					neighbours: make(map[string]*node),
 				}
-				n.Lock()
-				n.neighbours[neighbour.id] = neighbour
 				n.Unlock()
 			case "neighbour":
 				pbNetNeighbour := &pbNet.Neighbour{}
@@ -305,22 +321,24 @@ func (n *network) processNetChan(l tunnel.Listener) {
 				if pbNetNeighbour.Node.Id == n.options.Id {
 					continue
 				}
-				neighbour := &node{
-					id:         pbNetNeighbour.Node.Id,
-					address:    pbNetNeighbour.Node.Address,
-					neighbours: make(map[string]*node),
-					lastSeen:   time.Now(),
-				}
 				n.Lock()
-				// we override the existing neighbour map
-				n.neighbours[neighbour.id] = neighbour
-				// store the neighbouring node and its neighbours
+				// only add the neighbour if it's not already in the neighbourhood
+				if _, ok := n.neighbours[pbNetNeighbour.Node.Id]; !ok {
+					neighbour := &node{
+						id:         pbNetNeighbour.Node.Id,
+						address:    pbNetNeighbour.Node.Address,
+						neighbours: make(map[string]*node),
+						lastSeen:   time.Now(),
+					}
+					n.neighbours[pbNetNeighbour.Node.Id] = neighbour
+				}
+				// update/store the neighbour node neighbours
 				for _, pbNeighbour := range pbNetNeighbour.Neighbours {
 					neighbourNode := &node{
 						id:      pbNeighbour.Id,
 						address: pbNeighbour.Address,
 					}
-					n.neighbours[neighbour.id].neighbours[neighbourNode.id] = neighbourNode
+					n.neighbours[pbNetNeighbour.Node.Id].neighbours[neighbourNode.id] = neighbourNode
 				}
 				n.Unlock()
 			case "close":
diff --git a/network/handler/handler.go b/network/handler/handler.go
index 67bca232..3740d4a9 100644
--- a/network/handler/handler.go
+++ b/network/handler/handler.go
@@ -76,35 +76,35 @@ func (n *Network) Neighbourhood(ctx context.Context, req *pbNet.NeighbourhoodReq
 	// find a node with a given id
 	i := sort.Search(len(nodes), func(j int) bool { return nodes[j].Id() >= id })
 
-	var neighbours []*pbNet.Neighbour
+	var neighbours []*pbNet.Node
 	// collect all the nodes in the neighbourhood of the found node
 	if i < len(nodes) && nodes[i].Id() == id {
 		for _, neighbour := range nodes[i].Neighbourhood() {
-			var nodeNeighbours []*pbNet.Node
-			for _, nodeNeighbour := range neighbour.Neighbourhood() {
-				// don't return yourself in response
-				if nodeNeighbour.Id() == n.Network.Id() {
-					continue
-				}
-				nn := &pbNet.Node{
-					Id:      nodeNeighbour.Id(),
-					Address: nodeNeighbour.Address(),
-				}
-				nodeNeighbours = append(nodeNeighbours, nn)
+			// don't return yourself in response
+			if neighbour.Id() == n.Network.Id() {
+				continue
 			}
-			// node is present at node[i]
-			neighbour := &pbNet.Neighbour{
-				Node: &pbNet.Node{
-					Id:      neighbour.Id(),
-					Address: neighbour.Address(),
-				},
-				Neighbours: nodeNeighbours,
+			pbNeighbour := &pbNet.Node{
+				Id:      neighbour.Id(),
+				Address: neighbour.Address(),
 			}
-			neighbours = append(neighbours, neighbour)
+			neighbours = append(neighbours, pbNeighbour)
 		}
 	}
 
-	resp.Neighbours = neighbours
+	// requested neighbourhood node
+	node := &pbNet.Node{
+		Id:      nodes[i].Id(),
+		Address: nodes[i].Address(),
+	}
+
+	// creaate neighbourhood answer
+	neighbourhood := &pbNet.Neighbour{
+		Node:       node,
+		Neighbours: neighbours,
+	}
+
+	resp.Neighbourhood = neighbourhood
 
 	return nil
 }
diff --git a/network/proto/network.pb.go b/network/proto/network.pb.go
index a6837679..74124324 100644
--- a/network/proto/network.pb.go
+++ b/network/proto/network.pb.go
@@ -135,10 +135,10 @@ func (m *NeighbourhoodRequest) GetId() string {
 
 // NeighbourhoodResponse contains node neighbourhood hierarchy
 type NeighbourhoodResponse struct {
-	Neighbours           []*Neighbour `protobuf:"bytes,1,rep,name=neighbours,proto3" json:"neighbours,omitempty"`
-	XXX_NoUnkeyedLiteral struct{}     `json:"-"`
-	XXX_unrecognized     []byte       `json:"-"`
-	XXX_sizecache        int32        `json:"-"`
+	Neighbourhood        *Neighbour `protobuf:"bytes,1,opt,name=neighbourhood,proto3" json:"neighbourhood,omitempty"`
+	XXX_NoUnkeyedLiteral struct{}   `json:"-"`
+	XXX_unrecognized     []byte     `json:"-"`
+	XXX_sizecache        int32      `json:"-"`
 }
 
 func (m *NeighbourhoodResponse) Reset()         { *m = NeighbourhoodResponse{} }
@@ -166,9 +166,9 @@ func (m *NeighbourhoodResponse) XXX_DiscardUnknown() {
 
 var xxx_messageInfo_NeighbourhoodResponse proto.InternalMessageInfo
 
-func (m *NeighbourhoodResponse) GetNeighbours() []*Neighbour {
+func (m *NeighbourhoodResponse) GetNeighbourhood() *Neighbour {
 	if m != nil {
-		return m.Neighbours
+		return m.Neighbourhood
 	}
 	return nil
 }
@@ -369,27 +369,27 @@ func init() {
 func init() { proto.RegisterFile("network.proto", fileDescriptor_8571034d60397816) }
 
 var fileDescriptor_8571034d60397816 = []byte{
-	// 344 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x52, 0x41, 0x4f, 0xf2, 0x40,
-	0x10, 0xfd, 0x28, 0xf0, 0x11, 0x06, 0x31, 0x66, 0xa3, 0xa6, 0xa9, 0xc1, 0x90, 0x3d, 0x20, 0x31,
-	0x5a, 0x0c, 0x44, 0x2f, 0x7a, 0xe3, 0xe0, 0x85, 0x70, 0x68, 0xfc, 0x03, 0x96, 0xdd, 0x94, 0x8d,
-	0xb2, 0x83, 0xbb, 0xdb, 0xf8, 0x07, 0xfc, 0xe1, 0xa6, 0xdb, 0x2d, 0x16, 0x10, 0x0c, 0xb7, 0xce,
-	0xcc, 0x7b, 0xf3, 0x66, 0xfb, 0x1e, 0xb4, 0x25, 0x37, 0x9f, 0xa8, 0xde, 0xc2, 0xa5, 0x42, 0x83,
-	0xe4, 0x24, 0xc1, 0x70, 0x21, 0x66, 0x0a, 0x43, 0xd7, 0x0f, 0x46, 0x89, 0x30, 0xf3, 0x34, 0x0e,
-	0x67, 0xb8, 0x18, 0xd8, 0xc9, 0x20, 0xc1, 0xdb, 0xfc, 0x43, 0x61, 0x6a, 0xb8, 0x1a, 0x58, 0xa6,
-	0x2b, 0xf2, 0x35, 0xb4, 0x0d, 0xad, 0x89, 0xd0, 0x26, 0xe2, 0x1f, 0x29, 0xd7, 0x86, 0x3e, 0xc1,
-	0x51, 0x5e, 0xea, 0x25, 0x4a, 0x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07, 0xa4, 0xb7, 0xc7, 0xeb, 0x52, 0xa0,
-	0x82, 0xab, 0x3f, 0x71, 0x85, 0x46, 0xfc, 0xdf, 0xe6, 0x7c, 0xf4, 0x1d, 0x00, 0x00, 0xff, 0xff,
-	0x11, 0x41, 0xd2, 0x02, 0x3f, 0x03, 0x00, 0x00,
+	// 348 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x52, 0x41, 0x4f, 0x32, 0x31,
+	0x10, 0xfd, 0x58, 0xe0, 0x23, 0x0c, 0x62, 0x4c, 0xa3, 0x66, 0xb3, 0x06, 0x43, 0x7a, 0x40, 0x62,
+	0x74, 0x31, 0x10, 0x3d, 0x79, 0x31, 0x1c, 0xbc, 0x10, 0x0e, 0x7b, 0xf4, 0xe6, 0xd2, 0x66, 0x69,
+	0x94, 0x1d, 0x6c, 0xbb, 0xf1, 0x0f, 0xf8, 0xc3, 0x4d, 0xbb, 0x05, 0x97, 0x45, 0x30, 0xdc, 0xda,
+	0x99, 0xf7, 0xe6, 0xcd, 0xe4, 0x3d, 0x68, 0xa7, 0x5c, 0x7f, 0xa2, 0x7c, 0x0b, 0x97, 0x12, 0x35,
+	0x92, 0x93, 0x04, 0xc3, 0x85, 0x98, 0x49, 0x0c, 0x5d, 0x3d, 0x18, 0x25, 0x42, 0xcf, 0xb3, 0x38,
+	0x9c, 0xe1, 0x62, 0x60, 0x3b, 0x83, 0x04, 0x6f, 0xf3, 0x87, 0xc4, 0x4c, 0x73, 0x39, 0xb0, 0x4c,
+	0xf7, 0xc9, 0xc7, 0xd0, 0x36, 0xb4, 0x26, 0x42, 0xe9, 0x88, 0x7f, 0x64, 0x5c, 0x69, 0xfa, 0x08,
+	0x47, 0xf9, 0x57, 0x2d, 0x31, 0x55, 0x9c, 0xdc, 0x40, 0x3d, 0x45, 0xc6, 0x95, 0x5f, 0xe9, 0x56,
+	0xfb, 0xad, 0xe1, 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0x2b, 0x8d, 0xf8, 0xbf, 0x4d, 0xfb, 0xe8, 0x3b,
+	0x00, 0x00, 0xff, 0xff, 0xfb, 0xa1, 0x6b, 0xb0, 0x45, 0x03, 0x00, 0x00,
 }
diff --git a/network/proto/network.proto b/network/proto/network.proto
index f540aafe..cfba6d04 100644
--- a/network/proto/network.proto
+++ b/network/proto/network.proto
@@ -26,7 +26,7 @@ message NeighbourhoodRequest {
 
 // NeighbourhoodResponse contains node neighbourhood hierarchy
 message NeighbourhoodResponse {
-        repeated Neighbour neighbours = 1;
+        Neighbour neighbourhood = 1;
 }
 
 // Node is network node