From 7f828a4bb7d9a8c9d870bae4a111ef0fc2a14087 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Wed, 11 Oct 2023 12:59:59 -0400 Subject: [PATCH 1/4] fix: dont return homeorg when in pending status --- src/api/registry.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/api/registry.js b/src/api/registry.js index f2ee3b0..c067a1c 100644 --- a/src/api/registry.js +++ b/src/api/registry.js @@ -241,7 +241,13 @@ const getHomeOrg = async () => { (key) => response.body[key] ); - return orgArray.find((org) => org.isHome) || null; + const homeOrg = orgArray.find((org) => org.isHome) || null; + + if (homeOrg.orgUid === 'PENDING') { + return null; + } + + return homeOrg; } catch (error) { logger.error(`Could not get home org: ${error.message}`); From 01606907eb4a436b7f2729fc6cf1d9c3d98909e6 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Wed, 11 Oct 2023 13:17:19 -0400 Subject: [PATCH 2/4] fix: optional handling on empty registry --- src/api/registry.js | 18 ++++++++++++++++-- src/constants.js | 4 ++++ src/tasks/sync-retirements.js | 36 +++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 src/constants.js diff --git a/src/api/registry.js b/src/api/registry.js index c067a1c..2678aa2 100644 --- a/src/api/registry.js +++ b/src/api/registry.js @@ -4,6 +4,7 @@ const { CONFIG } = require("../config"); const { logger } = require("../logger"); const wallet = require("../chia/wallet"); const utils = require("../utils"); +const constants = require("../constants"); const registryUri = utils.generateUriForHostAndPort( CONFIG().CADT.PROTOCOL, @@ -243,7 +244,7 @@ const getHomeOrg = async () => { const homeOrg = orgArray.find((org) => org.isHome) || null; - if (homeOrg.orgUid === 'PENDING') { + if (homeOrg.orgUid === "PENDING") { return null; } @@ -528,7 +529,11 @@ const getOrgMetaData = async (orgUid) => { * * @returns {Promise} */ -const waitForRegistryDataSync = async () => { +const waitForRegistryDataSync = async (options = {}) => { + if (!options.throwOnEmptyRegistry) { + options.throwOnEmptyRegistry = false; + } + if (process.env.NODE_ENV === "test") { return; } @@ -573,6 +578,15 @@ const waitForRegistryDataSync = async () => { return waitForRegistryDataSync(); } + if ( + onChainRegistryRoot.hash === constants.emptySingletonHash && + options.throwOnEmptyRegistry + ) { + throw new Error( + "Registry is empty. Please add some data to run auto retirement task." + ); + } + if (onChainRegistryRoot.hash !== homeOrg.registryHash) { console.log("Waiting for Registry to sync with latest regisry root.", { onChainRoot: onChainRegistryRoot.hash, diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..0ba4879 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,4 @@ +module.exports = { + emptySingletonHash: + "0x0000000000000000000000000000000000000000000000000000000000000000", +}; diff --git a/src/tasks/sync-retirements.js b/src/tasks/sync-retirements.js index 6fa0ec6..d48830f 100644 --- a/src/tasks/sync-retirements.js +++ b/src/tasks/sync-retirements.js @@ -45,25 +45,29 @@ const job = new SimpleIntervalJob( * @returns {Promise} */ const startSyncRetirementsTask = async () => { - await registry.waitForRegistryDataSync(); - const homeOrg = await registry.getHomeOrg(); + try { + await registry.waitForRegistryDataSync({ throwOnEmptyRegistry: true }); + const homeOrg = await registry.getHomeOrg(); - if (!homeOrg) { - logger.warn( - "Can not attain home organization from the registry, skipping sync-retirements task" - ); - return; - } + if (!homeOrg) { + logger.warn( + "Can not attain home organization from the registry, skipping sync-retirements task" + ); + return; + } - const lastProcessedHeight = await registry.getLastProcessedHeight(); - if (lastProcessedHeight == null) { - logger.warn( - "Can not attain the last Processed Retirement Height from the registry, skipping sync-retirements task" - ); - return; - } + const lastProcessedHeight = await registry.getLastProcessedHeight(); + if (lastProcessedHeight == null) { + logger.warn( + "Can not attain the last Processed Retirement Height from the registry, skipping sync-retirements task" + ); + return; + } - await getAndProcessActivities(homeOrg, lastProcessedHeight); + await getAndProcessActivities(homeOrg, lastProcessedHeight); + } catch (error) { + logger.error(`Error in sync-retirements task: ${error.message}`); + } }; /** From ef245da3783ff4c8f3d450741705f7efa23dcff0 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Wed, 11 Oct 2023 16:01:20 -0400 Subject: [PATCH 3/4] fix: dont write last processed height when you didnt process retirements --- src/api/registry.js | 1 + src/tasks/sync-retirements.js | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/api/registry.js b/src/api/registry.js index 2678aa2..1f2281b 100644 --- a/src/api/registry.js +++ b/src/api/registry.js @@ -69,6 +69,7 @@ const sanitizeUnitForUpdate = (unit) => { delete cleanedUnit.issuanceId; delete cleanedUnit.orgUid; delete cleanedUnit.serialNumberBlock; + delete cleanedUnit.timeStaged; Object.keys(cleanedUnit).forEach((key) => { if (cleanedUnit[key] === null) { diff --git a/src/tasks/sync-retirements.js b/src/tasks/sync-retirements.js index d48830f..bba6688 100644 --- a/src/tasks/sync-retirements.js +++ b/src/tasks/sync-retirements.js @@ -86,22 +86,23 @@ const getAndProcessActivities = async (homeOrg, minHeight = 0) => { minHeight ); - if (!retirements.length) { + const ownedRetirements = retirements.filter( + (activity) => activity?.token?.org_uid === homeOrg.orgUid + ); + + if (!ownedRetirements.length) { break; } - for (const activity of retirements) { + for (const activity of ownedRetirements) { // You can only autoretire your own units - console.log(activity?.token?.org_uid, homeOrg.orgUid); - if (activity?.token?.org_uid === homeOrg.orgUid) { - logger.info(`PROCESSING RETIREMENT ACTIVITY: ${activity.coin_id}`); - await processResult({ - marketplaceIdentifier: activity.cw_unit.marketplaceIdentifier, - amount: activity.amount / 1000, - beneficiaryName: activity.beneficiary_name, - beneficiaryAddress: activity.beneficiary_address, - }); - } + logger.info(`PROCESSING RETIREMENT ACTIVITY: ${activity.coin_id}`); + await processResult({ + marketplaceIdentifier: activity.cw_unit.marketplaceIdentifier, + amount: activity.amount / 1000, + beneficiaryName: activity.beneficiary_name, + beneficiaryAddress: activity.beneficiary_address, + }); } const highestHeight = calcHighestActivityHeight(retirements); From ebd28d806838a511493ac24fc2c8d0f7e7da46d6 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Wed, 11 Oct 2023 16:15:43 -0400 Subject: [PATCH 4/4] chore: fix tests --- tests/tasks/sync-retirements.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tasks/sync-retirements.test.js b/tests/tasks/sync-retirements.test.js index f60ae06..5c4d39b 100644 --- a/tests/tasks/sync-retirements.test.js +++ b/tests/tasks/sync-retirements.test.js @@ -121,8 +121,8 @@ describe("Task: Sync Retirements", () => { expect(registryGetAssetUnitBlocksStub.called).to.be.false; - // expecting this to be true because the stub is returning activities even though none are processed - expect(registrySetLastProcessedHeightStub.called).to.be.true; + // expecting this to be false because we didnt get any activities from our home org + expect(registrySetLastProcessedHeightStub.called).to.be.false; }); it("Does not run the task if the task is already running", () => {