Skip to content

Commit

Permalink
Merge pull request #1243 from Chia-Network/add-datamodel-to-org-table
Browse files Browse the repository at this point in the history
Add datamodel to org table
  • Loading branch information
wwills2 authored Dec 23, 2024
2 parents 171500c + c393204 commit 5ff15a4
Show file tree
Hide file tree
Showing 15 changed files with 444 additions and 213 deletions.
11 changes: 6 additions & 5 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,15 @@ export const resyncOrganization = async (req, res) => {
try {
await assertIfReadOnlyMode();
await assertWalletIsSynced();
await assertHomeOrgExists();

const orgUid = req.body.orgUid;
transaction = await sequelize.transaction();

await Organization.update(
{ registryHash: '0' },
{ where: { orgUid: req?.body?.orgUid } },
);
const organization = await Organization.findOne({ where: { orgUid } });

await Organization.reconcileOrganization(organization);

await Organization.update({ registryHash: '0' }, { where: { orgUid } });

await Promise.all([
...Object.keys(ModelKeys).map(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export default {
up: async (queryInterface, Sequelize) => {
const tableInfo = await queryInterface.describeTable('organizations');

if (!tableInfo?.dataModelVersionStoreId) {
await queryInterface.addColumn(
'organizations',
'dataModelVersionStoreId',
{
type: Sequelize.STRING,
allowNull: true,
},
);
}

if (!tableInfo?.dataModelVersionStoreHash) {
await queryInterface.addColumn(
'organizations',
'dataModelVersionStoreHash',
{
type: Sequelize.STRING,
allowNull: true,
},
);
}
},

down: async (queryInterface) => {
const tableInfo = await queryInterface.describeTable('organizations');

if (tableInfo?.dataModelVersionStoreId) {
await queryInterface.removeColumn(
'organizations',
'dataModelVersionStoreId',
);
}

if (tableInfo?.dataModelVersionStoreHash) {
await queryInterface.removeColumn(
'organizations',
'dataModelVersionStoreHash',
);
}
},
};
5 changes: 5 additions & 0 deletions src/database/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import AddOrgMetadata from './20220831023546-add-org-metadata';
import OrgSyncStatus from './20231020201652-OrgSyncStatus';
import OrgSyncRemaining from './20231020214357-OrgSyncRemainingCount';
import AddGenerationIndexToAudit from './20231207142225-AddGenerationIndexToAudit';
import AddDataModelVersionStoreToOrganizationTable from './20241211153456-add-data-model-version-store-to-organization-table.js';

export const migrations = [
{
Expand Down Expand Up @@ -179,4 +180,8 @@ export const migrations = [
migration: AddGenerationIndexToAudit,
name: '20231207142225-AddGenerationIndexToAudit',
},
{
migration: AddDataModelVersionStoreToOrganizationTable,
name: '20241211153456-add-data-model-version-store-to-organization-table',
},
];
15 changes: 8 additions & 7 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ const getStoreData = async (storeId, rootHash) => {
return false;
};

const getRoot = async (storeId, ignoreEmptyStore = false) => {
const getRoot = async (storeId) => {
const url = `${CONFIG.DATALAYER_URL}/get_root`;
const { cert, key, timeout } = getBaseOptions();

Expand All @@ -394,21 +394,22 @@ const getRoot = async (storeId, ignoreEmptyStore = false) => {
.timeout(timeout)
.send({ id: storeId });

const { confirmed, hash } = response.body;
logger.debug(
`the current root data for store ${storeId} is ${JSON.stringify(response.body)}`,
);

if (confirmed && (ignoreEmptyStore || !hash.includes('0x00000000000'))) {
return response.body;
const { success, error, traceback } = response.body;

if (!success || error || traceback) {
throw new Error(`${error}, ${traceback}`);
}

return false;
return response.body;
} catch (error) {
logger.error(
`failed to get root for store ${storeId}. error: ${error.message}`,
`could not get root data for store ${storeId}. this could be due to the store being in the process of confirming. error: ${error.message}`,
);
return false;
return {};
}
};

Expand Down
8 changes: 5 additions & 3 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const getSubscribedStoreData = async (storeId) => {
logger.debug(
`syncService getSubscribedData() checking that data is available for ${storeId}.`,
);
const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId, true);
if (!storeExistAndIsConfirmed) {
const { confirmed } = await dataLayer.getRoot(storeId);
if (!confirmed) {
throw new Error(`Store not found in DataLayer: ${storeId}.`);
} else {
logger.debug(
Expand Down Expand Up @@ -192,7 +192,9 @@ const getStoreIfUpdated = async (storeId, lastRootHash, callback, onFail) => {
);
}
} catch (error) {
logger.error(error.message);
logger.error(
`getStoreIfUpdated() failed to get updated store data. Error: ${error.message}`,
);
onFail(error.message);
}
};
Expand Down
18 changes: 9 additions & 9 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const createDataLayerStore = async () => {
logger.info(
`Created storeId: ${storeId}, waiting for this to be confirmed on the blockchain.`,
);
await waitForStoreToBeConfirmed(storeId);
await waitForNewStoreToBeConfirmed(storeId);
await wallet.waitForAllTransactionsToConfirm();

// Default AUTO_MIRROR_EXTERNAL_STORES to true if it is null or undefined
Expand All @@ -42,23 +42,23 @@ const addMirror = async (storeId, url, force = false) => {
return dataLayer.addMirror(storeId, url, force);
};

const waitForStoreToBeConfirmed = async (storeId, retry = 0) => {
const waitForNewStoreToBeConfirmed = async (storeId, retry = 0) => {
if (retry > 120) {
throw new Error(
`Creating storeId: ${storeId} timed out. Its possible the transaction is stuck.`,
);
}

const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId);
const { confirmed } = await dataLayer.getRoot(storeId, true);

if (!storeExistAndIsConfirmed) {
if (!confirmed) {
logger.info(`Still waiting for ${storeId} to confirm`);
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 30000);
});
return waitForStoreToBeConfirmed(storeId, retry + 1);
return waitForNewStoreToBeConfirmed(storeId, retry + 1);
}
logger.info(`StoreId: ${storeId} has been confirmed. Congrats!`);
};
Expand Down Expand Up @@ -109,7 +109,7 @@ const retry = (storeId, changeList, failedCallback, retryAttempts) => {
logger.info(`Retrying pushing to store ${storeId}: ${retryAttempts}`);
if (retryAttempts >= 60) {
logger.info(
'Could not push changelist to datalayer after retrying 10 times',
'Could not push changelist to datalayer after retrying 60 times',
);
failedCallback();
return;
Expand Down Expand Up @@ -137,9 +137,9 @@ export const pushChangesWhenStoreIsAvailable = async (
const hasUnconfirmedTransactions =
await wallet.hasUnconfirmedTransactions();

const storeExistAndIsConfirmed = await dataLayer.getRoot(storeId);
const { confirmed } = await dataLayer.getRoot(storeId);

if (!hasUnconfirmedTransactions && storeExistAndIsConfirmed) {
if (!hasUnconfirmedTransactions && confirmed) {
logger.info(`pushing to datalayer ${storeId}`);

const success = await dataLayer.pushChangeListToDataLayer(
Expand All @@ -148,7 +148,7 @@ export const pushChangesWhenStoreIsAvailable = async (
);

if (!success) {
logger.info(
logger.error(
`RPC failed when pushing to store ${storeId}, attempting retry.`,
);
retry(storeId, changeList, failedCallback, retryAttempts);
Expand Down
Loading

0 comments on commit 5ff15a4

Please sign in to comment.