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

Commit b731a1d

Browse files
authored
feat: allow configurable validators and selectors (#57)
1 parent 31fb401 commit b731a1d

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ class KadDHT {
3636
* @param {number} options.kBucketSize k-bucket size (default 20)
3737
* @param {Datastore} options.datastore datastore (default MemoryDatastore)
3838
* @param {boolean} options.enabledDiscovery enable dht discovery (default true)
39+
* @param {object} options.validators validators object with namespace as keys and function(key, record, callback)
40+
* @param {object} options.selectors selectors object with namespace as keys and function(key, records)
3941
*/
4042
constructor (sw, options) {
4143
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
4244
options = options || {}
45+
options.validators = options.validators
46+
options.selectors = options.selectors
4347

4448
/**
4549
* Local reference to the libp2p-switch instance
@@ -83,8 +87,15 @@ class KadDHT {
8387
*/
8488
this.providers = new Providers(this.datastore, this.peerInfo.id)
8589

86-
this.validators = { pk: libp2pRecord.validator.validators.pk }
87-
this.selectors = { pk: libp2pRecord.selection.selectors.pk }
90+
this.validators = {
91+
pk: libp2pRecord.validator.validators.pk,
92+
...options.validators
93+
}
94+
95+
this.selectors = {
96+
pk: libp2pRecord.selection.selectors.pk,
97+
...options.selectors
98+
}
8899

89100
this.network = new Network(this)
90101

test/kad-dht.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ describe('KadDHT', () => {
139139
expect(dht).to.have.property('routingTable')
140140
})
141141

142+
it('create with validators and selectors', () => {
143+
const sw = new Switch(peerInfos[0], new PeerBook())
144+
sw.transport.add('tcp', new TCP())
145+
sw.connection.addStreamMuxer(Mplex)
146+
sw.connection.reuse()
147+
const dht = new KadDHT(sw, {
148+
validators: {
149+
ipns: {
150+
func: (key, record, cb) => cb()
151+
}
152+
},
153+
selectors: {
154+
ipns: (key, records) => 0
155+
}
156+
})
157+
158+
expect(dht).to.have.property('peerInfo').eql(peerInfos[0])
159+
expect(dht).to.have.property('switch').eql(sw)
160+
expect(dht).to.have.property('routingTable')
161+
expect(dht.validators).to.have.property('ipns')
162+
expect(dht.selectors).to.have.property('ipns')
163+
})
164+
142165
it('should be able to start and stop', function (done) {
143166
const sw = new Switch(peerInfos[0], new PeerBook())
144167
sw.transport.add('tcp', new TCP())
@@ -280,6 +303,39 @@ describe('KadDHT', () => {
280303
})
281304
})
282305

306+
it('put - get using key from provided validator and selector', function (done) {
307+
this.timeout(10 * 1000)
308+
const tdht = new TestDHT()
309+
310+
tdht.spawn(2, {
311+
validators: {
312+
ipns: {
313+
func: (key, record, cb) => cb()
314+
}
315+
},
316+
selectors: {
317+
ipns: (key, records) => 0
318+
}
319+
}, (err, dhts) => {
320+
expect(err).to.not.exist()
321+
const dhtA = dhts[0]
322+
const dhtB = dhts[1]
323+
324+
waterfall([
325+
(cb) => connect(dhtA, dhtB, cb),
326+
(cb) => dhtA.put(Buffer.from('/ipns/hello'), Buffer.from('world'), cb),
327+
(cb) => dhtB.get(Buffer.from('/ipns/hello'), { maxTimeout: 1000 }, cb),
328+
(res, cb) => {
329+
expect(res).to.eql(Buffer.from('world'))
330+
cb()
331+
}
332+
], (err) => {
333+
expect(err).to.not.exist()
334+
tdht.teardown(done)
335+
})
336+
})
337+
})
338+
283339
it('put - get should fail if unrecognized key prefix in get', function (done) {
284340
this.timeout(10 * 1000)
285341
const tdht = new TestDHT()

0 commit comments

Comments
 (0)