Skip to content

Commit

Permalink
Merge pull request #170 from Chia-Network/fix/dont-run-autoretire-whe…
Browse files Browse the repository at this point in the history
…n-pending-org

fix: dont return homeorg when in pending status
  • Loading branch information
MichaelTaylor3D authored Oct 17, 2023
2 parents 8690f84 + dc95712 commit 2e4e920
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
11 changes: 9 additions & 2 deletions src/api/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { CONFIG } = require("../config");
const { logger } = require("../logger");
const wallet = require("../chia/wallet");
const utils = require("../utils");
const constants = require("../constants.js");
const constants = require("../constants");
const { Mutex } = require("async-mutex");

const mutex = new Mutex();
Expand Down Expand Up @@ -72,6 +72,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) {
Expand Down Expand Up @@ -245,7 +246,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}`);

Expand Down
61 changes: 33 additions & 28 deletions src/tasks/sync-retirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,29 @@ const job = new SimpleIntervalJob(
* @returns {Promise<void>}
*/
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}`);
}
};

/**
Expand All @@ -82,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);
Expand Down
4 changes: 2 additions & 2 deletions tests/tasks/sync-retirements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,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", () => {
Expand Down

0 comments on commit 2e4e920

Please sign in to comment.