Skip to content

Commit 08486b2

Browse files
committed
feat(vms): refactoring
1 parent f9a251a commit 08486b2

File tree

7 files changed

+118
-62
lines changed

7 files changed

+118
-62
lines changed

packages/actions/src/helpers/ec2.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,11 @@ export const createSSMClient = async (): Promise<SSMClient> => {
7373

7474
/**
7575
* Generate the command to be run by the EC2 instance
76-
* @param r1csPath <string> path to r1cs file
7776
* @param zKeyPath <string> path to zkey file
7877
* @param ptauPath <string> path to ptau file
7978
* @returns <string[]> array of commands to be run by the EC2 instance
8079
*/
8180
export const generateVMCommand = (
82-
r1csPath: string,
8381
zKeyPath: string,
8482
ptauPath: string,
8583
): string[] => {
@@ -91,13 +89,9 @@ export const generateVMCommand = (
9189
"source ~/.bashrc",
9290
"nvm install 16",
9391
"nvm use 16",
94-
"npm install -g yarn",
9592
"npm install -g snarkjs",
96-
`aws s3 cp s3://${r1csPath} /var/tmp/circuit.r1cs`,
97-
`aws s3 cp s3://${zKeyPath} /var/tmp/genesisZkey.zkey`,
98-
`aws s3 cp s3://${ptauPath} /var/tmp/ptau.ptau`,
99-
"npm install -g p0tion-api",
100-
"p0tion-api /var/tmp/circuit.r1cs /var/tmp/genesisZkey.zkey /var/tmp/ptau.ptau",
93+
`aws s3 cp s3://${zKeyPath} ./genesisZkey.zkey`,
94+
`aws s3 cp s3://${ptauPath} ./pot.ptau`
10195
]
10296

10397
return command

packages/actions/test/unit/ec2.test.ts

+25-21
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,31 @@ describe("VMs", () => {
115115

116116
})
117117

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

123-
// // Initialize user and admin services.
124-
// const { userApp, userFunctions, userFirestore } = initializeUserServices()
125-
// const { adminFirestore, adminAuth } = initializeAdminServices()
126-
// const userAuth = getAuth(userApp)
123+
// // Initialize user and admin services.
124+
// const { userApp, userFunctions, userFirestore } = initializeUserServices()
125+
// const { adminFirestore, adminAuth } = initializeAdminServices()
126+
// const userAuth = getAuth(userApp)
127127

128-
// beforeAll(async () => {
129-
// // create 2 users the second is the coordinator
130-
// for (let i = 0; i < 2; i++) {
131-
// users[i].uid = await createMockUser(
132-
// userApp,
133-
// users[i].data.email,
134-
// passwords[i],
135-
// i === passwords.length - 1,
136-
// adminAuth
137-
// )
138-
// }
139-
// })
140-
// })
128+
// beforeAll(async () => {
129+
// // create 2 users the second is the coordinator
130+
// for (let i = 0; i < 2; i++) {
131+
// users[i].uid = await createMockUser(
132+
// userApp,
133+
// users[i].data.email,
134+
// passwords[i],
135+
// i === passwords.length - 1,
136+
// adminAuth
137+
// )
138+
// }
139+
// })
140+
141+
// it("should create a ceremony and two VMs should spin up", async () => {
142+
143+
// })
144+
})
141145
})

