Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/mirror logic #1021

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
data.sqlite3
test.sqlite3
test.sqlite3*
testMirror.sqlite3

# dependencies
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cadt",
"version": "1.7.9",
"version": "1.7.9b1",
"_comment": "DONT CHANGE MAJOR UNLESS DATAMODEL CHANGES: The major version corresponds to the datamodel version your using, so 2.0.0 means it'll use datamodel v2",
"private": true,
"bin": "build/server.js",
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ export const addMetadata = async (req, res) => {

export const addMirror = async (req, res) => {
try {
console.info(`ZGB: We are in the organization.controller.js file now`);

await assertIfReadOnlyMode();
await assertWalletIsSynced();
await assertHomeOrgExists();
Expand Down
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, true);

return data;
}
Expand Down
4 changes: 2 additions & 2 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const createDataLayerStore = async () => {
return storeId;
};

const addMirror = async (storeId, url) => {
return dataLayer.addMirror(storeId, url);
const addMirror = async (storeId, url, force = false) => {
return dataLayer.addMirror(storeId, url, force);
};

const waitForStoreToBeConfirmed = async (storeId, retry = 0) => {
Expand Down
4 changes: 2 additions & 2 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ class Organization extends Model {
return registryVersionId;
}

static async addMirror(storeId, url) {
await datalayer.addMirror(storeId, url);
static async addMirror(storeId, url, force = false) {
await datalayer.addMirror(storeId, url, force);
}

static async importHomeOrg(orgUid) {
Expand Down
23 changes: 12 additions & 11 deletions src/tasks/mirror-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ const job = new SimpleIntervalJob(
);

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

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

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) {
// 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);
await Organization.addMirror(orgData.orgUid, mirrorUrl, true);
await Organization.addMirror(orgData.registryId, mirrorUrl, true);
} else {
logger.error(
'DATALAYER_FILE_SERVER_URL not set, skipping mirror announcement',
);
}
}
};
Expand Down
17 changes: 9 additions & 8 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 '../config/logger.cjs';

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 () => {
const { DATALAYER_FILE_SERVER_URL } = getConfig().APP;
const chiaConfig = fullNode.getChiaConfig();
return (
DATALAYER_FILE_SERVER_URL ||
`http://${await publicIpv4()}:${chiaConfig.data_layer.host_port}`
);
try {
const { DATALAYER_FILE_SERVER_URL } = getConfig().APP;
logger.info(`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;
}
};
101 changes: 0 additions & 101 deletions src/utils/ip-tools.js

This file was deleted.

Loading