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

Commit

Permalink
feat: allow configurable validators and selectors (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Nov 29, 2018
1 parent 31fb401 commit b731a1d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
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

/**
* 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

0 comments on commit b731a1d

Please sign in to comment.