From 3cfeae2c460a5cb978e49ee21ec3263b4291f3b1 Mon Sep 17 00:00:00 2001 From: DarshanAgarwal09 Date: Fri, 22 Dec 2023 16:53:04 +0530 Subject: [PATCH 01/13] docker setup --- .dockerignore | 34 ++++++++++++++ DA/package.json | 3 +- DA/server.ts | 19 +++++++- Dockerfile | 67 +++++++++++++++++++++++++++ circuits/package.json | 3 +- circuits/scripts/dev-virtual.sh | 2 +- circuits/scripts/dev.sh | 2 +- client/client.ts | 4 +- compose.yaml | 75 +++++++++++++++++++++++++++++++ docker_scripts/client_setup.sh | 7 +++ docker_scripts/contracts_run.sh | 7 +++ docker_scripts/contracts_setup.sh | 17 +++++++ docker_scripts/da_setup.sh | 4 ++ docker_scripts/enclave_setup.sh | 9 ++++ enclave/server.ts | 4 +- 15 files changed, 247 insertions(+), 10 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml create mode 100644 docker_scripts/client_setup.sh create mode 100644 docker_scripts/contracts_run.sh create mode 100644 docker_scripts/contracts_setup.sh create mode 100644 docker_scripts/da_setup.sh create mode 100644 docker_scripts/enclave_setup.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d6ccf29 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +**/.classpath +**/.dockerignore +**/.git +**/.gitmodules +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/build +**/dist +LICENSE +README.md diff --git a/DA/package.json b/DA/package.json index dc5fced..830a0d5 100644 --- a/DA/package.json +++ b/DA/package.json @@ -14,6 +14,7 @@ "dotenv": "^16.3.1", "pg": "^8.11.3", "socket.io-client": "^4.7.2", - "tsx": "^3.14.0" + "tsx": "^3.14.0", + "pm2": "^5.3.0" } } diff --git a/DA/server.ts b/DA/server.ts index 1cd6d49..23c574a 100644 --- a/DA/server.ts +++ b/DA/server.ts @@ -8,7 +8,7 @@ import { ServerToClientEvents, ClientToServerEvents } from "../client/socket"; * Using Socket.IO to manage communication with enclave. */ const socket: Socket = io( - `http://localhost:${process.env.ENCLAVE_SERVER_PORT}` + `${process.env.ENCLAVE_ADDRESS}:${process.env.ENCLAVE_SERVER_PORT}` ); /* @@ -23,6 +23,7 @@ const pool = new Pool(); * clear old data and wait for enclave to submit new tiles. */ async function handshakeDAResponse(inRecoveryMode: boolean) { + await create_encrypted_tiles() if (inRecoveryMode) { // Print table count const client = await pool.connect(); @@ -107,7 +108,21 @@ async function saveToDatabase(encTile: any) { socket.emit("saveToDatabaseResponse"); } - +async function create_encrypted_tiles(){ + const client = await pool.connect(); + const tableName = 'encrypted_tiles'; + const columns = `symbol text, address text, ciphertext text, iv text, tag text`; + + const query = `CREATE TABLE IF NOT EXISTS ${tableName} (${columns});`; + await client.query(query, (err, res) => { + if (err) { + console.error('Error creating table:', err); + } else { + console.log('Table created successfully'); + } + }); + client.release(); +} /* * Clears the table of past encrypted tiles. */ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f5a9713 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,67 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/engine/reference/builder/ + +ARG NODE_VERSION=21.3.0 +ARG PNPM_VERSION=8.12.0 + +FROM node as contracts + +RUN apt-get update + +RUN npm install -g pnpm@${PNPM_VERSION} + +RUN apt-get install -y build-essential cmake libgmp-dev libsodium-dev nasm curl m4 + + +WORKDIR /usr/src/app +COPY . . +RUN curl -L https://foundry.paradigm.xyz | bash +RUN sh docker_scripts/contracts_setup.sh + +EXPOSE 8545 + +RUN ["chmod", "+x", "/usr/src/app/docker_scripts/contracts_run.sh"] +CMD ["/usr/src/app/docker_scripts/contracts_run.sh"] + + +FROM node as enclave + +RUN npm install -g pnpm@${PNPM_VERSION} + +WORKDIR /usr/src/app +COPY --from=contracts /usr/src/app /usr/src/app + +RUN sh docker_scripts/enclave_setup.sh + +EXPOSE 3000 + +CMD ["pnpm", "-C", "enclave", "dev"] + + +FROM node as DA +RUN npm install -g pnpm@${PNPM_VERSION} + +WORKDIR /usr/src/app + +COPY --from=contracts /usr/src/app /usr/src/app +RUN sh docker_scripts/da_setup.sh + +# Run the application. +CMD ["pnpm", "-C", "DA", "dev"] + + +FROM node as client +RUN npm install -g pnpm@${PNPM_VERSION} + +WORKDIR /usr/src/app +# COPY . . +COPY --from=contracts /usr/src/app /usr/src/app +RUN sh docker_scripts/client_setup.sh + +# Run the application. +#CMD ["pnpm", "-C", "client", "devA"] +CMD ["sleep", "3600"] + diff --git a/circuits/package.json b/circuits/package.json index 8814798..871f62d 100644 --- a/circuits/package.json +++ b/circuits/package.json @@ -6,11 +6,12 @@ "scripts": { "dev:move": "bash scripts/dev.sh move artifacts/powersOfTau28_hez_final_17.ptau", "dev:spawn": "bash scripts/dev.sh spawn artifacts/powersOfTau28_hez_final_17.ptau", - "dev:virtual": "bash scripts/dev-virtual.sh arm64 artifacts/powersOfTau28_hez_final_17.ptau", + "dev:virtual": "bash scripts/dev-virtual.sh x86_64 artifacts/powersOfTau28_hez_final_17.ptau", "dev": "yarn dev:move && yarn dev:spawn && yarn dev:virtual", "test": "mocha -r ts-node/register 'test/**/*.ts'" }, "dependencies": { + "circom2": "^0.2.16", "circomlib": "^2.0.5" }, "devDependencies": { diff --git a/circuits/scripts/dev-virtual.sh b/circuits/scripts/dev-virtual.sh index 6f9484b..7bda5f6 100755 --- a/circuits/scripts/dev-virtual.sh +++ b/circuits/scripts/dev-virtual.sh @@ -13,7 +13,7 @@ ARCH=$1 PTAU=$2 # virtual_cpp and js -circom virtual/virtual.circom --c --r1cs --wasm +circom2 virtual/virtual.circom --c --r1cs --wasm # Generate proving key yarn run snarkjs groth16 setup virtual.r1cs \ diff --git a/circuits/scripts/dev.sh b/circuits/scripts/dev.sh index 420a126..4fa2912 100644 --- a/circuits/scripts/dev.sh +++ b/circuits/scripts/dev.sh @@ -12,7 +12,7 @@ PTAU=$2 UPPER_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${NAME:0:1})${NAME:1}" # Compile circuit -circom ${NAME}/${NAME}.circom --r1cs --wasm +circom2 ${NAME}/${NAME}.circom --r1cs --wasm # Generate proving key yarn run snarkjs groth16 setup ${NAME}.r1cs \ diff --git a/client/client.ts b/client/client.ts index b320de0..0747d62 100644 --- a/client/client.ts +++ b/client/client.ts @@ -58,12 +58,12 @@ const abi = IWorldAbi.abi; const walletClient = createWalletClient({ account, chain: foundry, - transport: httpTransport(), + transport: httpTransport(process.env.RPC_URL), }); const publicClient = createPublicClient({ chain: foundry, - transport: httpTransport(), + transport: httpTransport(process.env.RPC_URL), }); const nStates = getContract({ diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..242d2df --- /dev/null +++ b/compose.yaml @@ -0,0 +1,75 @@ + +services: + contracts: + build: + context: . + target: contracts + environment: + - ANVIL_IP_ADDR=0.0.0.0 + volumes: + - common-contracts:/usr/src/app/contracts + ports: + - 8545:8545 + healthcheck: + test: ["CMD", "cat", "/usr/src/app/contracts/worlds.json"] + interval: 20s + timeout: 5s + retries: 5 + enclave: + build: + context: . + target: enclave + environment: + - PGHOST=db + ports: + - 3000:3000 + volumes: + - common-contracts:/usr/src/app/contracts + depends_on: + contracts: + condition: service_healthy + healthcheck: + test: "bash -c 'printf \"GET / HTTP/1.1\n\n\" > /dev/tcp/0.0.0.0/3000; exit $?;'" + interval: 5s + timeout: 5s + retries: 5 + da: + build: + context: . + target: DA + environment: + - PGHOST=db + volumes: + - common-contracts:/usr/src/app/contracts + depends_on: + enclave: + condition: service_healthy + db: + image: postgres + restart: always + user: postgres + volumes: + - db-data:/var/lib/postgresql/data + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=networkstates + + expose: + - 5432 + client: + build: + context: . + target: client + environment: + - PGHOST=db + volumes: + - common-contracts:/usr/src/app/contracts + depends_on: + enclave: + condition: service_healthy +volumes: + db-data: + driver: local + common-contracts: + driver: local \ No newline at end of file diff --git a/docker_scripts/client_setup.sh b/docker_scripts/client_setup.sh new file mode 100644 index 0000000..fbf664b --- /dev/null +++ b/docker_scripts/client_setup.sh @@ -0,0 +1,7 @@ +cd game +pnpm install +cd .. + +cd client +pnpm install +cd .. \ No newline at end of file diff --git a/docker_scripts/contracts_run.sh b/docker_scripts/contracts_run.sh new file mode 100644 index 0000000..31fdd86 --- /dev/null +++ b/docker_scripts/contracts_run.sh @@ -0,0 +1,7 @@ +rm -rf /usr/src/app/contracts/worlds.json + +. /root/.bashrc +foundryup + + +pnpm -C contracts dev \ No newline at end of file diff --git a/docker_scripts/contracts_setup.sh b/docker_scripts/contracts_setup.sh new file mode 100644 index 0000000..3dae6fb --- /dev/null +++ b/docker_scripts/contracts_setup.sh @@ -0,0 +1,17 @@ +. /root/.bashrc +foundryup + +. /usr/src/app/.env +cd circuits +yarn +mkdir artifacts +cd artifacts +wget https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_17.ptau +cd .. +yarn dev +rm -rf node_modules +cd .. + +cd contracts +pnpm build +cd .. \ No newline at end of file diff --git a/docker_scripts/da_setup.sh b/docker_scripts/da_setup.sh new file mode 100644 index 0000000..171e39f --- /dev/null +++ b/docker_scripts/da_setup.sh @@ -0,0 +1,4 @@ + +cd DA +pnpm install +cd .. \ No newline at end of file diff --git a/docker_scripts/enclave_setup.sh b/docker_scripts/enclave_setup.sh new file mode 100644 index 0000000..dd25536 --- /dev/null +++ b/docker_scripts/enclave_setup.sh @@ -0,0 +1,9 @@ + +cd game +pnpm install +cd .. + +cd enclave +pnpm install +cd .. + diff --git a/enclave/server.ts b/enclave/server.ts index 80bb79b..c88fe6e 100644 --- a/enclave/server.ts +++ b/enclave/server.ts @@ -62,12 +62,12 @@ const abi = IWorldAbi.abi; const walletClient = createWalletClient({ account, chain: foundry, - transport: httpTransport(), + transport: httpTransport(process.env.RPC_URL), }); const publicClient = createPublicClient({ chain: foundry, - transport: httpTransport(), + transport: httpTransport(process.env.RPC_URL), }); const nStates = getContract({ From e1e7b9365c18ecc9322025aefb3db54e603a5318 Mon Sep 17 00:00:00 2001 From: DarshanAgarwal09 Date: Mon, 1 Jan 2024 22:48:03 +0530 Subject: [PATCH 02/13] updated readme with docker setup --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++---- compose.yaml | 2 +- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4e04f18..fa1c34e 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,58 @@ # network-states-experiment +This README document typically outlines the necessary steps to get your application up and running. -### Set up local network +## What is this repository for? + +* To be determined (TBD). + +## How do I get set up? + +* The setup for this repository on the local system can be done through Docker or manually. + +### Using Docker + +* Clone the repository. +* Ensure that Docker is installed and running. Refer to [docker setup](https://docs.docker.com/engine/install/) for a fresh installation. +* Make sure you have the `.env` file (you can ask the repository manager to provide it). +* Run the following command to build and start Docker: + + ```bash + docker-compose up --build + ``` + +* After all the containers are running, go inside the client container by running: + + ```bash + docker-compose exec -it client bash + ``` + +* Now that you are inside the client container, start the client server: + + ```bash + cd client + pnpm devA # You can also use {devB, devC} + ``` + +* The client game should now be up, and you can use W (up), S (down), A (left), and D (right) to navigate. + +This README would normally document whatever steps are necessary to get your application up and running. + + +### Manual setup (WIP) + +* Set up local network ``` anvil ``` -### Compile circuit +* Compile circuit ``` cd circuits/ pnpm dev:move # This takes a while: it compiles the circuit and runs a smoke test ``` -### Deploy contracts +* Deploy contracts ``` cd contracts/scripts bash forge_create_local_verifier.sh @@ -21,15 +61,29 @@ bash forge_create_local.sh # copy over deploy address to CONTRACT_ADDR in .env ``` -### Run server +* Run server ``` cd enclave/ pnpm dev # Wait for "Server running..." log ``` -### Run client +* Run client ``` cd client/ pnpm devA # can also do {devB, devC} ``` + +### To run the testcases ### +TBD + +### Deployment instructions ### +TBD + +### Code guidelines ### + +TBD + +### Who do I talk to? ### + +* Contact Seismic team diff --git a/compose.yaml b/compose.yaml index 242d2df..cad7262 100644 --- a/compose.yaml +++ b/compose.yaml @@ -14,7 +14,7 @@ services: test: ["CMD", "cat", "/usr/src/app/contracts/worlds.json"] interval: 20s timeout: 5s - retries: 5 + retries: 10 enclave: build: context: . From 121bd183490a0f81c7be85f1ea678490c6e8ee02 Mon Sep 17 00:00:00 2001 From: DarshanAgarwal09 Date: Tue, 2 Jan 2024 19:15:57 +0530 Subject: [PATCH 03/13] address pr comments --- DA/server.ts | 3 ++ Dockerfile | 1 - README.md | 63 ++++++++++++++++++++++--------- compose.yaml | 4 +- docker_scripts/client_setup.sh | 2 +- docker_scripts/contracts_run.sh | 2 +- docker_scripts/contracts_setup.sh | 2 +- docker_scripts/da_setup.sh | 2 +- docker_scripts/enclave_setup.sh | 1 - 9 files changed, 55 insertions(+), 25 deletions(-) diff --git a/DA/server.ts b/DA/server.ts index 23c574a..9bc565a 100644 --- a/DA/server.ts +++ b/DA/server.ts @@ -108,6 +108,9 @@ async function saveToDatabase(encTile: any) { socket.emit("saveToDatabaseResponse"); } +/* + * Creates encrypted tiles tables if it doesn't exist. + */ async function create_encrypted_tiles(){ const client = await pool.connect(); const tableName = 'encrypted_tiles'; diff --git a/Dockerfile b/Dockerfile index f5a9713..44e08e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -62,6 +62,5 @@ COPY --from=contracts /usr/src/app /usr/src/app RUN sh docker_scripts/client_setup.sh # Run the application. -#CMD ["pnpm", "-C", "client", "devA"] CMD ["sleep", "3600"] diff --git a/README.md b/README.md index fa1c34e..eebade2 100644 --- a/README.md +++ b/README.md @@ -40,37 +40,64 @@ This README would normally document whatever steps are necessary to get your app ### Manual setup (WIP) -* Set up local network -``` -anvil +* Clone the repository. +* Make sure you have the `.env` file (you can ask the repository manager to provide it). +* Set up PNPM and yarn + ```bash +npm install -g pnpm +npm install -g yarn ``` - -* Compile circuit +* Set up Foundary in local network + ```bash +curl -L https://foundry.paradigm.xyz | bash +. /root/.bashrc +foundryup ``` -cd circuits/ -pnpm dev:move +* Setup postgres in local or u can use below docker command to setup postgres + ```bash +docker run --name some-postgres -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=networkstates -p 5432:5432 -d postgres +``` +* Compile circuit + ```bash +cd circuits +yarn +mkdir artifacts +cd artifacts +wget https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_17.ptau +cd .. +yarn dev +cd .. # This takes a while: it compiles the circuit and runs a smoke test ``` * Deploy contracts -``` -cd contracts/scripts -bash forge_create_local_verifier.sh -# copy over deploy address to verifierContract in contract/src/NStates.sol -bash forge_create_local.sh -# copy over deploy address to CONTRACT_ADDR in .env + ```bash +cd contracts +pnpm build +pnpm dev ``` * Run server -``` -cd enclave/ + ```bash +cd game +pnpm install +cd .. + +cd enclave +pnpm install pnpm dev # Wait for "Server running..." log ``` - +* Run DA server + ```bash +cd DA +pnpm install +pnpm dev +``` * Run client -``` -cd client/ + ```bash +cd client +pnpm install pnpm devA # can also do {devB, devC} ``` diff --git a/compose.yaml b/compose.yaml index cad7262..81e630c 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,6 +6,8 @@ services: target: contracts environment: - ANVIL_IP_ADDR=0.0.0.0 + - FOUNDRY_ETH_RPC_URL=http://0.0.0.0:8545 + - FORK_RPC_URL=http://0.0.0.0:8545 volumes: - common-contracts:/usr/src/app/contracts ports: @@ -72,4 +74,4 @@ volumes: db-data: driver: local common-contracts: - driver: local \ No newline at end of file + driver: local diff --git a/docker_scripts/client_setup.sh b/docker_scripts/client_setup.sh index fbf664b..8554af2 100644 --- a/docker_scripts/client_setup.sh +++ b/docker_scripts/client_setup.sh @@ -4,4 +4,4 @@ cd .. cd client pnpm install -cd .. \ No newline at end of file +cd .. diff --git a/docker_scripts/contracts_run.sh b/docker_scripts/contracts_run.sh index 31fdd86..c10619d 100644 --- a/docker_scripts/contracts_run.sh +++ b/docker_scripts/contracts_run.sh @@ -4,4 +4,4 @@ rm -rf /usr/src/app/contracts/worlds.json foundryup -pnpm -C contracts dev \ No newline at end of file +pnpm -C contracts dev diff --git a/docker_scripts/contracts_setup.sh b/docker_scripts/contracts_setup.sh index 3dae6fb..3ef02b2 100644 --- a/docker_scripts/contracts_setup.sh +++ b/docker_scripts/contracts_setup.sh @@ -14,4 +14,4 @@ cd .. cd contracts pnpm build -cd .. \ No newline at end of file +cd .. diff --git a/docker_scripts/da_setup.sh b/docker_scripts/da_setup.sh index 171e39f..11cc03b 100644 --- a/docker_scripts/da_setup.sh +++ b/docker_scripts/da_setup.sh @@ -1,4 +1,4 @@ cd DA pnpm install -cd .. \ No newline at end of file +cd .. diff --git a/docker_scripts/enclave_setup.sh b/docker_scripts/enclave_setup.sh index dd25536..71f1cfe 100644 --- a/docker_scripts/enclave_setup.sh +++ b/docker_scripts/enclave_setup.sh @@ -6,4 +6,3 @@ cd .. cd enclave pnpm install cd .. - From cbcaf3606305ae9182382e61da73b0cf033788a6 Mon Sep 17 00:00:00 2001 From: DarshanAgarwal09 Date: Wed, 3 Jan 2024 21:45:30 +0530 Subject: [PATCH 04/13] volume issue fix --- compose.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/compose.yaml b/compose.yaml index 81e630c..782ea5f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -72,6 +72,4 @@ services: condition: service_healthy volumes: db-data: - driver: local common-contracts: - driver: local From e39b665e7188a24e10f92cfe066b5ef6fdec91da Mon Sep 17 00:00:00 2001 From: DarshanAgarwal09 Date: Thu, 4 Jan 2024 16:47:48 +0530 Subject: [PATCH 05/13] fix --- docker_scripts/contracts_setup.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/docker_scripts/contracts_setup.sh b/docker_scripts/contracts_setup.sh index 3ef02b2..885b796 100644 --- a/docker_scripts/contracts_setup.sh +++ b/docker_scripts/contracts_setup.sh @@ -13,5 +13,6 @@ rm -rf node_modules cd .. cd contracts +pnpm install pnpm build cd .. From 9c5564e53c62507a0c544810bfe0d819d11a24a8 Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Mon, 8 Jan 2024 12:11:14 -0500 Subject: [PATCH 06/13] rapidsnark commit --- circuits/scripts/dev-virtual.sh | 6 ++--- circuits/yarn.lock | 46 +++++++++++++++++++++++++++++++-- enclave/server.ts | 2 -- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/circuits/scripts/dev-virtual.sh b/circuits/scripts/dev-virtual.sh index 7bda5f6..ff67b2f 100755 --- a/circuits/scripts/dev-virtual.sh +++ b/circuits/scripts/dev-virtual.sh @@ -36,7 +36,7 @@ yarn run snarkjs zkey export solidityverifier virtual.zkey \ sed -i -e 's/0.6.11;/0.8.13;/g' virtualVerifier.sol mv virtualVerifier.sol ../contracts/src/VirtualVerifier.sol -# Save proving key and witness generation script +# Save proving key, verifying key, and witness generation script mv virtual_js/virtual.wasm virtual.zkey virtual/ # Generate witness generator @@ -70,7 +70,7 @@ if [ "$ARCH" = "x86_64" ]; then make -j4 && make install cd $CURRENT_DIR - cp package/bin/prover ../virtual/virtual-prover + mv package/bin/prover ../virtual/virtual-prover elif [ "$ARCH" = "arm64" ]; then ./build_gmp.sh macos_arm64 mkdir build_prover_macos_arm64 && cd build_prover_macos_arm64 @@ -78,7 +78,7 @@ elif [ "$ARCH" = "arm64" ]; then make -j4 && make install cd $CURRENT_DIR - cp package_macos_arm64/bin/prover ../virtual/virtual-prover + mv package_macos_arm64/bin/prover ../virtual/virtual-prover fi cd $CURRENT_DIR cd .. diff --git a/circuits/yarn.lock b/circuits/yarn.lock index f19be72..4bf02fe 100644 --- a/circuits/yarn.lock +++ b/circuits/yarn.lock @@ -128,6 +128,16 @@ dependencies: "@types/yargs-parser" "*" +"@wasmer/wasi@^0.12.0": + version "0.12.0" + resolved "https://registry.yarnpkg.com/@wasmer/wasi/-/wasi-0.12.0.tgz#89c7c5e5ba58f7dfae4e323359346639c4ec382a" + integrity sha512-FJhLZKAfLWm/yjQI7eCRHNbA8ezmb7LSpUYFkHruZXs2mXk2+DaQtSElEtOoNrVQ4vApTyVaAd5/b7uEu8w6wQ== + dependencies: + browser-process-hrtime "^1.0.0" + buffer-es6 "^4.9.3" + path-browserify "^1.0.0" + randomfill "^1.0.4" + ansi-colors@4.1.1: version "4.1.1" resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.1.tgz#cbb9ae256bf750af1eab344f229aa27fe94ba348" @@ -251,11 +261,21 @@ braces@^3.0.2, braces@~3.0.2: dependencies: fill-range "^7.0.1" +browser-process-hrtime@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz#3c9b4b7d782c8121e56f10106d84c0d0ffc94626" + integrity sha512-9o5UecI3GhkpM6DrXr69PblIuWxPKk9Y0jHBRhdocZ2y7YECBFCsHm79Pr3OyR2AvjhDkabFJaDJMYRazHgsow== + browser-stdout@1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60" integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw== +buffer-es6@^4.9.3: + version "4.9.3" + resolved "https://registry.yarnpkg.com/buffer-es6/-/buffer-es6-4.9.3.tgz#f26347b82df76fd37e18bcb5288c4970cfd5c404" + integrity sha512-Ibt+oXxhmeYJSsCkODPqNpPmyegefiD8rfutH1NYGhMZQhSp95Rz7haemgnJ6dxa6LT+JLLbtgOMORRluwKktw== + buffer-from@^1.0.0, buffer-from@^1.1.0: version "1.1.2" resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5" @@ -337,6 +357,15 @@ ci-info@^3.2.0: resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-3.9.0.tgz#4279a62028a7b1f262f3473fc9605f5e218c59b4" integrity sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ== +circom2@^0.2.16: + version "0.2.16" + resolved "https://registry.yarnpkg.com/circom2/-/circom2-0.2.16.tgz#8f2476636aa00734e61aec730a57894927978aac" + integrity sha512-ZFE14aXH0xrEf+HoB1brnV7ID1AKdz3j88R4y8JWYN05PoTozuoIBYT80WAspjj7GeelIk9GWLZYdgITF+YABg== + dependencies: + "@wasmer/wasi" "^0.12.0" + is-typed-array "^1.1.8" + path-browserify "^1.0.1" + circom_runtime@0.1.24: version "0.1.24" resolved "https://registry.yarnpkg.com/circom_runtime/-/circom_runtime-0.1.24.tgz#60ca8a31c3675802fbab5a0bcdeb02556e510733" @@ -786,7 +815,7 @@ is-plain-obj@^2.1.0: resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287" integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA== -is-typed-array@^1.1.3: +is-typed-array@^1.1.3, is-typed-array@^1.1.8: version "1.1.12" resolved "https://registry.yarnpkg.com/is-typed-array/-/is-typed-array-1.1.12.tgz#d0bab5686ef4a76f7a73097b95470ab199c57d4a" integrity sha512-Z14TF2JNG8Lss5/HMqt0//T9JeHXttXy5pH/DBU4vi98ozO2btxzq9MwYDZYnKwU8nRsz/+GVFVRDq3DkVuSPg== @@ -1052,6 +1081,11 @@ p-locate@^5.0.0: dependencies: p-limit "^3.0.2" +path-browserify@^1.0.0, path-browserify@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-1.0.1.tgz#d98454a9c3753d5790860f16f68867b9e46be1fd" + integrity sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g== + path-exists@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3" @@ -1096,13 +1130,21 @@ r1csfile@0.0.47, r1csfile@^0.0.47: fastfile "0.0.20" ffjavascript "0.2.60" -randombytes@^2.1.0: +randombytes@^2.0.5, randombytes@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a" integrity sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ== dependencies: safe-buffer "^5.1.0" +randomfill@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/randomfill/-/randomfill-1.0.4.tgz#c92196fc86ab42be983f1bf31778224931d61458" + integrity sha512-87lcbR8+MhcWcUiQ+9e+Rwx8MyR2P7qnt15ynUlbm3TU/fjbgz4GsvfSUDTemtCCtVCqb4ZcEFlyPNTh9bBTLw== + dependencies: + randombytes "^2.0.5" + safe-buffer "^5.1.0" + react-is@^18.0.0: version "18.2.0" resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b" diff --git a/enclave/server.ts b/enclave/server.ts index c88fe6e..a083c2b 100644 --- a/enclave/server.ts +++ b/enclave/server.ts @@ -378,8 +378,6 @@ async function virtualZKP(virtTile: Tile, socketId: string) { let publicSignals; let proverStatus = ProverStatus.Incomplete; try { - // [TMP]: not compiling while dev environment is macOs - throw new Error("Rapidsnark is disabled"); // Unique ID for proof-related files const proofId = socketId + "-" + inputs.hVirt; From c86157918d6599cbf2feb3c9ca213721ac08b660 Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 10:05:47 -0500 Subject: [PATCH 07/13] enclave bin gen --- docker_scripts/enclave_setup.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/docker_scripts/enclave_setup.sh b/docker_scripts/enclave_setup.sh index 71f1cfe..b97bd3a 100644 --- a/docker_scripts/enclave_setup.sh +++ b/docker_scripts/enclave_setup.sh @@ -4,5 +4,6 @@ pnpm install cd .. cd enclave +mkdir bin pnpm install cd .. From 19843ca04066ef339c56a7c962ad2d252b9582ab Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 12:22:45 -0500 Subject: [PATCH 08/13] cors setting --- enclave/server.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/enclave/server.ts b/enclave/server.ts index a083c2b..13519d1 100644 --- a/enclave/server.ts +++ b/enclave/server.ts @@ -98,12 +98,14 @@ const CLAIMED_MOVE_LIFE_SPAN = BigInt( */ const app = express(); const server = http.createServer(app); + +console.log("Warning: currently accepting requests from all origins"); const io = new Server< ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData ->(server); +>(server, { cors: { origin: "*" } }); /* * Enclave randomness that it commits to in contract. Used for virtual tile From 8cc491f4b65ab249b5b5a2ea021a5aa2459517ce Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 14:08:47 -0500 Subject: [PATCH 09/13] moveZKP returns promise for zkp compute --- client/client.ts | 10 +++++++--- game/Board.ts | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client/client.ts b/client/client.ts index 0747d62..61703c8 100644 --- a/client/client.ts +++ b/client/client.ts @@ -23,7 +23,7 @@ import { TerrainUtils, Tile, Utils, - Location + Location, } from "@seismic-systems/ns-fow-game"; dotenv.config({ path: "../.env" }); @@ -226,13 +226,17 @@ async function move(inp: string, currentBlockHeight: bigint) { clientLatestMoveBlock = currentBlockHeight; - const [tFrom, tTo, uFrom, uTo, prf, pubSignals] = await b.moveZKP( + const [uFrom, uTo, moveZKPPromise] = await b.moveZKP( cursor, { r: nr, c: nc }, nStates ); + const moveRes = await moveZKPPromise; - formattedProof = await Utils.exportCallDataGroth16(prf, pubSignals); + formattedProof = await Utils.exportCallDataGroth16( + moveRes.proof, + moveRes.publicSignals + ); // Update player position cursor = { r: nr, c: nc }; diff --git a/game/Board.ts b/game/Board.ts index 80fbdac..486bf5b 100644 --- a/game/Board.ts +++ b/game/Board.ts @@ -336,7 +336,7 @@ export class Board { nStates: any, wasmPath?: string, zkeyPath?: string - ): Promise<[Tile, Tile, Tile, Tile, Groth16Proof, any]> { + ): Promise<[Tile, Tile, Promise]> { const tFrom: Tile = this.getTile(from, BigInt(0)); const tTo: Tile = this.getTile(to, BigInt(0)); @@ -391,7 +391,7 @@ export class Board { const wasm = wasmPath || Board.MOVE_WASM; const zkey = zkeyPath || Board.MOVE_PROVKEY; - const { proof, publicSignals } = await groth16.fullProve( + const moveZKPPromise = groth16.fullProve( { currentWaterInterval: currentWaterInterval.toString(), fromCityId: tFrom.cityId.toString(), @@ -421,6 +421,6 @@ export class Board { zkey ); - return [tFrom, tTo, uFrom, uTo, proof, publicSignals]; + return [uFrom, uTo, moveZKPPromise]; } } From f34e9de62ae9ada5ad44af3baff381aa280c517f Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 14:14:56 -0500 Subject: [PATCH 10/13] npm package version number --- game/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/package.json b/game/package.json index 4ab644f..03853e1 100644 --- a/game/package.json +++ b/game/package.json @@ -1,7 +1,7 @@ { "name": "@seismic-systems/ns-fow-game", "type": "module", - "version": "1.0.2", + "version": "1.0.3", "main": "index.ts", "license": "GPL-3.0", "dependencies": { From 2a6dbcd57b5be4f90539d4511e7b04be780a3406 Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 14:18:37 -0500 Subject: [PATCH 11/13] 1.0.4 --- game/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/package.json b/game/package.json index 03853e1..ba2d804 100644 --- a/game/package.json +++ b/game/package.json @@ -1,7 +1,7 @@ { "name": "@seismic-systems/ns-fow-game", "type": "module", - "version": "1.0.3", + "version": "1.0.4", "main": "index.ts", "license": "GPL-3.0", "dependencies": { From 4aab437d43185b4e4161501a55b77b86a1544ecb Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 17:09:15 -0500 Subject: [PATCH 12/13] timer for move/virtual proving --- client/client.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/client/client.ts b/client/client.ts index 093cdf5..7d24d37 100644 --- a/client/client.ts +++ b/client/client.ts @@ -116,6 +116,8 @@ let moveFormattedProof: Groth16ProofCalldata | undefined = undefined; let virtualFormattedProof: Groth16ProofCalldata | undefined = undefined; let enclaveSig: object | undefined = undefined; +let startProveTime: number, endProveTime: number; + /* * Using Socket.IO to manage communication with enclave. */ @@ -235,6 +237,7 @@ function decryptResponse(t: any) { * tile. */ async function move(inp: string, currentBlockHeight: bigint) { + startProveTime = Date.now(); try { if (inp !== "w" && inp !== "a" && inp !== "s" && inp !== "d") { throw new Error("Invalid move input."); @@ -332,6 +335,9 @@ async function tryToSubmitMove() { virtualFormattedProof ); + endProveTime = Date.now(); + console.log(`Time to prove: ${endProveTime - startProveTime}ms`); + await nStates.write.move([ moveInputs, moveProof, From 42b39cd61643724d0847e3876aecbc74c3612655 Mon Sep 17 00:00:00 2001 From: Alexander Lindenbaum Date: Tue, 9 Jan 2024 17:10:58 -0500 Subject: [PATCH 13/13] rename global vars --- client/client.ts | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/client/client.ts b/client/client.ts index 7d24d37..0be98c2 100644 --- a/client/client.ts +++ b/client/client.ts @@ -112,9 +112,9 @@ let clientLatestMoveBlock: bigint = 0n; /* * Store pending move. */ -let moveFormattedProof: Groth16ProofCalldata | undefined = undefined; -let virtualFormattedProof: Groth16ProofCalldata | undefined = undefined; -let enclaveSig: object | undefined = undefined; +let currentMoveFormattedProof: Groth16ProofCalldata | undefined = undefined; +let currentVirtualFormattedProof: Groth16ProofCalldata | undefined = undefined; +let currentEnclaveSig: object | undefined = undefined; let startProveTime: number, endProveTime: number; @@ -258,14 +258,14 @@ async function move(inp: string, currentBlockHeight: bigint) { { r: nr, c: nc }, nStates ); - - moveFormattedProof = undefined; - virtualFormattedProof = undefined; + + currentMoveFormattedProof = undefined; + currentVirtualFormattedProof = undefined; moveZKPPromise.then(async (moveRes) => { console.log("successfully proved move ZKP"); - moveFormattedProof = await Utils.exportCallDataGroth16( + currentMoveFormattedProof = await Utils.exportCallDataGroth16( moveRes.proof, moveRes.publicSignals ); @@ -304,12 +304,12 @@ async function moveSignatureResponse( console.log(`${proverStatus} successfully proved virtual ZKP`); } - virtualFormattedProof = await Utils.exportCallDataGroth16( + currentVirtualFormattedProof = await Utils.exportCallDataGroth16( virtPrf, virtPubSigs ); const unpackedSig = hexToSignature(sig as Address); - enclaveSig = { + currentEnclaveSig = { v: unpackedSig.v, r: unpackedSig.r, s: unpackedSig.s, @@ -326,13 +326,19 @@ async function moveSignatureResponse( * virtual ZKP and returned it with a signature. */ async function tryToSubmitMove() { - if (!moveFormattedProof || !virtualFormattedProof || !enclaveSig) { + if ( + !currentMoveFormattedProof || + !currentVirtualFormattedProof || + !currentEnclaveSig + ) { return; } - const [moveInputs, moveProof] = Utils.unpackMoveInputs(moveFormattedProof); + const [moveInputs, moveProof] = Utils.unpackMoveInputs( + currentMoveFormattedProof + ); const [virtInputs, virtProof] = Utils.unpackVirtualInputs( - virtualFormattedProof + currentVirtualFormattedProof ); endProveTime = Date.now(); @@ -343,13 +349,13 @@ async function tryToSubmitMove() { moveProof, virtInputs, virtProof, - enclaveSig, + currentEnclaveSig, ]); // Reset global variables when move has been submitted onchain - moveFormattedProof = undefined; - virtualFormattedProof = undefined; - enclaveSig = undefined; + currentMoveFormattedProof = undefined; + currentVirtualFormattedProof = undefined; + currentEnclaveSig = undefined; } /*