From 01fbdc3c6f5ce85d408de43d806043eb72d4e16c Mon Sep 17 00:00:00 2001 From: Kithmini Gunawardhana Date: Wed, 29 Jun 2022 17:38:14 +0530 Subject: [PATCH] Retrying connecting host with extending delays. (#144) (#147) --- mb-xrpl/lib/message-board.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/mb-xrpl/lib/message-board.js b/mb-xrpl/lib/message-board.js index 1ef354c..3e7374f 100644 --- a/mb-xrpl/lib/message-board.js +++ b/mb-xrpl/lib/message-board.js @@ -47,8 +47,7 @@ class MessageBoard { await this.xrplApi.connect(); this.hostClient = new evernode.HostClient(this.cfg.xrpl.address, this.cfg.xrpl.secret); - await this.hostClient.connect(); - + await this.#connectHost(); // Get last heartbeat moment from the host info. const hostInfo = await this.hostClient.getRegistration(); if (!hostInfo) @@ -137,6 +136,34 @@ class MessageBoard { this.hostClient.on(evernode.HostEvents.ExtendLease, r => this.handleExtendLease(r)); } + // Connect the host and trying to reconnect in the event of account not found error. + // Account not found error can be because of a network reset. (Dev and test nets) + async #connectHost() { + let attempts = 0; + // eslint-disable-next-line no-constant-condition + while (true) { + try { + attempts++; + const ret = await this.hostClient.connect(); + if (ret) + break; + } catch (error) { + if (error?.data?.error === 'actNotFound') { + let delaySec; + // The maximum delay will be 5 minutes. + if (attempts > 150) { + delaySec = 300; + } else { + delaySec = 2 * attempts; + } + console.log(`Network reset detected. Attempt ${attempts} failed. Retrying in ${delaySec}s...`); + await new Promise(resolve => setTimeout(resolve, delaySec * 1000)); + } else + throw error; + } + } + } + async recreateLeaseOffer(nfTokenId, tenantAddress, leaseIndex) { // Burn the NFTs and recreate the offer and send back the lease amount back to the tenant. await this.hostClient.expireLease(nfTokenId, tenantAddress).catch(console.error);