Skip to content

Commit

Permalink
Minor settings, privnet and process fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
everoddandeven committed Feb 8, 2025
1 parent bc0aac3 commit 57db3ea
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 58 deletions.
33 changes: 24 additions & 9 deletions app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,20 +539,35 @@ try {
});

ipcMain.handle('quit', async (event: IpcMainInvokeEvent) => {
if (isQuitting) {
console.warn("Already quitting");
return;
}

isQuitting = true;

if (monerodProcess) {
if (PrivateTestnet.started) {
await PrivateTestnet.stop();
try {
if (monerodProcess) {
if (PrivateTestnet.started) {
await PrivateTestnet.stop();
}
else await monerodProcess.stop();
monerodProcess = null;
}
else await monerodProcess.stop();
monerodProcess = null;
}
catch (error: any) {
console.error("An error occured while stopping monerod on quit handler", error);
}

tray.destroy();
win?.close();
win?.destroy();
app.quit();
try {
tray.destroy();
win?.close();
win?.destroy();
app.quit();
}
catch(error: any) {
console.error("An error occurred on quit handler: ", error);
}
});

ipcMain.handle('start-monerod', (event: IpcMainInvokeEvent, configFilePath: string[]) => {
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"email": "[email protected]"
},
"version": "1.1.0",

"main": "main.js",
"private": true,
"dependencies": {
Expand Down
4 changes: 4 additions & 0 deletions app/process/AppChildProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export class AppChildProcess {
return this._running;
}

public get stopping(): boolean {
return this._stopping;
}

constructor({ command, args, isExe } : { command: string, args?: string[], isExe?: boolean}) {
this._command = command;
this._args = args;
Expand Down
72 changes: 68 additions & 4 deletions app/process/PrivateTestnet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { MonerodProcess } from "./MonerodProcess";
import * as os from 'os';

function copyArray<T>(src: T[], target?: T[]): T[] {
if (!target) target = [];
if (target.length !== 0) {
target = [];
}

src.forEach((v) => target.push(v));
return target;
}

export abstract class PrivateTestnet {
private static get isWindows(): boolean {
return os.platform() === 'win32';
}

private static get directorySeparator(): '\\' | '/' {
return this.isWindows ? '\\' : '/';
}

private static readonly _node1Options: string[] = [
'--testnet', '--no-igd', '--hide-my-port', '--data-dir', '.localnet/xmr_local/node1',
'--testnet', '--no-igd', '--hide-my-port',
'--p2p-bind-ip', '127.0.0.1', '--log-level', '0', '--add-exclusive-node', '127.0.0.1:48080',
'--add-exclusive-node', '127.0.0.1:58080', '--rpc-access-control-origins', '*',
'--fixed-difficulty', '500', '--disable-rpc-ban', '--non-interactive'
];

private static readonly _node2Options: string[] = [
'--testnet', '--no-igd', '--hide-my-port', '--data-dir', '.localnet/xmr_local/node2',
'--testnet', '--no-igd', '--hide-my-port',
'--p2p-bind-ip', '127.0.0.1', '--p2p-bind-port', '48080', '--rpc-bind-port' ,'48081',
'--zmq-rpc-bind-port', '48082', '--log-level', '1', '--confirm-external-bind', '--add-exclusive-node', '127.0.0.1:28080',
'--add-exclusive-node', '127.0.0.1:58080', '--rpc-access-control-origins', '*',
Expand Down Expand Up @@ -41,20 +60,65 @@ export abstract class PrivateTestnet {
return this._mining;
}

private static replaceAll(value: string, oldValue: string, newValue: string): string {
let v = value;

while(v.includes(oldValue)) v = v.replace(oldValue, newValue);

return v;
}

private static parseDataDir(dataDir: string): string {
const separator = this.directorySeparator;
const dataDirSeparator = dataDir.includes('\\') ? '\\' : '/';
const needsReplace = separator !== dataDirSeparator;

if (needsReplace) return this.replaceAll(dataDir, dataDirSeparator, separator);
return dataDir;
}

private static buildDataDir(monerodPath: string, dataDir: string): string {
const separator = this.directorySeparator;

if (monerodPath === '') return dataDir;

const components = monerodPath.split(separator);
components.pop();
const path = components.join(separator);

return `${path}${separator}${this.parseDataDir(dataDir)}`;
}

private static buildDataDirFlags(monerodPath: string, dataDir: string): [string, string] {
return ['--data-dir', this.buildDataDir(monerodPath, dataDir)];
}

private static getNode1Flags(monerodPath: string): string[] {
const flags = copyArray<string>(this._node1Options);
flags.push(...this.buildDataDirFlags(monerodPath, '.localnet/xmr_local/node1'));
return flags;
}

private static getNode2Flags(monerodPath: string): string[] {
const flags = copyArray<string>(this._node2Options);
flags.push(...this.buildDataDirFlags(monerodPath, '.localnet/xmr_local/node2'));
return flags;
}

public static init(monerodPath: string): void {
if (this.initialized) {
throw new Error("Already initiliazed private testnet");
}

this._node1 = new MonerodProcess({
monerodCmd: monerodPath,
flags: this._node1Options,
flags: this.getNode1Flags(monerodPath),
isExe: true
});

this._node2 = new MonerodProcess({
monerodCmd: monerodPath,
flags: this._node2Options,
flags: this.getNode2Flags(monerodPath),
isExe: true
});

Expand Down
8 changes: 4 additions & 4 deletions src/app/core/services/daemon/daemon-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class DaemonDataService {
this.startLoop();
}
else {
this.stopLoop();
if (this.refreshInterval) this.stopLoop();
}
});
});
Expand Down Expand Up @@ -333,7 +333,7 @@ export class DaemonDataService {
private osType?: { platform: string };

private async refresh(): Promise<void> {
if (this.refreshing || this.tooEarlyForRefresh || this.daemonService.stopping) {
if (this.refreshing || this.tooEarlyForRefresh || this.daemonService.stopping || !this._daemonRunning) {
return;
}

Expand Down Expand Up @@ -420,7 +420,7 @@ export class DaemonDataService {
this._firstRefresh = false;

if (!this._daemonRunning) {
this.stopLoop();
if (this.refreshInterval) this.stopLoop();
this.syncEnd.emit();
return;
}
Expand Down Expand Up @@ -517,7 +517,7 @@ export class DaemonDataService {
this.syncError.emit(error);

if (!await this.daemonService.isRunning()) {
this.stopLoop();
if (this.refreshInterval) this.stopLoop();
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/app/core/services/daemon/daemon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,9 @@ export class DaemonService {
}

public async quit(): Promise<void> {

Check failure on line 1259 in src/app/core/services/daemon/daemon.service.ts

View workflow job for this annotation

GitHub Actions / build (20)

Async method 'quit' has no 'await' expression
if (this._quitting) throw new Error("Already quitting daemon");

this._quitting = true;
const running: boolean = await this.isRunning();

if (running) {
await this.stopDaemon();
}

window.electronAPI.quit();
}
Expand Down
8 changes: 5 additions & 3 deletions src/app/pages/settings/settings.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AfterViewInit, Component, NgZone } from '@angular/core';
import { NavbarLink } from '../../shared/components/navbar/navbar.model';
import { DaemonSettings, DefaultPrivnetNode2Settings } from '../../../common';
import { DaemonSettings, DefaultPrivnetNode2Settings, PrivnetDaemonSettings } from '../../../common';
import { DaemonService } from '../../core/services/daemon/daemon.service';
import { ElectronService } from '../../core/services';
import { DaemonSettingsError } from '../../../common';
Expand Down Expand Up @@ -331,7 +331,7 @@ export class SettingsComponent extends BasePageComponent implements AfterViewIni

this.isPortable = await this.electronService.isPortable();
this.networkType = this._currentSettings.mainnet ? 'mainnet' : this._currentSettings.testnet ? 'testnet' : this._currentSettings.stagenet ? 'stagenet' : this._currentSettings.privnet ? 'privnet' : 'mainnet';
if (this._privnetSettings.monerodPath == '') this._privnetSettings.monerodPath = this._currentSettings.monerodPath;
if (this._privnetSettings.monerodPath == '') this._privnetSettings.setMonerodPath(this._currentSettings.monerodPath);
this.refreshLogin();
}

Expand Down Expand Up @@ -606,7 +606,9 @@ export class SettingsComponent extends BasePageComponent implements AfterViewIni
const valid = await this.daemonService.checkValidMonerodPath(file);
if (valid) {
this.ngZone.run(() => {
this.currentSettings.monerodPath = file;
const currentSettings = this.currentSettings;
if (currentSettings instanceof PrivnetDaemonSettings) currentSettings.setMonerodPath(file);
else currentSettings.monerodPath = file;
});
}
else {
Expand Down
21 changes: 4 additions & 17 deletions src/common/DefaultPrivnetNode1Settings.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import { DaemonSettings } from "./DaemonSettings";
import { PrivnetDaemonSettings } from "./PrivnetDaemonSettings";

export class DefaultPrivnetNode1Settings extends DaemonSettings {

public override get isPrivnet(): boolean {
return true;
}
export class DefaultPrivnetNode1Settings extends PrivnetDaemonSettings {

constructor(monerodPath: string = '') {
super();
this.monerodPath = monerodPath;
this.testnet = true;
this.noIgd = true;
this.hideMyPort = true;
this.dataDir = '.localnet/xmr_local/node1';
this.p2pBindIp = '127.0.0.1';
this.logLevel = 0;
super(monerodPath, '.localnet/xmr_local/node1');

this.addExclusiveNode('127.0.0.1:48080');
this.addExclusiveNode('127.0.0.1:58080');
this.rpcAccessControlOrigins = '*';
this.fixedDifficulty = 500;
this.disableRpcBan = true;
}
}
19 changes: 3 additions & 16 deletions src/common/DefaultPrivnetNode2Settings.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
import { DaemonSettings } from "./DaemonSettings";
import { PrivnetDaemonSettings } from "./PrivnetDaemonSettings";

export class DefaultPrivnetNode2Settings extends DaemonSettings {

public override get isPrivnet(): boolean {
return true;
}
export class DefaultPrivnetNode2Settings extends PrivnetDaemonSettings {

constructor(monerodPath: string = '') {
super();
super(monerodPath, '.localnet/xmr_local/node2');

this.monerodPath = monerodPath;
this.testnet = true;
this.noIgd = true;
this.hideMyPort = true;
this.dataDir = '.localnet/xmr_local/node2';
this.p2pBindIp = '127.0.0.1';
this.p2pBindPort = 48080;
this.rpcBindPort = 48081;
this.zmqRpcBindPort = 48082;
this.logLevel = 1;
this.confirmExternalBind = true;
this.addExclusiveNode('127.0.0.1:28080');
this.addExclusiveNode('127.0.0.1:58080');
this.rpcAccessControlOrigins = '*';
this.fixedDifficulty = 500;
this.disableRpcBan = true;
}
}
50 changes: 50 additions & 0 deletions src/common/PrivnetDaemonSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { DaemonSettings } from "./DaemonSettings";

export class PrivnetDaemonSettings extends DaemonSettings {

protected _dataDir: string;

public override get isPrivnet(): boolean {
return true;
}

constructor(monerodPath: string, dataDir: string) {
super();
this.testnet = true;
this.noIgd = true;
this.hideMyPort = true;
this.p2pBindIp = '127.0.0.1';
this.logLevel = 0;
this.rpcAccessControlOrigins = '*';
this.fixedDifficulty = 500;
this.disableRpcBan = true;
this.syncOnWifi = true;
this._dataDir = dataDir;
this.setMonerodPath(monerodPath);
}

private refreshDataDir(): void {
if (this.monerodPath == '') {
this.dataDir = '';
return;
}

const separator: '\\' | '/' = this.monerodPath.includes('\\') ? '\\' : '/';
const dataDirSeparator: '\\' | '/' = this._dataDir.includes('\\') ? '\\' : '/';
const needsReplace = dataDirSeparator !== separator;

const dataDir = needsReplace ? this._dataDir.replaceAll(dataDirSeparator, separator) : this._dataDir;

const components = this.monerodPath.split(separator);
components.pop();
const path = components.join(separator);

this.dataDir = `${path}${separator}${dataDir}`;
}

public setMonerodPath(monerodPath: string): void {
this.monerodPath = monerodPath;

this.refreshDataDir();
}
}
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ export * from './error';
export * from './request';
export * from './utils';

export { PrivnetDaemonSettings } from "./PrivnetDaemonSettings";
export { DefaultPrivnetNode1Settings } from "./DefaultPrivnetNode1Settings";
export { DefaultPrivnetNode2Settings } from "./DefaultPrivnetNode2Settings";
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"es2016",
"es2015",
"es2018",
"es2021",
"dom"
],
"useDefineForClassFields": false
Expand Down

0 comments on commit 57db3ea

Please sign in to comment.