@@ -3,8 +3,6 @@ import { InvalidPublicKeyError, NotFoundError } from '@libp2p/interface'
3
3
import { peerIdFromPublicKey , peerIdFromMultihash } from '@libp2p/peer-id'
4
4
import { Libp2pRecord } from '@libp2p/record'
5
5
import * as Digest from 'multiformats/hashes/digest'
6
- import { xor as uint8ArrayXor } from 'uint8arrays/xor'
7
- import { xorCompare as uint8ArrayXorCompare } from 'uint8arrays/xor-compare'
8
6
import { QueryError , InvalidRecordError } from '../errors.js'
9
7
import { MessageType } from '../message/dht.js'
10
8
import { PeerDistanceList } from '../peer-distance-list.js'
@@ -14,13 +12,14 @@ import {
14
12
valueEvent
15
13
} from '../query/events.js'
16
14
import { verifyRecord } from '../record/validators.js'
17
- import { convertBuffer , convertPeerId , keyForPublicKey } from '../utils.js'
15
+ import { convertBuffer , keyForPublicKey } from '../utils.js'
18
16
import type { DHTRecord , FinalPeerEvent , QueryEvent , Validators } from '../index.js'
19
17
import type { Message } from '../message/dht.js'
20
18
import type { Network , SendMessageOptions } from '../network.js'
21
19
import type { QueryManager , QueryOptions } from '../query/manager.js'
22
20
import type { QueryFunc } from '../query/types.js'
23
21
import type { RoutingTable } from '../routing-table/index.js'
22
+ import type { GetClosestPeersOptions } from '../routing-table/k-bucket.ts'
24
23
import type { ComponentLogger , Logger , Metrics , PeerId , PeerInfo , PeerStore , RoutingOptions } from '@libp2p/interface'
25
24
import type { ConnectionManager } from '@libp2p/interface-internal'
26
25
import type { AbortOptions } from 'it-pushable'
@@ -246,7 +245,7 @@ export class PeerRouting {
246
245
const self = this
247
246
248
247
const getCloserPeersQuery : QueryFunc = async function * ( { peer, path, peerKadId, signal } ) {
249
- self . log ( 'getClosestPeers asking %p' , peer )
248
+ self . log ( 'getClosestPeers asking %p' , peer . id )
250
249
const request : Partial < Message > = {
251
250
type : MessageType . FIND_NODE ,
252
251
key
@@ -336,10 +335,9 @@ export class PeerRouting {
336
335
}
337
336
338
337
/**
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
341
339
*/
342
- async getCloserPeersOffline ( key : Uint8Array , closerThan : PeerId , options ?: AbortOptions ) : Promise < PeerInfo [ ] > {
340
+ async getClosestPeersOffline ( key : Uint8Array , options ?: GetClosestPeersOptions ) : Promise < PeerInfo [ ] > {
343
341
const output : PeerInfo [ ] = [ ]
344
342
345
343
// try getting the peer directly
@@ -356,19 +354,9 @@ export class PeerRouting {
356
354
} catch { }
357
355
358
356
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 )
362
358
363
359
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
-
372
360
try {
373
361
output . push ( await this . components . peerStore . getInfo ( peerId , options ) )
374
362
} catch ( err : any ) {
@@ -379,9 +367,9 @@ export class PeerRouting {
379
367
}
380
368
381
369
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 )
383
371
} 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 )
385
373
}
386
374
387
375
return output
0 commit comments