Sort the returned slice of nodes before searching

See docs:
https://golang.org/pkg/sort/#Search
This commit is contained in:
Milos Gajdos 2019-09-02 20:00:52 +01:00
parent 90745c14f2
commit a8d4299df9
No known key found for this signature in database
GPG Key ID: 8B31058CC55DFD4F

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(),