packages/backend/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"docs": "typedoc src/**/*.ts --out ../../docs/backend"
5151
},
5252
"devDependencies": {
53-
"@aws-sdk/client-ssm": "^3.357.0",
5453
"@firebase/rules-unit-testing": "^2.0.7",
5554
"@types/rollup-plugin-auto-external": "^2.0.2",
5655
"@types/uuid": "^9.0.1",
@@ -63,7 +62,9 @@
6362
},
6463
"dependencies": {
6564
"@adobe/node-fetch-retry": "^2.2.0",
65+
"@aws-sdk/client-ec2": "^3.357.0",
6666
"@aws-sdk/client-s3": "^3.329.0",
67+
"@aws-sdk/client-ssm": "^3.357.0",
6768
"@aws-sdk/middleware-endpoint": "^3.329.0",
6869
"@aws-sdk/s3-request-presigner": "^3.329.0",
6970
"@p0tion/actions": "^0.4.2",

packages/backend/src/functions/ceremony.ts

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ export const setupCeremony = functions
133133
const ec2Client = await createEC2Client()
134134
// generate the commands for startup
135135
const vmCommands = generateVMCommand(
136-
circuit.files?.r1csStoragePath!,
137136
circuit.files?.initialZkeyStoragePath!,
138137
circuit.files?.potStoragePath!
139138
)

packages/backend/src/functions/circuit.ts

+11-28
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import admin from "firebase-admin"
44
import dotenv from "dotenv"
55
import { QueryDocumentSnapshot } from "firebase-functions/v1/firestore"
66
import { Change } from "firebase-functions"
7-
import { zKey } from "snarkjs"
87
import fs from "fs"
9-
import fetch from "@adobe/node-fetch-retry"
108
import { Timer } from "timer-node"
119
import { FieldValue } from "firebase-admin/firestore"
1210
import {
@@ -33,6 +31,7 @@ import {
3331
startEC2Instance,
3432
stopEC2Instance,
3533
runCommandOnEC2,
34+
retrieveCommandOutput,
3635
} from "@p0tion/actions"
3736
import { FinalizeCircuitData, VerifyContributionData } from "../types/index"
3837
import { LogLevel } from "../types/enums"
@@ -444,10 +443,8 @@ export const verifycontribution = functionsV2.https.onCall(
444443
// check status
445444
const status = await checkEC2Status(ec2Client, instanceId)
446445
if (!status) {
447-
console.log("Not running yet")
446+
console.log("DEBUG Not running yet")
448447
}
449-
// // get ip
450-
// const ip = await getEC2Ip(ec2Client, instanceId)
451448

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

462459
const commands = [
463-
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} .`,
464-
`snarkjs zkey verify circuit.r1cs pot.ptau circuit_0003.zkey > verification_transcript.log`,
465-
`aws s3 cp verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename}`
460+
`aws s3 cp s3://${bucketName}/${lastZkeyStoragePath} ./lastZKey.zkey`,
461+
`snarkjs zkvi ./genesisZkey.zkey ./pot.ptau ./lastZKey.zkey | tee ./verification_transcript.log`,
462+
`aws s3 cp ./verification_transcript.log s3://${bucketName}/${verificationTranscriptStoragePathAndFilename}`,
463+
`rm lastZKey.zkey verification_transcript.log`
466464
]
467465

468466
const ssmClient = await createSSMClient()
469467
const commandId = await runCommandOnEC2(ssmClient, instanceId, commands)
468+
await sleep(5000)
470469

471-
// call api
472-
// const httpResponse = await fetch(`${ip}:5000/verification`, {
473-
// method: "POST",
474-
// body: JSON.stringify({
475-
// "bucketName": bucketName,
476-
// "objectKey": lastZkeyStoragePath
477-
// })
478-
// })
479-
480-
// if (httpResponse.status !== 200) throw new Error("Error while calling api")
481-
482-
// // wait how long it would usually take
483-
// await sleep(5000000)
484-
485-
// // call api for result
486-
// const httpResponseVerification = await fetch(`${ip}:5000/verification/${lastZkeyIndex}`, {"method": "GET"})
487-
488-
// isContributionValid = httpResponseVerification.status === 200 ? true : false
489-
470+
const commandOutput = await retrieveCommandOutput(ssmClient, commandId, instanceId)
471+
if (commandOutput.includes("ZKey Ok!")) isContributionValid = true
472+
console.log("dEBUG output", commandOutput)
473+
490474
// stop the VM
491475
await stopEC2Instance(ec2Client, instanceId)
492476

493477
// Step (1.A.2).
494478

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

498481
// Create a new contribution document.
499482
const contributionDoc = await firestore

packages/backend/src/lib/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from "firebase-admin/firestore"
99
import admin from "firebase-admin"
1010
import dotenv from "dotenv"
11-
import { EC2Client } from "@aws-sdk/client-ec2"
1211
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"
1312
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
1413
import { createWriteStream } from "node:fs"
@@ -33,6 +32,7 @@ import os from "os"
3332
import { COMMON_ERRORS, logAndThrowError, SPECIFIC_ERRORS } from "./errors"
3433
import { getS3Client } from "./services"
3534
import { SSMClient } from "@aws-sdk/client-ssm"
35+
import { EC2Client } from "@aws-sdk/client-ec2/dist-types/EC2Client"
3636

3737
dotenv.config()
3838

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

431431
const circuitData = circuitDoc.data()
432432

433-
const { instanceId } = circuitData!
433+
const { vmInstanceId } = circuitData!
434434

435-
return instanceId
435+
return vmInstanceId
436436
}

yarn.lock

+75
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,54 @@ __metadata:
217217
languageName: node
218218
linkType: hard
219219

