From 4d2a632b3e3c6b8438b58927bd6705ef77c83f27 Mon Sep 17 00:00:00 2001 From: vaultec <47548474+vaultec81@users.noreply.github.com> Date: Sat, 3 Jun 2023 23:04:44 -0700 Subject: [PATCH] feat: ipfs startup improvements + more --- src/common/constants.ts | 2 +- src/components/DHTProviders.tsx | 2 +- src/main/core/components/ipfsHandler.ts | 25 ++++++++++++---- src/main/core/index.ts | 40 ++++++++++++++++++++----- src/main/index.ts | 2 ++ src/renderer/views/UploaderView.tsx | 2 +- src/renderer/views/WatchView.tsx | 8 ++--- 7 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/common/constants.ts b/src/common/constants.ts index 724e468..112ad95 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -1,2 +1,2 @@ export const IPFS_SELF_MULTIADDR = '/ip4/127.0.0.1/tcp/5004' -export const IPFS_HOST = '127.0.0.1' +export const IPFS_HOST = 'http://127.0.0.1:5004' diff --git a/src/components/DHTProviders.tsx b/src/components/DHTProviders.tsx index 5f07309..9aef94f 100644 --- a/src/components/DHTProviders.tsx +++ b/src/components/DHTProviders.tsx @@ -5,7 +5,7 @@ import { IPFS_HOST } from '../common/constants'; let ipfsClient; try { - ipfsClient = IPFSHTTPClient.create({ host: IPFS_HOST }); + ipfsClient = IPFSHTTPClient.create({ url: IPFS_HOST }); } catch (error) { console.error(`Error creating IPFS client in watch.tsx: `, error); throw error; diff --git a/src/main/core/components/ipfsHandler.ts b/src/main/core/components/ipfsHandler.ts index 5a7f8c6..ab71b16 100644 --- a/src/main/core/components/ipfsHandler.ts +++ b/src/main/core/components/ipfsHandler.ts @@ -9,6 +9,11 @@ import { IPFS_HOST, IPFS_SELF_MULTIADDR } from '../../../common/constants' const waIpfs = require('wa-go-ipfs') const toUri = require('multiaddr-to-uri') +const IPFSPORTS = [ + '5001', + '5004' +] + const defaultIpfsConfig = { API: { HTTPHeaders: { @@ -56,6 +61,7 @@ const defaultIpfsConfig = { } export class IpfsHandler { static events: any + static isReady: boolean static get ready() { return new Promise(async (resolve, reject) => { let ipfsInfo = await IpfsHandler.getIpfs() @@ -70,6 +76,9 @@ export class IpfsHandler { }) } static async start(appPath) { + IpfsHandler.events.once('ready', async () => { + this.isReady = true + }) console.log(`4`) let ipfsInfo = await IpfsHandler.getIpfs() if (!ipfsInfo.exists) { @@ -83,7 +92,7 @@ export class IpfsHandler { IpfsHandler.events.emit('ready') } else { console.log(`6`) - if (ipfsInfo.ipfs) { + if (ipfsInfo.isRunning) { IpfsHandler.events.emit('ready') } else { fs.writeFileSync( @@ -107,6 +116,7 @@ export class IpfsHandler { const goIpfsPath = await waIpfs.getPath( waIpfs.getDefaultPath({ dev: process.env.NODE_ENV === 'development' }), ) + console.log('repoPath', {repoPath, goIpfsPath}) await execa(goIpfsPath, ['init'], { env: { IPFS_Path: repoPath, @@ -156,13 +166,12 @@ export class IpfsHandler { }) } static async getIpfs() { - const AppPath = Path.join(os.homedir(), '.blasio-app') let ipfsPath: string if (process.env.IPFS_Path) { ipfsPath = process.env.IPFS_Path } else { - ipfsPath = Path.join(os.homedir(), '.ipfs-3speak') + ipfsPath = Path.join(os.homedir(), '.ipfs-3speak2') } let exists @@ -188,20 +197,23 @@ export class IpfsHandler { }*/ } + let isRunning = false; if (apiAddr) { - ipfs = IPFSHTTPClient.create({ host: IPFS_HOST }) + ipfs = IPFSHTTPClient.create({ url: IPFS_HOST }) try { await ipfs.config.get('Addresses') + isRunning = true } catch (ex) { console.error(`Error getting IPFS address config`) console.error(ex) - ipfs = null + // ipfs = null } } else { console.error(`***** API addr is null!!******`) } - if (ipfs !== null && ipfs) { + if (ipfs !== null && isRunning) { + const gma = await ipfs.config.get('Addresses.Gateway') gateway = toUri(gma) + '/ipfs/' } else { @@ -210,6 +222,7 @@ export class IpfsHandler { return { isLocked, + isRunning, exists, ipfsPath, ipfs, diff --git a/src/main/core/index.ts b/src/main/core/index.ts index 86092b5..da13b80 100644 --- a/src/main/core/index.ts +++ b/src/main/core/index.ts @@ -1,5 +1,6 @@ import { IPFSHTTPClient } from 'ipfs-http-client' import winston from 'winston' +import execa from 'execa' import Components from './components' import DistillerDB from './components/DistillerDB' import { EncoderService } from './components/EncoderService' @@ -37,14 +38,19 @@ export class CoreService { async install() { this.start_progress.message = 'Installing IPFS' await waIpfs.install({ - version: 'v0.19.0', + version: 'v0.19.2', dev: process.env.NODE_ENV === 'development', recursive: true, }) await new Promise((resolve) => { setTimeout(async () => { + const ipfsInfo = await IpfsHandler.getIpfs() this.start_progress.message = 'Initializing IPFS' - await IpfsHandler.init(undefined) + try { + await IpfsHandler.init(ipfsInfo.ipfsPath) + } catch { + + } this.start_progress.message = null resolve() }, 5000) @@ -87,8 +93,19 @@ export class CoreService { fs.chmodSync(ipfsPath, 755) } } + const output = await execa(ipfsPath, ['version', '-n']) + console.log(output) + if(output.stdout !== "0.16.2") { + console.log('Ipfs not up to date') + throw new Error('Ipfs not up to date') + } } catch { - await this.install() + console.log('installing ipfs update') + try { + await this.install() + } catch(ex) { + console.log(ex) + } } this.config = new Components.Config(this._options.path) @@ -119,12 +136,19 @@ export class CoreService { this.events.emit('ready') this.start_progress.ready = true this.start_progress.message = null + + console.log('starting message LOOP') setInterval(async () => { - const peerIds = (await this.ipfs.config.get('Bootstrap')) as any - for (const peerId of peerIds) { - try { - await this.ipfs.swarm.connect(peerId) - } catch {} + console.log(IpfsHandler.isReady) + try { + const peerIds = (await this.ipfs.config.get('Bootstrap')) as any + for (const peerId of peerIds) { + try { + await this.ipfs.swarm.connect(peerId) + } catch {} + } + } catch { + } }, 60000) } diff --git a/src/main/index.ts b/src/main/index.ts index f35854a..6896ff8 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -101,6 +101,7 @@ async function startup(): Promise { void startup() process.on('unhandledRejection', (err: Error) => { + console.log(err) core.logger.error(`Unhandled rejection!`) core.logger.error(err.message) core.logger.error(err.stack) @@ -109,6 +110,7 @@ process.on('unhandledRejection', (err: Error) => { }) process.on('uncaughtException', (err: Error) => { + console.log('err', err) core.logger.error(`Uncaught exception!`) core.logger.error(err.message) core.logger.error(err.stack) diff --git a/src/renderer/views/UploaderView.tsx b/src/renderer/views/UploaderView.tsx index 9309a17..15fec4e 100644 --- a/src/renderer/views/UploaderView.tsx +++ b/src/renderer/views/UploaderView.tsx @@ -50,7 +50,7 @@ export function UploaderView() { const [blockedGlobalMessage, setBlockedGlobalMessage] = useState('') useEffect(() => { - ipfs.current = IPFSHTTPClient.create({ host: IPFS_HOST }) + ipfs.current = IPFSHTTPClient.create({ url: IPFS_HOST }) }, []) const handlePublish = async () => { diff --git a/src/renderer/views/WatchView.tsx b/src/renderer/views/WatchView.tsx index f44c864..ddb7bbd 100644 --- a/src/renderer/views/WatchView.tsx +++ b/src/renderer/views/WatchView.tsx @@ -22,12 +22,8 @@ import { PinLocally } from './WatchView/watchViewHelpers/PinLocally'; import { showDebug } from './WatchView/watchViewHelpers/showDebug'; const Finder = ArraySearch.Finder -let ipfsClient -try { - ipfsClient = IPFSHTTPClient.create({ host: IPFS_HOST }) -} catch (error) { - throw error -} +let ipfsClient = IPFSHTTPClient.create({ url: IPFS_HOST }) + export function WatchView(props: any) { const player = useRef()