mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-30 16:01:59 +00:00
refactor: add core modules to libp2p (#400)
* refactor: add js-libp2p-connection-manager to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> * test(conn-mgr): only run in node * refactor: add js-libp2p-identify to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Yusef Napora <yusef@protocol.ai> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-pnet to repo Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> * refactor: add libp2p-ping to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: João Antunes <j.goncalo.antunes@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-circuit to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: Victor Bjelkholm <victorbjelkholm@gmail.com> Co-authored-by: Yusef Napora <yusef@napora.org> Co-authored-by: dirkmc <dirk@mccormick.cx> * test(switch): avoid using instanceof * chore(switch): update bignumber dep * refactor(circuit): clean up tests * refactor(switch): consolidate get peer utils * test(identify): do deep checks of addresses * test(identify): bump timeout for identify test * test(switch): tidy up limit dialer test * refactor(switch): remove redundant circuit tests * chore: add coverage script * refactor(circuit): consolidate get peer info * docs: reference original repositories in each sub readme * docs: fix comment * refactor: clean up sub package.json files and readmes
This commit is contained in:
17
test/connection-manager/utils/connect-all.js
Normal file
17
test/connection-manager/utils/connect-all.js
Normal file
@@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
const eachSeries = require('async/eachSeries')
|
||||
|
||||
module.exports = (nodes, callback) => {
|
||||
eachSeries(
|
||||
nodes,
|
||||
(node, cb) => {
|
||||
eachSeries(
|
||||
nodes.filter(n => node !== n),
|
||||
(otherNode, cb) => node.dial(otherNode.peerInfo, cb),
|
||||
cb
|
||||
)
|
||||
},
|
||||
callback
|
||||
)
|
||||
}
|
50
test/connection-manager/utils/create-libp2p-node.js
Normal file
50
test/connection-manager/utils/create-libp2p-node.js
Normal file
@@ -0,0 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const Multiplex = require('libp2p-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const libp2p = require('../../../src')
|
||||
const waterfall = require('async/waterfall')
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const ConnManager = require('libp2p-connection-manager')
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (peerInfo) {
|
||||
const modules = {
|
||||
transport: [TCP],
|
||||
streamMuxer: [Multiplex],
|
||||
connEncryption: [SECIO]
|
||||
}
|
||||
|
||||
super({
|
||||
peerInfo,
|
||||
modules,
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: false
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function createLibp2pNode (options, callback) {
|
||||
let node
|
||||
|
||||
waterfall([
|
||||
(cb) => PeerId.create({ bits: 1024 }, cb),
|
||||
(id, cb) => PeerInfo.create(id, cb),
|
||||
(peerInfo, cb) => {
|
||||
peerInfo.multiaddrs.add('/ip4/127.0.0.1/tcp/0')
|
||||
node = new Node(peerInfo)
|
||||
// Replace the connection manager so we use source code instead of dep code
|
||||
node.connectionManager = new ConnManager(node, options)
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
}
|
||||
|
||||
exports = module.exports = createLibp2pNode
|
||||
exports.bundle = Node
|
83
test/connection-manager/utils/prepare.js
Normal file
83
test/connection-manager/utils/prepare.js
Normal file
@@ -0,0 +1,83 @@
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const expect = chai.expect
|
||||
|
||||
const series = require('async/series')
|
||||
const each = require('async/each')
|
||||
|
||||
const createLibp2pNode = require('./create-libp2p-node')
|
||||
const connectAll = require('./connect-all')
|
||||
const tryConnectAll = require('./try-connect-all')
|
||||
|
||||
module.exports = (count, options) => {
|
||||
let nodes
|
||||
|
||||
if (!Array.isArray(options)) {
|
||||
const opts = options
|
||||
options = []
|
||||
for (let n = 0; n < count; n++) {
|
||||
options[n] = opts
|
||||
}
|
||||
}
|
||||
|
||||
const create = (done) => {
|
||||
const tasks = []
|
||||
for (let i = 0; i < count; i++) {
|
||||
tasks.push((cb) => createLibp2pNode(options.shift() || {}, cb))
|
||||
}
|
||||
|
||||
series(tasks, (err, things) => {
|
||||
if (!err) {
|
||||
nodes = things
|
||||
expect(things.length).to.equal(count)
|
||||
}
|
||||
done(err)
|
||||
})
|
||||
}
|
||||
|
||||
const connect = function (done) {
|
||||
if (this && this.timeout) {
|
||||
this.timeout(10000)
|
||||
}
|
||||
connectAll(nodes, done)
|
||||
}
|
||||
|
||||
const tryConnectAllFn = function (done) {
|
||||
if (this && this.timeout) {
|
||||
this.timeout(10000)
|
||||
}
|
||||
tryConnectAll(nodes, done)
|
||||
}
|
||||
|
||||
const before = (done) => {
|
||||
if (this && this.timeout) {
|
||||
this.timeout(10000)
|
||||
}
|
||||
series([create, connect], done)
|
||||
}
|
||||
|
||||
const after = function (done) {
|
||||
if (this && this.timeout) {
|
||||
this.timeout(10000)
|
||||
}
|
||||
if (!nodes) { return done() }
|
||||
|
||||
each(nodes, (node, cb) => {
|
||||
series([
|
||||
(cb) => node.stop(cb)
|
||||
], cb)
|
||||
}, done)
|
||||
}
|
||||
|
||||
return {
|
||||
create,
|
||||
connect,
|
||||
tryConnectAll: tryConnectAllFn,
|
||||
before,
|
||||
after,
|
||||
things: () => nodes,
|
||||
connManagers: () => nodes.map((node) => node.connectionManager)
|
||||
}
|
||||
}
|
27
test/connection-manager/utils/try-connect-all.js
Normal file
27
test/connection-manager/utils/try-connect-all.js
Normal file
@@ -0,0 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const mapSeries = require('async/mapSeries')
|
||||
const eachSeries = require('async/eachSeries')
|
||||
|
||||
module.exports = (nodes, callback) => {
|
||||
mapSeries(
|
||||
nodes,
|
||||
(node, cb) => {
|
||||
const connectedTo = []
|
||||
eachSeries(
|
||||
nodes.filter(n => node !== n),
|
||||
(otherNode, cb) => {
|
||||
const otherNodePeerInfo = otherNode.peerInfo
|
||||
node.dial(otherNodePeerInfo, (err) => {
|
||||
if (!err) {
|
||||
connectedTo.push(otherNodePeerInfo.id.toB58String())
|
||||
}
|
||||
cb()
|
||||
})
|
||||
},
|
||||
(err) => cb(err, connectedTo)
|
||||
)
|
||||
},
|
||||
callback
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user