From 32d6e8cc26eb0f8196dbe7ef054e233f21c6d89f Mon Sep 17 00:00:00 2001 From: akirapham Date: Wed, 24 Apr 2024 15:40:53 +0700 Subject: [PATCH] fix: add ability of retrying syncGlobalAccount for a few times if the process failed --- src/GlobalAccount.ts | 112 ++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/src/GlobalAccount.ts b/src/GlobalAccount.ts index 7d7c10f4..376fc8da 100644 --- a/src/GlobalAccount.ts +++ b/src/GlobalAccount.ts @@ -47,69 +47,73 @@ export const loadGlobalAccounts = async (): Promise => { } } -export const syncGlobalAccount = async (): Promise => { +export const syncGlobalAccount = async (retry = 5): Promise => { const filteredArchivers = State.activeArchivers.filter( (archiver) => archiver.publicKey !== config.ARCHIVER_PUBLIC_KEY ) - try { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const queryFn = async (node: Node): Promise => { - return await postJson( - `http://${node.ip}:${node.port}/get_globalaccountreport_archiver`, - Crypto.sign({}) - ) - } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const equalFn = (info1: any, info2: any): boolean => { - const cm1 = deepCopy(info1) - const cm2 = deepCopy(info2) - delete cm1.sign - delete cm2.sign - const equivalent = isDeepStrictEqual(cm1, cm2) - return equivalent - } - - const globalAccsResponse = await robustQuery(filteredArchivers, queryFn, equalFn, 3, true) - Logger.mainLogger.debug('syncGlobalAccount() - globalAccsResponse', globalAccsResponse) - if (!globalAccsResponse) { - Logger.mainLogger.warn('() - robustResponse is null') - throw new Error('() - robustResponse is null') - } - const { - value: { accounts }, - } = globalAccsResponse - for (const { id, hash, timestamp } of accounts) { - globalAccountsMap.set(id, { hash, timestamp }) - } - - if (globalAccountsMap.has(config.globalNetworkAccount)) { - const savedNetworkAccount = await AccountDB.queryAccountByAccountId(config.globalNetworkAccount) - if ( - savedNetworkAccount && - savedNetworkAccount.hash === globalAccountsMap.get(config.globalNetworkAccount).hash - ) { - setGlobalNetworkAccount(savedNetworkAccount) - return - } + while (retry > 0) { + try { // eslint-disable-next-line @typescript-eslint/no-explicit-any const queryFn = async (node: Node): Promise => { - return await getJson(`http://${node.ip}:${node.port}/get-network-account?hash=false`) + return await postJson( + `http://${node.ip}:${node.port}/get_globalaccountreport_archiver`, + Crypto.sign({}) + ) } - const networkAccResponse = await robustQuery(filteredArchivers, queryFn, equalFn, 3, true) - Logger.mainLogger.debug('syncGlobalAccount() - networkAccResponse', networkAccResponse) - if (!networkAccResponse) { - Logger.mainLogger.warn('get-network-account() - robustResponse is null') - throw new Error('get-network-account() - robustResponse is null') + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const equalFn = (info1: any, info2: any): boolean => { + const cm1 = deepCopy(info1) + const cm2 = deepCopy(info2) + delete cm1.sign + delete cm2.sign + const equivalent = isDeepStrictEqual(cm1, cm2) + return equivalent + } + + const globalAccsResponse = await robustQuery(filteredArchivers, queryFn, equalFn, 3, true) + Logger.mainLogger.debug('syncGlobalAccount() - globalAccsResponse', globalAccsResponse) + if (!globalAccsResponse) { + Logger.mainLogger.warn('() - robustResponse is null') + throw new Error('() - robustResponse is null') } const { - value: { networkAccount }, - } = networkAccResponse - if (networkAccount) { - setGlobalNetworkAccount(networkAccount) + value: { accounts }, + } = globalAccsResponse + for (const { id, hash, timestamp } of accounts) { + globalAccountsMap.set(id, { hash, timestamp }) + } + + if (globalAccountsMap.has(config.globalNetworkAccount)) { + const savedNetworkAccount = await AccountDB.queryAccountByAccountId(config.globalNetworkAccount) + if ( + savedNetworkAccount && + savedNetworkAccount.hash === globalAccountsMap.get(config.globalNetworkAccount).hash + ) { + setGlobalNetworkAccount(savedNetworkAccount) + return + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const queryFn = async (node: Node): Promise => { + return await getJson(`http://${node.ip}:${node.port}/get-network-account?hash=false`) + } + const networkAccResponse = await robustQuery(filteredArchivers, queryFn, equalFn, 3, true) + Logger.mainLogger.debug('syncGlobalAccount() - networkAccResponse', networkAccResponse) + if (!networkAccResponse) { + Logger.mainLogger.warn('get-network-account() - robustResponse is null') + throw new Error('get-network-account() - robustResponse is null') + } + const { + value: { networkAccount }, + } = networkAccResponse + if (networkAccount) { + setGlobalNetworkAccount(networkAccount) + } } + return + } catch (e) { + Logger.mainLogger.error('Error in syncGlobalAccount()', e) + retry-- } - } catch (e) { - Logger.mainLogger.error('Error in syncGlobalAccount()', e) } }