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

Commit 940aba3

Browse files
authored
fix: update interfaces (#302)
Updates to latest code from libp2p/js-libp2p-interfaces#180
1 parent 8debbfe commit 940aba3

35 files changed

+592
-568
lines changed

package.json

+20-20
Original file line numberDiff line numberDiff line change
@@ -136,48 +136,48 @@
136136
"release": "semantic-release"
137137
},
138138
"dependencies": {
139-
"@libp2p/crypto": "^0.22.7",
140-
"@libp2p/interfaces": "^1.3.2",
141-
"@libp2p/logger": "^1.0.2",
142-
"@libp2p/peer-id": "^1.0.4",
139+
"@libp2p/crypto": "^0.22.9",
140+
"@libp2p/interfaces": "^1.3.14",
141+
"@libp2p/logger": "^1.1.2",
142+
"@libp2p/peer-id": "^1.1.8",
143143
"@libp2p/record": "^1.0.0",
144-
"@libp2p/topology": "^1.0.3",
145-
"@multiformats/multiaddr": "^10.0.0",
144+
"@libp2p/topology": "^1.1.6",
145+
"@multiformats/multiaddr": "^10.1.5",
146146
"any-signal": "^3.0.0",
147147
"datastore-core": "^7.0.0",
148-
"err-code": "^3.0.0",
148+
"err-code": "^3.0.1",
149149
"hashlru": "^2.3.0",
150-
"interface-datastore": "^6.0.2",
151-
"it-all": "^1.0.5",
152-
"it-drain": "^1.0.4",
153-
"it-first": "^1.0.4",
150+
"interface-datastore": "^6.1.0",
151+
"it-all": "^1.0.6",
152+
"it-drain": "^1.0.5",
153+
"it-first": "^1.0.6",
154154
"it-length": "^1.0.3",
155-
"it-length-prefixed": "^7.0.0",
156-
"it-map": "^1.0.5",
155+
"it-length-prefixed": "^7.0.1",
156+
"it-map": "^1.0.6",
157157
"it-merge": "^1.0.3",
158158
"it-parallel": "^2.0.1",
159159
"it-pipe": "^2.0.3",
160160
"it-stream-types": "^1.0.4",
161161
"it-take": "^1.0.2",
162162
"k-bucket": "^5.1.0",
163-
"multiformats": "^9.4.5",
163+
"multiformats": "^9.6.3",
164164
"p-defer": "^4.0.0",
165165
"p-map": "^5.3.0",
166166
"p-queue": "^7.2.0",
167167
"private-ip": "^2.3.3",
168-
"protobufjs": "^6.10.2",
168+
"protobufjs": "^6.11.2",
169169
"streaming-iterables": "^6.0.0",
170170
"timeout-abort-controller": "^3.0.0",
171171
"uint8arrays": "^3.0.0",
172172
"varint": "^6.0.0"
173173
},
174174
"devDependencies": {
175-
"@libp2p/interface-compliance-tests": "^1.1.3",
176-
"@libp2p/peer-id-factory": "^1.0.4",
177-
"@libp2p/peer-store": "^1.0.1",
175+
"@libp2p/interface-compliance-tests": "^1.1.16",
176+
"@libp2p/peer-id-factory": "^1.0.8",
177+
"@libp2p/peer-store": "^1.0.6",
178178
"@types/lodash.random": "^3.2.6",
179179
"@types/lodash.range": "^3.2.6",
180-
"@types/node": "^16.11.7",
180+
"@types/node": "^16.11.26",
181181
"@types/varint": "^6.0.0",
182182
"@types/which": "^2.0.1",
183183
"aegir": "^36.1.3",
@@ -187,11 +187,11 @@
187187
"it-filter": "^1.0.3",
188188
"it-last": "^1.0.6",
189189
"it-pair": "^2.0.2",
190-
"libp2p": "^0.36.2",
191190
"lodash.random": "^3.2.0",
192191
"lodash.range": "^3.2.0",
193192
"p-retry": "^5.0.0",
194193
"sinon": "^13.0.1",
194+
"ts-sinon": "^2.0.2",
195195
"which": "^2.0.2"
196196
},
197197
"browser": {

src/content-fetching/index.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,17 @@ import {
1616
} from '../constants.js'
1717
import { createPutRecord, convertBuffer, bufferToRecordKey } from '../utils.js'
1818
import { logger } from '@libp2p/logger'
19-
import type { PeerId } from '@libp2p/interfaces/peer-id'
2019
import type { Validators, Selectors, ValueEvent, QueryOptions } from '@libp2p/interfaces/dht'
21-
import type { Datastore } from 'interface-datastore'
2220
import type { PeerRouting } from '../peer-routing/index.js'
2321
import type { QueryManager } from '../query/manager.js'
2422
import type { RoutingTable } from '../routing-table/index.js'
2523
import type { Network } from '../network.js'
2624
import type { Logger } from '@libp2p/logger'
2725
import type { AbortOptions } from '@libp2p/interfaces'
2826
import type { QueryFunc } from '../query/types.js'
27+
import { Components, Initializable } from '@libp2p/interfaces/components'
2928

30-
export interface ContentFetchingOptions {
31-
peerId: PeerId
32-
datastore: Datastore
29+
export interface ContentFetchingInit {
3330
validators: Validators
3431
selectors: Selectors
3532
peerRouting: PeerRouting
@@ -39,22 +36,19 @@ export interface ContentFetchingOptions {
3936
lan: boolean
4037
}
4138

42-
export class ContentFetching {
39+
export class ContentFetching implements Initializable {
4340
private readonly log: Logger
44-
private readonly peerId: PeerId
45-
private readonly datastore: Datastore
41+
private components: Components = new Components()
4642
private readonly validators: Validators
4743
private readonly selectors: Selectors
4844
private readonly peerRouting: PeerRouting
4945
private readonly queryManager: QueryManager
5046
private readonly routingTable: RoutingTable
5147
private readonly network: Network
5248

53-
constructor (options: ContentFetchingOptions) {
54-
const { peerId, datastore, validators, selectors, peerRouting, queryManager, routingTable, network, lan } = options
55-
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:content-fetching:${peerId.toString()}`)
56-
this.peerId = peerId
57-
this.datastore = datastore
49+
constructor (init: ContentFetchingInit) {
50+
const { validators, selectors, peerRouting, queryManager, routingTable, network, lan } = init
51+
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:content-fetching`)
5852
this.validators = validators
5953
this.selectors = selectors
6054
this.peerRouting = peerRouting
@@ -63,9 +57,13 @@ export class ContentFetching {
6357
this.network = network
6458
}
6559

60+
init (components: Components): void {
61+
this.components = components
62+
}
63+
6664
async putLocal (key: Uint8Array, rec: Uint8Array) { // eslint-disable-line require-await
6765
const dsKey = bufferToRecordKey(key)
68-
await this.datastore.put(dsKey, rec)
66+
await this.components.getDatastore().put(dsKey, rec)
6967
}
7068

7169
/**
@@ -79,7 +77,7 @@ export class ContentFetching {
7977

8078
this.log('fetching record for key %k', dsKey)
8179

82-
const raw = await this.datastore.get(dsKey)
80+
const raw = await this.components.getDatastore().get(dsKey)
8381
this.log('found %k in local datastore', dsKey)
8482

8583
const rec = Libp2pRecord.deserialize(raw)
@@ -104,11 +102,11 @@ export class ContentFetching {
104102
}
105103

106104
// correct ourself
107-
if (this.peerId.equals(from)) {
105+
if (this.components.getPeerId().equals(from)) {
108106
try {
109107
const dsKey = bufferToRecordKey(key)
110108
this.log(`Storing corrected record for key ${dsKey.toString()}`)
111-
await this.datastore.put(dsKey, fixupRec)
109+
await this.components.getDatastore().put(dsKey, fixupRec)
112110
} catch (err: any) {
113111
this.log.error('Failed error correcting self', err)
114112
}
@@ -149,7 +147,7 @@ export class ContentFetching {
149147
// store the record locally
150148
const dsKey = bufferToRecordKey(key)
151149
this.log(`storing record for key ${dsKey.toString()}`)
152-
await this.datastore.put(dsKey, record)
150+
await this.components.getDatastore().put(dsKey, record)
153151

154152
// put record to the closest peers
155153
yield * pipe(
@@ -247,7 +245,7 @@ export class ContentFetching {
247245

248246
yield valueEvent({
249247
value: localRec.value,
250-
from: this.peerId
248+
from: this.components.getPeerId()
251249
})
252250
} catch (err: any) {
253251
this.log('error getting local value for %b', key, err)

src/content-routing/index.ts

+18-21
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,51 @@ import {
1010
providerEvent
1111
} from '../query/events.js'
1212
import { logger } from '@libp2p/logger'
13-
import type { PeerId } from '@libp2p/interfaces/peer-id'
1413
import type { QueryEvent, QueryOptions } from '@libp2p/interfaces/dht'
1514
import type { PeerRouting } from '../peer-routing/index.js'
1615
import type { QueryManager } from '../query/manager.js'
1716
import type { RoutingTable } from '../routing-table/index.js'
1817
import type { Network } from '../network.js'
1918
import type { Logger } from '@libp2p/logger'
2019
import type { Providers } from '../providers.js'
21-
import type { PeerStore } from '@libp2p/interfaces/peer-store'
2220
import type { QueryFunc } from '../query/types.js'
2321
import type { CID } from 'multiformats/cid'
2422
import type { AbortOptions } from '@libp2p/interfaces'
2523
import type { Multiaddr } from '@multiformats/multiaddr'
2624
import type { PeerData } from '@libp2p/interfaces/peer-data'
27-
import { base58btc } from 'multiformats/bases/base58'
25+
import { Components, Initializable } from '@libp2p/interfaces/components'
2826

29-
export interface ContentRoutingOptions {
30-
peerId: PeerId
27+
export interface ContentRoutingInit {
3128
network: Network
3229
peerRouting: PeerRouting
3330
queryManager: QueryManager
3431
routingTable: RoutingTable
3532
providers: Providers
36-
peerStore: PeerStore
3733
lan: boolean
3834
}
3935

40-
export class ContentRouting {
36+
export class ContentRouting implements Initializable {
4137
private readonly log: Logger
42-
private readonly peerId: PeerId
38+
private components: Components = new Components()
4339
private readonly network: Network
4440
private readonly peerRouting: PeerRouting
4541
private readonly queryManager: QueryManager
4642
private readonly routingTable: RoutingTable
4743
private readonly providers: Providers
48-
private readonly peerStore: PeerStore
4944

50-
constructor (options: ContentRoutingOptions) {
51-
const { peerId, network, peerRouting, queryManager, routingTable, providers, peerStore, lan } = options
45+
constructor (init: ContentRoutingInit) {
46+
const { network, peerRouting, queryManager, routingTable, providers, lan } = init
5247

5348
this.log = logger(`libp2p:kad-dht:${lan ? 'lan' : 'wan'}:content-routing`)
54-
this.peerId = peerId
5549
this.network = network
5650
this.peerRouting = peerRouting
5751
this.queryManager = queryManager
5852
this.routingTable = routingTable
5953
this.providers = providers
60-
this.peerStore = peerStore
54+
}
55+
56+
init (components: Components): void {
57+
this.components = components
6158
}
6259

6360
/**
@@ -68,11 +65,11 @@ export class ContentRouting {
6865
this.log('provide %s', key)
6966

7067
// Add peer as provider
71-
await this.providers.addProvider(key, this.peerId)
68+
await this.providers.addProvider(key, this.components.getPeerId())
7269

7370
const msg = new Message(MESSAGE_TYPE.ADD_PROVIDER, key.bytes, 0)
7471
msg.providerPeers = [{
75-
id: this.peerId,
72+
id: this.components.getPeerId(),
7673
multiaddrs,
7774
protocols: []
7875
}]
@@ -147,13 +144,13 @@ export class ContentRouting {
147144
for (const peerId of provs.slice(0, toFind)) {
148145
providers.push({
149146
id: peerId,
150-
multiaddrs: ((await this.peerStore.addressBook.get(peerId)) ?? []).map(address => address.multiaddr),
147+
multiaddrs: ((await this.components.getPeerStore().addressBook.get(peerId)) ?? []).map(address => address.multiaddr),
151148
protocols: []
152149
})
153150
}
154151

155-
yield peerResponseEvent({ from: this.peerId, messageType: MESSAGE_TYPE.GET_PROVIDERS, providers })
156-
yield providerEvent({ from: this.peerId, providers: providers })
152+
yield peerResponseEvent({ from: this.components.getPeerId(), messageType: MESSAGE_TYPE.GET_PROVIDERS, providers })
153+
yield providerEvent({ from: this.components.getPeerId(), providers: providers })
157154
}
158155

159156
// All done
@@ -170,7 +167,7 @@ export class ContentRouting {
170167
yield * self.network.sendRequest(peer, request, { signal })
171168
}
172169

173-
const providers = new Set(provs.map(p => p.toString(base58btc)))
170+
const providers = new Set(provs.map(p => p.toString()))
174171

175172
for await (const event of this.queryManager.run(target, this.routingTable.closestPeers(id), findProvidersQuery, options)) {
176173
yield event
@@ -181,11 +178,11 @@ export class ContentRouting {
181178
const newProviders = []
182179

183180
for (const peer of event.providers) {
184-
if (providers.has(peer.id.toString(base58btc))) {
181+
if (providers.has(peer.id.toString())) {
185182
continue
186183
}
187184

188-
providers.add(peer.id.toString(base58btc))
185+
providers.add(peer.id.toString())
189186
newProviders.push(peer)
190187
}
191188

src/dual-kad-dht.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,29 @@ import errCode from 'err-code'
33
import merge from 'it-merge'
44
import { queryErrorEvent } from './query/events.js'
55
import type { KadDHT } from './kad-dht.js'
6-
import type { DHT, QueryOptions } from '@libp2p/interfaces/dht'
6+
import type { DualDHT, QueryOptions } from '@libp2p/interfaces/dht'
77
import { AbortOptions, EventEmitter, CustomEvent } from '@libp2p/interfaces'
88
import type { CID } from 'multiformats'
99
import type { PeerId } from '@libp2p/interfaces/peer-id'
1010
import type { PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
11-
import type { PeerStore } from '@libp2p/interfaces/peer-store'
11+
import { Components, Initializable } from '@libp2p/interfaces/components'
1212

1313
const log = logger('libp2p:kad-dht')
1414

1515
/**
1616
* A DHT implementation modelled after Kademlia with S/Kademlia modifications.
1717
* Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.
1818
*/
19-
export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT {
19+
export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT, Initializable {
2020
public wan: KadDHT
2121
public lan: KadDHT
22-
public peerId: PeerId
23-
public peerStore: PeerStore
22+
public components: Components = new Components()
2423

25-
constructor (wan: KadDHT, lan: KadDHT, peerId: PeerId, peerStore: PeerStore) {
24+
constructor (wan: KadDHT, lan: KadDHT) {
2625
super()
2726

2827
this.wan = wan
2928
this.lan = lan
30-
this.peerId = peerId
31-
this.peerStore = peerStore
3229

3330
// handle peers being discovered during processing of DHT messages
3431
this.wan.addEventListener('peer', (evt) => {
@@ -43,6 +40,12 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT
4340
})
4441
}
4542

43+
init (components: Components): void {
44+
this.components = components
45+
this.wan.init(components)
46+
this.lan.init(components)
47+
}
48+
4649
/**
4750
* Is this DHT running.
4851
*/
@@ -133,7 +136,7 @@ export class DualKadDHT extends EventEmitter<PeerDiscoveryEvents> implements DHT
133136

134137
if (!foundValue) {
135138
yield queryErrorEvent({
136-
from: this.peerId,
139+
from: this.components.getPeerId(),
137140
error: errCode(new Error('Not found'), 'ERR_NOT_FOUND')
138141
})
139142
}

0 commit comments

Comments
 (0)