Skip to content

Commit

Permalink
fix: organization workflow fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Sep 11, 2022
1 parent b6b8ebb commit dda357f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const getBaseOptions = () => {
method: 'POST',
cert: fs.readFileSync(certFile),
key: fs.readFileSync(keyFile),
timeout: 60000,
};

return baseOptions;
Expand All @@ -59,6 +60,8 @@ const createDataLayerStore = async () => {

const pushChangeListToDataLayer = async (storeId, changelist) => {
try {
await wallet.waitForAllTransactionsToConfirm();

const options = {
url: `${CONFIG.DATALAYER_URL}/batch_update`,
body: JSON.stringify({
Expand All @@ -73,6 +76,8 @@ const pushChangeListToDataLayer = async (storeId, changelist) => {

const data = JSON.parse(response);

console.log(data);

if (data.success) {
logger.info(
`Success!, Changes were submitted to the datalayer for storeId: ${storeId}`,
Expand All @@ -94,6 +99,7 @@ const pushChangeListToDataLayer = async (storeId, changelist) => {
);
return false;
} catch (error) {
logger.error(error.message);
logger.info('There was an error pushing your changes to the datalayer');
}
};
Expand Down Expand Up @@ -190,6 +196,7 @@ const getStoreData = async (storeId, rootHash) => {
}
}

logger.info(`Unable to find store data for ${storeId}}`);
return false;
};

Expand Down
10 changes: 10 additions & 0 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ const getRootDiff = (storeId, root1, root2) => {
};

const getStoreData = async (storeId, callback, onFail, retry = 0) => {
logger.info(`Getting store data, retry: ${retry}`);
if (retry <= 10) {
const encodedData = await dataLayer.getStoreData(storeId);
if (_.isEmpty(encodedData?.keys_values)) {
Expand Down Expand Up @@ -344,6 +345,14 @@ export const getLocalStoreData = async (storeId) => {
return decodeDataLayerResponse(encodedData);
};

export const waitForAllTransactionsToConfirm = async () => {
if (USE_SIMULATOR) {
return true;
}

return dataLayer.waitForAllTransactionsToConfirm();
};

export default {
startDataLayerUpdatePolling,
syncDataLayerStoreToClimateWarehouse,
Expand All @@ -358,4 +367,5 @@ export default {
POLLING_INTERVAL,
getCurrentStoreData,
unsubscribeFromDataLayerStore,
waitForAllTransactionsToConfirm,
};
23 changes: 10 additions & 13 deletions src/datalayer/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getConfig } from '../utils/config-loader';
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;

const rpcUrl = getConfig().APP.WALLET_URL;
const USE_SIMULATOR = getConfig().APP.USE_SIMULATOR;

const getBaseOptions = () => {
const homeDir = os.homedir();
Expand Down Expand Up @@ -94,20 +95,17 @@ const getWalletBalance = async () => {
};

const waitForAllTransactionsToConfirm = async () => {
if (await hasUnconfirmedTransactions()) {
await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 15000);
});
if (USE_SIMULATOR) {
return true;
}

const unconfirmedTransactions = await hasUnconfirmedTransactions();
await new Promise((resolve) => setTimeout(() => resolve(), 15000));

if (unconfirmedTransactions) {
return waitForAllTransactionsToConfirm();
}

await new Promise((resolve) => {
setTimeout(() => {
resolve();
}, 15000);
});
return true;
};

Expand All @@ -130,11 +128,10 @@ const hasUnconfirmedTransactions = async () => {
data.transactions.filter((transaction) => !transaction.confirmed).length
}`,
);

return data.transactions.some((transaction) => !transaction.confirmed);
}

console.log(data);

return false;
};

Expand Down
14 changes: 14 additions & 0 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ class Organization extends Model {
]);
};

if (!USE_SIMULATOR) {
await new Promise((resolve) => setTimeout(() => resolve(), 30000));
await datalayer.waitForAllTransactionsToConfirm();
}

// sync the organization store
await datalayer.syncDataLayer(
newOrganizationId,
Expand All @@ -120,6 +125,11 @@ class Organization extends Model {
revertOrganizationIfFailed,
);

if (!USE_SIMULATOR) {
await new Promise((resolve) => setTimeout(() => resolve(), 30000));
await datalayer.waitForAllTransactionsToConfirm();
}

//sync the registry store
await datalayer.syncDataLayer(
newRegistryId,
Expand All @@ -129,6 +139,9 @@ class Organization extends Model {
revertOrganizationIfFailed,
);

await new Promise((resolve) => setTimeout(() => resolve(), 30000));
await datalayer.waitForAllTransactionsToConfirm();

await Promise.all([
Organization.create({
orgUid: newOrganizationId,
Expand Down Expand Up @@ -165,6 +178,7 @@ class Organization extends Model {

return newOrganizationId;
} catch (error) {
console.trace(error);
logger.error(error.message);
logger.info('Reverting Failed Organization');
await Organization.destroy({ where: { isHome: true } });
Expand Down

0 comments on commit dda357f

Please sign in to comment.