diff --git a/bin/chlu-service-node.js b/bin/chlu-service-node.js index 6e94e82..93e5a8d 100755 --- a/bin/chlu-service-node.js +++ b/bin/chlu-service-node.js @@ -23,9 +23,10 @@ async function start(options){ directory: options.directory, ipfs: { remote: options.externalIpfs, - enableRelayHop: options.enableRelayHop + enableRelayHop: options.relay }, listen: options.listen, + useCircuit: options.circuit || options.relay, useRendezvous: options.rendezvous }; serviceNode = new ChluIPFS(config); @@ -54,12 +55,14 @@ cli cli .command('start') .description('run the Service Node') - .option('-n, --network ', 'use a custom network instead of production') + .option('-n, --network ', 'use a custom network instead of experimental') .option('-d, --directory ', 'where to store chlu data, defaults to ~/.chlu') - .option('-e, --external-ipfs', 'connect to a running IPFS node at localhost:5001') - .option('-r, --relay', 'act as libp2p relay to help nodes connect to each other') .option('--no-rendezvous', 'disable usage of rendezvous servers (not recommended right now)') - .option('--no-listen', "don't listen for incoming connections") + .option('--listen', 'listen for incoming connections (not recommended right now)') + // TODO: reenable these when they are supported again +// .option('-e, --external-ipfs', 'connect to a running IPFS node at localhost:5001 instead of running IPFS internally') +// .option('-c, --circuit', 'enable libp2p circuit relay to use relays to connect to peers') +// .option('-r, --relay', 'act as libp2p relay to help nodes connect to each other (implicitly turns on --circuit)') .action(handleErrors(async cmd => { await start(cmd); })); diff --git a/package.json b/package.json index 21283b1..af3acf1 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "orbit-db": "orbitdb/orbit-db#master", "protons": "~1.0.1" }, - "resolutions": { + "//resolutions": { "ipfs-log": "ChluNetwork/ipfs-log#custom-verification", "ipfs-pubsub-1on1": "ChluNetwork/ipfs-pubsub-1on1#ipfs-api-fix" }, diff --git a/src/ChluIPFS.js b/src/ChluIPFS.js index 1b88a07..f6cc533 100644 --- a/src/ChluIPFS.js +++ b/src/ChluIPFS.js @@ -52,14 +52,25 @@ class ChluIPFS { const defaultSwarmAddresses = env.isNode() ? constants.defaultSwarmAddresses.nodeJs : constants.defaultSwarmAddresses.browser; - const swarmAddresses = options.listen === false ? [] : defaultSwarmAddresses; + const swarmAddresses = options.listen ? defaultSwarmAddresses : []; if (options.useRendezvous !== false) { // By default, use rendezvous points for websocket-star swarmAddresses.push(...constants.defaultSwarmAddresses.rendezvous); } + if (options.circuit || options.relay) { + // Do not use circuit relay by default + // Acting as relay requires enabling circuit + this.circuit = true; + } + if (options.relay) { + // Do not act as relay by default + this.relay = true; + } this.ipfsOptions = Object.assign( {}, + // default IPFS config constants.defaultIPFSOptions, + // pass swarm addresses determined just now { config: { Addresses: { @@ -67,19 +78,17 @@ class ChluIPFS { } } }, + // additional options set up just now additionalOptions, + // finally options passed by the caller options.ipfs || {} ); // Set up OrbitDB Directory/Path this.orbitDbDirectory = options.orbitDbDirectory || IPFSUtils.getDefaultOrbitDBPath(this.directory); // Set up Chlu bootstrap nodes this.chluBootstrapNodes = cloneDeep(constants.chluBootstrapNodes); - // enable Chlu bootstrap by default - if (options.bootstrap === false) { - this.bootstrap = false; - } else { - this.bootstrap = true; - } + // disable Chlu bootstrap by default + this.bootstrap = options.bootstrap; // Check Chlu node type this.type = options.type; if (Object.values(constants.types).indexOf(this.type) < 0) { diff --git a/src/constants.js b/src/constants.js index 244ea06..6feed93 100644 --- a/src/constants.js +++ b/src/constants.js @@ -27,11 +27,11 @@ module.exports = { defaultIPFSOptions: { // ipfs EXPERIMENTAL: { - pubsub: true, + pubsub: true, // REQUIRED for chlu to work relay: { - enabled: true, // enable this to use relays to connect to nodes + enabled: false, // enable this to use relays to connect to nodes hop: { - enabled: false // disable this, we don't want to act as relay to save bandwidth + enabled: false // enable this to act as relay } } }, @@ -52,7 +52,7 @@ module.exports = { ], rendezvous: [ // Helps browser connectivity, until circuit relay can replace this - '/dns4/ws-star-signal-2.servep2p.com/tcp/443/wss/p2p-websocket-star' + '/dns4/ren.chlu.io/tcp/443/wss/p2p-websocket-star' ] }, chluBootstrapNodes: { diff --git a/src/modules/ipfs.js b/src/modules/ipfs.js index 85c2229..5ae9480 100644 --- a/src/modules/ipfs.js +++ b/src/modules/ipfs.js @@ -14,19 +14,20 @@ class IPFS { if (!this.chluIpfs.ipfs) { logger.debug('Initializing IPFS, type: ' + (this.chluIpfs.ipfsOptions.type || 'JS (Internal)')); logger.debug('Detected environment: ' + env.isNode() ? 'Node.JS' : 'Browser'); - if (this.chluIpfs.ipfsOptions.enableRelayHop) { - logger.info('Acting as libp2p relay'); - } let ipfs; if (this.chluIpfs.ipfsOptions.remote) { logger.debug('Connecting to IPFS API'); // Connect to existing IPFS Node ipfs = IPFSAPI(this.chluIpfs.ipfsOptions); + // TODO: check that pubsub is supported (it might be disabled) } else { logger.debug('Starting JS-IPFS in this process'); // Default: Start a local IPFS Node - if (this.chluIpfs.ipfsOptions.enableRelayHop) { - set(this.chluIpfs.ipfsOptions, 'EXPERIMENTAL.relay.hop.enable', true); + if (this.chluIpfs.circuit) { + set(this.chluIpfs.ipfsOptions, 'EXPERIMENTAL.relay.enabled', true); + } + if (this.chluIpfs.relay) { + set(this.chluIpfs.ipfsOptions, 'EXPERIMENTAL.relay.hop.enabled', true); } ipfs = await utils.createIPFS(this.chluIpfs.ipfsOptions); } @@ -36,8 +37,7 @@ class IPFS { if (this.chluIpfs.bootstrap) { logger.debug('Connecting to bootstrap Chlu nodes'); const nodes = env.isNode() ? this.chluIpfs.chluBootstrapNodes.nodeJs : this.chluIpfs.chluBootstrapNodes.browser; - await this.connectToNodes(nodes); - logger.debug('Connected to bootstrap Chlu nodes'); + this.connectToNodes(nodes); // do not await for this, let it run in the background } else { logger.debug('Skipping Chlu bootstrap phase'); } @@ -55,17 +55,25 @@ class IPFS { async connectToNodes(addrs) { const total = addrs.length; - return Promise.all(addrs.map(async (addr, ii) => { - const i = ii + 1; - try { - this.chluIpfs.logger.debug('Connecting to IPFS address (' + i + '/' + total + ') ' + addr); - await this.chluIpfs.ipfs.swarm.connect(addr); - this.chluIpfs.logger.debug('Connected to IPFS address (' + i + '/' + total + ') ' + addr); - } catch (error) { - this.chluIpfs.logger.warn('Connection FAILED to IPFS address (' + i + '/' + total + ') ' + addr); - console.trace(error); - } - })); + this.chluIpfs.logger.debug('Started connection attempt to ' + total + ' addresses'); + let connectedCount = 0; + return new Promise(resolve => { + addrs.map(async (addr, ii) => { + const i = ii + 1; + try { + this.chluIpfs.logger.debug('Connecting to IPFS address (' + i + '/' + total + ') ' + addr); + await this.chluIpfs.ipfs.swarm.connect(addr); + connectedCount++; + this.chluIpfs.logger.debug('Connected to IPFS address (' + i + '/' + total + ') ' + addr); + } catch (error) { + this.chluIpfs.logger.debug('Connection FAILED to IPFS address (' + i + '/' + total + ') ' + addr); + } + if (i === total) { + this.chluIpfs.logger.debug('Connect attempts finished. Connected to ' + connectedCount + '/' + total); + resolve(connectedCount); + } + }); + }); } async id() { diff --git a/yarn.lock b/yarn.lock index 13ad3a3..efec089 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4004,7 +4004,7 @@ ipfs-block@~0.6.1: dependencies: cids "^0.5.2" -ipfs-log@ChluNetwork/ipfs-log#custom-verification, ipfs-log@~4.1.0: +ipfs-log@~4.1.0: version "4.1.0" resolved "https://codeload.github.com/ChluNetwork/ipfs-log/tar.gz/36129b31913706183e2b625c7aae658a713aba6c" dependencies: @@ -4018,7 +4018,7 @@ ipfs-multipart@~0.1.0: content "^3.0.0" dicer "^0.2.5" -ipfs-pubsub-1on1@ChluNetwork/ipfs-pubsub-1on1#ipfs-api-fix, ipfs-pubsub-1on1@~0.0.3: +ipfs-pubsub-1on1@~0.0.3: version "0.0.3" resolved "https://codeload.github.com/ChluNetwork/ipfs-pubsub-1on1/tar.gz/f6214bb3ecce9bd25243465d09a0211f88c895ba" dependencies: