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

feat: allow configurable validators and selectors #57

Merged
merged 2 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ class KadDHT {
* @param {number} options.kBucketSize k-bucket size (default 20)
* @param {Datastore} options.datastore datastore (default MemoryDatastore)
* @param {boolean} options.enabledDiscovery enable dht discovery (default true)
* @param {object} options.validators validators object with namespace as keys and function(key, record, callback)
* @param {object} options.selectors selectors object with namespace as keys and function(key, records)
*/
constructor (sw, options) {
assert(sw, 'libp2p-kad-dht requires a instance of Switch')
options = options || {}
options.validators = options.validators || {}
options.selectors = options.selectors || {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary isn't it? The spread operator will ignore undefined and null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested it. You are right, removed the fallback 😀


/**
* Local reference to the libp2p-switch instance
Expand Down Expand Up @@ -83,8 +87,15 @@ class KadDHT {
*/
this.providers = new Providers(this.datastore, this.peerInfo.id)

this.validators = { pk: libp2pRecord.validator.validators.pk }
this.selectors = { pk: libp2pRecord.selection.selectors.pk }
this.validators = {
pk: libp2pRecord.validator.validators.pk,
...options.validators
}

this.selectors = {
pk: libp2pRecord.selection.selectors.pk,
...options.selectors
}

this.network = new Network(this)

Expand Down
56 changes: 56 additions & 0 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ describe('KadDHT', () => {
expect(dht).to.have.property('routingTable')
})

it('create with validators and selectors', () => {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
sw.connection.addStreamMuxer(Mplex)
sw.connection.reuse()
const dht = new KadDHT(sw, {
validators: {
ipns: {
func: (key, record, cb) => cb()
}
},
selectors: {
ipns: (key, records) => 0
}
})

expect(dht).to.have.property('peerInfo').eql(peerInfos[0])
expect(dht).to.have.property('switch').eql(sw)
expect(dht).to.have.property('routingTable')
expect(dht.validators).to.have.property('ipns')
expect(dht.selectors).to.have.property('ipns')
})

it('should be able to start and stop', function (done) {
const sw = new Switch(peerInfos[0], new PeerBook())
sw.transport.add('tcp', new TCP())
Expand Down Expand Up @@ -280,6 +303,39 @@ describe('KadDHT', () => {
})
})

it('put - get using key from provided validator and selector', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()

tdht.spawn(2, {
validators: {
ipns: {
func: (key, record, cb) => cb()
}
},
selectors: {
ipns: (key, records) => 0
}
}, (err, dhts) => {
expect(err).to.not.exist()
const dhtA = dhts[0]
const dhtB = dhts[1]

waterfall([
(cb) => connect(dhtA, dhtB, cb),
(cb) => dhtA.put(Buffer.from('/ipns/hello'), Buffer.from('world'), cb),
(cb) => dhtB.get(Buffer.from('/ipns/hello'), { maxTimeout: 1000 }, cb),
(res, cb) => {
expect(res).to.eql(Buffer.from('world'))
cb()
}
], (err) => {
expect(err).to.not.exist()
tdht.teardown(done)
})
})
})

it('put - get should fail if unrecognized key prefix in get', function (done) {
this.timeout(10 * 1000)
const tdht = new TestDHT()
Expand Down