From 861fdc65e6c2cd552feb7d13fe328f9c0f3aabf8 Mon Sep 17 00:00:00 2001 From: apostolof Date: Sun, 7 Feb 2021 22:19:02 +0200 Subject: [PATCH] refactor: resolve concordia rendezvous domain --- packages/concordia-pinner/src/app.js | 16 ++++--- packages/concordia-pinner/src/constants.js | 5 +-- packages/concordia-pinner/src/index.js | 5 ++- .../src/options/ipfsOptions.js | 43 ++++++++++--------- .../src/options/libp2pBundle.js | 5 ++- .../concordia-pinner/src/utils/ipfsUtils.js | 11 +++++ .../concordia-pinner/src/utils/orbitUtils.js | 2 +- 7 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 packages/concordia-pinner/src/utils/ipfsUtils.js diff --git a/packages/concordia-pinner/src/app.js b/packages/concordia-pinner/src/app.js index 690e450..32e628e 100644 --- a/packages/concordia-pinner/src/app.js +++ b/packages/concordia-pinner/src/app.js @@ -3,7 +3,7 @@ import _ from 'lodash'; import isReachable from 'is-reachable'; import { pinnerApiPort } from 'concordia-shared/src/environment/interpolated/pinner'; import getWeb3ProviderUrl from 'concordia-shared/src/utils/web3'; -import getRendezvousUrl from 'concordia-shared/src/utils/rendezvous'; +import { getResolvedRendezvousUrl } from './utils/ipfsUtils'; const POLLING_INTERVAL = 1000; @@ -13,11 +13,17 @@ const responseBody = { }, orbit: { identity: {}, databases: [] }, web3: { url: getWeb3ProviderUrl(), reachable: false }, - rendezvous: { url: getRendezvousUrl(), reachable: false }, + rendezvous: { url: '', reachable: false }, timestamp: 0, }; -async function getStats(orbit) { +getResolvedRendezvousUrl().then(({ address }) => { + responseBody.rendezvous.url = address; +}); + +const getStats = async (orbit) => { + const { address: resolvedRendezvousUrl } = await getResolvedRendezvousUrl(); + try { // eslint-disable-next-line no-underscore-dangle const ipfs = orbit._ipfs; @@ -29,7 +35,7 @@ async function getStats(orbit) { const orbitIdentity = orbit.identity; const databases = Object.keys(orbit.stores); const isWeb3Reachable = await isReachable(getWeb3ProviderUrl()); - const isRendezvousReachable = await isReachable(getRendezvousUrl()); + const isRendezvousReachable = await isReachable(resolvedRendezvousUrl.address); const timestamp = +new Date(); responseBody.ipfs.id = id; @@ -45,7 +51,7 @@ async function getStats(orbit) { } catch (err) { console.error('Error while getting stats:', err); } -} +}; const startAPI = (orbit) => { const app = express(); diff --git a/packages/concordia-pinner/src/constants.js b/packages/concordia-pinner/src/constants.js index 6a8055f..bdec2d4 100644 --- a/packages/concordia-pinner/src/constants.js +++ b/packages/concordia-pinner/src/constants.js @@ -1,6 +1,5 @@ import path from 'path'; -import getBreezeConfiguration from 'concordia-shared/src/configuration/breezeConfiguration'; -export const swarmAddresses = getBreezeConfiguration().ipfs.config.Addresses.Swarm; +const ORBIT_DIRECTORY_DEFAULT = path.join(__dirname, '..', 'orbitdb'); -export const ORBIT_DIRECTORY_DEFAULT = path.join(__dirname, '..', 'orbitdb'); +export default ORBIT_DIRECTORY_DEFAULT; diff --git a/packages/concordia-pinner/src/index.js b/packages/concordia-pinner/src/index.js index aa5dbb2..f869266 100644 --- a/packages/concordia-pinner/src/index.js +++ b/packages/concordia-pinner/src/index.js @@ -5,9 +5,9 @@ import { contracts } from 'concordia-contracts'; import { FORUM_CONTRACT } from 'concordia-shared/src/constants/contracts/ContractNames'; import getWeb3ProviderUrl from 'concordia-shared/src/utils/web3'; import { createOrbitInstance, getPeerDatabases, openKVDBs } from './utils/orbitUtils'; -import ipfsOptions from './options/ipfsOptions'; import startAPI from './app'; import downloadContractArtifacts from './utils/drizzleUtils'; +import getIpfsOptions from './options/ipfsOptions'; process.on('unhandledRejection', (error) => { // This happens when attempting to initialize without any available Swarm addresses (e.g. Rendezvous) @@ -68,7 +68,8 @@ const main = async () => { const web3 = new Web3(new Web3.providers.WebsocketProvider(getWeb3ProviderUrl())); getDeployedContract(web3) - .then(({ contract, contractAddress }) => IPFS.create(ipfsOptions) + .then(({ contract, contractAddress }) => getIpfsOptions() + .then((ipfsOptions) => IPFS.create(ipfsOptions)) .then((ipfs) => createOrbitInstance(ipfs, contractAddress)) .then((orbit) => openExistingUsersDatabases(contract, orbit) .then(() => { diff --git a/packages/concordia-pinner/src/options/ipfsOptions.js b/packages/concordia-pinner/src/options/ipfsOptions.js index b25b5e6..3e56cc9 100644 --- a/packages/concordia-pinner/src/options/ipfsOptions.js +++ b/packages/concordia-pinner/src/options/ipfsOptions.js @@ -1,22 +1,25 @@ -import libp2pBundle from './libp2pBundle'; -import { swarmAddresses } from '../constants'; +import getLibp2pBundle from './libp2pBundle'; +import { getSwarmAddresses } from '../utils/ipfsUtils'; -export default { - repo: 'ipfs', - config: { - Profile: 'server', - Addresses: { - Swarm: swarmAddresses, +const getIpfsOptions = async () => getSwarmAddresses() + .then((swarmAddresses) => ({ + repo: 'ipfs', + config: { + Profile: 'server', + Addresses: { + Swarm: swarmAddresses, + }, }, - }, - libp2p: libp2pBundle, - EXPERIMENTAL: { - pubsub: true, - }, - preload: { - enabled: false, - }, - init: { - emptyRepo: true, - }, -}; + libp2p: getLibp2pBundle(swarmAddresses), + EXPERIMENTAL: { + pubsub: true, + }, + preload: { + enabled: false, + }, + init: { + emptyRepo: true, + }, + })); + +export default getIpfsOptions; diff --git a/packages/concordia-pinner/src/options/libp2pBundle.js b/packages/concordia-pinner/src/options/libp2pBundle.js index 4de108b..94c0b08 100644 --- a/packages/concordia-pinner/src/options/libp2pBundle.js +++ b/packages/concordia-pinner/src/options/libp2pBundle.js @@ -7,10 +7,9 @@ import Gossipsub from 'libp2p-gossipsub'; import KadDHT from 'libp2p-kad-dht'; import MPLEX from 'libp2p-mplex'; import { NOISE } from 'libp2p-noise'; -import { swarmAddresses } from '../constants'; // See also: https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md -export default (opts) => new Libp2p({ +const getLibp2pBundle = (swarmAddresses) => (opts) => new Libp2p({ peerId: opts.peerId, addresses: { listen: swarmAddresses, @@ -88,3 +87,5 @@ export default (opts) => new Libp2p({ maxOldPeersRetention: 50, }, }); + +export default getLibp2pBundle; diff --git a/packages/concordia-pinner/src/utils/ipfsUtils.js b/packages/concordia-pinner/src/utils/ipfsUtils.js new file mode 100644 index 0000000..6fa68c2 --- /dev/null +++ b/packages/concordia-pinner/src/utils/ipfsUtils.js @@ -0,0 +1,11 @@ +import dns from 'dns'; +import util from 'util'; +import { rendezvousHost, rendezvousPort } from 'concordia-shared/src/environment/interpolated/rendezvous'; + +const dnsLookup = util.promisify(dns.lookup); + +export const getResolvedRendezvousUrl = async () => dnsLookup(rendezvousHost, { family: 4 }) + .catch((error) => console.error(new Error(`DNS lookup of ${rendezvousHost} failed.\n${error}`))); + +export const getSwarmAddresses = async () => getResolvedRendezvousUrl() + .then((resolvedRendezvousUrl) => [`/ip4/${resolvedRendezvousUrl.address}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`]); diff --git a/packages/concordia-pinner/src/utils/orbitUtils.js b/packages/concordia-pinner/src/utils/orbitUtils.js index 71f47bf..74aae64 100644 --- a/packages/concordia-pinner/src/utils/orbitUtils.js +++ b/packages/concordia-pinner/src/utils/orbitUtils.js @@ -2,7 +2,7 @@ import OrbitDB from 'orbit-db'; import Identities from 'orbit-db-identity-provider'; import { EthereumContractIdentityProvider } from '@ezerous/eth-identity-provider'; import Web3 from 'web3'; -import { ORBIT_DIRECTORY_DEFAULT } from '../constants'; +import ORBIT_DIRECTORY_DEFAULT from '../constants'; // TODO: share code below with frontend (?) const determineDBAddress = async ({