Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use endTime of ritual initiation for managing timeouts
Browse files Browse the repository at this point in the history
theref committed Jul 24, 2023

Verified

This commit was signed with the committer’s verified signature.
sunsided Markus Mayer
1 parent 6d7ae37 commit 4eb712b
Showing 2 changed files with 29 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/agents/coordinator.ts
Original file line number Diff line number Diff line change
@@ -97,6 +97,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,
27 changes: 20 additions & 7 deletions src/dkg.ts
Original file line number Diff line number Diff line change
@@ -122,15 +122,28 @@ 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}`);

0 comments on commit 4eb712b

Please sign in to comment.