Created
June 6, 2020 11:09
-
-
Save vgiotsas/573aa050a481db6a49f670d4ca32fb5f to your computer and use it in GitHub Desktop.
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
"use strict"; | |
const IPFS = require('ipfs') | |
const Protector = require('libp2p/src/pnet') | |
const TCP = require('libp2p-tcp') | |
const KadDHT = require('libp2p-kad-dht') | |
const MPLEX = require('libp2p-mplex') | |
const SECIO = require('libp2p-secio') | |
const { NOISE } = require('libp2p-noise') | |
const Bootstrap = require('libp2p-bootstrap') | |
var fs = require('fs'); | |
var os = require('os'); | |
var events = require("events"); | |
// the path to my swarm key | |
const swarmKeyPath = '/path/to/swarm.key'; | |
const createNode = async (i, bootstrapers) => { | |
var node_repo = '/tmp/ipfs' + i; | |
var swarm_port = 4002 + i; | |
var api_port = 5002 + i; | |
var gateway_port = 8082 + i; | |
const node = await IPFS.create( | |
{ | |
repo: node_repo, | |
libp2p: { | |
connectionManager: { | |
minPeers: 25, | |
maxPeers: 100, | |
pollInterval: 5000 | |
}, | |
modules: { | |
transport: [TCP], | |
streamMuxer: [MPLEX], | |
connEncryption: [NOISE, SECIO], | |
peerDiscovery: [ | |
Bootstrap | |
], | |
dht: KadDHT, | |
connProtector: new Protector(fs.readFileSync(swarmKeyPath)) | |
}, | |
config:{ | |
dht: { | |
enabled: true, | |
kBucketSize: 20, | |
randomWalk: { | |
enabled: true, | |
interval: 10e3, | |
timeout: 2e3 | |
} | |
}, | |
relay: { | |
enabled: true, | |
hop: { | |
enabled: true, | |
active: true | |
} | |
}, | |
peerDiscovery: | |
{ | |
autoDial: true, | |
bootstrap: { | |
interval: 60e3, | |
enabled: true, | |
list: bootstrapers | |
} | |
} | |
} | |
}, | |
config: { | |
Addresses: { | |
Swarm: [ | |
'/ip4/0.0.0.0/tcp/' + swarm_port | |
], | |
API: '/ip4/127.0.0.1/tcp/' + api_port, | |
Gateway: '/ip4/127.0.0.1/tcp/' + gateway_port | |
}, | |
Bootstrap: bootstrapers | |
} | |
} | |
) | |
return node; | |
} | |
function connectNodes(nodes, multiaddress, nid){ | |
nodes.forEach(async function(node){ | |
var info = await node.id(); | |
var node_ma = info.addresses[1].toString(); | |
if (multiaddress != node_ma){ | |
try{ | |
await node.swarm.connect(multiaddress); | |
await nodes[0].dht.findPeer(nid); | |
} | |
catch(e){ | |
console.log(e); | |
} | |
} | |
}); | |
} | |
// My private bootstrap node | |
const bootstrapers = [ | |
'/ip4/172.31.57.88/tcp/4001/p2p/QmWxCxpRLwoNtjFh9gcM1zwPjHGfSYFAWmMY62wkr5R7wQ' | |
] | |
;(async() => { | |
var nodes = []; | |
var multiaddresses = []; | |
var nodes_num = 2; | |
for (var i = 0; i < nodes_num; i++) { | |
var nodePromise = createNode(i, bootstrapers); | |
var n = await Promise.resolve(nodePromise); | |
var info = await n.id(); | |
await connectNodes(nodes, info.addresses[1].toString(), info.id); | |
nodes.push(n); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment