|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Libp2p = require('libp2p') |
| 4 | +const IPFS = require('ipfs') |
| 5 | +const TCP = require('libp2p-tcp') |
| 6 | +const MulticastDNS = require('libp2p-mdns') |
| 7 | +const WebSocketStar = require('libp2p-websocket-star') |
| 8 | +const Bootstrap = require('libp2p-railing') |
| 9 | +const SPDY = require('libp2p-spdy') |
| 10 | +const KadDHT = require('libp2p-kad-dht') |
| 11 | +const MPLEX = require('libp2p-mplex') |
| 12 | +const SECIO = require('libp2p-secio') |
| 13 | +const assert = require('assert') |
| 14 | + |
| 15 | +/** |
| 16 | + * Options for the libp2p bundle |
| 17 | + * @typedef {Object} libp2pBundle~options |
| 18 | + * @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node |
| 19 | + * @property {PeerBook} peerBook - The PeerBook of the IPFS node |
| 20 | + * @property {Object} config - The config of the IPFS node |
| 21 | + * @property {Object} options - The options given to the IPFS node |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * This is the bundle we will use to create our fully customized libp2p bundle. |
| 26 | + * |
| 27 | + * @param {libp2pBundle~options} opts The options to use when generating the libp2p node |
| 28 | + * @returns {Libp2p} Our new libp2p node |
| 29 | + */ |
| 30 | +const libp2pBundle = (opts) => { |
| 31 | + // Set convenience variables to clearly showcase some of the useful things that are available |
| 32 | + const peerInfo = opts.peerInfo |
| 33 | + const peerBook = opts.peerBook |
| 34 | + const bootstrapList = opts.config.Bootstrap |
| 35 | + |
| 36 | + // Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node |
| 37 | + const wsstar = new WebSocketStar({ |
| 38 | + id: peerInfo.id |
| 39 | + }) |
| 40 | + |
| 41 | + // Build and return our libp2p node |
| 42 | + return new Libp2p({ |
| 43 | + peerInfo, |
| 44 | + peerBook, |
| 45 | + // Lets limit the connection managers peers and have it check peer health less frequently |
| 46 | + connectionManager: { |
| 47 | + maxPeers: 25, |
| 48 | + pollInterval: 5000 |
| 49 | + }, |
| 50 | + modules: { |
| 51 | + transport: [ |
| 52 | + TCP, |
| 53 | + wsstar |
| 54 | + ], |
| 55 | + streamMuxer: [ |
| 56 | + MPLEX, |
| 57 | + SPDY |
| 58 | + ], |
| 59 | + connEncryption: [ |
| 60 | + SECIO |
| 61 | + ], |
| 62 | + peerDiscovery: [ |
| 63 | + MulticastDNS, |
| 64 | + Bootstrap, |
| 65 | + wsstar.discovery |
| 66 | + ], |
| 67 | + dht: KadDHT |
| 68 | + }, |
| 69 | + config: { |
| 70 | + peerDiscovery: { |
| 71 | + mdns: { |
| 72 | + interval: 10000, |
| 73 | + enabled: true |
| 74 | + }, |
| 75 | + bootstrap: { |
| 76 | + interval: 10000, |
| 77 | + enabled: true, |
| 78 | + list: bootstrapList |
| 79 | + } |
| 80 | + }, |
| 81 | + // Turn on relay with hop active so we can connect to more peers |
| 82 | + relay: { |
| 83 | + enabled: true, |
| 84 | + hop: { |
| 85 | + enabled: true, |
| 86 | + active: true |
| 87 | + } |
| 88 | + }, |
| 89 | + dht: { |
| 90 | + kBucketSize: 20 |
| 91 | + }, |
| 92 | + EXPERIMENTAL: { |
| 93 | + dht: true, |
| 94 | + pubsub: true |
| 95 | + } |
| 96 | + } |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// Now that we have our custom libp2p bundle, let's start up the ipfs node! |
| 101 | +const node = new IPFS({ |
| 102 | + libp2p: libp2pBundle |
| 103 | +}) |
| 104 | + |
| 105 | +// Listen for the node to start, so we can log out some metrics |
| 106 | +node.once('start', (err) => { |
| 107 | + assert.ifError(err, 'Should startup without issue') |
| 108 | + |
| 109 | + // Lets log out the number of peers we have every 2 seconds |
| 110 | + setInterval(() => { |
| 111 | + node.swarm.peers((err, peers) => { |
| 112 | + if (err) { |
| 113 | + console.log('An error occurred trying to check our peers:', err) |
| 114 | + process.exit(1) |
| 115 | + } |
| 116 | + console.log(`The node now has ${peers.length} peers.`) |
| 117 | + }) |
| 118 | + }, 2000) |
| 119 | + |
| 120 | + // Log out the bandwidth stats every 4 seconds so we can see how our configuration is doing |
| 121 | + setInterval(() => { |
| 122 | + node.stats.bw((err, stats) => { |
| 123 | + if (err) { |
| 124 | + console.log('An error occurred trying to check our stats:', err) |
| 125 | + } |
| 126 | + console.log(`\nBandwidth Stats: ${JSON.stringify(stats, null, 2)}\n`) |
| 127 | + }) |
| 128 | + }, 4000) |
| 129 | +}) |
0 commit comments