mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-30 22:31:03 +00:00
* 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
146 lines
3.7 KiB
JavaScript
146 lines
3.7 KiB
JavaScript
/* eslint no-console: ["off"] */
|
|
'use strict'
|
|
|
|
const IPFS = require('ipfs')
|
|
const assert = require('assert').strict
|
|
const writeKey = require('libp2p-pnet').generate
|
|
const path = require('path')
|
|
const fs = require('fs')
|
|
const privateLibp2pBundle = require('./libp2p-bundle')
|
|
const { mkdirp } = require('./utils')
|
|
|
|
// Create two separate repo paths so we can run two nodes and check their output
|
|
const repo1 = path.resolve('./tmp', 'repo1', '.ipfs')
|
|
const repo2 = path.resolve('./tmp', 'repo2', '.ipfs')
|
|
mkdirp(repo1)
|
|
mkdirp(repo2)
|
|
|
|
// Create a buffer and write the swarm key to it
|
|
const swarmKey = Buffer.alloc(95)
|
|
writeKey(swarmKey)
|
|
|
|
// This key is for the `TASK` mentioned in the writeFileSync calls below
|
|
const otherSwarmKey = Buffer.alloc(95)
|
|
writeKey(otherSwarmKey)
|
|
|
|
// Add the swarm key to both repos
|
|
const swarmKey1Path = path.resolve(repo1, 'swarm.key')
|
|
const swarmKey2Path = path.resolve(repo2, 'swarm.key')
|
|
fs.writeFileSync(swarmKey1Path, swarmKey)
|
|
// TASK: switch the commented out line below so we're using a different key, to see the nodes fail to connect
|
|
fs.writeFileSync(swarmKey2Path, swarmKey)
|
|
// fs.writeFileSync(swarmKey2Path, otherSwarmKey)
|
|
|
|
// Create the first ipfs node
|
|
const node1 = new IPFS({
|
|
repo: repo1,
|
|
libp2p: privateLibp2pBundle(swarmKey1Path),
|
|
config: {
|
|
Addresses: {
|
|
// Set the swarm address so we dont get port collision on the nodes
|
|
Swarm: ['/ip4/0.0.0.0/tcp/9101']
|
|
}
|
|
}
|
|
})
|
|
|
|
// Create the second ipfs node
|
|
const node2 = new IPFS({
|
|
repo: repo2,
|
|
libp2p: privateLibp2pBundle(swarmKey2Path),
|
|
config: {
|
|
Addresses: {
|
|
// Set the swarm address so we dont get port collision on the nodes
|
|
Swarm: ['/ip4/0.0.0.0/tcp/9102']
|
|
}
|
|
}
|
|
})
|
|
|
|
console.log('auto starting the nodes...')
|
|
|
|
// `nodesStarted` keeps track of how many of our nodes have started
|
|
let nodesStarted = 0
|
|
/**
|
|
* Calls `connectAndTalk` when both nodes have started
|
|
* @returns {void}
|
|
*/
|
|
const didStartHandler = () => {
|
|
if (++nodesStarted === 2) {
|
|
// If both nodes are up, start talking
|
|
connectAndTalk()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Exits the process when all started nodes have stopped
|
|
* @returns {void}
|
|
*/
|
|
const didStopHandler = () => {
|
|
if (--nodesStarted < 1) {
|
|
console.log('all nodes stopped, exiting.')
|
|
process.exit(0)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Stops the running nodes
|
|
* @param {Error} err An optional error to log to the console
|
|
* @returns {void}
|
|
*/
|
|
const doStop = (err) => {
|
|
if (err) {
|
|
console.error(err)
|
|
}
|
|
|
|
console.log('Shutting down...')
|
|
node1.stop()
|
|
node2.stop()
|
|
}
|
|
|
|
/**
|
|
* Connects the IPFS nodes and transfers data between them
|
|
* @returns {void}
|
|
*/
|
|
const connectAndTalk = async () => {
|
|
console.log('connecting the nodes...')
|
|
const node2Id = await node2.id()
|
|
const dataToAdd = Buffer.from('Hello, private friend!')
|
|
|
|
// Connect the nodes
|
|
// This will error when different private keys are used
|
|
try {
|
|
await node1.swarm.connect(node2Id.addresses[0])
|
|
} catch (err) {
|
|
return doStop(err)
|
|
}
|
|
console.log('the nodes are connected, let\'s add some data')
|
|
|
|
// Add some data to node 1
|
|
let addedCID
|
|
try {
|
|
addedCID = await node1.files.add(dataToAdd)
|
|
} catch (err) {
|
|
return doStop(err)
|
|
}
|
|
console.log(`added ${addedCID[0].path} to the node1`)
|
|
|
|
// Retrieve the data from node 2
|
|
let cattedData
|
|
try {
|
|
cattedData = await node2.files.cat(addedCID[0].path)
|
|
} catch (err) {
|
|
return doStop(err)
|
|
}
|
|
assert.deepEqual(cattedData.toString(), dataToAdd.toString(), 'Should have equal data')
|
|
console.log(`successfully retrieved "${dataToAdd.toString()}" from node2`)
|
|
|
|
doStop()
|
|
}
|
|
|
|
// Wait for the nodes to boot
|
|
node1.once('start', didStartHandler)
|
|
node2.once('start', didStartHandler)
|
|
|
|
// Listen for the nodes stopping so we can cleanup
|
|
node1.once('stop', didStopHandler)
|
|
node2.once('stop', didStopHandler)
|