Skip to content

Commit

Permalink
Merge pull request #174 from Chia-Network/tweaks-to-sync-gov-on-first…
Browse files Browse the repository at this point in the history
…-start

fix: governance syncing on new install first start
  • Loading branch information
wwills2 authored Feb 3, 2025
2 parents c1d5597 + 19bd7c3 commit 19bcdd4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,20 @@ const cancelOffer = async (tradeId) => {
}
};

/**
* @typedef {Object} SyncStatus
* @property {number} generation - The current generation of the Merkle tree.
* @property {string} root_hash - The root hash of the Merkle tree.
* @property {number} target_generation - The target generation of the Merkle tree.
* @property {string} target_root_hash - The target root hash of the Merkle tree.
*/

/**
* Fetches the synchronization status for a given store.
*
* @param {string} storeId - The identifier of the store.
* @returns {Promise<{sync_status: SyncStatus} | boolean>} - A promise that resolves to an object containing the sync status or `false` if the status cannot be retrieved.
*/
const getSyncStatus = async (storeId) => {
const url = `${CONFIG().CHIA.DATALAYER_HOST}/get_sync_status`;
const { cert, key, timeout } = getBaseOptions();
Expand All @@ -821,7 +835,7 @@ const getSyncStatus = async (storeId) => {
const data = response.body;

logger.trace(
`the /get_sync_status RPC response for store ${storeId} is ${JSON.parse(data)}`,
`the /get_sync_status RPC response for store ${storeId} is ${JSON.stringify(data)}`,
);

// We just care that we got some response, not what the response is
Expand All @@ -832,7 +846,7 @@ const getSyncStatus = async (storeId) => {
return false;
} catch (error) {
logger.error(
`failed to get sync status for store ${storeId}. Error: ${error.message}`,
`failed to get sync status for store ${storeId}. Error: ${JSON.stringify(error.message)}`,
);
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions src/datalayer/syncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const unsubscribeFromDataLayerStoreWithRetry = async (
`failed to unsubscribe from store ${storeId} after ${maxRetries} attempts`,
);
}
await new Promise((resolve) => setTimeout(() => resolve, retryWaitMs));
await new Promise((resolve) =>
setTimeout(() => resolve(), retryWaitMs),
);
}
}
}
Expand Down Expand Up @@ -92,14 +94,14 @@ const getSubscribedStoreData = async (
if (waitForSync) {
let synced = false;
while (!synced) {
const syncStatus = dataLayer.getSyncStatus(storeId);
synced = isDlStoreSynced(syncStatus);
const syncStatus = await dataLayer.getSyncStatus(storeId);
synced = isDlStoreSynced(syncStatus?.sync_status);

if (!synced) {
logger.warn(
`datalayer has not fully synced subscribed store ${storeId}. waiting to return data until store is synced`,
);
await new Promise((resolve) => setTimeout(() => resolve, 10000));
await new Promise((resolve) => setTimeout(() => resolve(), 10000));
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/utils/datalayer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ export const getMirrorUrl = async () => {
}
};

/**
* @param syncStatus {SyncStatus}
* @returns {boolean}
*/
export const isDlStoreSynced = (syncStatus) => {
return syncStatus.generation === syncStatus.target_generation;
if (syncStatus?.generation && syncStatus?.target_generation) {
return syncStatus.generation === syncStatus.target_generation;
}

return false;
};

0 comments on commit 19bcdd4

Please sign in to comment.