Skip to content

Commit

Permalink
fix: mirror logic fixes for url and scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastCicada committed Feb 29, 2024
1 parent 5f89cce commit 96ac698
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
23 changes: 15 additions & 8 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import fs from 'fs';
import path from 'path';
import superagent from 'superagent';
import { getConfig } from '../utils/config-loader';
import fullNode from './fullNode';
import { publicIpv4 } from '../utils/ip-tools';
import wallet from './wallet';
import { Organization } from '../models';
import { logger } from '../config/logger.cjs';
import { getChiaRoot } from '../utils/chia-root.js';
import { getMirrorUrl } from '../utils/datalayer-utils';

process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

Expand Down Expand Up @@ -117,6 +116,17 @@ const addMirror = async (storeId, url, forceAddMirror = false) => {
await wallet.waitForAllTransactionsToConfirm();
const homeOrg = await Organization.getHomeOrg();

logger.info(
`Checking mirrors for storeID is ${storeId} with mirror URL ${url}`,
);

if (!url) {
logger.info(
`No DATALAYER_FILE_SERVER_URL specified so skipping mirror for ${storeId}`,
);
return false;
}

if (!homeOrg && !forceAddMirror) {
logger.info(`No home org detected so skipping mirror for ${storeId}`);
return false;
Expand All @@ -130,7 +140,7 @@ const addMirror = async (storeId, url, forceAddMirror = false) => {
);

if (mirror) {
logger.info(`Mirror already available for ${storeId}`);
logger.info(`Mirror already available for ${storeId} at ${url}`);
return true;
}

Expand Down Expand Up @@ -553,12 +563,9 @@ const subscribeToStoreOnDataLayer = async (storeId) => {
if (Object.keys(data).includes('success') && data.success) {
logger.info(`Successfully Subscribed: ${storeId}`);

const chiaConfig = fullNode.getChiaConfig();
const mirrorUrl = await getMirrorUrl();

await addMirror(
storeId,
`http://${await publicIpv4()}:${chiaConfig.data_layer.host_port}`,
);
await addMirror(storeId, mirrorUrl);

return data;
}
Expand Down
15 changes: 10 additions & 5 deletions src/tasks/mirror-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ const job = new SimpleIntervalJob(
);

const runMirrorCheck = async () => {
const homeOrg = Organization.getHomeOrg();
const homeOrg = await Organization.getHomeOrg();

if (homeOrg) {
const organizations = Organization.getOrgsMap();
const organizations = await Organization.getOrgsMap();
const orgs = Object.keys(organizations);
for (const org of orgs) {
const orgData = organizations[org];
const mirrorUrl = await getMirrorUrl();

// There is logic within the addMirror function to check if the mirror already exists
await Organization.addMirror(orgData.orgUid, mirrorUrl);
await Organization.addMirror(orgData.registryId, mirrorUrl);
if (mirrorUrl) {
// There is logic within the addMirror function to check if the mirror already exists
await Organization.addMirror(orgData.orgUid, mirrorUrl);
await Organization.addMirror(orgData.registryId, mirrorUrl);
} else {
logger.error(
'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcement',
);
}
}
};
Expand Down
15 changes: 8 additions & 7 deletions src/utils/datalayer-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getConfig } from './config-loader';
import fullNode from '../datalayer/fullNode';
import { publicIpv4 } from './ip-tools';
import { logger } from '../logger.js';

export const encodeHex = (str) => {
return Buffer.from(str).toString('hex');
Expand Down Expand Up @@ -113,10 +112,12 @@ export const optimizeAndSortKvDiff = (kvDiff) => {
};

export const getMirrorUrl = async () => {
try {
const { DATALAYER_FILE_SERVER_URL } = getConfig().APP;
const chiaConfig = fullNode.getChiaConfig();
return (
DATALAYER_FILE_SERVER_URL ||
`http://${await publicIpv4()}:${chiaConfig.data_layer.host_port}`
);
logger.debug(`Resolved Mirror Url: ${DATALAYER_FILE_SERVER_URL}`);
return DATALAYER_FILE_SERVER_URL;
} catch (error) {
logger.error('Error getting DATALAYER_FILE_SERVER_URL: ${error}');
return null;
}
};

0 comments on commit 96ac698

Please sign in to comment.