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: use genesisID in custom config name #1339

Merged
merged 1 commit into from
Jul 12, 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
15 changes: 4 additions & 11 deletions desktop/SmesherManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ class SmesherManager extends AbstractManager {

private genesisID: string;

private netName: string;

constructor(mainWindow: BrowserWindow, genesisID: string, netName: string) {
constructor(mainWindow: BrowserWindow, genesisID: string) {
super(mainWindow);
this.smesherService = new SmesherService();
this.smesherService.createService();
this.genesisID = genesisID;
this.netName = netName;
}

unsubscribe = () => {
Expand Down Expand Up @@ -85,10 +82,6 @@ class SmesherManager extends AbstractManager {
this.genesisID = id;
};

setNetName = (netName: string) => {
this.netName = netName;
};

updateSmesherState = async () => {
await this.sendSmesherSettingsAndStartupState();
await this.sendPostSetupProviders();
Expand Down Expand Up @@ -227,7 +220,7 @@ class SmesherManager extends AbstractManager {
await this.clearSmesherMetadata();

await updateSmeshingOpts(
this.netName,
this.genesisID,
deleteFiles ? {} : { 'smeshing-start': false }
);

Expand Down Expand Up @@ -270,7 +263,7 @@ class SmesherManager extends AbstractManager {
maxFileSize,
} = postSetupOpts;
const { nonces, threads } = provingOpts;
const customNodeConfig = await loadCustomNodeConfig(this.netName);
const customNodeConfig = await loadCustomNodeConfig(this.genesisID);
const opts = safeSmeshingOpts(
{
'smeshing-coinbase': coinbase,
Expand All @@ -295,7 +288,7 @@ class SmesherManager extends AbstractManager {
genesisID
);

await updateSmeshingOpts(this.netName, opts);
await updateSmeshingOpts(this.genesisID, opts);

return true;
};
Expand Down
34 changes: 14 additions & 20 deletions desktop/main/NodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export const writeNodeConfig = async (config: NodeConfig): Promise<void> => {
}
};

const getCustomNodeConfigName = (name: string) =>
`node-config.${name.toLowerCase().replace(' ', '_')}.json`;
const getCustomNodeConfigName = (genesisID: string) =>
`node-config.${genesisID.substring(0, 8)}.json`;

export const loadCustomNodeConfig = async (
netName: string
genesisID: string
): Promise<Partial<NodeConfig>> => {
const customConfigPath = path.join(
USERDATA_DIR,
getCustomNodeConfigName(netName)
getCustomNodeConfigName(genesisID)
);

return existsSync(customConfigPath)
Expand All @@ -62,10 +62,10 @@ export const loadCustomNodeConfig = async (
};

export const writeCustomNodeConfig = async (
netName: string,
genesisID: string,
config: Partial<NodeConfig>
): Promise<void> => {
const filePath = path.join(USERDATA_DIR, getCustomNodeConfigName(netName));
const filePath = path.join(USERDATA_DIR, getCustomNodeConfigName(genesisID));
try {
await fs.writeFile(filePath, JSON.stringify(config, null, 2), {
encoding: 'utf8',
Expand All @@ -83,20 +83,19 @@ export const writeCustomNodeConfig = async (
};

export const saveSmeshingOptsInCustomConfig = async (
netName: string,
genesisID: string,
opts: Partial<NodeConfig['smeshing']>
): Promise<Partial<NodeConfig>> => {
const customConfig = await loadCustomNodeConfig(netName);
const customConfig = await loadCustomNodeConfig(genesisID);

customConfig.smeshing = opts;

await writeCustomNodeConfig(netName, customConfig);
await writeCustomNodeConfig(genesisID, customConfig);

return customConfig;
};

const createCustomNodeConfig = async (
netName: string,
genesisID: string
): Promise<Partial<NodeConfig>> => {
const opts: Partial<NodeConfig['smeshing']> | undefined = StoreService.get(
Expand All @@ -106,19 +105,18 @@ const createCustomNodeConfig = async (
// migrate options from StoreService or place only default opts
const smeshingOpts = opts || safeSmeshingOpts(undefined, genesisID); // set default dir path

const config = await saveSmeshingOptsInCustomConfig(netName, smeshingOpts);
const config = await saveSmeshingOptsInCustomConfig(genesisID, smeshingOpts);

StoreService.remove(`smeshing.${genesisID}`);

return config;
};
export const loadOrCreateCustomConfig = async (
netName: string,
genesisID: string
): Promise<Partial<NodeConfig>> =>
existsSync(path.join(USERDATA_DIR, getCustomNodeConfigName(netName)))
? loadCustomNodeConfig(netName)
: createCustomNodeConfig(netName, genesisID);
existsSync(path.join(USERDATA_DIR, getCustomNodeConfigName(genesisID)))
? loadCustomNodeConfig(genesisID)
: createCustomNodeConfig(genesisID);

export const updateSmeshingOpts = async (
netName: string,
Expand All @@ -145,13 +143,9 @@ export const updateSmeshingOpts = async (
return mergedConfig;
};

export const downloadNodeConfig = async (
netName: string,
networkConfigUrl: string
) => {
export const downloadNodeConfig = async (networkConfigUrl: string) => {
const discoveryConfig = await fetchNodeConfig(networkConfigUrl);
const customNodeConfig = await loadOrCreateCustomConfig(
netName,
generateGenesisIDFromConfig(discoveryConfig)
);
const mergedConfig = R.mergeLeft(customNodeConfig, discoveryConfig);
Expand Down
24 changes: 7 additions & 17 deletions desktop/main/reactions/spawnManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { BrowserWindow } from 'electron';
import {
distinctUntilChanged,
from,
Observable,
ReplaySubject,
Subject,
switchMap,
withLatestFrom,
} from 'rxjs';
import { Network, NodeConfig } from '../../../shared/types';
import { NodeConfig } from '../../../shared/types';
import { Managers } from '../app.types';
import { generateGenesisIDFromConfig } from '../Networks';
import SmesherManager from '../../SmesherManager';
Expand All @@ -19,22 +18,20 @@ let managers: Managers | null = null;

const spawnManagers = async (
mainWindow: BrowserWindow,
genesisID: string,
netName: string
genesisID: string
): Promise<Managers> => {
if (!mainWindow)
throw new Error('Cannot spawn managers: MainWindow not found');

// init managers
if (!managers) {
const smesher = new SmesherManager(mainWindow, genesisID, netName);
const smesher = new SmesherManager(mainWindow, genesisID);
const node = new NodeManager(mainWindow, genesisID, smesher);
const wallet = new WalletManager(mainWindow, node);

managers = { smesher, node, wallet };
} else {
// update GenesisID and netName for instance
managers.smesher.setNetName(netName);
managers.smesher.setGenesisID(genesisID);
managers.node.setGenesisID(genesisID);

Expand All @@ -50,21 +47,14 @@ const spawnManagers = async (
const spawnManagers$ = (
$nodeConfig: Subject<NodeConfig>,
$managers: Subject<Managers>,
$mainWindow: ReplaySubject<BrowserWindow>,
$currentNetwork: Observable<Network | null>
$mainWindow: ReplaySubject<BrowserWindow>
) => {
const sub = $nodeConfig
.pipe(
distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)),
withLatestFrom($mainWindow, $currentNetwork),
switchMap(([nodeConfig, mainWindow, currentNetwork]) =>
from(
spawnManagers(
mainWindow,
generateGenesisIDFromConfig(nodeConfig),
currentNetwork?.netName || ''
)
)
withLatestFrom($mainWindow),
switchMap(([nodeConfig, mainWindow]) =>
from(spawnManagers(mainWindow, generateGenesisIDFromConfig(nodeConfig)))
)
)
.subscribe((newManagers) => {
Expand Down
2 changes: 1 addition & 1 deletion desktop/main/reactions/syncNodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default (
filter(Boolean)
)
).pipe(
switchMap((net) => from(downloadNodeConfig(net.netName, net.conf))),
switchMap((net) => from(downloadNodeConfig(net.conf))),
retry(5),
delay(500),
catchError((err: any) => {
Expand Down
2 changes: 1 addition & 1 deletion desktop/main/startApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const startApp = (): AppStore => {
// List of unsubscribe functions
const unsubs = [
// Spawn managers (and handle unsubscribing)
spawnManagers($nodeConfig, $managers, $mainWindow, $currentNetwork),
spawnManagers($nodeConfig, $managers, $mainWindow),
// On changing network -> update node config
syncNodeConfig(
$currentNetwork,
Expand Down
Loading
Loading