From 80986abe52b205c16b1fa63b0bdf48f581c3f002 Mon Sep 17 00:00:00 2001 From: William Wills Date: Tue, 29 Oct 2024 11:31:51 -0400 Subject: [PATCH 1/4] feat: mirror check task automatically adds missing governance mirrors --- src/models/governance/governance.model.js | 4 +- src/tasks/mirror-check.js | 53 ++++++++++++++++++----- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/models/governance/governance.model.js b/src/models/governance/governance.model.js index 333452d2..fe9ad6a8 100644 --- a/src/models/governance/governance.model.js +++ b/src/models/governance/governance.model.js @@ -30,7 +30,7 @@ class Governance extends Model { const governanceVersionId = await datalayer.createDataLayerStore(); const revertOrganizationIfFailed = async () => { - logger.info('Reverting Failed Governance Body Creation'); + logger.warn('Reverting Failed Governance Body Creation'); await Meta.destroy({ where: { metaKey: 'governanceBodyId' } }); }; @@ -44,7 +44,6 @@ class Governance extends Model { ); const onConfirm = async () => { - logger.info('Organization confirmed, you are ready to go'); await Meta.upsert({ metaKey: 'governanceBodyId', metaValue: governanceVersionId, @@ -53,6 +52,7 @@ class Governance extends Model { metaKey: 'mainGoveranceBodyId', metaValue: governanceBodyId, }); + logger.info('Governance body confirmed, you are ready to go'); }; if (!USE_SIMULATOR) { diff --git a/src/tasks/mirror-check.js b/src/tasks/mirror-check.js index 123cf549..b4178e83 100644 --- a/src/tasks/mirror-check.js +++ b/src/tasks/mirror-check.js @@ -1,5 +1,5 @@ import { SimpleIntervalJob, Task } from 'toad-scheduler'; -import { Organization } from '../models'; +import { Meta, Organization } from '../models'; import { assertDataLayerAvailable, assertWalletIsSynced, @@ -23,7 +23,7 @@ const task = new Task('mirror-check', async () => { const shouldMirror = CONFIG?.AUTO_MIRROR_EXTERNAL_STORES ?? true; if (!CONFIG.USE_SIMULATOR && shouldMirror) { - runMirrorCheck(); + await runMirrorCheck(); } } catch (error) { logger.error( @@ -43,20 +43,49 @@ const job = new SimpleIntervalJob( ); const runMirrorCheck = async () => { - const organizations = await Organization.getOrgsMap(); - const orgs = Object.keys(organizations); - for (const org of orgs) { - const orgData = organizations[org]; - const mirrorUrl = await getMirrorUrl(); - if (mirrorUrl) { + const mirrorUrl = await getMirrorUrl(); + + if (mirrorUrl) { + const governanceOrgUidResult = await Meta.findOne({ + where: { metaKey: 'governanceBodyId' }, + attributes: ['metaValue'], + raw: true, + }); + const governanceRegistryIdResult = await Meta.findOne({ + where: { metaKey: 'mainGoveranceBodyId' }, + attributes: ['metaValue'], + raw: true, + }); + + if ( + governanceOrgUidResult?.metaValue && + governanceRegistryIdResult?.metaValue + ) { + // There is logic within the addMirror function to check if the mirror already exists + await Organization.addMirror( + governanceOrgUidResult?.metaValue, + mirrorUrl, + true, + ); + await Organization.addMirror( + governanceRegistryIdResult?.metaValue, + mirrorUrl, + true, + ); + } + + const organizations = await Organization.getOrgsMap(); + const orgs = Object.keys(organizations); + for (const org of orgs) { + const orgData = organizations[org]; // There is logic within the addMirror function to check if the mirror already exists await Organization.addMirror(orgData.orgUid, mirrorUrl, true); await Organization.addMirror(orgData.registryId, mirrorUrl, true); - } else { - logger.info( - 'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcement', - ); } + } else { + logger.info( + 'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcements', + ); } }; From 721344924c121a05e5dbf6d84facbb8dfb006197 Mon Sep 17 00:00:00 2001 From: William Wills Date: Wed, 30 Oct 2024 10:37:46 -0400 Subject: [PATCH 2/4] feat: mirror check adds governance mirrors if non-governance instance --- src/tasks/mirror-check.js | 102 ++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 43 deletions(-) diff --git a/src/tasks/mirror-check.js b/src/tasks/mirror-check.js index b4178e83..a2d8a8c8 100644 --- a/src/tasks/mirror-check.js +++ b/src/tasks/mirror-check.js @@ -8,8 +8,10 @@ import { logger } from '../config/logger.js'; import { getConfig } from '../utils/config-loader'; import { getMirrorUrl } from '../utils/datalayer-utils'; import dotenv from 'dotenv'; +import datalayer from '../datalayer'; -const CONFIG = getConfig().APP; +const APP_CONFIG = getConfig().APP; +const GOVERNANCE_CONFIG = getConfig().GOVERNANCE; dotenv.config(); // This task checks if there are any mirrors that have not been properly mirrored and then mirrors them if not @@ -20,14 +22,14 @@ const task = new Task('mirror-check', async () => { await assertWalletIsSynced(); // Default AUTO_MIRROR_EXTERNAL_STORES to true if it is null or undefined - const shouldMirror = CONFIG?.AUTO_MIRROR_EXTERNAL_STORES ?? true; + const shouldMirror = APP_CONFIG?.AUTO_MIRROR_EXTERNAL_STORES ?? true; - if (!CONFIG.USE_SIMULATOR && shouldMirror) { + if (!APP_CONFIG.USE_SIMULATOR && shouldMirror) { await runMirrorCheck(); } } catch (error) { logger.error( - `Retrying in ${CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300} seconds`, + `Retrying in ${APP_CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300} seconds`, error, ); } @@ -35,7 +37,7 @@ const task = new Task('mirror-check', async () => { const job = new SimpleIntervalJob( { - seconds: CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300, + seconds: APP_CONFIG?.TASKS?.MIRROR_CHECK_TASK_INTERVAL || 300, runImmediately: true, }, task, @@ -45,47 +47,61 @@ const job = new SimpleIntervalJob( const runMirrorCheck = async () => { const mirrorUrl = await getMirrorUrl(); - if (mirrorUrl) { - const governanceOrgUidResult = await Meta.findOne({ - where: { metaKey: 'governanceBodyId' }, - attributes: ['metaValue'], - raw: true, - }); - const governanceRegistryIdResult = await Meta.findOne({ - where: { metaKey: 'mainGoveranceBodyId' }, - attributes: ['metaValue'], - raw: true, - }); - - if ( - governanceOrgUidResult?.metaValue && - governanceRegistryIdResult?.metaValue - ) { - // There is logic within the addMirror function to check if the mirror already exists - await Organization.addMirror( - governanceOrgUidResult?.metaValue, - mirrorUrl, - true, - ); - await Organization.addMirror( - governanceRegistryIdResult?.metaValue, - mirrorUrl, - true, - ); - } - - const organizations = await Organization.getOrgsMap(); - const orgs = Object.keys(organizations); - for (const org of orgs) { - const orgData = organizations[org]; - // There is logic within the addMirror function to check if the mirror already exists - await Organization.addMirror(orgData.orgUid, mirrorUrl, true); - await Organization.addMirror(orgData.registryId, mirrorUrl, true); - } - } else { + if (!mirrorUrl) { logger.info( 'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcements', ); + return; + } + + // get governance info if governance node + const governanceOrgUidResult = await Meta.findOne({ + where: { metaKey: 'governanceBodyId' }, + attributes: ['metaValue'], + raw: true, + }); + const governanceRegistryIdResult = await Meta.findOne({ + where: { metaKey: 'mainGoveranceBodyId' }, + attributes: ['metaValue'], + raw: true, + }); + + if ( + governanceOrgUidResult?.metaValue && + governanceRegistryIdResult?.metaValue + ) { + // add governance mirrors if instance is governance + // There is logic within the addMirror function to check if the mirror already exists + await Organization.addMirror( + governanceOrgUidResult?.metaValue, + mirrorUrl, + true, + ); + await Organization.addMirror( + governanceRegistryIdResult?.metaValue, + mirrorUrl, + true, + ); + } else if (GOVERNANCE_CONFIG?.GOVERNANCE_BODY_ID) { + const governanceStoreValue = await datalayer.getSubscribedStoreData( + GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, + ); + + // add governance mirrors if non-governance instance + await Organization.addMirror( + GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, + mirrorUrl, + true, + ); + await Organization.addMirror(governanceStoreValue, mirrorUrl, true); + } + + const organizations = await Organization.getOrgsMap(); + const orgs = Object.keys(organizations); + for (const org of orgs) { + const orgData = organizations[org]; + await Organization.addMirror(orgData.orgUid, mirrorUrl, true); + await Organization.addMirror(orgData.registryId, mirrorUrl, true); } }; From 7fac2c786c4037cedbd7997c088ebcca898d9487 Mon Sep 17 00:00:00 2001 From: William Wills Date: Wed, 30 Oct 2024 10:42:06 -0400 Subject: [PATCH 3/4] feat: mirror check adds governance mirrors if non-governance instance --- src/tasks/mirror-check.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/tasks/mirror-check.js b/src/tasks/mirror-check.js index a2d8a8c8..632ac468 100644 --- a/src/tasks/mirror-check.js +++ b/src/tasks/mirror-check.js @@ -87,13 +87,17 @@ const runMirrorCheck = async () => { GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, ); - // add governance mirrors if non-governance instance - await Organization.addMirror( - GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, - mirrorUrl, - true, - ); - await Organization.addMirror(governanceStoreValue, mirrorUrl, true); + if (governanceStoreValue?.v1) { + // add governance mirrors if non-governance instance + await Organization.addMirror( + GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, + mirrorUrl, + true, + ); + await Organization.addMirror(governanceStoreValue, mirrorUrl, true); + } else { + logger.warn('error adding governance mirrors'); + } } const organizations = await Organization.getOrgsMap(); From 7270d3b5a6c5d7c43d24fba9309668d86cd39cd8 Mon Sep 17 00:00:00 2001 From: William Wills Date: Wed, 30 Oct 2024 10:42:06 -0400 Subject: [PATCH 4/4] feat: mirror check adds governance mirrors if non-governance instance --- src/tasks/mirror-check.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/tasks/mirror-check.js b/src/tasks/mirror-check.js index a2d8a8c8..03a0a873 100644 --- a/src/tasks/mirror-check.js +++ b/src/tasks/mirror-check.js @@ -87,13 +87,17 @@ const runMirrorCheck = async () => { GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, ); - // add governance mirrors if non-governance instance - await Organization.addMirror( - GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, - mirrorUrl, - true, - ); - await Organization.addMirror(governanceStoreValue, mirrorUrl, true); + if (governanceStoreValue?.v1) { + // add governance mirrors if non-governance instance + await Organization.addMirror( + GOVERNANCE_CONFIG.GOVERNANCE_BODY_ID, + mirrorUrl, + true, + ); + await Organization.addMirror(governanceStoreValue?.v1, mirrorUrl, true); + } else { + logger.warn('error adding governance mirrors'); + } } const organizations = await Organization.getOrgsMap();