Merge pull request #723 from milosgajdos83/sort-nodes-search

Sort the returned slice of nodes before searching
This commit is contained in:
Asim Aslam 2019-09-02 20:13:20 +01:00 committed by GitHub
commit 2f5e3c66b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,14 +71,21 @@ func (n *Network) Neighbourhood(ctx context.Context, req *pbNet.NeighbourhoodReq
// get all the nodes in the network
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
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
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(),