From 6d7ae37257b2c3877a2fdb18accef19027d96d93 Mon Sep 17 00:00:00 2001 From: James Campbell Date: Wed, 19 Jul 2023 12:24:29 +0200 Subject: [PATCH] Add timeout buffer, catch timeout errors, check the ritual state, and return a new error --- src/dkg.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/dkg.ts b/src/dkg.ts index 7c672bd9b..70cce3823 100644 --- a/src/dkg.ts +++ b/src/dkg.ts @@ -123,17 +123,24 @@ export class DkgClient { if (waitUntilEnd) { const timeout = await DkgCoordinatorAgent.getTimeout(web3Provider); - const isSuccessful = await Promise.race([ - DkgClient.waitUntilRitualEnd(web3Provider, ritualId), - new Promise((_, reject) => - setTimeout(() => reject(new Error('Ritual initialization timed out')), timeout) - ), - ]); - if (!isSuccessful) { + const bufferedTimeout = timeout * 1.1; + try { + const isSuccessful = await Promise.race([ + DkgClient.waitUntilRitualEnd(web3Provider, ritualId), + new Promise((_, reject) => + setTimeout(() => reject(new Error('Ritual initialization timed out')), bufferedTimeout) + ), + ]); + + if (!isSuccessful) { + throw new Error(`Ritual initialization failed. Ritual id ${ritualId}`); + } + } catch (error) { const ritualState = await DkgCoordinatorAgent.getRitualState( web3Provider, ritualId ); + throw new Error( `Ritual initialization failed. Ritual id ${ritualId} is in state ${ritualState}` );