-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: cleanup core test: auto dial on startup * fix: make hangup work properly * chore: fix lint * chore: apply suggestions from code review Co-Authored-By: Vasco Santos <vasco.santos@moxy.studio>
- Loading branch information
1 parent
17946fb
commit 480373d
Showing
8 changed files
with
365 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,82 @@ | ||
'use strict' | ||
|
||
const nextTick = require('async/nextTick') | ||
const errCode = require('err-code') | ||
const promisify = require('promisify-es6') | ||
|
||
const { messages, codes } = require('./errors') | ||
|
||
module.exports = (node) => { | ||
return { | ||
put: promisify((key, value, callback) => { | ||
if (!node._dht) { | ||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)) | ||
} | ||
module.exports = (node, DHT, config) => { | ||
const dht = new DHT({ | ||
dialer: { | ||
dial: (peer, options) => node.dial(peer, options), | ||
dialProtocol: (peer, protocols, options) => { | ||
const recordedPeer = node.peerStore.get(peer.toB58String()) | ||
|
||
node._dht.put(key, value, callback) | ||
}), | ||
get: promisify((key, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
if (recordedPeer) { | ||
peer = recordedPeer | ||
} | ||
return node.dialProtocol(peer, protocols, options) | ||
} | ||
}, | ||
peerInfo: node.peerInfo, | ||
peerStore: node.peerStore, | ||
registrar: node.registrar, | ||
datastore: this.datastore, | ||
...config | ||
}) | ||
|
||
if (!node._dht) { | ||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)) | ||
return { | ||
/** | ||
* Store the given key/value pair in the DHT. | ||
* @param {Buffer} key | ||
* @param {Buffer} value | ||
* @param {Object} [options] - put options | ||
* @param {number} [options.minPeers] - minimum number of peers required to successfully put | ||
* @returns {Promise<void>} | ||
*/ | ||
put: (key, value, options) => { | ||
if (!node.isStarted() || !dht.isStarted) { | ||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) | ||
} | ||
|
||
node._dht.get(key, options, callback) | ||
}), | ||
getMany: promisify((key, nVals, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
return dht.put(key, value, options) | ||
}, | ||
|
||
/** | ||
* Get the value to the given key. | ||
* Times out after 1 minute by default. | ||
* @param {Buffer} key | ||
* @param {Object} [options] - get options | ||
* @param {number} [options.timeout] - optional timeout (default: 60000) | ||
* @returns {Promise<{from: PeerId, val: Buffer}>} | ||
*/ | ||
get: (key, options) => { | ||
if (!node.isStarted() || !dht.isStarted) { | ||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) | ||
} | ||
|
||
if (!node._dht) { | ||
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)) | ||
return dht.get(key, options) | ||
}, | ||
|
||
/** | ||
* Get the `n` values to the given key without sorting. | ||
* @param {Buffer} key | ||
* @param {number} nVals | ||
* @param {Object} [options] - get options | ||
* @param {number} [options.timeout] - optional timeout (default: 60000) | ||
* @returns {Promise<Array<{from: PeerId, val: Buffer}>>} | ||
*/ | ||
getMany: (key, nVals, options) => { | ||
if (!node.isStarted() || !dht.isStarted) { | ||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) | ||
} | ||
|
||
node._dht.getMany(key, nVals, options, callback) | ||
}) | ||
return dht.getMany(key, nVals, options) | ||
}, | ||
|
||
_dht: dht, | ||
|
||
start: () => dht.start(), | ||
|
||
stop: () => dht.stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict' | ||
/* eslint-env mocha */ | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
const { expect } = chai | ||
|
||
const mergeOptions = require('merge-options') | ||
const multiaddr = require('multiaddr') | ||
|
||
const { create } = require('../../src') | ||
const { baseOptions, subsystemOptions } = require('./utils') | ||
const peerUtils = require('../utils/creators/peer') | ||
|
||
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0') | ||
|
||
describe('DHT subsystem is configurable', () => { | ||
let libp2p | ||
|
||
afterEach(async () => { | ||
libp2p && await libp2p.stop() | ||
}) | ||
|
||
it('should not exist if no module is provided', async () => { | ||
libp2p = await create(baseOptions) | ||
expect(libp2p._dht).to.not.exist() | ||
}) | ||
|
||
it('should exist if the module is provided', async () => { | ||
libp2p = await create(subsystemOptions) | ||
expect(libp2p._dht).to.exist() | ||
}) | ||
|
||
it('should start and stop by default once libp2p starts', async () => { | ||
const [peerInfo] = await peerUtils.createPeerInfoFromFixture(1) | ||
peerInfo.multiaddrs.add(listenAddr) | ||
|
||
const customOptions = mergeOptions(subsystemOptions, { | ||
peerInfo | ||
}) | ||
|
||
libp2p = await create(customOptions) | ||
expect(libp2p._dht._dht.isStarted).to.equal(false) | ||
|
||
await libp2p.start() | ||
expect(libp2p._dht._dht.isStarted).to.equal(true) | ||
|
||
await libp2p.stop() | ||
expect(libp2p._dht._dht.isStarted).to.equal(false) | ||
}) | ||
|
||
it('should not start if disabled once libp2p starts', async () => { | ||
const [peerInfo] = await peerUtils.createPeerInfoFromFixture(1) | ||
peerInfo.multiaddrs.add(listenAddr) | ||
|
||
const customOptions = mergeOptions(subsystemOptions, { | ||
peerInfo, | ||
config: { | ||
dht: { | ||
enabled: false | ||
} | ||
} | ||
}) | ||
|
||
libp2p = await create(customOptions) | ||
expect(libp2p._dht._dht.isStarted).to.equal(false) | ||
|
||
await libp2p.start() | ||
expect(libp2p._dht._dht.isStarted).to.equal(false) | ||
}) | ||
|
||
it('should allow a manual start', async () => { | ||
const [peerInfo] = await peerUtils.createPeerInfoFromFixture(1) | ||
peerInfo.multiaddrs.add(listenAddr) | ||
|
||
const customOptions = mergeOptions(subsystemOptions, { | ||
peerInfo, | ||
config: { | ||
dht: { | ||
enabled: false | ||
} | ||
} | ||
}) | ||
|
||
libp2p = await create(customOptions) | ||
await libp2p.start() | ||
expect(libp2p._dht._dht.isStarted).to.equal(false) | ||
|
||
await libp2p._dht.start() | ||
expect(libp2p._dht._dht.isStarted).to.equal(true) | ||
}) | ||
}) |
Oops, something went wrong.