Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default post-dir: add genesis id into default post-dir path (required as a default location of key.bin) #1216

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/redux/smesher/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
};
Expand Down
4 changes: 3 additions & 1 deletion desktop/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
45 changes: 29 additions & 16 deletions desktop/SmesherManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { readFileAsync, writeFileAsync } from './utils';
import AbstractManager from './AbstractManager';
import StoreService from './storeService';
import { generateGenesisIDFromConfig } from './main/Networks';
import { safeSmeshingOpts } from './main/smeshingOpts';

const checkDiskSpace = require('check-disk-space');

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -340,6 +349,10 @@ class SmesherManager extends AbstractManager {
error: any,
status: Partial<PostSetupStatus>
) => {
if (error) {
logger.error('handlePostDataCreationStatusStream', error);
return;
}
this.mainWindow.webContents.send(
ipcConsts.SMESHER_POST_DATA_CREATION_PROGRESS,
{ error, status }
Expand Down
3 changes: 1 addition & 2 deletions desktop/main/NodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export const loadNodeConfig = async (): Promise<NodeConfig> =>
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) => {
Expand Down
6 changes: 3 additions & 3 deletions desktop/main/reactions/handleSmesherIpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { from, Observable, Subject, switchMap, withLatestFrom } from 'rxjs';
import { from, Subject, switchMap, withLatestFrom } from 'rxjs';
import { ipcConsts } from '../../../app/vars';
import { PostSetupOpts } from '../../../shared/types';
import { SmeshingSetupState } from '../../NodeManager';
Expand All @@ -13,7 +13,7 @@ const startSmeshing = (managers: Managers, opts: PostSetupOpts) =>

export default (
$managers: Subject<Managers>,
$smeshingStarted: Observable<SmeshingSetupState>
$smeshingSetupState: Subject<SmeshingSetupState>
) => {
const startSmeshingRequest = fromIPC<PostSetupOpts>(
ipcConsts.SMESHER_START_SMESHING
Expand All @@ -28,7 +28,7 @@ export default (
} else if (!res) {
logger.error('NodeManager.startSmeshing not started', err, res);
} else {
$smeshingStarted.next(res);
$smeshingSetupState.next(res);
}
});

Expand Down
38 changes: 35 additions & 3 deletions desktop/main/smeshingOpts.ts
Original file line number Diff line number Diff line change
@@ -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': {
Expand All @@ -13,6 +22,11 @@ export type SmeshingOpts = {
'smeshing-start': boolean;
};

export type ValidSmeshingOpts =
| NoSmeshingDefaults
| SmeshingOpts
| Partial<SmeshingOpts>;

export const isSmeshingOpts = (a: any): a is SmeshingOpts =>
a &&
typeof a['smeshing-coinbase'] === 'string' &&
Expand All @@ -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 =
Expand All @@ -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;
}
};
2 changes: 1 addition & 1 deletion desktop/main/startApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const startApp = (): AppStore => {
$warnings
),
// Handle Start Smeshing request
handleSmesherIpc($managers, $smeshingStarted),
handleSmesherIpc($managers, $smeshingSetupState),
// Handle show file
handleShowFile($currentNetwork),
// IPC Reactions
Expand Down
4 changes: 2 additions & 2 deletions desktop/storeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +14,7 @@ export interface ConfigStore {
dataPath: string;
port: string;
};
smeshing: Record<HexString, SmeshingOpts>;
smeshing: Record<HexString, ValidSmeshingOpts>;
walletFiles: string[];
}

Expand Down
8 changes: 4 additions & 4 deletions shared/types/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down