mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-01 13:32:14 +00:00
chore: run node tests in ci (#1057)
Looks like this project stopped running the `test:node` npm script when it was migrated to gh actions. Re-enable it and fix all the related test failures.
This commit is contained in:
parent
b539f9b655
commit
1b46f47fdb
6
.github/workflows/main.yml
vendored
6
.github/workflows/main.yml
vendored
@ -37,7 +37,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: ${{ matrix.node }}
|
node-version: ${{ matrix.node }}
|
||||||
- run: npm install
|
- run: npm install
|
||||||
- run: npx aegir test -t node --cov --bail
|
- run: npm run test:node -- --cov --bail
|
||||||
- uses: codecov/codecov-action@v1
|
- uses: codecov/codecov-action@v1
|
||||||
test-chrome:
|
test-chrome:
|
||||||
needs: check
|
needs: check
|
||||||
@ -48,7 +48,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: lts/*
|
node-version: lts/*
|
||||||
- run: npm install
|
- run: npm install
|
||||||
- run: npx aegir test -t browser -t webworker --bail
|
- run: npm run test:browser -- -t browser -t webworker --bail
|
||||||
test-firefox:
|
test-firefox:
|
||||||
needs: check
|
needs: check
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@ -58,7 +58,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: lts/*
|
node-version: lts/*
|
||||||
- run: npm install
|
- run: npm install
|
||||||
- run: npx aegir test -t browser -t webworker --bail -- --browser firefox
|
- run: npm run test:browser -- -t browser -t webworker --bail -- --browser firefox
|
||||||
test-ts:
|
test-ts:
|
||||||
needs: check
|
needs: check
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
@ -151,7 +151,7 @@
|
|||||||
"libp2p": ".",
|
"libp2p": ".",
|
||||||
"libp2p-bootstrap": "^0.14.0",
|
"libp2p-bootstrap": "^0.14.0",
|
||||||
"libp2p-delegated-content-routing": "^0.11.0",
|
"libp2p-delegated-content-routing": "^0.11.0",
|
||||||
"libp2p-delegated-peer-routing": "^0.11.0",
|
"libp2p-delegated-peer-routing": "^0.11.1",
|
||||||
"libp2p-floodsub": "^0.27.0",
|
"libp2p-floodsub": "^0.27.0",
|
||||||
"libp2p-gossipsub": "^0.12.1",
|
"libp2p-gossipsub": "^0.12.1",
|
||||||
"libp2p-interfaces-compliance-tests": "^2.0.1",
|
"libp2p-interfaces-compliance-tests": "^2.0.1",
|
||||||
|
@ -4,7 +4,7 @@ const debug = require('debug')
|
|||||||
const log = Object.assign(debug('libp2p:relay'), {
|
const log = Object.assign(debug('libp2p:relay'), {
|
||||||
error: debug('libp2p:relay:err')
|
error: debug('libp2p:relay:err')
|
||||||
})
|
})
|
||||||
|
const { codes } = require('./../errors')
|
||||||
const {
|
const {
|
||||||
setDelayedInterval,
|
setDelayedInterval,
|
||||||
clearDelayedInterval
|
clearDelayedInterval
|
||||||
@ -88,7 +88,7 @@ class Relay {
|
|||||||
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
||||||
await this._libp2p.contentRouting.provide(cid)
|
await this._libp2p.contentRouting.provide(cid)
|
||||||
} catch (/** @type {any} */ err) {
|
} catch (/** @type {any} */ err) {
|
||||||
if (err.code === 'NO_ROUTERS_AVAILABLE') {
|
if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) {
|
||||||
log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err)
|
log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err)
|
||||||
// Stop the advertise
|
// Stop the advertise
|
||||||
this.stop()
|
this.stop()
|
||||||
|
@ -60,13 +60,7 @@ const DefaultConfig = {
|
|||||||
protocolPrefix: 'ipfs',
|
protocolPrefix: 'ipfs',
|
||||||
dht: {
|
dht: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
kBucketSize: 20,
|
kBucketSize: 20
|
||||||
randomWalk: {
|
|
||||||
enabled: false, // disabled waiting for https://github.com/libp2p/js-libp2p-kad-dht/issues/86
|
|
||||||
queriesPerPeriod: 1,
|
|
||||||
interval: 300e3,
|
|
||||||
timeout: 10e3
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
nat: {
|
nat: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
@ -54,7 +54,7 @@ class ContentRouting {
|
|||||||
*/
|
*/
|
||||||
async * findProviders (key, options = {}) {
|
async * findProviders (key, options = {}) {
|
||||||
if (!this.routers.length) {
|
if (!this.routers.length) {
|
||||||
throw errCode(new Error('No content this.routers available'), 'NO_ROUTERS_AVAILABLE')
|
throw errCode(new Error('No content this.routers available'), codes.ERR_NO_ROUTERS_AVAILABLE)
|
||||||
}
|
}
|
||||||
|
|
||||||
yield * pipe(
|
yield * pipe(
|
||||||
@ -77,7 +77,7 @@ class ContentRouting {
|
|||||||
*/
|
*/
|
||||||
async provide (key) {
|
async provide (key) {
|
||||||
if (!this.routers.length) {
|
if (!this.routers.length) {
|
||||||
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
|
throw errCode(new Error('No content routers available'), codes.ERR_NO_ROUTERS_AVAILABLE)
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(this.routers.map((router) => router.provide(key)))
|
await Promise.all(this.routers.map((router) => router.provide(key)))
|
||||||
|
@ -27,8 +27,12 @@ class DHTPeerRouting {
|
|||||||
*/
|
*/
|
||||||
async findPeer (peerId, options = {}) {
|
async findPeer (peerId, options = {}) {
|
||||||
for await (const event of this._dht.findPeer(peerId, options)) {
|
for await (const event of this._dht.findPeer(peerId, options)) {
|
||||||
if (event.name === 'FINAL_PEER') {
|
if (event.name === 'PEER_RESPONSE') {
|
||||||
return event.peer
|
const peer = event.closer.find(peerData => peerData.id.equals(peerId))
|
||||||
|
|
||||||
|
if (peer) {
|
||||||
|
return peer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ exports.codes = {
|
|||||||
ERR_INVALID_MULTIADDR: 'ERR_INVALID_MULTIADDR',
|
ERR_INVALID_MULTIADDR: 'ERR_INVALID_MULTIADDR',
|
||||||
ERR_SIGNATURE_NOT_VALID: 'ERR_SIGNATURE_NOT_VALID',
|
ERR_SIGNATURE_NOT_VALID: 'ERR_SIGNATURE_NOT_VALID',
|
||||||
ERR_FIND_SELF: 'ERR_FIND_SELF',
|
ERR_FIND_SELF: 'ERR_FIND_SELF',
|
||||||
ERR_NO_ROUTERS: 'ERR_NO_ROUTERS',
|
ERR_NO_ROUTERS_AVAILABLE: 'ERR_NO_ROUTERS_AVAILABLE',
|
||||||
ERR_CONNECTION_NOT_MULTIPLEXED: 'ERR_CONNECTION_NOT_MULTIPLEXED',
|
ERR_CONNECTION_NOT_MULTIPLEXED: 'ERR_CONNECTION_NOT_MULTIPLEXED',
|
||||||
ERR_NO_DIAL_TOKENS: 'ERR_NO_DIAL_TOKENS',
|
ERR_NO_DIAL_TOKENS: 'ERR_NO_DIAL_TOKENS',
|
||||||
ERR_KEYCHAIN_REQUIRED: 'ERR_KEYCHAIN_REQUIRED',
|
ERR_KEYCHAIN_REQUIRED: 'ERR_KEYCHAIN_REQUIRED',
|
||||||
|
@ -56,16 +56,9 @@ const { updateSelfPeerRecord } = require('./record/utils')
|
|||||||
* @property {MuxedStream} stream
|
* @property {MuxedStream} stream
|
||||||
* @property {string} protocol
|
* @property {string} protocol
|
||||||
*
|
*
|
||||||
* @typedef {Object} RandomWalkOptions
|
|
||||||
* @property {boolean} [enabled = false]
|
|
||||||
* @property {number} [queriesPerPeriod = 1]
|
|
||||||
* @property {number} [interval = 300e3]
|
|
||||||
* @property {number} [timeout = 10e3]
|
|
||||||
*
|
|
||||||
* @typedef {Object} DhtOptions
|
* @typedef {Object} DhtOptions
|
||||||
* @property {boolean} [enabled = false]
|
* @property {boolean} [enabled = false]
|
||||||
* @property {number} [kBucketSize = 20]
|
* @property {number} [kBucketSize = 20]
|
||||||
* @property {RandomWalkOptions} [randomWalk]
|
|
||||||
* @property {boolean} [clientMode]
|
* @property {boolean} [clientMode]
|
||||||
* @property {import('libp2p-interfaces/src/types').DhtSelectors} [selectors]
|
* @property {import('libp2p-interfaces/src/types').DhtSelectors} [selectors]
|
||||||
* @property {import('libp2p-interfaces/src/types').DhtValidators} [validators]
|
* @property {import('libp2p-interfaces/src/types').DhtValidators} [validators]
|
||||||
|
@ -105,7 +105,7 @@ class PeerRouting {
|
|||||||
*/
|
*/
|
||||||
async findPeer (id, options) { // eslint-disable-line require-await
|
async findPeer (id, options) { // eslint-disable-line require-await
|
||||||
if (!this._routers.length) {
|
if (!this._routers.length) {
|
||||||
throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS)
|
throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS_AVAILABLE)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id.toB58String() === this._peerId.toB58String()) {
|
if (id.toB58String() === this._peerId.toB58String()) {
|
||||||
@ -145,7 +145,7 @@ class PeerRouting {
|
|||||||
*/
|
*/
|
||||||
async * getClosestPeers (key, options = { timeout: 30e3 }) {
|
async * getClosestPeers (key, options = { timeout: 30e3 }) {
|
||||||
if (!this._routers.length) {
|
if (!this._routers.length) {
|
||||||
throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS)
|
throw errCode(new Error('No peer routers available'), errors.codes.ERR_NO_ROUTERS_AVAILABLE)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.timeout) {
|
if (options.timeout) {
|
||||||
|
@ -36,14 +36,14 @@ describe('content-routing', () => {
|
|||||||
throw new Error('.findProviders should return an error')
|
throw new Error('.findProviders should return an error')
|
||||||
} catch (/** @type {any} */ err) {
|
} catch (/** @type {any} */ err) {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist()
|
||||||
expect(err.code).to.equal('NO_ROUTERS_AVAILABLE')
|
expect(err.code).to.equal('ERR_NO_ROUTERS_AVAILABLE')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('.provide should return an error', async () => {
|
it('.provide should return an error', async () => {
|
||||||
await expect(node.contentRouting.provide('a cid'))
|
await expect(node.contentRouting.provide('a cid'))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
.and.to.have.property('code', 'NO_ROUTERS_AVAILABLE')
|
.and.to.have.property('code', 'ERR_NO_ROUTERS_AVAILABLE')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -87,8 +87,11 @@ describe('content-routing', () => {
|
|||||||
sinon.stub(nodes[0]._dht, 'findProviders').callsFake(function * () {
|
sinon.stub(nodes[0]._dht, 'findProviders').callsFake(function * () {
|
||||||
deferred.resolve()
|
deferred.resolve()
|
||||||
yield {
|
yield {
|
||||||
id: providerPeerId,
|
name: 'PROVIDER',
|
||||||
multiaddrs: []
|
providers: [{
|
||||||
|
id: providerPeerId,
|
||||||
|
multiaddrs: []
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -361,7 +364,12 @@ describe('content-routing', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sinon.stub(node._dht, 'findProviders').callsFake(async function * () {
|
sinon.stub(node._dht, 'findProviders').callsFake(async function * () {
|
||||||
yield result1
|
yield {
|
||||||
|
name: 'PROVIDER',
|
||||||
|
providers: [
|
||||||
|
result1
|
||||||
|
]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
sinon.stub(delegate, 'findProviders').callsFake(async function * () {
|
||||||
yield result2
|
yield result2
|
||||||
@ -382,7 +390,8 @@ describe('content-routing', () => {
|
|||||||
const dhtDeferred = pDefer()
|
const dhtDeferred = pDefer()
|
||||||
const delegatedDeferred = pDefer()
|
const delegatedDeferred = pDefer()
|
||||||
|
|
||||||
sinon.stub(node._dht, 'provide').callsFake(() => {
|
sinon.stub(node._dht, 'provide').callsFake(async function * () {
|
||||||
|
yield
|
||||||
dhtDeferred.resolve()
|
dhtDeferred.resolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -406,7 +415,12 @@ describe('content-routing', () => {
|
|||||||
}]
|
}]
|
||||||
|
|
||||||
sinon.stub(node._dht, 'findProviders').callsFake(function * () {
|
sinon.stub(node._dht, 'findProviders').callsFake(function * () {
|
||||||
yield results[0]
|
yield {
|
||||||
|
name: 'PROVIDER',
|
||||||
|
providers: [
|
||||||
|
results[0]
|
||||||
|
]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
sinon.stub(delegate, 'findProviders').callsFake(function * () { // eslint-disable-line require-yield
|
sinon.stub(delegate, 'findProviders').callsFake(function * () { // eslint-disable-line require-yield
|
||||||
|
@ -38,13 +38,13 @@ describe('DHT subsystem is configurable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
libp2p = await create(customOptions)
|
libp2p = await create(customOptions)
|
||||||
expect(libp2p._dht.isStarted).to.equal(false)
|
expect(libp2p._dht.isStarted()).to.equal(false)
|
||||||
|
|
||||||
await libp2p.start()
|
await libp2p.start()
|
||||||
expect(libp2p._dht.isStarted).to.equal(true)
|
expect(libp2p._dht.isStarted()).to.equal(true)
|
||||||
|
|
||||||
await libp2p.stop()
|
await libp2p.stop()
|
||||||
expect(libp2p._dht.isStarted).to.equal(false)
|
expect(libp2p._dht.isStarted()).to.equal(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not start if disabled once libp2p starts', async () => {
|
it('should not start if disabled once libp2p starts', async () => {
|
||||||
@ -63,10 +63,10 @@ describe('DHT subsystem is configurable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
libp2p = await create(customOptions)
|
libp2p = await create(customOptions)
|
||||||
expect(libp2p._dht.isStarted).to.equal(false)
|
expect(libp2p._dht.isStarted()).to.equal(false)
|
||||||
|
|
||||||
await libp2p.start()
|
await libp2p.start()
|
||||||
expect(libp2p._dht.isStarted).to.equal(false)
|
expect(libp2p._dht.isStarted()).to.equal(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should allow a manual start', async () => {
|
it('should allow a manual start', async () => {
|
||||||
@ -86,9 +86,9 @@ describe('DHT subsystem is configurable', () => {
|
|||||||
|
|
||||||
libp2p = await create(customOptions)
|
libp2p = await create(customOptions)
|
||||||
await libp2p.start()
|
await libp2p.start()
|
||||||
expect(libp2p._dht.isStarted).to.equal(false)
|
expect(libp2p._dht.isStarted()).to.equal(false)
|
||||||
|
|
||||||
await libp2p._dht.start()
|
await libp2p._dht.start()
|
||||||
expect(libp2p._dht.isStarted).to.equal(true)
|
expect(libp2p._dht.isStarted()).to.equal(true)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -60,8 +60,8 @@ describe('DHT subsystem operates correctly', () => {
|
|||||||
expect(connection).to.exist()
|
expect(connection).to.exist()
|
||||||
|
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
pWaitFor(() => libp2p._dht.routingTable.size === 1),
|
pWaitFor(() => libp2p._dht._lan._routingTable.size === 1),
|
||||||
pWaitFor(() => remoteLibp2p._dht.routingTable.size === 1)
|
pWaitFor(() => remoteLibp2p._dht._lan._routingTable.size === 1)
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -71,14 +71,14 @@ describe('DHT subsystem operates correctly', () => {
|
|||||||
|
|
||||||
await libp2p.dialProtocol(remAddr, subsystemMulticodecs)
|
await libp2p.dialProtocol(remAddr, subsystemMulticodecs)
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
pWaitFor(() => libp2p._dht.routingTable.size === 1),
|
pWaitFor(() => libp2p._dht._lan._routingTable.size === 1),
|
||||||
pWaitFor(() => remoteLibp2p._dht.routingTable.size === 1)
|
pWaitFor(() => remoteLibp2p._dht._lan._routingTable.size === 1)
|
||||||
])
|
])
|
||||||
|
|
||||||
await libp2p.contentRouting.put(key, value)
|
await libp2p.contentRouting.put(key, value)
|
||||||
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
|
|
||||||
|
|
||||||
expect(fetchedValue).to.eql(value)
|
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
|
||||||
|
expect(fetchedValue).to.have.property('val').that.equalBytes(value)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -119,11 +119,13 @@ describe('DHT subsystem operates correctly', () => {
|
|||||||
const connection = await libp2p.dial(remAddr)
|
const connection = await libp2p.dial(remAddr)
|
||||||
|
|
||||||
expect(connection).to.exist()
|
expect(connection).to.exist()
|
||||||
expect(libp2p._dht.routingTable.size).to.be.eql(0)
|
expect(libp2p._dht._lan._routingTable.size).to.be.eql(0)
|
||||||
expect(remoteLibp2p._dht.routingTable.size).to.be.eql(0)
|
|
||||||
|
|
||||||
await remoteLibp2p._dht.start()
|
await remoteLibp2p._dht.start()
|
||||||
return pWaitFor(() => libp2p._dht.routingTable.size === 1)
|
// should be 0 directly after start - TODO this may be susceptible to timing bugs, we should have
|
||||||
|
// the ability to report stats on the DHT routing table instead of reaching into it's heart like this
|
||||||
|
expect(remoteLibp2p._dht._lan._routingTable.size).to.be.eql(0)
|
||||||
|
return pWaitFor(() => libp2p._dht._lan._routingTable.size === 1)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should put on a peer and get from the other', async () => {
|
it('should put on a peer and get from the other', async () => {
|
||||||
@ -133,12 +135,12 @@ describe('DHT subsystem operates correctly', () => {
|
|||||||
const value = uint8ArrayFromString('world')
|
const value = uint8ArrayFromString('world')
|
||||||
|
|
||||||
await remoteLibp2p._dht.start()
|
await remoteLibp2p._dht.start()
|
||||||
await pWaitFor(() => libp2p._dht.routingTable.size === 1)
|
await pWaitFor(() => libp2p._dht._lan._routingTable.size === 1)
|
||||||
|
|
||||||
await libp2p.contentRouting.put(key, value)
|
await libp2p.contentRouting.put(key, value)
|
||||||
|
|
||||||
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
|
const fetchedValue = await remoteLibp2p.contentRouting.get(key)
|
||||||
expect(fetchedValue).to.eql(value)
|
expect(fetchedValue).to.have.property('val').that.equalBytes(value)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const KadDht = require('libp2p-kad-dht')
|
const KadDht = require('libp2p-kad-dht')
|
||||||
const { multicodec } = require('libp2p-kad-dht')
|
|
||||||
const Crypto = require('../../../src/insecure/plaintext')
|
const Crypto = require('../../../src/insecure/plaintext')
|
||||||
const Muxer = require('libp2p-mplex')
|
const Muxer = require('libp2p-mplex')
|
||||||
const Transport = require('libp2p-tcp')
|
const Transport = require('libp2p-tcp')
|
||||||
@ -25,13 +24,12 @@ const subsystemOptions = mergeOptions(baseOptions, {
|
|||||||
config: {
|
config: {
|
||||||
dht: {
|
dht: {
|
||||||
kBucketSize: 20,
|
kBucketSize: 20,
|
||||||
randomWalk: {
|
|
||||||
enabled: true
|
|
||||||
},
|
|
||||||
enabled: true
|
enabled: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports.subsystemOptions = subsystemOptions
|
module.exports.subsystemOptions = subsystemOptions
|
||||||
module.exports.subsystemMulticodecs = [multicodec]
|
module.exports.subsystemMulticodecs = [
|
||||||
|
'/ipfs/lan/kad/1.0.0'
|
||||||
|
]
|
||||||
|
@ -13,9 +13,6 @@ const routingOptions = mergeOptions(baseOptions, {
|
|||||||
config: {
|
config: {
|
||||||
dht: {
|
dht: {
|
||||||
kBucketSize: 20,
|
kBucketSize: 20,
|
||||||
randomWalk: {
|
|
||||||
enabled: true
|
|
||||||
},
|
|
||||||
enabled: true
|
enabled: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,6 +244,10 @@ describe('Nat Manager (TCP)', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('shuts the nat api down when stopping', async function () {
|
it('shuts the nat api down when stopping', async function () {
|
||||||
|
if (process.env.CI) {
|
||||||
|
return this.skip('CI environments will not let us map external ports')
|
||||||
|
}
|
||||||
|
|
||||||
function findRoutableAddress () {
|
function findRoutableAddress () {
|
||||||
const interfaces = networkInterfaces()
|
const interfaces = networkInterfaces()
|
||||||
|
|
||||||
@ -261,7 +265,7 @@ describe('Nat Manager (TCP)', () => {
|
|||||||
|
|
||||||
if (!addr) {
|
if (!addr) {
|
||||||
// skip test if no non-loopback address is found
|
// skip test if no non-loopback address is found
|
||||||
this.skip()
|
return this.skip()
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
@ -161,20 +161,13 @@ describe('peer discovery scenarios', () => {
|
|||||||
autoDial: false
|
autoDial: false
|
||||||
},
|
},
|
||||||
dht: {
|
dht: {
|
||||||
randomWalk: {
|
|
||||||
enabled: false,
|
|
||||||
delay: 1000, // start the first query quickly
|
|
||||||
interval: 10000,
|
|
||||||
timeout: 5000
|
|
||||||
},
|
|
||||||
enabled: true
|
enabled: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const localConfig = getConfig(peerId)
|
const localConfig = getConfig(peerId)
|
||||||
// Only run random walk on our local node
|
|
||||||
localConfig.config.dht.randomWalk.enabled = true
|
|
||||||
libp2p = new Libp2p(localConfig)
|
libp2p = new Libp2p(localConfig)
|
||||||
|
|
||||||
const remoteLibp2p1 = new Libp2p(getConfig(remotePeerId1))
|
const remoteLibp2p1 = new Libp2p(getConfig(remotePeerId1))
|
||||||
|
@ -36,7 +36,7 @@ describe('peer-routing', () => {
|
|||||||
it('.findPeer should return an error', async () => {
|
it('.findPeer should return an error', async () => {
|
||||||
await expect(node.peerRouting.findPeer('a cid'))
|
await expect(node.peerRouting.findPeer('a cid'))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
.and.to.have.property('code', 'NO_ROUTERS_AVAILABLE')
|
.and.to.have.property('code', 'ERR_NO_ROUTERS_AVAILABLE')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('.getClosestPeers should return an error', async () => {
|
it('.getClosestPeers should return an error', async () => {
|
||||||
@ -45,7 +45,7 @@ describe('peer-routing', () => {
|
|||||||
throw new Error('.getClosestPeers should return an error')
|
throw new Error('.getClosestPeers should return an error')
|
||||||
} catch (/** @type {any} */ err) {
|
} catch (/** @type {any} */ err) {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist()
|
||||||
expect(err.code).to.equal('NO_ROUTERS_AVAILABLE')
|
expect(err.code).to.equal('ERR_NO_ROUTERS_AVAILABLE')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -72,33 +72,38 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
after(() => Promise.all(nodes.map((n) => n.stop())))
|
after(() => Promise.all(nodes.map((n) => n.stop())))
|
||||||
|
|
||||||
it('should use the nodes dht', () => {
|
it('should use the nodes dht', async () => {
|
||||||
const deferred = pDefer()
|
sinon.stub(nodes[0]._dht, 'findPeer').callsFake(async function * () {
|
||||||
|
|
||||||
sinon.stub(nodes[0]._dht, 'findPeer').callsFake(() => {
|
|
||||||
deferred.resolve()
|
|
||||||
return nodes[1].peerId
|
|
||||||
})
|
|
||||||
|
|
||||||
nodes[0].peerRouting.findPeer()
|
|
||||||
return deferred.promise
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should use the nodes dht to get the closest peers', async () => {
|
|
||||||
const deferred = pDefer()
|
|
||||||
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
|
||||||
|
|
||||||
sinon.stub(nodes[0]._dht, 'getClosestPeers').callsFake(function * () {
|
|
||||||
deferred.resolve()
|
|
||||||
yield {
|
yield {
|
||||||
id: remotePeerId,
|
name: 'PEER_RESPONSE',
|
||||||
multiaddrs: []
|
closer: [{
|
||||||
|
id: nodes[1].peerId,
|
||||||
|
multiaddrs: []
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
await nodes[0].peerRouting.getClosestPeers().next()
|
expect(nodes[0]._dht.findPeer.called).to.be.false()
|
||||||
|
await nodes[0].peerRouting.findPeer(nodes[1].peerId)
|
||||||
|
expect(nodes[0]._dht.findPeer.called).to.be.true()
|
||||||
|
nodes[0]._dht.findPeer.restore()
|
||||||
|
})
|
||||||
|
|
||||||
return deferred.promise
|
it('should use the nodes dht to get the closest peers', async () => {
|
||||||
|
sinon.stub(nodes[0]._dht, 'getClosestPeers').callsFake(async function * () {
|
||||||
|
yield {
|
||||||
|
name: 'PEER_RESPONSE',
|
||||||
|
closer: [{
|
||||||
|
id: nodes[1].peerId,
|
||||||
|
multiaddrs: []
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(nodes[0]._dht.getClosestPeers.called).to.be.false()
|
||||||
|
await drain(nodes[0].peerRouting.getClosestPeers(nodes[1].peerId))
|
||||||
|
expect(nodes[0]._dht.getClosestPeers.called).to.be.true()
|
||||||
|
nodes[0]._dht.getClosestPeers.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should error when peer tries to find itself', async () => {
|
it('should error when peer tries to find itself', async () => {
|
||||||
@ -234,36 +239,35 @@ describe('peer-routing', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should use the delegate router to find peers', async () => {
|
it('should use the delegate router to find peers', async () => {
|
||||||
const deferred = pDefer()
|
|
||||||
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
||||||
|
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(() => {
|
sinon.stub(delegate, 'findPeer').callsFake(() => {
|
||||||
deferred.resolve()
|
|
||||||
return {
|
return {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: []
|
multiaddrs: []
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
await node.peerRouting.findPeer()
|
expect(delegate.findPeer.called).to.be.false()
|
||||||
return deferred.promise
|
await node.peerRouting.findPeer(remotePeerId)
|
||||||
|
expect(delegate.findPeer.called).to.be.true()
|
||||||
|
delegate.findPeer.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should use the delegate router to get the closest peers', async () => {
|
it('should use the delegate router to get the closest peers', async () => {
|
||||||
const deferred = pDefer()
|
|
||||||
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
||||||
|
|
||||||
sinon.stub(delegate, 'getClosestPeers').callsFake(function * () {
|
sinon.stub(delegate, 'getClosestPeers').callsFake(function * () {
|
||||||
deferred.resolve()
|
|
||||||
yield {
|
yield {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: []
|
multiaddrs: []
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
await node.peerRouting.getClosestPeers().next()
|
expect(delegate.getClosestPeers.called).to.be.false()
|
||||||
|
await drain(node.peerRouting.getClosestPeers(remotePeerId))
|
||||||
return deferred.promise
|
expect(delegate.getClosestPeers.called).to.be.true()
|
||||||
|
delegate.getClosestPeers.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should be able to find a peer', async () => {
|
it('should be able to find a peer', async () => {
|
||||||
@ -289,7 +293,7 @@ describe('peer-routing', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should error when a peer cannot be found', async () => {
|
it('should error when a peer cannot be found', async () => {
|
||||||
const peerKey = 'key of a peer not on the network'
|
const peerId = await PeerId.create({ keyType: 'ed25519' })
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
const mockApi = nock('http://0.0.0.0:60197')
|
||||||
.post('/api/v0/dht/findpeer')
|
.post('/api/v0/dht/findpeer')
|
||||||
.query(true)
|
.query(true)
|
||||||
@ -298,20 +302,20 @@ describe('peer-routing', () => {
|
|||||||
'X-Chunked-Output', '1'
|
'X-Chunked-Output', '1'
|
||||||
])
|
])
|
||||||
|
|
||||||
await expect(node.peerRouting.findPeer(peerKey))
|
await expect(node.peerRouting.findPeer(peerId))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
expect(mockApi.isDone()).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle errors from the api', async () => {
|
it('should handle errors from the api', async () => {
|
||||||
const peerKey = 'key of a peer not on the network'
|
const peerId = await PeerId.create({ keyType: 'ed25519' })
|
||||||
const mockApi = nock('http://0.0.0.0:60197')
|
const mockApi = nock('http://0.0.0.0:60197')
|
||||||
.post('/api/v0/dht/findpeer')
|
.post('/api/v0/dht/findpeer')
|
||||||
.query(true)
|
.query(true)
|
||||||
.reply(502)
|
.reply(502)
|
||||||
|
|
||||||
await expect(node.peerRouting.findPeer(peerKey))
|
await expect(node.peerRouting.findPeer(peerId))
|
||||||
.to.eventually.be.rejected()
|
.to.eventually.be.rejected()
|
||||||
|
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
expect(mockApi.isDone()).to.equal(true)
|
||||||
@ -319,7 +323,6 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
it('should be able to get the closest peers', async () => {
|
it('should be able to get the closest peers', async () => {
|
||||||
const peerId = await PeerId.create({ keyType: 'ed25519' })
|
const peerId = await PeerId.create({ keyType: 'ed25519' })
|
||||||
|
|
||||||
const closest1 = '12D3KooWLewYMMdGWAtuX852n4rgCWkK7EBn4CWbwwBzhsVoKxk3'
|
const closest1 = '12D3KooWLewYMMdGWAtuX852n4rgCWkK7EBn4CWbwwBzhsVoKxk3'
|
||||||
const closest2 = '12D3KooWDtoQbpKhtnWddfj72QmpFvvLDTsBLTFkjvgQm6cde2AK'
|
const closest2 = '12D3KooWDtoQbpKhtnWddfj72QmpFvvLDTsBLTFkjvgQm6cde2AK'
|
||||||
|
|
||||||
@ -338,15 +341,12 @@ describe('peer-routing', () => {
|
|||||||
'X-Chunked-Output', '1'
|
'X-Chunked-Output', '1'
|
||||||
])
|
])
|
||||||
|
|
||||||
const closestPeers = []
|
const closestPeers = await all(node.peerRouting.getClosestPeers(peerId.id, { timeout: 1000 }))
|
||||||
for await (const peer of node.peerRouting.getClosestPeers(peerId.id, { timeout: 1000 })) {
|
|
||||||
closestPeers.push(peer)
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(closestPeers).to.have.length(2)
|
expect(closestPeers).to.have.length(2)
|
||||||
expect(closestPeers[0].id.toB58String()).to.equal(closest2)
|
expect(closestPeers[0].id.toB58String()).to.equal(closest1)
|
||||||
expect(closestPeers[0].multiaddrs).to.have.lengthOf(2)
|
expect(closestPeers[0].multiaddrs).to.have.lengthOf(2)
|
||||||
expect(closestPeers[1].id.toB58String()).to.equal(closest1)
|
expect(closestPeers[1].id.toB58String()).to.equal(closest2)
|
||||||
expect(closestPeers[1].multiaddrs).to.have.lengthOf(2)
|
expect(closestPeers[1].multiaddrs).to.have.lengthOf(2)
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
expect(mockApi.isDone()).to.equal(true)
|
||||||
})
|
})
|
||||||
@ -405,7 +405,7 @@ describe('peer-routing', () => {
|
|||||||
multiaddrs: []
|
multiaddrs: []
|
||||||
}
|
}
|
||||||
|
|
||||||
sinon.stub(node._dht, 'findPeer').callsFake(() => {})
|
sinon.stub(node._dht, 'findPeer').callsFake(async function * () {})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(() => {
|
sinon.stub(delegate, 'findPeer').callsFake(() => {
|
||||||
return results
|
return results
|
||||||
})
|
})
|
||||||
@ -423,7 +423,8 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
const defer = pDefer()
|
const defer = pDefer()
|
||||||
|
|
||||||
sinon.stub(node._dht, 'findPeer').callsFake(async () => {
|
sinon.stub(node._dht, 'findPeer').callsFake(async function * () {
|
||||||
|
yield
|
||||||
await defer.promise
|
await defer.promise
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(() => {
|
sinon.stub(delegate, 'findPeer').callsFake(() => {
|
||||||
@ -438,29 +439,34 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
it('should not wait for the delegate to return if the dht does first', async () => {
|
it('should not wait for the delegate to return if the dht does first', async () => {
|
||||||
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
||||||
const results = {
|
const result = {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: []
|
multiaddrs: []
|
||||||
}
|
}
|
||||||
|
|
||||||
const defer = pDefer()
|
const defer = pDefer()
|
||||||
|
|
||||||
sinon.stub(node._dht, 'findPeer').callsFake(() => {
|
sinon.stub(node._dht, 'findPeer').callsFake(async function * () {
|
||||||
return results
|
yield {
|
||||||
|
name: 'PEER_RESPONSE',
|
||||||
|
closer: [
|
||||||
|
result
|
||||||
|
]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(async () => {
|
sinon.stub(delegate, 'findPeer').callsFake(async () => {
|
||||||
await defer.promise
|
await defer.promise
|
||||||
})
|
})
|
||||||
|
|
||||||
const peer = await node.peerRouting.findPeer(remotePeerId)
|
const peer = await node.peerRouting.findPeer(remotePeerId)
|
||||||
expect(peer).to.eql(results)
|
expect(peer).to.eql(result)
|
||||||
|
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should store the addresses of the found peer', async () => {
|
it('should store the addresses of the found peer', async () => {
|
||||||
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
const [remotePeerId] = await peerUtils.createPeerId({ fixture: false })
|
||||||
const results = {
|
const result = {
|
||||||
id: remotePeerId,
|
id: remotePeerId,
|
||||||
multiaddrs: [
|
multiaddrs: [
|
||||||
new Multiaddr('/ip4/123.123.123.123/tcp/38982')
|
new Multiaddr('/ip4/123.123.123.123/tcp/38982')
|
||||||
@ -469,14 +475,19 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
const spy = sinon.spy(node.peerStore.addressBook, 'add')
|
const spy = sinon.spy(node.peerStore.addressBook, 'add')
|
||||||
|
|
||||||
sinon.stub(node._dht, 'findPeer').callsFake(() => {
|
sinon.stub(node._dht, 'findPeer').callsFake(async function * () {
|
||||||
return results
|
yield {
|
||||||
|
name: 'PEER_RESPONSE',
|
||||||
|
closer: [
|
||||||
|
result
|
||||||
|
]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
sinon.stub(delegate, 'findPeer').callsFake(() => {})
|
sinon.stub(delegate, 'findPeer').callsFake(() => {})
|
||||||
|
|
||||||
await node.peerRouting.findPeer(remotePeerId)
|
await node.peerRouting.findPeer(remotePeerId)
|
||||||
|
|
||||||
expect(spy.calledWith(results.id, results.multiaddrs)).to.be.true()
|
expect(spy.calledWith(result.id, result.multiaddrs)).to.be.true()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should use the delegate if the dht fails to get the closest peer', async () => {
|
it('should use the delegate if the dht fails to get the closest peer', async () => {
|
||||||
@ -576,8 +587,18 @@ describe('peer-routing', () => {
|
|||||||
|
|
||||||
sinon.spy(node.peerStore.addressBook, 'add')
|
sinon.spy(node.peerStore.addressBook, 'add')
|
||||||
sinon.stub(node._dht, 'getClosestPeers').callsFake(function * () {
|
sinon.stub(node._dht, 'getClosestPeers').callsFake(function * () {
|
||||||
yield results[0]
|
yield {
|
||||||
yield results[1]
|
name: 'PEER_RESPONSE',
|
||||||
|
closer: [
|
||||||
|
results[0]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
yield {
|
||||||
|
name: 'PEER_RESPONSE',
|
||||||
|
closer: [
|
||||||
|
results[1]
|
||||||
|
]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
await node.start()
|
await node.start()
|
||||||
@ -611,7 +632,7 @@ describe('peer-routing', () => {
|
|||||||
started: false
|
started: false
|
||||||
})
|
})
|
||||||
|
|
||||||
sinon.stub(node._dht, 'getClosestPeers').callsFake(function * () {
|
sinon.stub(node._dht, 'getClosestPeers').callsFake(async function * () {
|
||||||
yield
|
yield
|
||||||
throw new Error('should not be called')
|
throw new Error('should not be called')
|
||||||
})
|
})
|
||||||
|
@ -13,9 +13,6 @@ const routingOptions = mergeOptions(baseOptions, {
|
|||||||
config: {
|
config: {
|
||||||
dht: {
|
dht: {
|
||||||
kBucketSize: 20,
|
kBucketSize: 20,
|
||||||
randomWalk: {
|
|
||||||
enabled: true
|
|
||||||
},
|
|
||||||
enabled: true
|
enabled: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ const { Multiaddr } = require('multiaddr')
|
|||||||
const mockUpgrader = require('../utils/mockUpgrader')
|
const mockUpgrader = require('../utils/mockUpgrader')
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
const Peers = require('../fixtures/peers')
|
const Peers = require('../fixtures/peers')
|
||||||
|
const pWaitFor = require('p-wait-for')
|
||||||
const addrs = [
|
const addrs = [
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/0'),
|
new Multiaddr('/ip4/127.0.0.1/tcp/0'),
|
||||||
new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
@ -72,9 +73,13 @@ describe('Transport Manager (TCP)', () => {
|
|||||||
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
|
||||||
await tm.listen(addrs)
|
await tm.listen(addrs)
|
||||||
|
|
||||||
// Should created Self Peer record on new listen address
|
// Should created Self Peer record on new listen address, but it is done async
|
||||||
signedPeerRecord = await tm.libp2p.peerStore.addressBook.getPeerRecord(localPeer)
|
// with no event so we have to wait a bit
|
||||||
expect(signedPeerRecord).to.exist()
|
await pWaitFor(async () => {
|
||||||
|
signedPeerRecord = await tm.libp2p.peerStore.addressBook.getPeerRecord(localPeer)
|
||||||
|
|
||||||
|
return signedPeerRecord != null
|
||||||
|
}, { interval: 100, timeout: 2000 })
|
||||||
|
|
||||||
const record = PeerRecord.createFromProtobuf(signedPeerRecord.payload)
|
const record = PeerRecord.createFromProtobuf(signedPeerRecord.payload)
|
||||||
expect(record).to.exist()
|
expect(record).to.exist()
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
"libp2p": "file:../..",
|
"libp2p": "file:../..",
|
||||||
"libp2p-bootstrap": "^0.13.0",
|
"libp2p-bootstrap": "^0.13.0",
|
||||||
"libp2p-delegated-content-routing": "^0.11.0",
|
"libp2p-delegated-content-routing": "^0.11.0",
|
||||||
"libp2p-delegated-peer-routing": "^0.10.0",
|
"libp2p-delegated-peer-routing": "^0.11.1",
|
||||||
"libp2p-gossipsub": "^0.9.0",
|
"libp2p-gossipsub": "^0.9.0",
|
||||||
"libp2p-interfaces": "^1.0.1",
|
"libp2p-interfaces": "^1.0.1",
|
||||||
"libp2p-kad-dht": "^0.26.5",
|
"libp2p-kad-dht": "^0.26.5",
|
||||||
|
@ -123,11 +123,6 @@ async function main() {
|
|||||||
dht: {
|
dht: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
kBucketSize: 20,
|
kBucketSize: 20,
|
||||||
randomWalk: {
|
|
||||||
enabled: true, // Allows to disable discovery (enabled by default)
|
|
||||||
interval: 300e3,
|
|
||||||
timeout: 10e3
|
|
||||||
},
|
|
||||||
clientMode: true,
|
clientMode: true,
|
||||||
validators: {
|
validators: {
|
||||||
pk: Libp2pRecord.validator.validators.pk
|
pk: Libp2pRecord.validator.validators.pk
|
||||||
|
Loading…
x
Reference in New Issue
Block a user