Skip to content

Commit

Permalink
Merge pull request #1751 from spacemeshos/feat-1737-export-net-info
Browse files Browse the repository at this point in the history
Export network info from Settings screen
  • Loading branch information
brusherru authored Jun 25, 2024
2 parents 52c4ffa + cc03221 commit 41c4fe2
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 11 deletions.
2 changes: 2 additions & 0 deletions app/infra/eventsService/eventsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ class EventsService {

/** ************************************** MISC ***************************************** */

static exportNetInfo = () => ipcRenderer.invoke(ipcConsts.EXPORT_NET_INFO);

static print = ({ content }: { content: string }) =>
ipcRenderer.send(ipcConsts.PRINT, { content });

Expand Down
11 changes: 11 additions & 0 deletions app/screens/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,17 @@ class Settings extends Component<Props, State> {
))}
</SettingsSection>
<SettingsSection title={categories[3]} name={categories[3]}>
<SettingRow
upperPartLeft="Export network information"
upperPartRight={
<Button
onClick={eventsService.exportNetInfo}
text="EXPORT INFO"
width={180}
/>
}
rowName=""
/>
<SettingRow
upperPartLeft={getFormattedTimestamp(genesisTime)}
isUpperPartLeftText
Expand Down
1 change: 1 addition & 0 deletions app/vars/ipcConsts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ enum IpcChannel {
REQUEST_QUICKSYNC_CHECK = 'REQUEST_QUICKSYNC_CHECK',
UPDATE_QUICKSYNC_STATUS = 'UPDATE_QUICKSYNC_STATUS',
UPDATE_ATX_COUNT = 'UPDATE_ATX_COUNT',
EXPORT_NET_INFO = 'EXPORT_NET_INFO',
}

export default IpcChannel;
18 changes: 18 additions & 0 deletions desktop/AdminService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProtoGrpcType } from '../api/generated';
import { Event } from '../api/generated/spacemesh/v1/Event';

import { PeerInfo__Output } from '../api/generated/spacemesh/v1/PeerInfo';
import Logger from './logger';
import NetServiceFactory from './NetServiceFactory';
import { getPrivateNodeConnectionConfig } from './main/utils';
Expand Down Expand Up @@ -37,6 +38,23 @@ class AdminService extends NetServiceFactory<
);
return this.cancelEventsStream;
};

getPeerInfo = () =>
new Promise((resolve, reject) => {
if (!this.service) {
reject(new Error('Service is not started yet'));
return;
}

const result: PeerInfo__Output[] = [];
const stream = this.service?.PeerInfoStream({});
if (!stream) {
resolve([]);
return;
}
stream.on('data', (d) => result.push(d));
stream.on('close', () => resolve(result));
});
}

export default AdminService;
28 changes: 28 additions & 0 deletions desktop/DebugService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ProtoGrpcType } from '../api/generated';

import Logger from './logger';
import NetServiceFactory from './NetServiceFactory';
import { getPrivateNodeConnectionConfig } from './main/utils';

const PROTO_PATH = 'vendor/api/spacemesh/v1/debug.proto';

class DebugService extends NetServiceFactory<
ProtoGrpcType,
'v1',
'DebugService'
> {
logger = Logger({ className: 'DebugService' });

createService = () => {
this.createNetService(
PROTO_PATH,
getPrivateNodeConnectionConfig(),
'v1',
'DebugService'
);
};

getNetworkInfo = () => this.callService('NetworkInfo', {});
}

export default DebugService;
63 changes: 63 additions & 0 deletions desktop/main/reactions/handleExportInfoIPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { writeFile } from 'fs/promises';
import { combineLatest, from, map, mergeMap, Observable } from 'rxjs';
import { BrowserWindow, dialog } from 'electron';
import { handleIPC, handlerResult, makeSubscription } from '../rx.utils';
import { ipcConsts } from '../../../app/vars';
import AdminService from '../../AdminService';
import DebugService from '../../DebugService';
import Logger from '../../logger';

const logger = Logger({ className: 'handleExportInfoIPC' });

const saveDialog = async (
browser: BrowserWindow,
data: Record<string, any>
) => {
const { canceled, filePath } = await dialog.showSaveDialog(browser, {
title: 'Export network info',
buttonLabel: 'Export',
defaultPath: 'netInfo.json',
});
if (canceled || !filePath) {
return { filePath: null, data, canceled };
}
return { filePath, data, canceled };
};

const handleExportInfoIPC = ($mainWindow: Observable<BrowserWindow>) =>
makeSubscription(
handleIPC(
ipcConsts.EXPORT_NET_INFO,
() => {
const adminService = new AdminService();
const debugService = new DebugService();
debugService.createService();
adminService.createService();

const netInfo = from(debugService.getNetworkInfo());
const peerInfo = from(adminService.getPeerInfo());

return combineLatest([netInfo, peerInfo, $mainWindow]).pipe(
mergeMap(([netInfo, peerInfo, browser]) =>
from(saveDialog(browser, { netInfo, peerInfo }))
),
map(handlerResult)
);
},
(data) => data
),
({ filePath, data }) => {
if (filePath) {
writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8')
// eslint-disable-next-line promise/always-return
.then(() => {
logger.log('Network data exported', { filePath, data });
})
.catch((err) =>
logger.error('Cannot write file', err, { filePath, data })
);
}
}
);

export default handleExportInfoIPC;
2 changes: 2 additions & 0 deletions desktop/main/startApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import syncAutoStartAndConfig from './reactions/syncAutoStartAndConfig';
import restartNode from './reactions/restartNode';
import { updateConfigHash } from './configHash';
import { ensureConfigCacheDir } from './fallbackConfigs';
import handleExportInfoIPC from './reactions/handleExportInfoIPC';

const positiveNum = (def: number, n: number) => (n > 0 ? n : def);

Expand Down Expand Up @@ -271,6 +272,7 @@ const startApp = (): AppStore => {
listenNodeConfigAndRestartNode($nodeConfig, $managers),
handleBenchmarksIpc($mainWindow, $nodeConfig),
syncAutoStartAndConfig($warnings),
handleExportInfoIPC($mainWindow),
];

return {
Expand Down
15 changes: 4 additions & 11 deletions desktop/testMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,19 @@ export const getTestModeNodeConfig = (): NodeConfig => ({
main: {
'layer-duration': '6s',
'layers-per-epoch': 10,
'eligibility-confidence-param': 18,
'tick-size': 67000,
'network-hrp': HRP.Standalone,
},
genesis: {
'genesis-time': TEST_MODE_GENESIS_TIME,
'genesis-extra-data': 'standalone',
},
poet: {
'phase-shift': '30s',
'cycle-gap': '30s',
'grace-period': '10s',
},
post: {
'post-labels-per-unit': 128,
'post-max-numunits': 4,
'post-min-numunits': 1,
'post-k1': 12,
'post-k2': 4,
'post-k3': 4,
},
genesis: {
'genesis-time': TEST_MODE_GENESIS_TIME,
'genesis-extra-data': 'standalone',
},
});

Expand Down
1 change: 1 addition & 0 deletions shared/types/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export interface NodeConfig {
post: {
'post-labels-per-unit': number;
'post-max-numunits': number;
'post-min-numunits'?: number;
};
}

Expand Down

0 comments on commit 41c4fe2

Please sign in to comment.