@@ -57,6 +57,7 @@ import {
57
57
sleep ,
58
58
uploadFileToBucket
59
59
} from "../lib/utils"
60
+ import { EC2Client } from "@aws-sdk/client-ec2"
60
61
61
62
dotenv . config ( )
62
63
@@ -387,6 +388,34 @@ export const coordinateCeremonyParticipant = functionsV1
387
388
}
388
389
} )
389
390
391
+
392
+ /**
393
+ * Recursive function to check whether an EC2 is in a running state
394
+ * @notice required step to run commands
395
+ * @param ec2 <EC2Client> - the EC2Client object
396
+ * @param vmInstanceId <string> - the instance Id
397
+ * @param attempts <number> - how many times to retry before failing
398
+ * @returns <Promise<boolean>> - whether the VM was started
399
+ */
400
+ const checkIfVMRunning = async (
401
+ ec2 : EC2Client ,
402
+ vmInstanceId : string ,
403
+ attempts = 5
404
+ ) : Promise < boolean > => {
405
+ // if we tried 5 times, then throw an error
406
+ if ( attempts <= 0 ) logAndThrowError ( SPECIFIC_ERRORS . SE_VM_NOT_RUNNING )
407
+
408
+ await sleep ( 60000 ) ; // Wait for 1 min
409
+ const isVMRunning = await checkIfRunning ( ec2 , vmInstanceId )
410
+
411
+ if ( ! isVMRunning ) {
412
+ printLog ( `VM not running, ${ attempts - 1 } attempts remaining. Retrying in 1 minute...` , LogLevel . DEBUG )
413
+ return await checkIfVMRunning ( ec2 , vmInstanceId , attempts - 1 )
414
+ } else {
415
+ return true
416
+ }
417
+ }
418
+
390
419
/**
391
420
* Verify the contribution of a participant computed while contributing to a specific circuit of a ceremony.
392
421
* @dev a huge amount of resources (memory, CPU, and execution time) is required for the contribution verification task.
@@ -729,10 +758,10 @@ export const verifycontribution = functionsV2.https.onCall(
729
758
// Step (1.A.3.1).
730
759
await startEC2Instance ( ec2 , vmInstanceId )
731
760
732
- await sleep ( 180000 ) // nb. wait for VM startup (3 mins).
761
+ await sleep ( 60000 ) // nb. wait for VM startup (1 mins + retry ).
733
762
734
763
// Check if the startup is running.
735
- isVMRunning = await checkIfRunning ( ec2 , vmInstanceId )
764
+ isVMRunning = await checkIfVMRunning ( ec2 , vmInstanceId )
736
765
737
766
printLog ( `VM running: ${ isVMRunning } ` , LogLevel . DEBUG )
738
767
0 commit comments