Merge pull request #723 from milosgajdos83/sort-nodes-search
Sort the returned slice of nodes before searching
This commit is contained in:
		| @@ -71,14 +71,21 @@ func (n *Network) Neighbourhood(ctx context.Context, req *pbNet.NeighbourhoodReq | |||||||
| 	// get all the nodes in the network | 	// get all the nodes in the network | ||||||
| 	nodes := n.Network.Nodes() | 	nodes := n.Network.Nodes() | ||||||
|  |  | ||||||
| 	var neighbours []*pbNet.Neighbour | 	// sort the slice of nodes | ||||||
|  | 	sort.Slice(nodes, func(i, j int) bool { return nodes[i].Id() <= nodes[j].Id() }) | ||||||
| 	// find a node with a given id | 	// find a node with a given id | ||||||
| 	i := sort.Search(len(nodes), func(i int) bool { return nodes[i].Id() == id }) | 	i := sort.Search(len(nodes), func(j int) bool { return nodes[j].Id() >= id }) | ||||||
|  |  | ||||||
|  | 	var neighbours []*pbNet.Neighbour | ||||||
| 	// collect all the nodes in the neighbourhood of the found node | 	// collect all the nodes in the neighbourhood of the found node | ||||||
| 	if i < len(nodes) && nodes[i].Id() == id { | 	if i < len(nodes) && nodes[i].Id() == id { | ||||||
| 		for _, neighbour := range nodes[i].Neighbourhood() { | 		for _, neighbour := range nodes[i].Neighbourhood() { | ||||||
| 			var nodeNeighbours []*pbNet.Node | 			var nodeNeighbours []*pbNet.Node | ||||||
| 			for _, nodeNeighbour := range neighbour.Neighbourhood() { | 			for _, nodeNeighbour := range neighbour.Neighbourhood() { | ||||||
|  | 				// don't return yourself in response | ||||||
|  | 				if nodeNeighbour.Id() == n.Network.Id() { | ||||||
|  | 					continue | ||||||
|  | 				} | ||||||
| 				nn := &pbNet.Node{ | 				nn := &pbNet.Node{ | ||||||
| 					Id:      nodeNeighbour.Id(), | 					Id:      nodeNeighbour.Id(), | ||||||
| 					Address: nodeNeighbour.Address(), | 					Address: nodeNeighbour.Address(), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user