From a267ba20db0eeb2082c287ca7b69e37d6f272eb9 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Mon, 8 Jul 2024 14:09:44 -0700 Subject: [PATCH] fixup! feat: chainHub retries --- packages/orchestration/src/exos/chain-hub.js | 111 +++++++++++-------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/packages/orchestration/src/exos/chain-hub.js b/packages/orchestration/src/exos/chain-hub.js index b6bf1ccd9f1..68a4d6e197f 100644 --- a/packages/orchestration/src/exos/chain-hub.js +++ b/packages/orchestration/src/exos/chain-hub.js @@ -103,6 +103,31 @@ export const makeChainHub = (agoricNames, vowTools) => { valueShape: IBCConnectionInfoShape, }); + // eslint-disable-next-line no-restricted-syntax -- TODO more exact rules for vow best practices + const lookupChainInfo = async chainName => { + await null; + try { + const chainInfo = await E(agoricNames).lookup(CHAIN_KEY, chainName); + chainInfos.init(chainName, chainInfo); + return chainInfo; + } catch (_) { + throw makeError(`chain not found:${chainName}`); + } + }; + + // eslint-disable-next-line no-restricted-syntax -- TODO more exact rules for vow best practices + const lookupConnectionInfo = async (chainId1, chainId2) => { + await null; + const key = connectionKey(chainId1, chainId2); + try { + const connectionInfo = await E(agoricNames).lookup(CONNECTIONS_KEY, key); + connectionInfos.init(key, connectionInfo); + return connectionInfo; + } catch (_) { + throw makeError(`connection not found: ${chainId1}<->${chainId2}`); + } + }; + const chainHub = zone.exo('ChainHub', ChainHubI, { /** * Register a new chain. The name will override a name in well known chain @@ -134,20 +159,10 @@ export const makeChainHub = (agoricNames, vowTools) => { const lookup = vowTools.retriable( zone, - 'getConnectionInfo', - // eslint-disable-next-line no-restricted-syntax -- TODO more exact rules for vow best practices - async () => { - await null; - try { - const chainInfo = await E(agoricNames).lookup(CHAIN_KEY, chainName); - chainInfos.init(chainName, chainInfo); - return chainInfo; - } catch (_) { - throw makeError(`chain not found:${chainName}`); - } - }, + 'lookupChainInfo', + lookupChainInfo, ); - return lookup(); + return lookup(chainName); }, /** * @param {string} chainId1 @@ -174,23 +189,10 @@ export const makeChainHub = (agoricNames, vowTools) => { const lookup = vowTools.retriable( zone, - 'getConnectionInfo', - // eslint-disable-next-line no-restricted-syntax -- TODO more exact rules for vow best practices - async () => { - await null; - try { - const connectionInfo = await E(agoricNames).lookup( - CONNECTIONS_KEY, - key, - ); - connectionInfos.init(key, connectionInfo); - return connectionInfo; - } catch (_) { - throw makeError(`connection not found: ${chainId1}<->${chainId2}`); - } - }, + 'lookupConnectionInfo', + lookupConnectionInfo, ); - return lookup(); + return lookup(chainId1, chainId2); }, /** @@ -205,29 +207,42 @@ export const makeChainHub = (agoricNames, vowTools) => { getChainsAndConnection(chainName1, chainName2) { const lookup = vowTools.retriable( zone, - 'getChainsAndConnection', - // eslint-disable-next-line no-restricted-syntax -- TODO more exact rules for vow best practices - async () => { - const [chain1, chain2] = await vowTools.asPromise( - vowTools.allVows([ - chainHub.getChainInfo(chainName1), - chainHub.getChainInfo(chainName2), - ]), - ); - const connectionInfo = await vowTools.asPromise( - chainHub.getConnectionInfo(chain2, chain1), - ); - return /** @type {[ActualChainInfo, ActualChainInfo, IBCConnectionInfo]} */ ([ - chain1, - chain2, - connectionInfo, - ]); - }, + 'lookupChainsAndConnection', + // eslint-disable-next-line no-use-before-define -- needs to reference this exo + lookupChainsAndConnection, ); - return lookup(); + // @ts-expect-error XXX generic parameter propagation + return lookup(chainName1, chainName2); }, }); + /** + * @template {string} C1 + * @template {string} C2 + * @param {C1} chainName1 + * @param {C2} chainName2 + * @returns {Promise< + * [ActualChainInfo, ActualChainInfo, IBCConnectionInfo] + * >} + */ + // eslint-disable-next-line no-restricted-syntax -- TODO more exact rules for vow best practices + const lookupChainsAndConnection = async (chainName1, chainName2) => { + const [chain1, chain2] = await vowTools.asPromise( + vowTools.allVows([ + chainHub.getChainInfo(chainName1), + chainHub.getChainInfo(chainName2), + ]), + ); + const connectionInfo = await vowTools.asPromise( + chainHub.getConnectionInfo(chain2, chain1), + ); + return /** @type {[ActualChainInfo, ActualChainInfo, IBCConnectionInfo]} */ ([ + chain1, + chain2, + connectionInfo, + ]); + }; + return chainHub; }; /** @typedef {ReturnType} ChainHub */