Skip to content

Commit

Permalink
fix: clear pending root if detected on push
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Nov 30, 2023
1 parent d2e6147 commit e280c9a
Showing 1 changed file with 74 additions and 36 deletions.
110 changes: 74 additions & 36 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -663,4 +700,5 @@ export {
cancelOffer,
verifyOffer,
takeOffer,
clearPendingRoots,
};

0 comments on commit e280c9a

Please sign in to comment.