Skip to content

Commit

Permalink
feat(contribute.ts): avoid duplicate invocation of compute step
Browse files Browse the repository at this point in the history
Adds guard to prevent race condition when conditions allow start of compute step

fix #268
  • Loading branch information
glamperd committed Mar 7, 2024
1 parent 67a4629 commit 4ab581e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions packages/phase2cli/src/commands/contribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ export const listenToCeremonyCircuitDocumentChanges = (
})
}

let contributionInProgress = false

/**
* Listen to current authenticated participant document changes.
* @dev this is the core business logic related to the execution of the contribute command.
Expand Down Expand Up @@ -711,6 +713,12 @@ export const listenToParticipantDocumentChanges = async (

// Scenario (3.B).
if (isCurrentContributor && hasResumableStep && startingOrResumingContribution) {
if (contributionInProgress) {
console.warn(
`\n${theme.symbols.warning} Received instruction to start/resume contribution but contribution is already in progress...[skipping]`
)
return
}
// Communicate resume / start of the contribution to participant.
await simpleLoader(
`${
Expand All @@ -720,18 +728,24 @@ export const listenToParticipantDocumentChanges = async (
3000
)

// Start / Resume the contribution for the participant.
await handleStartOrResumeContribution(
cloudFunctions,
firestoreDatabase,
ceremony,
circuit,
participant,
entropy,
providerUserId,
false, // not finalizing.
circuits.length
)
try {
contributionInProgress = true

// Start / Resume the contribution for the participant.
await handleStartOrResumeContribution(
cloudFunctions,
firestoreDatabase,
ceremony,
circuit,
participant,
entropy,
providerUserId,
false, // not finalizing.
circuits.length
)
} finally {
contributionInProgress = false
}
}
// Scenario (3.A).
else if (isWaitingForContribution)
Expand Down
Empty file modified packages/phase2cli/src/index.ts
100755 → 100644
Empty file.

0 comments on commit 4ab581e

Please sign in to comment.