diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index 6d5c3776..bd254874 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -62,6 +62,32 @@ const getMirrors = async (storeId) => { } }; +const clearPendingRoots = async (storeId) => { + const url = `${CONFIG.DATALAYER_URL}/clear_pending_roots`; + const { cert, key, timeout } = getBaseOptions(); + + try { + const response = await superagent + .post(url) + .key(key) + .cert(cert) + .timeout(timeout) + .send({ id: storeId }); + + const data = response.body; + + if (data.success) { + return data.mirrors; + } + + logger.error(`FAILED GETTING MIRRORS FOR ${storeId}`); + return []; + } catch (error) { + logger.error(error); + return []; + } +}; + const addMirror = async (storeId, url, forceAddMirror = false) => { await wallet.waitForAllTransactionsToConfirm(); const homeOrg = await Organization.getHomeOrg(); @@ -373,51 +399,62 @@ const getRoots = async (storeIds) => { }; const pushChangeListToDataLayer = async (storeId, changelist) => { - try { - await wallet.waitForAllTransactionsToConfirm(); + let attempts = 0; + const maxAttempts = 5; - const url = `${CONFIG.DATALAYER_URL}/batch_update`; - const { cert, key, timeout } = getBaseOptions(); + while (attempts < maxAttempts) { + try { + await wallet.waitForAllTransactionsToConfirm(); - const response = await superagent - .post(url) - .key(key) - .cert(cert) - .timeout(timeout) - .send({ - changelist, - id: storeId, - fee: _.get(CONFIG, 'DEFAULT_FEE', 300000000), - }); + const url = `${CONFIG.DATALAYER_URL}/batch_update`; + const { cert, key, timeout } = getBaseOptions(); - const data = response.body; + const response = await superagent + .post(url) + .key(key) + .cert(cert) + .timeout(timeout) + .send({ + changelist, + id: storeId, + fee: _.get(CONFIG, 'DEFAULT_FEE', 300000000), + }); - console.log(data); + const data = response.body; + console.log(data); - if (data.success) { - logger.info( - `Success!, Changes were submitted to the datalayer for storeId: ${storeId}`, - ); - return true; - } + if (data.success) { + logger.info( + `Success!, Changes were submitted to the datalayer for storeId: ${storeId}`, + ); + return true; + } - if (data.error.includes('Key already present')) { - logger.info( - `The datalayer key was already present, its possible your data was pushed to the datalayer but never broadcasted to the blockchain. This can create a mismatched state in your node.`, + if (data.error.includes('Key already present')) { + logger.info('Pending root detected, waiting 5 seconds and retrying'); + await clearPendingRoots(storeId); + attempts++; + await new Promise((resolve) => setTimeout(resolve, 5000)); + continue; // Retry + } + + logger.error( + `There was an error pushing your changes to the datalayer, ${JSON.stringify( + data, + )}`, ); - return true; + return false; + } catch (error) { + logger.error(error.message); + logger.info('There was an error pushing your changes to the datalayer'); + return false; } - - logger.error( - `There was an error pushing your changes to the datalayer, ${JSON.stringify( - data, - )}`, - ); - return false; - } catch (error) { - logger.error(error.message); - logger.info('There was an error pushing your changes to the datalayer'); } + + logger.error( + 'Maximum attempts reached. Unable to push changes to the datalayer.', + ); + return false; }; const createDataLayerStore = async () => { @@ -663,4 +700,5 @@ export { cancelOffer, verifyOffer, takeOffer, + clearPendingRoots, };