Skip to content

Commit

Permalink
feat(vms): refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Jun 22, 2023
1 parent f9a251a commit 08486b2
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 62 deletions.
10 changes: 2 additions & 8 deletions packages/actions/src/helpers/ec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ export const createSSMClient = async (): Promise<SSMClient> => {

/**
* Generate the command to be run by the EC2 instance
* @param r1csPath <string> path to r1cs file
* @param zKeyPath <string> path to zkey file
* @param ptauPath <string> path to ptau file
* @returns <string[]> array of commands to be run by the EC2 instance
*/
export const generateVMCommand = (
r1csPath: string,
zKeyPath: string,
ptauPath: string,
): string[] => {
Expand All @@ -91,13 +89,9 @@ export const generateVMCommand = (
"source ~/.bashrc",
"nvm install 16",
"nvm use 16",
"npm install -g yarn",
"npm install -g snarkjs",
`aws s3 cp s3://${r1csPath} /var/tmp/circuit.r1cs`,
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
`aws s3 cp s3://${ptauPath} /var/tmp/ptau.ptau`,
"npm install -g p0tion-api",
"p0tion-api /var/tmp/circuit.r1cs /var/tmp/genesisZkey.zkey /var/tmp/ptau.ptau",
`aws s3 cp s3://${zKeyPath} ./genesisZkey.zkey`,
`aws s3 cp s3://${ptauPath} ./pot.ptau`
]

return command
Expand Down
46 changes: 25 additions & 21 deletions packages/actions/test/unit/ec2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,31 @@ describe("VMs", () => {

})

// describe.skip("Setup a ceremony that uses two VMs", () => {
// Sample data for running the test.
// const users = [fakeUsersData.fakeUser1, fakeUsersData.fakeUser2]
// const passwords = generateUserPasswords(2)
describe("Setup a ceremony that uses two VMs", () => {
// // Sample data for running the test.
// const users = [fakeUsersData.fakeUser1, fakeUsersData.fakeUser2]
// const passwords = generateUserPasswords(2)

// // Initialize user and admin services.
// const { userApp, userFunctions, userFirestore } = initializeUserServices()
// const { adminFirestore, adminAuth } = initializeAdminServices()
// const userAuth = getAuth(userApp)
// // Initialize user and admin services.
// const { userApp, userFunctions, userFirestore } = initializeUserServices()
// const { adminFirestore, adminAuth } = initializeAdminServices()
// const userAuth = getAuth(userApp)

// beforeAll(async () => {
// // create 2 users the second is the coordinator
// for (let i = 0; i < 2; i++) {
// users[i].uid = await createMockUser(
// userApp,
// users[i].data.email,
// passwords[i],
// i === passwords.length - 1,
// adminAuth
// )
// }
// })
// })
// beforeAll(async () => {
// // create 2 users the second is the coordinator
// for (let i = 0; i < 2; i++) {
// users[i].uid = await createMockUser(
// userApp,
// users[i].data.email,
// passwords[i],
// i === passwords.length - 1,
// adminAuth
// )
// }
// })

// it("should create a ceremony and two VMs should spin up", async () => {

// })
})
})
3 changes: 2 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"docs": "typedoc src/**/*.ts --out ../../docs/backend"
},
"devDependencies": {
"@aws-sdk/client-ssm": "^3.357.0",
"@firebase/rules-unit-testing": "^2.0.7",
"@types/rollup-plugin-auto-external": "^2.0.2",
"@types/uuid": "^9.0.1",
Expand All @@ -63,7 +62,9 @@
},
"dependencies": {
"@adobe/node-fetch-retry": "^2.2.0",
"@aws-sdk/client-ec2": "^3.357.0",
"@aws-sdk/client-s3": "^3.329.0",
"@aws-sdk/client-ssm": "^3.357.0",
"@aws-sdk/middleware-endpoint": "^3.329.0",
"@aws-sdk/s3-request-presigner": "^3.329.0",
"@p0tion/actions": "^0.4.2",
Expand Down
1 change: 0 additions & 1 deletion packages/backend/src/functions/ceremony.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ export const setupCeremony = functions
const ec2Client = await createEC2Client()
// generate the commands for startup
const vmCommands = generateVMCommand(
circuit.files?.r1csStoragePath!,
circuit.files?.initialZkeyStoragePath!,
circuit.files?.potStoragePath!
)
Expand Down
39 changes: 11 additions & 28 deletions packages/backend/src/functions/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import admin from "firebase-admin"
import dotenv from "dotenv"
import { QueryDocumentSnapshot } from "firebase-functions/v1/firestore"
import { Change } from "firebase-functions"
import { zKey } from "snarkjs"
import fs from "fs"
import fetch from "@adobe/node-fetch-retry"
import { Timer } from "timer-node"
import { FieldValue } from "firebase-admin/firestore"
import {
Expand All @@ -33,6 +31,7 @@ import {
startEC2Instance,
stopEC2Instance,
runCommandOnEC2,
retrieveCommandOutput,
} from "@p0tion/actions"
import { FinalizeCircuitData, VerifyContributionData } from "../types/index"
import { LogLevel } from "../types/enums"
Expand Down Expand Up @@ -444,10 +443,8 @@ export const verifycontribution = functionsV2.https.onCall(
// check status
const status = await checkEC2Status(ec2Client, instanceId)
if (!status) {
console.log("Not running yet")
console.log("DEBUG Not running yet")
}
// // get ip
// const ip = await getEC2Ip(ec2Client, instanceId)

// Prepare timer. (@todo check where to move this)
const verificationTaskTimer = new Timer({ label: `${ceremonyId}-${circuitId}-${participantDoc.id}` })
Expand All @@ -460,40 +457,26 @@ export const verifycontribution = functionsV2.https.onCall(
printLog(`The contribution has been verified - Result ${isContributionValid}`, LogLevel.DEBUG)

const commands = [
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} .`,
`snarkjs zkey verify circuit.r1cs pot.ptau circuit_0003.zkey > verification_transcript.log`,
`aws s3 cp verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename}`
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} ./lastZKey.zkey`,
`snarkjs zkvi ./genesisZkey.zkey ./pot.ptau ./lastZKey.zkey | tee ./verification_transcript.log`,
`aws s3 cp ./verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename}`,
`rm lastZKey.zkey verification_transcript.log`
]

const ssmClient = await createSSMClient()
const commandId = await runCommandOnEC2(ssmClient, instanceId, commands)
await sleep(5000)

// call api
// const httpResponse = await fetch(`${ip}:5000/verification`, {
// method: "POST",
// body: JSON.stringify({
// "bucketName": bucketName,
// "objectKey": lastZkeyStoragePath
// })
// })

// if (httpResponse.status !== 200) throw new Error("Error while calling api")

// // wait how long it would usually take
// await sleep(5000000)

// // call api for result
// const httpResponseVerification = await fetch(`${ip}:5000/verification/${lastZkeyIndex}`, {"method": "GET"})

// isContributionValid = httpResponseVerification.status === 200 ? true : false

const commandOutput = await retrieveCommandOutput(ssmClient, commandId, instanceId)
if (commandOutput.includes("ZKey Ok!")) isContributionValid = true
console.log("dEBUG output", commandOutput)

// stop the VM
await stopEC2Instance(ec2Client, instanceId)

// Step (1.A.2).

// Compute contribution hash. (@todo compute on VM api)
// const lastZkeyBlake2bHash = httpResponseVerification.body

// Create a new contribution document.
const contributionDoc = await firestore
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "firebase-admin/firestore"
import admin from "firebase-admin"
import dotenv from "dotenv"
import { EC2Client } from "@aws-sdk/client-ec2"
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
import { createWriteStream } from "node:fs"
Expand All @@ -33,6 +32,7 @@ import os from "os"
import { COMMON_ERRORS, logAndThrowError, SPECIFIC_ERRORS } from "./errors"
import { getS3Client } from "./services"
import { SSMClient } from "@aws-sdk/client-ssm"
import { EC2Client } from "@aws-sdk/client-ec2/dist-types/EC2Client"

dotenv.config()

Expand Down Expand Up @@ -430,7 +430,7 @@ export const getEC2InstanceId = async (circuitId: string): Promise<string> => {

const circuitData = circuitDoc.data()

const { instanceId } = circuitData!
const { vmInstanceId } = circuitData!

return instanceId
return vmInstanceId
}
75 changes: 75 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,54 @@ __metadata:
languageName: node
linkType: hard

"@aws-sdk/client-ec2@npm:^3.357.0":
version: 3.357.0
resolution: "@aws-sdk/client-ec2@npm:3.357.0"
dependencies:
"@aws-crypto/sha256-browser": 3.0.0
"@aws-crypto/sha256-js": 3.0.0
"@aws-sdk/client-sts": 3.357.0
"@aws-sdk/config-resolver": 3.357.0
"@aws-sdk/credential-provider-node": 3.357.0
"@aws-sdk/fetch-http-handler": 3.357.0
"@aws-sdk/hash-node": 3.357.0
"@aws-sdk/invalid-dependency": 3.357.0
"@aws-sdk/middleware-content-length": 3.357.0
"@aws-sdk/middleware-endpoint": 3.357.0
"@aws-sdk/middleware-host-header": 3.357.0
"@aws-sdk/middleware-logger": 3.357.0
"@aws-sdk/middleware-recursion-detection": 3.357.0
"@aws-sdk/middleware-retry": 3.357.0
"@aws-sdk/middleware-sdk-ec2": 3.357.0
"@aws-sdk/middleware-serde": 3.357.0
"@aws-sdk/middleware-signing": 3.357.0
"@aws-sdk/middleware-stack": 3.357.0
"@aws-sdk/middleware-user-agent": 3.357.0
"@aws-sdk/node-config-provider": 3.357.0
"@aws-sdk/node-http-handler": 3.357.0
"@aws-sdk/smithy-client": 3.357.0
"@aws-sdk/types": 3.357.0
"@aws-sdk/url-parser": 3.357.0
"@aws-sdk/util-base64": 3.310.0
"@aws-sdk/util-body-length-browser": 3.310.0
"@aws-sdk/util-body-length-node": 3.310.0
"@aws-sdk/util-defaults-mode-browser": 3.357.0
"@aws-sdk/util-defaults-mode-node": 3.357.0
"@aws-sdk/util-endpoints": 3.357.0
"@aws-sdk/util-retry": 3.357.0
"@aws-sdk/util-user-agent-browser": 3.357.0
"@aws-sdk/util-user-agent-node": 3.357.0
"@aws-sdk/util-utf8": 3.310.0
"@aws-sdk/util-waiter": 3.357.0
"@smithy/protocol-http": ^1.0.1
"@smithy/types": ^1.0.0
fast-xml-parser: 4.2.4
tslib: ^2.5.0
uuid: ^8.3.2
checksum: 7e409d4987321b6b166cc4f6f96dc77a0dedd1d59e783cdb68a3db8f3ace9dc6c918db9b7936dadd22aaf61449f66477b15d9260f0f24f552bf558d8bf3f08dd
languageName: node
linkType: hard

"@aws-sdk/client-s3@npm:^3.329.0":
version: 3.329.0
resolution: "@aws-sdk/client-s3@npm:3.329.0"
Expand Down Expand Up @@ -1530,6 +1578,21 @@ __metadata:
languageName: node
linkType: hard

"@aws-sdk/middleware-sdk-ec2@npm:3.357.0":
version: 3.357.0
resolution: "@aws-sdk/middleware-sdk-ec2@npm:3.357.0"
dependencies:
"@aws-sdk/middleware-endpoint": 3.357.0
"@aws-sdk/protocol-http": 3.357.0
"@aws-sdk/signature-v4": 3.357.0
"@aws-sdk/smithy-client": 3.357.0
"@aws-sdk/types": 3.357.0
"@aws-sdk/util-format-url": 3.357.0
tslib: ^2.5.0
checksum: 21304aae291c6d4161a4ddaace7a7ed01516995d725f594f89996870995cd1a497856971a92703cf7651dfc3a98da77d5986c45f183e29e96bd1498b5f5005b5
languageName: node
linkType: hard

"@aws-sdk/middleware-sdk-s3@npm:3.329.0":
version: 3.329.0
resolution: "@aws-sdk/middleware-sdk-s3@npm:3.329.0"
Expand Down Expand Up @@ -2377,6 +2440,17 @@ __metadata:
languageName: node
linkType: hard

"@aws-sdk/util-format-url@npm:3.357.0":
version: 3.357.0
resolution: "@aws-sdk/util-format-url@npm:3.357.0"
dependencies:
"@aws-sdk/querystring-builder": 3.357.0
"@aws-sdk/types": 3.357.0
tslib: ^2.5.0
checksum: c9c880f396335ad5b1c87612be9c4ae3c99a2e7aa4ec218979082e7decfb53827d7b0250a1f361ccde496ce63c90fa909a8f6e35ccae00fbc386369ef76de175
languageName: node
linkType: hard

"@aws-sdk/util-hex-encoding@npm:3.310.0":
version: 3.310.0
resolution: "@aws-sdk/util-hex-encoding@npm:3.310.0"
Expand Down Expand Up @@ -6490,6 +6564,7 @@ __metadata:
resolution: "@p0tion/backend@workspace:packages/backend"
dependencies:
"@adobe/node-fetch-retry": ^2.2.0
"@aws-sdk/client-ec2": ^3.357.0
"@aws-sdk/client-s3": ^3.329.0
"@aws-sdk/client-ssm": ^3.357.0
"@aws-sdk/middleware-endpoint": ^3.329.0
Expand Down

0 comments on commit 08486b2

Please sign in to comment.