Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 2c0fc68

Browse files
authored
deps: update @libp2p/interface-peer-discovery to 2.0.0 (#469)
Updates the interface and symbol return type
1 parent c5749ed commit 2c0fc68

File tree

21 files changed

+351
-181
lines changed

21 files changed

+351
-181
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@
147147
"@libp2p/interface-connection": "^5.0.1",
148148
"@libp2p/interface-connection-manager": "^3.0.0",
149149
"@libp2p/interface-content-routing": "^2.1.0",
150-
"@libp2p/interface-dht": "^2.0.0",
151150
"@libp2p/interface-metrics": "^4.0.0",
152-
"@libp2p/interface-peer-discovery": "^1.0.1",
151+
"@libp2p/interface-peer-discovery": "^2.0.0",
153152
"@libp2p/interface-peer-id": "^2.0.0",
154153
"@libp2p/interface-peer-info": "^1.0.3",
155154
"@libp2p/interface-peer-routing": "^1.1.0",
@@ -191,7 +190,7 @@
191190
},
192191
"devDependencies": {
193192
"@libp2p/interface-libp2p": "^3.0.0",
194-
"@libp2p/interface-mocks": "^11.0.0",
193+
"@libp2p/interface-mocks": "^12.0.0",
195194
"@libp2p/peer-id-factory": "^2.0.0",
196195
"@libp2p/peer-store": "^8.0.0",
197196
"@types/lodash.random": "^3.2.6",

src/content-fetching/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import {
1616
queryErrorEvent
1717
} from '../query/events.js'
1818
import { createPutRecord, convertBuffer, bufferToRecordKey } from '../utils.js'
19-
import type { KadDHTComponents } from '../index.js'
19+
import type { KadDHTComponents, Validators, Selectors, ValueEvent, QueryOptions, QueryEvent } from '../index.js'
2020
import type { Network } from '../network.js'
2121
import type { PeerRouting } from '../peer-routing/index.js'
2222
import type { QueryManager } from '../query/manager.js'
2323
import type { QueryFunc } from '../query/types.js'
2424
import type { RoutingTable } from '../routing-table/index.js'
25-
import type { Validators, Selectors, ValueEvent, QueryOptions, QueryEvent } from '@libp2p/interface-dht'
2625
import type { AbortOptions } from '@libp2p/interfaces'
2726
import type { Logger } from '@libp2p/logger'
2827

src/content-routing/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import {
1010
providerEvent
1111
} from '../query/events.js'
1212
import { convertBuffer } from '../utils.js'
13-
import type { KadDHTComponents } from '../index.js'
13+
import type { KadDHTComponents, PeerResponseEvent, ProviderEvent, QueryEvent, QueryOptions } from '../index.js'
1414
import type { Network } from '../network.js'
1515
import type { PeerRouting } from '../peer-routing/index.js'
1616
import type { Providers } from '../providers.js'
1717
import type { QueryManager } from '../query/manager.js'
1818
import type { QueryFunc } from '../query/types.js'
1919
import type { RoutingTable } from '../routing-table/index.js'
20-
import type { PeerResponseEvent, ProviderEvent, QueryEvent, QueryOptions } from '@libp2p/interface-dht'
2120
import type { PeerInfo } from '@libp2p/interface-peer-info'
2221
import type { AbortOptions } from '@libp2p/interfaces'
2322
import type { Logger } from '@libp2p/logger'

src/dual-kad-dht.ts

Lines changed: 107 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,117 @@
1-
import { symbol } from '@libp2p/interface-peer-discovery'
1+
import { type ContentRouting, contentRouting } from '@libp2p/interface-content-routing'
2+
import { type PeerDiscovery, peerDiscovery, type PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
3+
import { type PeerRouting, peerRouting } from '@libp2p/interface-peer-routing'
24
import { CodeError } from '@libp2p/interfaces/errors'
35
import { EventEmitter, CustomEvent } from '@libp2p/interfaces/events'
46
import { logger } from '@libp2p/logger'
7+
import drain from 'it-drain'
58
import merge from 'it-merge'
9+
import { DefaultKadDHT } from './kad-dht.js'
610
import { queryErrorEvent } from './query/events.js'
7-
import type { KadDHTComponents } from './index.js'
8-
import type { KadDHT } from './kad-dht.js'
9-
import type { DualDHT, QueryEvent, QueryOptions } from '@libp2p/interface-dht'
10-
import type { PeerDiscoveryEvents } from '@libp2p/interface-peer-discovery'
11+
import type { DualKadDHT, KadDHT, KadDHTComponents, KadDHTInit, QueryEvent, QueryOptions } from './index.js'
1112
import type { PeerId } from '@libp2p/interface-peer-id'
13+
import type { PeerInfo } from '@libp2p/interface-peer-info'
1214
import type { AbortOptions } from '@libp2p/interfaces'
13-
import type { CID } from 'multiformats'
15+
import type { CID } from 'multiformats/cid'
1416

1517
const log = logger('libp2p:kad-dht')
1618

19+
/**
20+
* Wrapper class to convert events into returned values
21+
*/
22+
class DHTContentRouting implements ContentRouting {
23+
private readonly dht: KadDHT
24+
25+
constructor (dht: KadDHT) {
26+
this.dht = dht
27+
}
28+
29+
async provide (cid: CID): Promise<void> {
30+
await drain(this.dht.provide(cid))
31+
}
32+
33+
async * findProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<PeerInfo, void, undefined> {
34+
for await (const event of this.dht.findProviders(cid, options)) {
35+
if (event.name === 'PROVIDER') {
36+
yield * event.providers
37+
}
38+
}
39+
}
40+
41+
async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise<void> {
42+
await drain(this.dht.put(key, value, options))
43+
}
44+
45+
async get (key: Uint8Array, options?: AbortOptions): Promise<Uint8Array> {
46+
for await (const event of this.dht.get(key, options)) {
47+
if (event.name === 'VALUE') {
48+
return event.value
49+
}
50+
}
51+
52+
throw new CodeError('Not found', 'ERR_NOT_FOUND')
53+
}
54+
}
55+
56+
/**
57+
* Wrapper class to convert events into returned values
58+
*/
59+
class DHTPeerRouting implements PeerRouting {
60+
private readonly dht: KadDHT
61+
62+
constructor (dht: KadDHT) {
63+
this.dht = dht
64+
}
65+
66+
async findPeer (peerId: PeerId, options: AbortOptions = {}): Promise<PeerInfo> {
67+
for await (const event of this.dht.findPeer(peerId, options)) {
68+
if (event.name === 'FINAL_PEER') {
69+
return event.peer
70+
}
71+
}
72+
73+
throw new CodeError('Not found', 'ERR_NOT_FOUND')
74+
}
75+
76+
async * getClosestPeers (key: Uint8Array, options: AbortOptions = {}): AsyncIterable<PeerInfo> {
77+
for await (const event of this.dht.getClosestPeers(key, options)) {
78+
if (event.name === 'FINAL_PEER') {
79+
yield event.peer
80+
}
81+
}
82+
}
83+
}
84+
1785
/**
1886
* A DHT implementation modelled after Kademlia with S/Kademlia modifications.
1987
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
2088
*/
21-
export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT {
22-
public readonly wan: KadDHT
23-
public readonly lan: KadDHT
89+
export class DefaultDualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DualKadDHT, PeerDiscovery {
90+
public readonly wan: DefaultKadDHT
91+
public readonly lan: DefaultKadDHT
2492
public readonly components: KadDHTComponents
93+
private readonly contentRouting: ContentRouting
94+
private readonly peerRouting: PeerRouting
2595

26-
constructor (components: KadDHTComponents, wan: KadDHT, lan: KadDHT) {
96+
constructor (components: KadDHTComponents, init: KadDHTInit = {}) {
2797
super()
2898

2999
this.components = components
30-
this.wan = wan
31-
this.lan = lan
100+
101+
this.wan = new DefaultKadDHT(components, {
102+
protocolPrefix: '/ipfs',
103+
...init,
104+
lan: false
105+
})
106+
this.lan = new DefaultKadDHT(components, {
107+
protocolPrefix: '/ipfs',
108+
...init,
109+
clientMode: false,
110+
lan: true
111+
})
112+
113+
this.contentRouting = new DHTContentRouting(this)
114+
this.peerRouting = new DHTPeerRouting(this)
32115

33116
// handle peers being discovered during processing of DHT messages
34117
this.wan.addEventListener('peer', (evt) => {
@@ -43,10 +126,20 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements Dua
43126
})
44127
}
45128

46-
readonly [symbol] = true
47-
48129
readonly [Symbol.toStringTag] = '@libp2p/dual-kad-dht'
49130

131+
get [contentRouting] (): ContentRouting {
132+
return this.contentRouting
133+
}
134+
135+
get [peerRouting] (): PeerRouting {
136+
return this.peerRouting
137+
}
138+
139+
get [peerDiscovery] (): PeerDiscovery {
140+
return this
141+
}
142+
50143
/**
51144
* Is this DHT running.
52145
*/

0 commit comments

Comments
 (0)