From bca0a8dddb55f65e40ac1459a8abbb257bc72542 Mon Sep 17 00:00:00 2001 From: brusher_ru Date: Tue, 11 Apr 2023 16:32:21 -0600 Subject: [PATCH] fix: use `~/post/genID` for default location of key.bin --- app/redux/smesher/selectors.ts | 6 ++--- desktop/NodeManager.ts | 4 +++- desktop/SmesherManager.ts | 41 +++++++++++++++++++++------------- desktop/main/NodeConfig.ts | 3 +-- desktop/main/smeshingOpts.ts | 38 ++++++++++++++++++++++++++++--- desktop/storeService.ts | 4 ++-- shared/types/node.ts | 8 +++---- 7 files changed, 73 insertions(+), 31 deletions(-) diff --git a/app/redux/smesher/selectors.ts b/app/redux/smesher/selectors.ts index 5d1c6173b..21d46d895 100644 --- a/app/redux/smesher/selectors.ts +++ b/app/redux/smesher/selectors.ts @@ -26,11 +26,11 @@ export const isErrorState = (state: RootState) => getPostSetupState(state) === PostSetupState.STATE_ERROR; export const isSmeshingPaused = (state: RootState) => { - const isPausedState = - getPostSetupState(state) === PostSetupState.STATE_PAUSED; + const posState = getPostSetupState(state); const opts = getSmeshingOpts(state); return ( - (isPausedState || !state.smesher.isSmeshingStarted) && + (posState === PostSetupState.STATE_PAUSED || + posState === PostSetupState.STATE_NOT_STARTED) && isValidSmeshingOpts(opts) ); }; diff --git a/desktop/NodeManager.ts b/desktop/NodeManager.ts index 493600a45..a1dd8fb45 100644 --- a/desktop/NodeManager.ts +++ b/desktop/NodeManager.ts @@ -324,7 +324,9 @@ class NodeManager extends AbstractManager { } // Temporary solution of https://github.com/spacemeshos/smapp/issues/823 - const CURRENT_DATADIR_PATH = await this.smesherManager.getCurrentDataDir(); + const CURRENT_DATADIR_PATH = await this.smesherManager.getCurrentDataDir( + this.genesisID + ); const CURRENT_KEYBIN_PATH = path.resolve(CURRENT_DATADIR_PATH, 'key.bin'); const NEXT_KEYBIN_PATH = path.resolve(postSetupOpts.dataDir, 'key.bin'); diff --git a/desktop/SmesherManager.ts b/desktop/SmesherManager.ts index 1e43edb67..e4432ce8c 100644 --- a/desktop/SmesherManager.ts +++ b/desktop/SmesherManager.ts @@ -18,6 +18,7 @@ import { readFileAsync, writeFileAsync } from './utils'; import AbstractManager from './AbstractManager'; import StoreService from './storeService'; import { generateGenesisIDFromConfig } from './main/Networks'; +import { safeSmeshingOpts, ValidSmeshingOpts } from './main/smeshingOpts'; const checkDiskSpace = require('check-disk-space'); @@ -69,11 +70,12 @@ class SmesherManager extends AbstractManager { return res.smesherId; }; - getCurrentDataDir = async () => { + getCurrentDataDir = async (genesisID: HexString) => { const smeshingConfig = await this.getSmeshingConfig(); const DEFAULT_KEYBIN_PATH = path.resolve( app.getPath('home'), - './post/data' + './post/', + genesisID.substring(0, 8) ); return R.pathOr( DEFAULT_KEYBIN_PATH, @@ -217,15 +219,19 @@ class SmesherManager extends AbstractManager { deleteFiles: deleteFiles || false, }); const config = await this.loadConfig(); + const genesisId = generateGenesisIDFromConfig(config); if (deleteFiles) { config.smeshing = {}; } else { - config.smeshing = config.smeshing || {}; - config.smeshing['smeshing-start'] = false; + config.smeshing = { + ...config.smeshing, + 'smeshing-start': false, + }; } + const smeshingOpts = safeSmeshingOpts(config.smeshing, genesisId); + config.smeshing = smeshingOpts; await this.writeConfig(config); - const genesisId = generateGenesisIDFromConfig(config); - StoreService.remove(`smeshing.${genesisId}`); + StoreService.set(`smeshing.${genesisId}`, smeshingOpts); return res?.error; } ); @@ -266,17 +272,20 @@ class SmesherManager extends AbstractManager { throttle, maxFileSize, } = postSetupOpts; - const opts = { - 'smeshing-coinbase': coinbase, - 'smeshing-opts': { - 'smeshing-opts-datadir': dataDir, - 'smeshing-opts-maxfilesize': maxFileSize, - 'smeshing-opts-numunits': numUnits, - 'smeshing-opts-provider': computeProviderId, - 'smeshing-opts-throttle': throttle, + const opts = safeSmeshingOpts( + { + 'smeshing-coinbase': coinbase, + 'smeshing-opts': { + 'smeshing-opts-datadir': dataDir, + 'smeshing-opts-maxfilesize': maxFileSize, + 'smeshing-opts-numunits': numUnits, + 'smeshing-opts-provider': computeProviderId, + 'smeshing-opts-throttle': throttle, + }, + 'smeshing-start': true, }, - 'smeshing-start': true, - }; + genesisID + ); StoreService.set(`smeshing.${genesisID}`, opts); const config = await this.loadConfig(); diff --git a/desktop/main/NodeConfig.ts b/desktop/main/NodeConfig.ts index b9462533f..78864c4c6 100644 --- a/desktop/main/NodeConfig.ts +++ b/desktop/main/NodeConfig.ts @@ -23,8 +23,7 @@ export const loadNodeConfig = async (): Promise => const loadSmeshingOpts = async (nodeConfig) => { const id = generateGenesisIDFromConfig(nodeConfig); const opts = StoreService.get(`smeshing.${id}`); - // const opts = (await loadNodeConfig()).smeshing; - return safeSmeshingOpts(opts); + return safeSmeshingOpts(opts, id); }; export const downloadNodeConfig = async (networkConfigUrl: string) => { diff --git a/desktop/main/smeshingOpts.ts b/desktop/main/smeshingOpts.ts index e61df30c1..a3503060c 100644 --- a/desktop/main/smeshingOpts.ts +++ b/desktop/main/smeshingOpts.ts @@ -1,6 +1,15 @@ +import { homedir } from 'os'; +import { resolve } from 'path'; import Bech32 from '@spacemesh/address-wasm'; +import { HexString } from '../../shared/types'; import { fromHexString } from '../../shared/utils'; +export type NoSmeshingDefaults = { + 'smeshing-opts': { + 'smeshing-opts-datadir': string; + }; +}; + export type SmeshingOpts = { 'smeshing-coinbase': string; 'smeshing-opts': { @@ -13,6 +22,11 @@ export type SmeshingOpts = { 'smeshing-start': boolean; }; +export type ValidSmeshingOpts = + | NoSmeshingDefaults + | SmeshingOpts + | Partial; + export const isSmeshingOpts = (a: any): a is SmeshingOpts => a && typeof a['smeshing-coinbase'] === 'string' && @@ -28,8 +42,21 @@ export const isSmeshingOpts = (a: any): a is SmeshingOpts => a['smeshing-opts']['smeshing-opts-numunits'] >= 1 && a['smeshing-opts']['smeshing-opts-provider'] >= 0; -export const safeSmeshingOpts = (opts: SmeshingOpts) => { - if (!isSmeshingOpts(opts)) return {}; +export const safeSmeshingOpts = ( + opts: any, + genesisId: HexString +): ValidSmeshingOpts => { + const defaultPosDir = resolve( + homedir(), + `./post/${genesisId.substring(0, 8)}` + ); + const defaultSmeshingOpts = { + 'smeshing-opts': { + 'smeshing-opts-datadir': defaultPosDir, + }, + }; + + if (!isSmeshingOpts(opts)) return defaultSmeshingOpts; const oCoinbase = opts['smeshing-coinbase']; const coinbase = @@ -39,9 +66,14 @@ export const safeSmeshingOpts = (opts: SmeshingOpts) => { if (Bech32.verify(coinbase)) { return { ...opts, + 'smeshing-opts': { + ...opts['smeshing-opts'], + 'smeshing-opts-datadir': + opts['smeshing-opts']['smeshing-opts-datadir'] || defaultPosDir, + }, 'smeshing-coinbase': coinbase, }; } else { - return {}; + return defaultSmeshingOpts; } }; diff --git a/desktop/storeService.ts b/desktop/storeService.ts index 80c0b066b..170caa763 100644 --- a/desktop/storeService.ts +++ b/desktop/storeService.ts @@ -5,7 +5,7 @@ import { AutoPath } from 'ts-toolbelt/out/Function/AutoPath'; import { Split } from 'ts-toolbelt/out/String/Split'; import { HexString } from '../shared/types'; import { USERDATA_DIR } from './main/constants'; -import { SmeshingOpts } from './main/smeshingOpts'; +import { ValidSmeshingOpts } from './main/smeshingOpts'; export interface ConfigStore { isAutoStartEnabled: boolean; @@ -14,7 +14,7 @@ export interface ConfigStore { dataPath: string; port: string; }; - smeshing: Record; + smeshing: Record; walletFiles: string[]; } diff --git a/shared/types/node.ts b/shared/types/node.ts index 3c9db64b8..6e0eabf76 100644 --- a/shared/types/node.ts +++ b/shared/types/node.ts @@ -58,10 +58,10 @@ export interface NodeConfig { 'smeshing-start'?: boolean; 'smeshing-opts'?: { 'smeshing-opts-datadir': string; - 'smeshing-opts-maxfilesize': number; - 'smeshing-opts-numunits': number; - 'smeshing-opts-provider': number; - 'smeshing-opts-throttle': boolean; + 'smeshing-opts-maxfilesize'?: number; + 'smeshing-opts-numunits'?: number; + 'smeshing-opts-provider'?: number; + 'smeshing-opts-throttle'?: boolean; }; }; main: {