From 912ee978b81680bb07feaa7ce9569356422812e1 Mon Sep 17 00:00:00 2001 From: hatim boufnichel Date: Thu, 25 Apr 2024 20:03:23 +0200 Subject: [PATCH] up --- src/tests/networkSetup.ts | 42 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/tests/networkSetup.ts b/src/tests/networkSetup.ts index 9f23f9f8..393b4ba4 100644 --- a/src/tests/networkSetup.ts +++ b/src/tests/networkSetup.ts @@ -8,26 +8,32 @@ export const setupNetwork = async () => { await core.InitAddress() await core.Mine(1) const lnd = new LND(settings.lndSettings, () => { }, () => { }, () => { }, () => { }) - await lnd.ConnectPeer({ pubkey: '03cc09d839293195eb10af7df99e5ba5bbac12c2526ec67d174dcbc84d9c97bae4', host: "bob:9735" }) - await lnd.ConnectPeer({ pubkey: '0232842d81b2423df97aa8a264f8c0811610a736af65afe2e145279f285625c1e4', host: "carol:9735" }) - await lnd.ConnectPeer({ pubkey: '027c50fde118af534ff27e59da722422d2f3e06505c31e94c1b40c112c48a83b1c', host: "dave:9735" }) - await new Promise(resolve => setTimeout(resolve, 500)) - for (let i = 0; i < 30; i++) { + await tryUntil(async i => { + await lnd.ConnectPeer({ pubkey: '03cc09d839293195eb10af7df99e5ba5bbac12c2526ec67d174dcbc84d9c97bae4', host: "bob:9735" }) + await lnd.ConnectPeer({ pubkey: '0232842d81b2423df97aa8a264f8c0811610a736af65afe2e145279f285625c1e4', host: "carol:9735" }) + await lnd.ConnectPeer({ pubkey: '027c50fde118af534ff27e59da722422d2f3e06505c31e94c1b40c112c48a83b1c', host: "dave:9735" }) + }, 15, 2000) + + await tryUntil(async i => { + const info = await lnd.GetInfo() + if (!info.syncedToChain) { + throw new Error("not synced to chain") + } + if (!info.syncedToGraph) { + //await lnd.ConnectPeer({}) + throw new Error("not synced to graph") + } + }, 15, 2000) +} + +const tryUntil = async (fn: (attempt: number) => Promise, maxTries: number, interval: number) => { + for (let i = 0; i < maxTries; i++) { try { - const info = await lnd.GetInfo() - if (!info.syncedToChain) { - throw new Error("not synced to chain") - } - if (!info.syncedToGraph) { - //await lnd.ConnectPeer({}) - throw new Error("not synced to graph") - } - return + return await fn(i) } catch (e) { - console.log("waiting for lnd to be ready", e) - console.log(await lnd.ListPeers()) - await new Promise(resolve => setTimeout(resolve, 1000)) + console.log("tryUntil error", e) + await new Promise(resolve => setTimeout(resolve, interval)) } } - throw new Error("lnd is not ready after 30 seconds") + throw new Error("tryUntil failed") } \ No newline at end of file