From dda357f07075c6bb157f6ce0c7550f3a29727a27 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Sun, 11 Sep 2022 15:57:34 -0400 Subject: [PATCH] fix: organization workflow fixes --- src/datalayer/persistance.js | 7 ++++++ src/datalayer/syncService.js | 10 ++++++++ src/datalayer/wallet.js | 23 ++++++++----------- .../organizations/organizations.model.js | 14 +++++++++++ 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index 34e6e069..3d3ffab1 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -35,6 +35,7 @@ const getBaseOptions = () => { method: 'POST', cert: fs.readFileSync(certFile), key: fs.readFileSync(keyFile), + timeout: 60000, }; return baseOptions; @@ -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({ @@ -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}`, @@ -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'); } }; @@ -190,6 +196,7 @@ const getStoreData = async (storeId, rootHash) => { } } + logger.info(`Unable to find store data for ${storeId}}`); return false; }; diff --git a/src/datalayer/syncService.js b/src/datalayer/syncService.js index 8c00ccc2..2bfa6a8f 100644 --- a/src/datalayer/syncService.js +++ b/src/datalayer/syncService.js @@ -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)) { @@ -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, @@ -358,4 +367,5 @@ export default { POLLING_INTERVAL, getCurrentStoreData, unsubscribeFromDataLayerStore, + waitForAllTransactionsToConfirm, }; diff --git a/src/datalayer/wallet.js b/src/datalayer/wallet.js index c552d1d7..d23328f7 100644 --- a/src/datalayer/wallet.js +++ b/src/datalayer/wallet.js @@ -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(); @@ -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; }; @@ -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; }; diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index fbe47c5a..6be95925 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -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, @@ -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, @@ -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, @@ -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 } });