Skip to content

Commit

Permalink
dht: kick off all the queries wit every node in our rt
Browse files Browse the repository at this point in the history
s/kademlia calls for makign sure to query all peers we
have in our routing table, not just those closest. this
helps ensure most queries resolve properly.
  • Loading branch information
jbenet committed Jan 19, 2015
1 parent 1db5be1 commit 71aa5f9
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions routing/dht/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
// get closest peers in the routing table
rtp := dht.routingTable.ListPeers()
log.Debugf("peers in rt: %s", len(rtp), rtp)

closest := dht.routingTable.NearestPeers(kb.ConvertKey(key), PoolSize)
if closest == nil || len(closest) == 0 {
if len(rtp) == 0 {
log.Warning("No peers from routing table!")
return nil, errors.Wrap(kb.ErrLookupFailure)
}
Expand All @@ -111,7 +109,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
})

// run it!
result, err := query.Run(ctx, closest)
result, err := query.Run(ctx, rtp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -170,7 +168,7 @@ func (dht *IpfsDHT) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerIn
// to the given key
func (dht *IpfsDHT) getClosestPeers(ctx context.Context, key u.Key) (<-chan peer.ID, error) {
e := log.EventBegin(ctx, "getClosestPeers", &key)
tablepeers := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
tablepeers := dht.routingTable.ListPeers()
if len(tablepeers) == 0 {
return nil, errors.Wrap(kb.ErrLookupFailure)
}
Expand Down Expand Up @@ -313,7 +311,7 @@ func (dht *IpfsDHT) findProvidersAsyncRoutine(ctx context.Context, key u.Key, co
return &dhtQueryResult{closerPeers: clpeers}, nil
})

peers := dht.routingTable.NearestPeers(kb.ConvertKey(key), AlphaValue)
peers := dht.routingTable.ListPeers()
_, err := query.Run(ctx, peers)
if err != nil {
log.Errorf("Query error: %s", err)
Expand All @@ -329,13 +327,13 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (peer.PeerInfo, er
return pi, nil
}

closest := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), AlphaValue)
if closest == nil || len(closest) == 0 {
peers := dht.routingTable.ListPeers()
if len(peers) == 0 {
return peer.PeerInfo{}, errors.Wrap(kb.ErrLookupFailure)
}

// Sanity...
for _, p := range closest {
for _, p := range peers {
if p == id {
log.Error("Found target peer in list of closest peers...")
return dht.peerstore.PeerInfo(p), nil
Expand Down Expand Up @@ -367,7 +365,7 @@ func (dht *IpfsDHT) FindPeer(ctx context.Context, id peer.ID) (peer.PeerInfo, er
})

// run it!
result, err := query.Run(ctx, closest)
result, err := query.Run(ctx, peers)
if err != nil {
return peer.PeerInfo{}, err
}
Expand All @@ -386,8 +384,8 @@ func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<
peerchan := make(chan peer.PeerInfo, asyncQueryBuffer)
peersSeen := peer.Set{}

closest := dht.routingTable.NearestPeers(kb.ConvertPeerID(id), AlphaValue)
if closest == nil || len(closest) == 0 {
peers := dht.routingTable.ListPeers()
if len(peers) == 0 {
return nil, errors.Wrap(kb.ErrLookupFailure)
}

Expand Down Expand Up @@ -432,7 +430,7 @@ func (dht *IpfsDHT) FindPeersConnectedToPeer(ctx context.Context, id peer.ID) (<
// run it! run it asynchronously to gen peers as results are found.
// this does no error checking
go func() {
if _, err := query.Run(ctx, closest); err != nil {
if _, err := query.Run(ctx, peers); err != nil {
log.Error(err)
}

Expand Down

0 comments on commit 71aa5f9

Please sign in to comment.