Browse Source

refactor: resolve concordia rendezvous domain

develop
Apostolos Fanakis 4 years ago
parent
commit
861fdc65e6
  1. 16
      packages/concordia-pinner/src/app.js
  2. 5
      packages/concordia-pinner/src/constants.js
  3. 5
      packages/concordia-pinner/src/index.js
  4. 13
      packages/concordia-pinner/src/options/ipfsOptions.js
  5. 5
      packages/concordia-pinner/src/options/libp2pBundle.js
  6. 11
      packages/concordia-pinner/src/utils/ipfsUtils.js
  7. 2
      packages/concordia-pinner/src/utils/orbitUtils.js

16
packages/concordia-pinner/src/app.js

@ -3,7 +3,7 @@ import _ from 'lodash';
import isReachable from 'is-reachable'; import isReachable from 'is-reachable';
import { pinnerApiPort } from 'concordia-shared/src/environment/interpolated/pinner'; import { pinnerApiPort } from 'concordia-shared/src/environment/interpolated/pinner';
import getWeb3ProviderUrl from 'concordia-shared/src/utils/web3'; 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; const POLLING_INTERVAL = 1000;
@ -13,11 +13,17 @@ const responseBody = {
}, },
orbit: { identity: {}, databases: [] }, orbit: { identity: {}, databases: [] },
web3: { url: getWeb3ProviderUrl(), reachable: false }, web3: { url: getWeb3ProviderUrl(), reachable: false },
rendezvous: { url: getRendezvousUrl(), reachable: false }, rendezvous: { url: '', reachable: false },
timestamp: 0, timestamp: 0,
}; };
async function getStats(orbit) { getResolvedRendezvousUrl().then(({ address }) => {
responseBody.rendezvous.url = address;
});
const getStats = async (orbit) => {
const { address: resolvedRendezvousUrl } = await getResolvedRendezvousUrl();
try { try {
// eslint-disable-next-line no-underscore-dangle // eslint-disable-next-line no-underscore-dangle
const ipfs = orbit._ipfs; const ipfs = orbit._ipfs;
@ -29,7 +35,7 @@ async function getStats(orbit) {
const orbitIdentity = orbit.identity; const orbitIdentity = orbit.identity;
const databases = Object.keys(orbit.stores); const databases = Object.keys(orbit.stores);
const isWeb3Reachable = await isReachable(getWeb3ProviderUrl()); const isWeb3Reachable = await isReachable(getWeb3ProviderUrl());
const isRendezvousReachable = await isReachable(getRendezvousUrl()); const isRendezvousReachable = await isReachable(resolvedRendezvousUrl.address);
const timestamp = +new Date(); const timestamp = +new Date();
responseBody.ipfs.id = id; responseBody.ipfs.id = id;
@ -45,7 +51,7 @@ async function getStats(orbit) {
} catch (err) { } catch (err) {
console.error('Error while getting stats:', err); console.error('Error while getting stats:', err);
} }
} };
const startAPI = (orbit) => { const startAPI = (orbit) => {
const app = express(); const app = express();

5
packages/concordia-pinner/src/constants.js

@ -1,6 +1,5 @@
import path from 'path'; 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;

5
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 { FORUM_CONTRACT } from 'concordia-shared/src/constants/contracts/ContractNames';
import getWeb3ProviderUrl from 'concordia-shared/src/utils/web3'; import getWeb3ProviderUrl from 'concordia-shared/src/utils/web3';
import { createOrbitInstance, getPeerDatabases, openKVDBs } from './utils/orbitUtils'; import { createOrbitInstance, getPeerDatabases, openKVDBs } from './utils/orbitUtils';
import ipfsOptions from './options/ipfsOptions';
import startAPI from './app'; import startAPI from './app';
import downloadContractArtifacts from './utils/drizzleUtils'; import downloadContractArtifacts from './utils/drizzleUtils';
import getIpfsOptions from './options/ipfsOptions';
process.on('unhandledRejection', (error) => { process.on('unhandledRejection', (error) => {
// This happens when attempting to initialize without any available Swarm addresses (e.g. Rendezvous) // 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())); const web3 = new Web3(new Web3.providers.WebsocketProvider(getWeb3ProviderUrl()));
getDeployedContract(web3) getDeployedContract(web3)
.then(({ contract, contractAddress }) => IPFS.create(ipfsOptions) .then(({ contract, contractAddress }) => getIpfsOptions()
.then((ipfsOptions) => IPFS.create(ipfsOptions))
.then((ipfs) => createOrbitInstance(ipfs, contractAddress)) .then((ipfs) => createOrbitInstance(ipfs, contractAddress))
.then((orbit) => openExistingUsersDatabases(contract, orbit) .then((orbit) => openExistingUsersDatabases(contract, orbit)
.then(() => { .then(() => {

13
packages/concordia-pinner/src/options/ipfsOptions.js

@ -1,7 +1,8 @@
import libp2pBundle from './libp2pBundle'; import getLibp2pBundle from './libp2pBundle';
import { swarmAddresses } from '../constants'; import { getSwarmAddresses } from '../utils/ipfsUtils';
export default { const getIpfsOptions = async () => getSwarmAddresses()
.then((swarmAddresses) => ({
repo: 'ipfs', repo: 'ipfs',
config: { config: {
Profile: 'server', Profile: 'server',
@ -9,7 +10,7 @@ export default {
Swarm: swarmAddresses, Swarm: swarmAddresses,
}, },
}, },
libp2p: libp2pBundle, libp2p: getLibp2pBundle(swarmAddresses),
EXPERIMENTAL: { EXPERIMENTAL: {
pubsub: true, pubsub: true,
}, },
@ -19,4 +20,6 @@ export default {
init: { init: {
emptyRepo: true, emptyRepo: true,
}, },
}; }));
export default getIpfsOptions;

5
packages/concordia-pinner/src/options/libp2pBundle.js

@ -7,10 +7,9 @@ import Gossipsub from 'libp2p-gossipsub';
import KadDHT from 'libp2p-kad-dht'; import KadDHT from 'libp2p-kad-dht';
import MPLEX from 'libp2p-mplex'; import MPLEX from 'libp2p-mplex';
import { NOISE } from 'libp2p-noise'; import { NOISE } from 'libp2p-noise';
import { swarmAddresses } from '../constants';
// See also: https://github.com/libp2p/js-libp2p/blob/master/doc/CONFIGURATION.md // 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, peerId: opts.peerId,
addresses: { addresses: {
listen: swarmAddresses, listen: swarmAddresses,
@ -88,3 +87,5 @@ export default (opts) => new Libp2p({
maxOldPeersRetention: 50, maxOldPeersRetention: 50,
}, },
}); });
export default getLibp2pBundle;

11
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`]);

2
packages/concordia-pinner/src/utils/orbitUtils.js

@ -2,7 +2,7 @@ import OrbitDB from 'orbit-db';
import Identities from 'orbit-db-identity-provider'; import Identities from 'orbit-db-identity-provider';
import { EthereumContractIdentityProvider } from '@ezerous/eth-identity-provider'; import { EthereumContractIdentityProvider } from '@ezerous/eth-identity-provider';
import Web3 from 'web3'; import Web3 from 'web3';
import { ORBIT_DIRECTORY_DEFAULT } from '../constants'; import ORBIT_DIRECTORY_DEFAULT from '../constants';
// TODO: share code below with frontend (?) // TODO: share code below with frontend (?)
const determineDBAddress = async ({ const determineDBAddress = async ({

Loading…
Cancel
Save