Skip to content

Commit

Permalink
Use endTime of ritual initiation for managing timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
theref committed Jul 24, 2023
1 parent 6d7ae37 commit 16903d7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
14 changes: 11 additions & 3 deletions src/agents/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ export class DkgCoordinatorAgent {

public static async getTimeout(
provider: ethers.providers.Web3Provider
): Promise<number> {
): Promise<number> {
const Coordinator = await this.connectReadOnly(provider);
const timeout = await Coordinator.timeout();
return timeout;
}

}

public static async getRitualState(
provider: ethers.providers.Web3Provider,
Expand All @@ -97,6 +96,15 @@ export class DkgCoordinatorAgent {
return await Coordinator.getRitualState(ritualId);
}

public static async getRitualInitTime(
provider: ethers.providers.Web3Provider,
ritualId: number
): Promise<number> {
const Coordinator = await this.connectReadOnly(provider);
const ritual = await Coordinator.rituals(ritualId);
return ritual[2];
}

public static async onRitualEndEvent(
provider: ethers.providers.Web3Provider,
ritualId: number,
Expand Down
40 changes: 30 additions & 10 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,45 @@ export class DkgClient {
);

if (waitUntilEnd) {
const initTimestamp = await DkgCoordinatorAgent.getRitualInitTime(
web3Provider,
ritualId
);
const timeout = await DkgCoordinatorAgent.getTimeout(web3Provider);
const bufferedTimeout = timeout * 1.1;
const endTime = initTimestamp + timeout;

// Wait until the current time is past the endTime
while (Math.floor(Date.now() / 1000) < endTime) {
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before checking again
}

// Wait until current block time is also past the endTime
let currentBlockTime;
do {
const block = await web3Provider.getBlock('latest');
currentBlockTime = block.timestamp;
if (currentBlockTime < endTime) {
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second before checking again
}
} while (currentBlockTime < endTime);

try {
const isSuccessful = await Promise.race([
DkgClient.waitUntilRitualEnd(web3Provider, ritualId),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Ritual initialization timed out')), bufferedTimeout)
),
]);

const isSuccessful = await DkgClient.waitUntilRitualEnd(
web3Provider,
ritualId
);

if (!isSuccessful) {
throw new Error(`Ritual initialization failed. Ritual id ${ritualId}`);
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}`
);
Expand Down

0 comments on commit 16903d7

Please sign in to comment.