220+
"@aws-sdk/client-ec2@npm:^3.357.0":
221+
version: 3.357.0
222+
resolution: "@aws-sdk/client-ec2@npm:3.357.0"
223+
dependencies:
224+
"@aws-crypto/sha256-browser": 3.0.0
225+
"@aws-crypto/sha256-js": 3.0.0
226+
"@aws-sdk/client-sts": 3.357.0
227+
"@aws-sdk/config-resolver": 3.357.0
228+
"@aws-sdk/credential-provider-node": 3.357.0
229+
"@aws-sdk/fetch-http-handler": 3.357.0
230+
"@aws-sdk/hash-node": 3.357.0
231+
"@aws-sdk/invalid-dependency": 3.357.0
232+
"@aws-sdk/middleware-content-length": 3.357.0
233+
"@aws-sdk/middleware-endpoint": 3.357.0
234+
"@aws-sdk/middleware-host-header": 3.357.0
235+
"@aws-sdk/middleware-logger": 3.357.0
236+
"@aws-sdk/middleware-recursion-detection": 3.357.0
237+
"@aws-sdk/middleware-retry": 3.357.0
238+
"@aws-sdk/middleware-sdk-ec2": 3.357.0
239+
"@aws-sdk/middleware-serde": 3.357.0
240+
"@aws-sdk/middleware-signing": 3.357.0
241+
"@aws-sdk/middleware-stack": 3.357.0
242+
"@aws-sdk/middleware-user-agent": 3.357.0
243+
"@aws-sdk/node-config-provider": 3.357.0
244+
"@aws-sdk/node-http-handler": 3.357.0
245+
"@aws-sdk/smithy-client": 3.357.0
246+
"@aws-sdk/types": 3.357.0
247+
"@aws-sdk/url-parser": 3.357.0
248+
"@aws-sdk/util-base64": 3.310.0
249+
"@aws-sdk/util-body-length-browser": 3.310.0
250+
"@aws-sdk/util-body-length-node": 3.310.0
251+
"@aws-sdk/util-defaults-mode-browser": 3.357.0
252+
"@aws-sdk/util-defaults-mode-node": 3.357.0
253+
"@aws-sdk/util-endpoints": 3.357.0
254+
"@aws-sdk/util-retry": 3.357.0
255+
"@aws-sdk/util-user-agent-browser": 3.357.0
256+
"@aws-sdk/util-user-agent-node": 3.357.0
257+
"@aws-sdk/util-utf8": 3.310.0
258+
"@aws-sdk/util-waiter": 3.357.0
259+
"@smithy/protocol-http": ^1.0.1
260+
"@smithy/types": ^1.0.0
261+
fast-xml-parser: 4.2.4
262+
tslib: ^2.5.0
263+
uuid: ^8.3.2
264+
checksum: 7e409d4987321b6b166cc4f6f96dc77a0dedd1d59e783cdb68a3db8f3ace9dc6c918db9b7936dadd22aaf61449f66477b15d9260f0f24f552bf558d8bf3f08dd
265+
languageName: node
266+
linkType: hard
267+
220268
"@aws-sdk/client-s3@npm:^3.329.0":
221269
version: 3.329.0
222270
resolution: "@aws-sdk/client-s3@npm:3.329.0"
@@ -1530,6 +1578,21 @@ __metadata:
15301578
languageName: node
15311579
linkType: hard
15321580

1581+
"@aws-sdk/middleware-sdk-ec2@npm:3.357.0":
1582+
version: 3.357.0
1583+
resolution: "@aws-sdk/middleware-sdk-ec2@npm:3.357.0"
1584+
dependencies:
1585+
"@aws-sdk/middleware-endpoint": 3.357.0
1586+
"@aws-sdk/protocol-http": 3.357.0
1587+
"@aws-sdk/signature-v4": 3.357.0
1588+
"@aws-sdk/smithy-client": 3.357.0
1589+
"@aws-sdk/types": 3.357.0
1590+
"@aws-sdk/util-format-url": 3.357.0
1591+
tslib: ^2.5.0
1592+
checksum: 21304aae291c6d4161a4ddaace7a7ed01516995d725f594f89996870995cd1a497856971a92703cf7651dfc3a98da77d5986c45f183e29e96bd1498b5f5005b5
1593+
languageName: node
1594+
linkType: hard
1595+
15331596
"@aws-sdk/middleware-sdk-s3@npm:3.329.0":
15341597
version: 3.329.0
15351598
resolution: "@aws-sdk/middleware-sdk-s3@npm:3.329.0"
@@ -2377,6 +2440,17 @@ __metadata:
23772440
languageName: node
23782441
linkType: hard
23792442

2443+
"@aws-sdk/util-format-url@npm:3.357.0":
2444+
version: 3.357.0
2445+
resolution: "@aws-sdk/util-format-url@npm:3.357.0"
2446+
dependencies:
2447+
"@aws-sdk/querystring-builder": 3.357.0
2448+
"@aws-sdk/types": 3.357.0
2449+
tslib: ^2.5.0
2450+
checksum: c9c880f396335ad5b1c87612be9c4ae3c99a2e7aa4ec218979082e7decfb53827d7b0250a1f361ccde496ce63c90fa909a8f6e35ccae00fbc386369ef76de175
2451+
languageName: node
2452+
linkType: hard
2453+
23802454
"@aws-sdk/util-hex-encoding@npm:3.310.0":
23812455
version: 3.310.0
23822456
resolution: "@aws-sdk/util-hex-encoding@npm:3.310.0"
@@ -6490,6 +6564,7 @@ __metadata:
64906564
resolution: "@p0tion/backend@workspace:packages/backend"
64916565
dependencies:
64926566
"@adobe/node-fetch-retry": ^2.2.0
6567+
"@aws-sdk/client-ec2": ^3.357.0
64936568
"@aws-sdk/client-s3": ^3.329.0
64946569
"@aws-sdk/client-ssm": ^3.357.0
64956570
"@aws-sdk/middleware-endpoint": ^3.329.0

0 commit comments

Comments
 (0)