-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathindex.ts
More file actions
394 lines (336 loc) · 11.7 KB
/
index.ts
File metadata and controls
394 lines (336 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import { publicKeyFromProtobuf } from '@libp2p/crypto/keys'
import { InvalidPublicKeyError, NotFoundError } from '@libp2p/interface'
import { peerIdFromPublicKey, peerIdFromMultihash } from '@libp2p/peer-id'
import { Libp2pRecord } from '@libp2p/record'
import * as Digest from 'multiformats/hashes/digest'
import { QueryError, InvalidRecordError } from '../errors.js'
import { MessageType } from '../message/dht.js'
import { PeerDistanceList } from '../peer-distance-list.js'
import {
queryErrorEvent,
finalPeerEvent,
valueEvent
} from '../query/events.js'
import { verifyRecord } from '../record/validators.js'
import { convertBuffer, keyForPublicKey } from '../utils.js'
import type { DHTRecord, FinalPeerEvent, QueryEvent, Validators } from '../index.js'
import type { Message } from '../message/dht.js'
import type { Network, SendMessageOptions } from '../network.js'
import type { QueryManager, QueryOptions } from '../query/manager.js'
import type { QueryFunc } from '../query/types.js'
import type { RoutingTable } from '../routing-table/index.js'
import type { GetClosestPeersOptions } from '../routing-table/k-bucket.ts'
import type { ComponentLogger, Logger, Metrics, PeerId, PeerInfo, PeerStore, RoutingOptions } from '@libp2p/interface'
import type { ConnectionManager } from '@libp2p/interface-internal'
import type { AbortOptions } from 'it-pushable'
export interface PeerRoutingComponents {
peerId: PeerId
peerStore: PeerStore
logger: ComponentLogger
metrics?: Metrics
connectionManager: ConnectionManager
}
export interface PeerRoutingInit {
routingTable: RoutingTable
network: Network
validators: Validators
queryManager: QueryManager
logPrefix: string
}
export class PeerRouting {
private readonly log: Logger
private readonly routingTable: RoutingTable
private readonly network: Network
private readonly validators: Validators
private readonly queryManager: QueryManager
private readonly components: PeerRoutingComponents
constructor (components: PeerRoutingComponents, init: PeerRoutingInit) {
this.routingTable = init.routingTable
this.network = init.network
this.validators = init.validators
this.queryManager = init.queryManager
this.components = components
this.log = components.logger.forComponent(`${init.logPrefix}:peer-routing`)
this.findPeer = components.metrics?.traceFunction('libp2p.kadDHT.findPeer', this.findPeer.bind(this), {
optionsIndex: 1
}) ?? this.findPeer
this.getClosestPeers = components.metrics?.traceFunction('libp2p.kadDHT.getClosestPeers', this.getClosestPeers.bind(this), {
optionsIndex: 1
}) ?? this.getClosestPeers
}
/**
* Look if we are connected to a peer with the given id.
* Returns its id and addresses, if found, otherwise `undefined`.
*/
async findPeerLocal (peer: PeerId, options?: AbortOptions): Promise<PeerInfo | undefined> {
let peerData
const p = await this.routingTable.find(peer, options)
if (p != null) {
this.log('findPeerLocal found %p in routing table', peer)
try {
peerData = await this.components.peerStore.get(p, options)
} catch (err: any) {
if (err.name !== 'NotFoundError') {
throw err
}
}
}
if (peerData == null) {
try {
peerData = await this.components.peerStore.get(peer, options)
} catch (err: any) {
if (err.name !== 'NotFoundError') {
throw err
}
}
}
if (peerData != null) {
this.log('findPeerLocal found %p in peer store', peer)
return {
id: peerData.id,
multiaddrs: peerData.addresses.map((address) => address.multiaddr)
}
}
return undefined
}
/**
* Get a value via rpc call for the given parameters
*/
async * _getValueSingle (peer: PeerId, key: Uint8Array, options: SendMessageOptions): AsyncGenerator<QueryEvent> {
const msg: Partial<Message> = {
type: MessageType.GET_VALUE,
key
}
yield * this.network.sendRequest(peer, msg, options)
}
/**
* Get the public key directly from a node
*/
async * getPublicKeyFromNode (peer: PeerId, options: RoutingOptions = {}): AsyncGenerator<QueryEvent> {
const pkKey = keyForPublicKey(peer)
const path = {
index: -1,
queued: 0,
running: 0,
total: 0
}
for await (const event of this._getValueSingle(peer, pkKey, {
...options,
path
})) {
yield event
if (event.name === 'PEER_RESPONSE' && event.record != null) {
const publicKey = publicKeyFromProtobuf(event.record.value)
const recPeer = peerIdFromPublicKey(publicKey)
// compare hashes of the pub key
if (!recPeer.equals(peer)) {
throw new InvalidPublicKeyError('public key does not match id')
}
if (recPeer.publicKey == null) {
throw new InvalidPublicKeyError('public key missing')
}
yield valueEvent({
from: peer,
value: event.record.value,
path
}, options)
}
}
throw new QueryError(`Node not responding with its public key: ${peer.toString()}`)
}
/**
* Search for a peer with the given ID
*/
async * findPeer (id: PeerId, options: RoutingOptions = {}): AsyncGenerator<FinalPeerEvent | QueryEvent> {
this.log('findPeer %p', id)
if (options.useCache !== false) {
// Try to find locally
const pi = await this.findPeerLocal(id, options)
// already got it
if (pi != null) {
this.log('found local')
yield finalPeerEvent({
from: this.components.peerId,
peer: pi,
path: {
index: -1,
queued: 0,
running: 0,
total: 0
}
}, options)
return
}
}
let foundPeer = false
if (options.useNetwork !== false) {
const self = this
const findPeerQuery: QueryFunc = async function * ({ peer, signal, path }) {
const request: Partial<Message> = {
type: MessageType.FIND_NODE,
key: id.toMultihash().bytes
}
for await (const event of self.network.sendRequest(peer.id, request, {
...options,
signal,
path
})) {
yield event
if (event.name === 'PEER_RESPONSE') {
const match = event.closer.find((p) => p.id.equals(id))
// found the peer
if (match != null) {
yield finalPeerEvent({
from: event.from,
peer: match,
path: event.path
}, options)
}
}
}
}
for await (const event of this.queryManager.run(id.toMultihash().bytes, findPeerQuery, options)) {
if (event.name === 'FINAL_PEER') {
foundPeer = true
}
yield event
}
}
if (!foundPeer) {
throw new NotFoundError('Not found')
}
}
/**
* Kademlia 'FIND_NODE' operation on a key, which could be the bytes from a
* multihash or a peer ID
*/
async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncGenerator<QueryEvent> {
this.log('getClosestPeers to %b', key)
const self = this
// Accumulate the K closest peers that respond during the traversal.
// FINAL_PEER events are only emitted after the query fully converges -
// partial results from a timed-out query are not emitted because the DHT
// requires crossover between independent nodes resolving the same key.
const keyKadId = await convertBuffer(key, options)
const closestPeers = new PeerDistanceList(keyKadId, this.routingTable.kBucketSize)
const getCloserPeersQuery: QueryFunc = async function * ({ peer, path, signal }) {
self.log('getClosestPeers asking %p', peer.id)
const request: Partial<Message> = {
type: MessageType.FIND_NODE,
key
}
let contacted = false
for await (const event of self.network.sendRequest(peer.id, request, {
...options,
signal,
path
})) {
if (event.name === 'PEER_RESPONSE') {
contacted = true
}
yield event
}
if (!contacted) {
return
}
try {
let peerInfo = peer
if (peerInfo.multiaddrs.length === 0) {
peerInfo = await self.components.peerStore.getInfo(peer.id)
}
if (peerInfo.multiaddrs.length > 0) {
// omit signal - peer ID hashing is fast and we don't want
// an aborted signal to prevent recording a successful contact
await closestPeers.add(peerInfo, path)
}
} catch {
// peer info may not be in the peer store
}
}
yield * this.queryManager.run(key, getCloserPeersQuery, options)
// only reached on successful convergence - emit the K closest peers found
for (const { peer, path } of closestPeers.peers) {
yield finalPeerEvent({
from: this.components.peerId,
peer,
path: {
index: path.index,
queued: 0,
running: 0,
total: 0
}
}, options)
}
}
/**
* Query a particular peer for the value for the given key.
* It will either return the value or a list of closer peers.
*
* Note: The peerStore is updated with new addresses found for the given peer.
*/
async * getValueOrPeers (peer: PeerId, key: Uint8Array, options: SendMessageOptions): AsyncGenerator<QueryEvent> {
for await (const event of this._getValueSingle(peer, key, options)) {
if (event.name === 'PEER_RESPONSE') {
if (event.record != null) {
// We have a record
try {
await this._verifyRecordOnline(event.record, options)
} catch (err: any) {
const errMsg = 'invalid record received, discarded'
this.log(errMsg)
yield queryErrorEvent({
from: event.from,
error: new QueryError(errMsg),
path: options.path
}, options)
continue
}
}
}
yield event
}
}
/**
* Verify a record, fetching missing public keys from the network.
* Throws an error if the record is invalid.
*/
async _verifyRecordOnline (record: DHTRecord, options?: AbortOptions): Promise<void> {
if (record.timeReceived == null) {
throw new InvalidRecordError('invalid record received')
}
await verifyRecord(this.validators, new Libp2pRecord(record.key, record.value, record.timeReceived), options)
}
/**
* Get the peers in our routing table that are closest to the passed key
*/
async getClosestPeersOffline (key: Uint8Array, options?: GetClosestPeersOptions): Promise<PeerInfo[]> {
const output: PeerInfo[] = []
// try getting the peer directly
try {
const multihash = Digest.decode(key)
const targetPeerId = peerIdFromMultihash(multihash)
const peer = await this.components.peerStore.get(targetPeerId, options)
output.push({
id: peer.id,
multiaddrs: peer.addresses.map(({ multiaddr }) => multiaddr)
})
} catch { /* key may not be a valid peer multihash */ }
const keyKadId = await convertBuffer(key, options)
const ids = this.routingTable.closestPeers(keyKadId, options)
for (const peerId of ids) {
try {
output.push(await this.components.peerStore.getInfo(peerId, options))
} catch (err: any) {
if (err.name !== 'NotFoundError') {
throw err
}
}
}
if (output.length > 0) {
this.log('getClosestPeersOffline returning the %d closest peer(s) we know to %b', output.length, key)
} else {
this.log('getClosestPeersOffline could not any peers close to %b with %d peers in the routing table', key, this.routingTable.size)
}
return output
}
}