From 9aec4e7188653acb4bdce5f17f5a161918ee768d Mon Sep 17 00:00:00 2001 From: ctrlc03 <93448202+ctrlc03@users.noreply.github.com> Date: Wed, 28 Jun 2023 09:19:12 +0100 Subject: [PATCH] fix(vms): fixed wrong path in blake3 bin command and various fixes on the verification CF --- packages/actions/src/helpers/vm.ts | 9 +--- packages/backend/src/functions/circuit.ts | 62 +++++++++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/packages/actions/src/helpers/vm.ts b/packages/actions/src/helpers/vm.ts index 67ec8603..9a68a6ac 100644 --- a/packages/actions/src/helpers/vm.ts +++ b/packages/actions/src/helpers/vm.ts @@ -102,7 +102,7 @@ export const vmContributionVerificationCommand = ( `aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} /var/tmp/lastZKey.zkey &>/dev/null`, `snarkjs zkvi /var/tmp/genesisZkey.zkey /var/tmp/pot.ptau /var/tmp/lastZKey.zkey > /var/tmp/verification_transcript.log`, `aws s3 cp /var/tmp/verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename} &>/dev/null`, - `./var/tmp/blake3.bin /var/tmp/verification_transcript.log | awk '{print $1}'`, + `/var/tmp/blake3.bin /var/tmp/verification_transcript.log | awk '{print $1}'`, `rm /var/tmp/lastZKey.zkey /var/tmp/verification_transcript.log &>/dev/null` ] @@ -332,6 +332,7 @@ export const retrieveCommandOutput = async (ssm: SSMClient, instanceId: string, try { // Run the command. const response = await ssm.send(command) + console.log("DEBUG", response) if (response.$metadata.httpStatusCode !== 200) throw new Error( @@ -363,12 +364,6 @@ export const retrieveCommandStatus = async (ssm: SSMClient, instanceId: string, try { // Run the command. const response = await ssm.send(command) - - if (response.$metadata.httpStatusCode !== 200) - throw new Error( - `Something went wrong when trying to retrieve the command ${commandId} status on the EC2 instance (${instanceId}). More details ${response}` - ) - return response.Status! } catch (error: any) { throw new Error( diff --git a/packages/backend/src/functions/circuit.ts b/packages/backend/src/functions/circuit.ts index f956b6fa..099378c6 100644 --- a/packages/backend/src/functions/circuit.ts +++ b/packages/backend/src/functions/circuit.ts @@ -54,7 +54,8 @@ import { getDocumentById, getFinalContribution, sleep, - uploadFileToBucket + uploadFileToBucket, + uploadFileToBucketNoFile } from "../lib/utils" dotenv.config() @@ -485,14 +486,20 @@ export const verifycontribution = functionsV2.https.onCall( // @todo check sleep await sleep(1000) - + console.log("COMMAND SENT DEBUG") + let success: boolean = false // Wait until the command completes with a success status. const interval = setInterval(async () => { + printLog("I started the intrarval function", LogLevel.DEBUG) try { const cmdStatus = await retrieveCommandStatus(ssm, commandId, vmInstanceId) printLog("CMD STATUS" + cmdStatus, LogLevel.DEBUG) // TODO: make an enum. - if (cmdStatus === "Success") clearInterval(interval) + if (cmdStatus === "Success") { + printLog("DEBUG SUCCESS command", LogLevel.DEBUG) + success = true + clearInterval(interval) + } else if (cmdStatus === "Failed" || cmdStatus === "AccessDenied") // Refactoring error. logAndThrowError(makeError("aborted", `Invalid command execution ${cmdStatus}`)) @@ -502,13 +509,26 @@ export const verifycontribution = functionsV2.https.onCall( } }, 60000) - // TODO To be deleted - // // Retrieve the command output. - // const commandOutput = await retrieveCommandOutput(ssm, commandId, vmInstanceId) - - // // Check contribution validity. - // if (commandOutput.includes("ZKey Ok!")) isContributionValid = true - // else isContributionValid = false + printLog("EXITED INTERVAL", LogLevel.DEBUG) + printLog("Success " + success, LogLevel.DEBUG) + // if the command was successful we need to check whether the zKey is valid or not + if (success) { + // download verification transcript which would have been uploaded to S3 by the VM + verificationTranscriptTemporaryLocalPath = createTemporaryLocalPath(verificationTranscriptCompleteFilename) + printLog("DOWNLOADING ARTIFACT", LogLevel.DEBUG) + await downloadArtifactFromS3Bucket(bucketName, verificationTranscriptStoragePathAndFilename, verificationTranscriptCompleteFilename) + // read the transcript and check if it contains the string "ZKey Ok!" + const content = fs.readFileSync(verificationTranscriptTemporaryLocalPath, "utf-8") + if (content.includes("ZKey Ok!")) isContributionValid = true + printLog("is valid " + isContributionValid, LogLevel.DEBUG) + + // if the contribution is valid then format the transcript and upload save it again to disk + if (isContributionValid) { + const updated = content.replace(/\x1b\[[0-9;]*m/g, ""); + fs.writeFileSync(verificationTranscriptTemporaryLocalPath, updated) + } + + } // Stop the VM. // await stopEC2Instance(ec2, vmInstanceId) @@ -591,24 +611,22 @@ export const verifycontribution = functionsV2.https.onCall( true ) } else { - // Download verification transcript file from S3. - verificationTranscriptTemporaryLocalPath = createTemporaryLocalPath( - verificationTranscriptCompleteFilename - ) - await downloadArtifactFromS3Bucket( - bucketName, - verificationTranscriptStoragePathAndFilename, - verificationTranscriptTemporaryLocalPath - ) - // Retrieve the contribution hash from the command output. lastZkeyBlake2bHash = await retrieveCommandOutput(await createSSMClient(), commandId, vmInstanceId) + // re upload the formatted verification transcript + await uploadFileToBucket( + bucketName, + verificationTranscriptStoragePathAndFilename, + verificationTranscriptTemporaryLocalPath, + true + ) + fs.unlinkSync(verificationTranscriptTemporaryLocalPath) } // Compute verification transcript hash. transcriptBlake2bHash = await blake512FromPath(verificationTranscriptTemporaryLocalPath) - // Free resources by unlinking transcript temporary folder. + // Free resources by unlinking transcript temporary file. fs.unlinkSync(verificationTranscriptTemporaryLocalPath) // Filter participant contributions to find the data related to the one verified. @@ -636,7 +654,7 @@ export const verifycontribution = functionsV2.https.onCall( transcriptStoragePath: verificationTranscriptStoragePathAndFilename, lastZkeyStoragePath, transcriptBlake2bHash, - lastZkeyBlake2bHash // @todo we need the hash of the last zkey + lastZkeyBlake2bHash }, verificationSoftware: { name: String(process.env.CUSTOM_CONTRIBUTION_VERIFICATION_SOFTWARE_NAME),