From 1c15bcf0bc4674257cd46290a267160d76f2c637 Mon Sep 17 00:00:00 2001 From: gbayasgalan <73820006+gbayasgalan@users.noreply.github.com> Date: Mon, 6 Nov 2023 13:47:05 +0100 Subject: [PATCH] FIX: validator keys grouping, removing keys (#1536) --- .../src/backend/ValidatorAccountManager.js | 61 +++++++++++++------ launcher/src/background.js | 4 +- launcher/src/composables/validators.js | 9 ++- launcher/src/store/ControlService.js | 7 ++- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/launcher/src/backend/ValidatorAccountManager.js b/launcher/src/backend/ValidatorAccountManager.js index 5fcfff1f9..97196c9ec 100755 --- a/launcher/src/backend/ValidatorAccountManager.js +++ b/launcher/src/backend/ValidatorAccountManager.js @@ -19,6 +19,8 @@ export class ValidatorAccountManager { this.nodeConnection = nodeConnection; this.serviceManager = serviceManager; this.batches = []; + this.callCount = 0; + this.storedKeys = []; } createBatch(files, password, slashingDB, chunkSize = 100) { @@ -235,7 +237,7 @@ export class ValidatorAccountManager { } } - async listValidators(serviceID) { + async listValidators(serviceID, numRunningValidatorService) { const ref = StringUtils.createRandomString(); this.nodeConnection.taskManager.otherTasksHandler(ref, `Listing Keys`); try { @@ -246,7 +248,7 @@ export class ValidatorAccountManager { const data = JSON.parse(result.stdout); if (!data.data) data.data = []; - await this.writeKeys(data.data.map((k) => k.validating_pubkey)); + await this.storeKeys(data.data, numRunningValidatorService); this.nodeConnection.taskManager.otherTasksHandler(ref, `Write Keys to keys.yaml`, true); this.nodeConnection.taskManager.otherTasksHandler(ref); @@ -523,31 +525,54 @@ export class ValidatorAccountManager { } } + async storeKeys(data, numRunningValidatorService) { + this.callCount++; + if (this.callCount % numRunningValidatorService !== 0 && !isNaN(this.callCount % numRunningValidatorService)) { + this.storedKeys.push(...data.map((k) => k.validating_pubkey)); + } else if (this.callCount % numRunningValidatorService === 0) { + this.storedKeys.push(...data.map((k) => k.validating_pubkey)); + await this.writeKeys(this.storedKeys); + this.storedKeys = []; + this.callCount = 0; + } else if (data.length === 0) { + await this.writeKeys(data); + this.callCount = 0; + } + } + async writeKeys(keys) { let obj = keys; - console.log(obj); if (Array.isArray(keys)) { const existing = await this.readKeys(); - obj = existing ? existing : {}; - keys.forEach((key) => { - if (existing && existing[key]) { - if (typeof existing[key] === "string") { + if (existing) { + obj = existing; + if (Object.keys(existing).length !== 0) { + Object.keys(existing).forEach((key) => { + obj = !keys.includes(key) ? {} : obj; + }); + } + keys.forEach((key) => { + if (existing[key]) { + if (typeof existing[key] === "string") { + obj[key] = { + keyName: existing[key], + groupName: "", + groupID: null, + }; + } else { + obj[key] = existing[key]; + } + } else { obj[key] = { - keyName: existing[key], + keyName: "", groupName: "", groupID: null, }; - } else { - obj[key] = existing[key]; } - } else { - obj[key] = { - keyName: "", - groupName: "", - groupID: null, - }; - } - }); + }); + } else { + obj = {}; + } } await this.nodeConnection.sshService.exec( "echo -e " + StringUtils.escapeStringForShell(YAML.stringify(obj)) + " > /etc/stereum/keys.yaml" diff --git a/launcher/src/background.js b/launcher/src/background.js index 7283f9135..157a25a79 100755 --- a/launcher/src/background.js +++ b/launcher/src/background.js @@ -240,7 +240,7 @@ ipcMain.handle("deleteValidators", async (event, args) => { }); ipcMain.handle("listValidators", async (event, args) => { - return await validatorAccountManager.listValidators(args); + return await validatorAccountManager.listValidators(args.serviceID, args.numRunningValidatorService); }); ipcMain.handle("listServices", async () => { @@ -479,7 +479,7 @@ ipcMain.handle("IpScanLan", async () => { }); ipcMain.handle("beaconchainMonitoringModification", async (event, args) => { - return await serviceManager.beaconchainMonitoringModification(args) + return await serviceManager.beaconchainMonitoringModification(args); }); // Scheme must be registered before the app is ready diff --git a/launcher/src/composables/validators.js b/launcher/src/composables/validators.js index 53bdc99d5..00dedf00f 100644 --- a/launcher/src/composables/validators.js +++ b/launcher/src/composables/validators.js @@ -8,12 +8,19 @@ export async function useListKeys(forceRefresh) { const serviceStore = useServices(); const nodeManageStore = useNodeManage(); const stakingStore = useStakingStore(); + let numRunningValidatorService = 0; let keyStats = []; let clients = serviceStore.installedServices.filter( (s) => s.category == "validator" && s.service != "CharonService" && s.service != "SSVNetworkService" ); if (clients && clients.length > 0 && nodeManageStore.currentNetwork.network != "") { + for (let client of clients) { + if (client.state === "running" && client.service.includes("ValidatorService")) { + numRunningValidatorService++; + } + } + for (let client of clients) { //if there is already a list of keys () if ( @@ -21,7 +28,7 @@ export async function useListKeys(forceRefresh) { client.state === "running" ) { //refresh validaotr list - let result = await ControlService.listValidators(client.config.serviceID); + let result = await ControlService.listValidators(client.config.serviceID, numRunningValidatorService); if ( !client.service.includes("Lighthouse") && !client.service.includes("Lodestar") && diff --git a/launcher/src/store/ControlService.js b/launcher/src/store/ControlService.js index bba794f27..901e80e53 100755 --- a/launcher/src/store/ControlService.js +++ b/launcher/src/store/ControlService.js @@ -206,8 +206,11 @@ class ControlService extends EventEmitter { return await this.promiseIpc.send("deleteValidators", args); } - async listValidators(args) { - return await this.promiseIpc.send("listValidators", args); + async listValidators(serviceID, numRunningValidatorService) { + return await this.promiseIpc.send("listValidators", { + serviceID: serviceID, + numRunningValidatorService: numRunningValidatorService, + }); } async listServices() {