@@ -3,8 +3,6 @@ import { InvalidPublicKeyError, NotFoundError } from '@libp2p/interface'
33import { peerIdFromPublicKey , peerIdFromMultihash } from '@libp2p/peer-id'
44import { Libp2pRecord } from '@libp2p/record'
55import * as Digest from 'multiformats/hashes/digest'
6- import { xor as uint8ArrayXor } from 'uint8arrays/xor'
7- import { xorCompare as uint8ArrayXorCompare } from 'uint8arrays/xor-compare'
86import { QueryError , InvalidRecordError } from '../errors.js'
97import { MessageType } from '../message/dht.js'
108import { PeerDistanceList } from '../peer-distance-list.js'
@@ -14,13 +12,14 @@ import {
1412 valueEvent
1513} from '../query/events.js'
1614import { verifyRecord } from '../record/validators.js'
17- import { convertBuffer , convertPeerId , keyForPublicKey } from '../utils.js'
15+ import { convertBuffer , keyForPublicKey } from '../utils.js'
1816import type { DHTRecord , FinalPeerEvent , QueryEvent , Validators } from '../index.js'
1917import type { Message } from '../message/dht.js'
2018import type { Network , SendMessageOptions } from '../network.js'
2119import type { QueryManager , QueryOptions } from '../query/manager.js'
2220import type { QueryFunc } from '../query/types.js'
2321import type { RoutingTable } from '../routing-table/index.js'
22+ import type { GetClosestPeersOptions } from '../routing-table/k-bucket.ts'
2423import type { ComponentLogger , Logger , Metrics , PeerId , PeerInfo , PeerStore , RoutingOptions } from '@libp2p/interface'
2524import type { ConnectionManager } from '@libp2p/interface-internal'
2625import type { AbortOptions } from 'it-pushable'
@@ -246,7 +245,7 @@ export class PeerRouting {
246245 const self = this
247246
248247 const getCloserPeersQuery : QueryFunc = async function * ( { peer, path, peerKadId, signal } ) {
249- self . log ( 'getClosestPeers asking %p' , peer )
248+ self . log ( 'getClosestPeers asking %p' , peer . id )
250249 const request : Partial < Message > = {
251250 type : MessageType . FIND_NODE ,
252251 key
@@ -336,10 +335,9 @@ export class PeerRouting {
336335 }
337336
338337 /**
339- * Get the peers in our routing table that are closer than the passed PeerId
340- * to the passed key
338+ * Get the peers in our routing table that are closest to the passed key
341339 */
342- async getCloserPeersOffline ( key : Uint8Array , closerThan : PeerId , options ?: AbortOptions ) : Promise < PeerInfo [ ] > {
340+ async getClosestPeersOffline ( key : Uint8Array , options ?: GetClosestPeersOptions ) : Promise < PeerInfo [ ] > {
343341 const output : PeerInfo [ ] = [ ]
344342
345343 // try getting the peer directly
@@ -356,19 +354,9 @@ export class PeerRouting {
356354 } catch { }
357355
358356 const keyKadId = await convertBuffer ( key , options )
359- const ids = this . routingTable . closestPeers ( keyKadId )
360- const closerThanKadId = await convertPeerId ( closerThan , options )
361- const requesterXor = uint8ArrayXor ( closerThanKadId , keyKadId )
357+ const ids = this . routingTable . closestPeers ( keyKadId , options )
362358
363359 for ( const peerId of ids ) {
364- const peerKadId = await convertPeerId ( peerId , options )
365- const peerXor = uint8ArrayXor ( peerKadId , keyKadId )
366-
367- // only include if peer is closer than requester
368- if ( uint8ArrayXorCompare ( peerXor , requesterXor ) !== - 1 ) {
369- continue
370- }
371-
372360 try {
373361 output . push ( await this . components . peerStore . getInfo ( peerId , options ) )
374362 } catch ( err : any ) {
@@ -379,9 +367,9 @@ export class PeerRouting {
379367 }
380368
381369 if ( output . length > 0 ) {
382- this . log ( 'getCloserPeersOffline found %d peer(s) closer to %b than %p ' , output . length , key , closerThan )
370+ this . log ( 'getClosestPeersOffline returning the %d closest peer(s) %b we know ' , output . length , key )
383371 } else {
384- this . log ( 'getCloserPeersOffline could not find peer closer to %b than %p with %d peers in the routing table' , key , closerThan , this . routingTable . size )
372+ this . log ( 'getClosestPeersOffline could not any peers close to %b with %d peers in the routing table' , key , this . routingTable . size )
385373 }
386374
387375 return output
0 commit comments