Skip to content

Commit

Permalink
move lookup result construction into its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Mar 23, 2020
1 parent 3696bf5 commit 0a01437
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ func (dht *IpfsDHT) runDisjointQueries(ctx context.Context, d int, target string
queryCtx, cancelQuery := context.WithCancel(ctx)

// pick the K closest peers to the key in our Routing table and shuffle them.
seedPeers := dht.routingTable.NearestPeers(kb.ConvertKey(target), dht.bucketSize)
targetKadID := kb.ConvertKey(target)
seedPeers := dht.routingTable.NearestPeers(targetKadID, dht.bucketSize)
if len(seedPeers) == 0 {
routing.PublishQueryEvent(ctx, &routing.QueryEvent{
Type: routing.QueryError,
Expand Down Expand Up @@ -186,6 +187,12 @@ func (dht *IpfsDHT) runDisjointQueries(ctx context.Context, d int, target string
}
}

res := dht.constructLookupResult(queries, targetKadID)
return res, nil
}

// constructLookupResult takes the query information and uses it to construct the lookup result
func (dht *IpfsDHT) constructLookupResult(queries []*query, target kb.ID) *lookupResult {
// determine if any queries terminated early
completed := true
for _, q := range queries {
Expand Down Expand Up @@ -218,7 +225,7 @@ func (dht *IpfsDHT) runDisjointQueries(ctx context.Context, d int, target string
}

// get the top K overall peers
sortedPeers := kb.SortClosestPeers(peers, kb.ConvertKey(target))
sortedPeers := kb.SortClosestPeers(peers, target)
if len(sortedPeers) > dht.bucketSize {
sortedPeers = sortedPeers[:dht.bucketSize]
}
Expand All @@ -233,7 +240,8 @@ func (dht *IpfsDHT) runDisjointQueries(ctx context.Context, d int, target string
for i, p := range sortedPeers {
res.state[i] = peerState[p]
}
return res, nil

return res
}

type queryUpdate struct {
Expand Down

0 comments on commit 0a01437

Please sign in to comment.