From 8b3ada923b48b999a1171bf6827c26f61f7759de Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Fri, 15 Nov 2024 14:31:51 +0100 Subject: [PATCH 1/8] test: update docker-compose --- fhevm-engine/coprocessor/docker-compose.yml | 41 ++++++++++++++------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml index ffaed7ac..ca86fb76 100644 --- a/fhevm-engine/coprocessor/docker-compose.yml +++ b/fhevm-engine/coprocessor/docker-compose.yml @@ -1,37 +1,52 @@ -version: '3.8' +name: fhevm + services: db: + container_name: db image: postgres:15.7 restart: always environment: - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres ports: - '5432:5432' - volumes: + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 10s + timeout: 5s + retries: 3 + volumes: - db:/var/lib/postgresql/data - coproc: - image: ghcr.io/zama-ai/fhevm-coprocessor:v7 + + coprocessor: + container_name: coprocessor + image: ghcr.io/zama-ai/fhevm-coprocessor:latest environment: - - DATABASE_URL=postgresql://postgres:postgres@db:5432/coprocessor + DATABASE_URL: postgresql://postgres:postgres@db:5432/coprocessor ports: - '50051:50051' volumes: - - ${PWD}/coprocessor.key:/usr/share/coprocessor.key + - ./coprocessor.key:/usr/share/coprocessor.key command: - --run-bg-worker - --run-server - --server-addr=0.0.0.0:50051 - --coprocessor-private-key=/usr/share/coprocessor.key + depends_on: + db: + condition: service_healthy + geth: - image: ghcr.io/zama-ai/geth-coprocessor-devnode:v6 + container_name: geth + image: ghcr.io/zama-ai/go-ethereum-coprocessor-dev:latest environment: - - FHEVM_COPROCESSOR_API_KEY=a1503fb6-d79b-4e9e-826d-44cf262f3e05 - - FHEVM_COPROCESSOR_URL=coproc:50051 - - COPROCESSOR_CONTRACT_ADDRESS=0x6819e3aDc437fAf9D533490eD3a7552493fCE3B1 - - ACL_CONTRACT_ADDRESS=0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 + FHEVM_COPROCESSOR_API_KEY: a1503fb6-d79b-4e9e-826d-44cf262f3e05 + FHEVM_COPROCESSOR_URL: coprocessor:50051 + COPROCESSOR_CONTRACT_ADDRESS: 0x6819e3aDc437fAf9D533490eD3a7552493fCE3B1 + ACL_CONTRACT_ADDRESS: 0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 ports: - '8745:8545' + volumes: db: driver: local From 281f2120714e243a258f8937af8b0442e2227233 Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Fri, 15 Nov 2024 14:32:21 +0100 Subject: [PATCH 2/8] test: update Makefile --- fhevm-engine/coprocessor/Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fhevm-engine/coprocessor/Makefile b/fhevm-engine/coprocessor/Makefile index 7b1d62fb..012d2236 100644 --- a/fhevm-engine/coprocessor/Makefile +++ b/fhevm-engine/coprocessor/Makefile @@ -11,12 +11,16 @@ cleanup: .PHONY: init_db init_db: - docker compose up -d + docker compose up -d db sleep 3 $(DB_URL) sqlx db create $(DB_URL) sqlx migrate run $(DB_URL) cargo test setup_test_user -- --nocapture --ignored +.PHONY: start +start: + docker compose up -d + .PHONY: recreate_db recreate_db: $(MAKE) cleanup From fe54d2db095b9f6162ada312872d85429c6721aa Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Wed, 20 Nov 2024 18:57:42 +0100 Subject: [PATCH 3/8] test: add init db via script --- fhevm-engine/coprocessor/docker-compose.yml | 68 +++++++++++-------- fhevm-engine/coprocessor/initialize_db.sh | 29 ++++++++ ...20241120173212_insert_test_tenant.down.sql | 1 + .../20241120173212_insert_test_tenant.up.sql | 19 ++++++ 4 files changed, 90 insertions(+), 27 deletions(-) create mode 100755 fhevm-engine/coprocessor/initialize_db.sh create mode 100644 fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.down.sql create mode 100644 fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.up.sql diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml index ca86fb76..72ea9dfe 100644 --- a/fhevm-engine/coprocessor/docker-compose.yml +++ b/fhevm-engine/coprocessor/docker-compose.yml @@ -18,34 +18,48 @@ services: volumes: - db:/var/lib/postgresql/data - coprocessor: - container_name: coprocessor - image: ghcr.io/zama-ai/fhevm-coprocessor:latest - environment: - DATABASE_URL: postgresql://postgres:postgres@db:5432/coprocessor - ports: - - '50051:50051' - volumes: - - ./coprocessor.key:/usr/share/coprocessor.key - command: - - --run-bg-worker - - --run-server - - --server-addr=0.0.0.0:50051 - - --coprocessor-private-key=/usr/share/coprocessor.key - depends_on: - db: - condition: service_healthy + migration: + container_name: migration + image: rust:1.74 + volumes: + - ./initialize_db.sh:/initialize_db.sh + - ./migrations:/migrations + - ../fhevm-keys:/fhevm-keys + entrypoint: + - /bin/bash + - /initialize_db.sh + depends_on: + db: + condition: service_healthy - geth: - container_name: geth - image: ghcr.io/zama-ai/go-ethereum-coprocessor-dev:latest - environment: - FHEVM_COPROCESSOR_API_KEY: a1503fb6-d79b-4e9e-826d-44cf262f3e05 - FHEVM_COPROCESSOR_URL: coprocessor:50051 - COPROCESSOR_CONTRACT_ADDRESS: 0x6819e3aDc437fAf9D533490eD3a7552493fCE3B1 - ACL_CONTRACT_ADDRESS: 0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 - ports: - - '8745:8545' + # coprocessor: + # container_name: coprocessor + # image: ghcr.io/zama-ai/fhevm-coprocessor:latest + # environment: + # DATABASE_URL: postgresql://postgres:postgres@db:5432/postgres + # ports: + # - '50051:50051' + # volumes: + # - ./coprocessor.key:/usr/share/coprocessor.key + # command: + # - --run-bg-worker + # - --run-server + # - --server-addr=0.0.0.0:50051 + # - --coprocessor-private-key=/usr/share/coprocessor.key + # depends_on: + # db: + # condition: service_healthy + + # geth: + # container_name: geth + # image: ghcr.io/zama-ai/go-ethereum-coprocessor-dev:latest + # environment: + # FHEVM_COPROCESSOR_API_KEY: a1503fb6-d79b-4e9e-826d-44cf262f3e05 + # FHEVM_COPROCESSOR_URL: coprocessor:50051 + # COPROCESSOR_CONTRACT_ADDRESS: 0x6819e3aDc437fAf9D533490eD3a7552493fCE3B1 + # ACL_CONTRACT_ADDRESS: 0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 + # ports: + # - '8745:8545' volumes: db: diff --git a/fhevm-engine/coprocessor/initialize_db.sh b/fhevm-engine/coprocessor/initialize_db.sh new file mode 100755 index 00000000..886f974a --- /dev/null +++ b/fhevm-engine/coprocessor/initialize_db.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Database configuration +TENANT_API_KEY="e5f23363-ada1-47ba-9a7b-2c33c6231ad8" +CHAIN_ID=11155111 +ACL_CONTRACT_ADDRESS="0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2" +VERIFYING_CONTRACT_ADDRESS="0x69dE3158643e738a0724418b21a35FAA20CBb1c5" +PKS_KEY="/fhevm-keys/pks" +SKS_KEY="/fhevm-keys/sks" +PUBLIC_PARAMS="/fhevm-keys/pp" +CKS_KEY="/fhevm-keys/cks" + +# 0. Setup environment +apt-get update && apt-get install -y libpq-dev && \ + cargo install sqlx-cli --no-default-features --features postgres --locked && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +echo "Setting up the database with sqlx..." +export DATABASE_URL="postgres://postgres:postgres@db:5432/coprocessor" + +# 1: Create Database +echo "Creating database..." +sqlx database create + +# 2: Run sqlx migrations +echo "Running migrations..." +sqlx migrate run --source /migrations || { echo "Failed to run migrations."; exit 1; } + +echo "Database initialization complete." diff --git a/fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.down.sql b/fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.down.sql new file mode 100644 index 00000000..a35a24a9 --- /dev/null +++ b/fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.down.sql @@ -0,0 +1 @@ +DELETE FROM tenants WHERE tenant_api_key = 'a1503fb6-d79b-4e9e-826d-44cf262f3e05'; diff --git a/fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.up.sql b/fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.up.sql new file mode 100644 index 00000000..dfcdbe69 --- /dev/null +++ b/fhevm-engine/coprocessor/migrations/20241120173212_insert_test_tenant.up.sql @@ -0,0 +1,19 @@ +INSERT INTO tenants ( + tenant_api_key, + chain_id, + acl_contract_address, + verifying_contract_address, + pks_key, + sks_key, + public_params, + cks_key +) VALUES ( + 'a1503fb6-d79b-4e9e-826d-44cf262f3e05', + 12345, + '0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2', + '0x69dE3158643e738a0724418b21a35FAA20CBb1c5', + '/fhevm-keys/pks', + '/fhevm-keys/sks', + '/fhevm-keys/pp', + '/fhevm-keys/cks' +) ON CONFLICT DO NOTHING; From 0809da2f54d1f1f9ea5ea0efc7a87a9c1ec29a12 Mon Sep 17 00:00:00 2001 From: David Kazlauskas Date: Wed, 20 Nov 2024 09:58:32 +0200 Subject: [PATCH 4/8] feat: allow to customize key directory in the executor --- fhevm-engine/executor/src/cli.rs | 5 +++ fhevm-engine/executor/src/server.rs | 43 ++------------------ fhevm-engine/executor/tests/utils.rs | 4 +- fhevm-engine/fhevm-engine-common/src/keys.rs | 15 ++++--- 4 files changed, 20 insertions(+), 47 deletions(-) diff --git a/fhevm-engine/executor/src/cli.rs b/fhevm-engine/executor/src/cli.rs index e634a9fb..a4e920a4 100644 --- a/fhevm-engine/executor/src/cli.rs +++ b/fhevm-engine/executor/src/cli.rs @@ -14,6 +14,11 @@ pub struct Args { #[arg(long, default_value = "127.0.0.1:50051")] pub server_addr: String, + + /// directory for fhe keys, target directory expected to contain files named: + /// sks (server evaluation key), pks (compact public key), pp (public key params) + #[arg(long)] + pub fhe_keys_directory: String, } pub fn parse_args() -> Args { diff --git a/fhevm-engine/executor/src/server.rs b/fhevm-engine/executor/src/server.rs index 68aa2699..6062f3af 100644 --- a/fhevm-engine/executor/src/server.rs +++ b/fhevm-engine/executor/src/server.rs @@ -32,10 +32,10 @@ thread_local! { } pub fn start(args: &crate::cli::Args) -> Result<()> { - let keys: FhevmKeys = SerializedFhevmKeys::load_from_disk().into(); + let keys: FhevmKeys = SerializedFhevmKeys::load_from_disk(&args.fhe_keys_directory).into(); SERVER_KEY.set(Some(keys.server_key.clone())); LOCAL_RAYON_THREADS.set(args.policy_fhe_compute_threads); - let executor = FhevmExecutorService::new(); + let executor = FhevmExecutorService::new(keys.clone()); let runtime = tokio::runtime::Builder::new_multi_thread() .worker_threads(args.tokio_threads) .max_blocking_threads(args.fhe_compute_threads) @@ -150,9 +150,9 @@ impl FhevmExecutor for FhevmExecutorService { } impl FhevmExecutorService { - fn new() -> Self { + fn new(keys: FhevmKeys) -> Self { FhevmExecutorService { - keys: SerializedFhevmKeys::load_from_disk().into(), + keys, } } @@ -300,41 +300,6 @@ impl FhevmExecutorService { } } -pub fn run_computation( - operation: i32, - inputs: Result, SyncComputeError>, - graph_node_index: usize, -) -> Result<(usize, InMemoryCiphertext), SyncComputeError> { - let op = FheOperation::try_from(operation); - match inputs { - Ok(inputs) => match op { - Ok(FheOperation::FheGetCiphertext) => { - let res = InMemoryCiphertext { - expanded: inputs[0].clone(), - compressed: inputs[0].clone().compress().1, - }; - Ok((graph_node_index, res)) - } - Ok(_) => match perform_fhe_operation(operation as i16, &inputs) { - Ok(result) => { - let res = InMemoryCiphertext { - expanded: result.clone(), - compressed: result.compress().1, - }; - Ok((graph_node_index, res)) - } - Err(_) => Err::<(usize, InMemoryCiphertext), SyncComputeError>( - SyncComputeError::ComputationFailed, - ), - }, - _ => Err::<(usize, InMemoryCiphertext), SyncComputeError>( - SyncComputeError::InvalidOperation, - ), - }, - Err(_) => Err(SyncComputeError::ComputationFailed), - } -} - pub fn build_taskgraph_from_request( dfg: &mut DFGraph, req: &SyncComputeRequest, diff --git a/fhevm-engine/executor/tests/utils.rs b/fhevm-engine/executor/tests/utils.rs index 78664c6b..aa5f60e9 100644 --- a/fhevm-engine/executor/tests/utils.rs +++ b/fhevm-engine/executor/tests/utils.rs @@ -19,10 +19,10 @@ pub struct TestInstance { impl TestInstance { pub async fn new() -> Self { // Get defaults by parsing a cmd line without any arguments. - let args = Args::parse_from(&["test"]); + let args = Args::parse_from(&["test", "--fhe-keys-directory", "../fhevm-keys"]); let instance = TestInstance { - keys: SerializedFhevmKeys::load_from_disk().into(), + keys: SerializedFhevmKeys::load_from_disk("../fhevm-keys").into(), server_addr: format!("http://{}", args.server_addr), }; diff --git a/fhevm-engine/fhevm-engine-common/src/keys.rs b/fhevm-engine/fhevm-engine-common/src/keys.rs index c1f3b5fb..56cdbc7f 100644 --- a/fhevm-engine/fhevm-engine-common/src/keys.rs +++ b/fhevm-engine/fhevm-engine-common/src/keys.rs @@ -28,6 +28,7 @@ pub const TFHE_KS_PARAMS: ShortintKeySwitchingParameters = pub const MAX_BITS_TO_PROVE: usize = 2048; +#[derive(Clone)] pub struct FhevmKeys { pub server_key: ServerKey, pub client_key: Option, @@ -79,6 +80,7 @@ impl SerializedFhevmKeys { const PKS: &'static str = "../fhevm-keys/pks"; const PUBLIC_PARAMS: &'static str = "../fhevm-keys/pp"; + // generating keys is only for testing, so it is okay these are hardcoded pub fn save_to_disk(self) { println!("Creating directory {}", Self::DIRECTORY); std::fs::create_dir_all(Self::DIRECTORY).expect("create keys directory"); @@ -99,14 +101,15 @@ impl SerializedFhevmKeys { .expect("write public params"); } - pub fn load_from_disk() -> Self { - let server_key = read(Self::SKS).expect("read server key"); - let client_key = read(Self::CKS); - let compact_public_key = read(Self::PKS).expect("read compact public key"); - let public_params = read(Self::PUBLIC_PARAMS).expect("read public params"); + pub fn load_from_disk(keys_directory: &str) -> Self { + let keys_dir = std::path::Path::new(&keys_directory); + let server_key = read(keys_dir.join("sks")).expect("read server key"); + let client_key = read(keys_dir.join("cks")).ok(); + let compact_public_key = read(keys_dir.join("pks")).expect("read compact public key"); + let public_params = read(keys_dir.join("pp")).expect("read public params"); SerializedFhevmKeys { server_key, - client_key: client_key.ok(), + client_key, compact_public_key, public_params, } From 10808a88da2342e081ec4163602051eb02bdf2d1 Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Wed, 20 Nov 2024 19:54:13 +0100 Subject: [PATCH 5/8] test: update Makefile --- fhevm-engine/coprocessor/Makefile | 12 +---- fhevm-engine/coprocessor/docker-compose.yml | 59 +++++++++++---------- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/fhevm-engine/coprocessor/Makefile b/fhevm-engine/coprocessor/Makefile index 012d2236..bf78884e 100644 --- a/fhevm-engine/coprocessor/Makefile +++ b/fhevm-engine/coprocessor/Makefile @@ -9,16 +9,8 @@ build: cleanup: docker compose down -v -.PHONY: init_db -init_db: - docker compose up -d db - sleep 3 - $(DB_URL) sqlx db create - $(DB_URL) sqlx migrate run - $(DB_URL) cargo test setup_test_user -- --nocapture --ignored - -.PHONY: start -start: +.PHONY: run +run: docker compose up -d .PHONY: recreate_db diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml index 72ea9dfe..d1a4d249 100644 --- a/fhevm-engine/coprocessor/docker-compose.yml +++ b/fhevm-engine/coprocessor/docker-compose.yml @@ -32,34 +32,39 @@ services: db: condition: service_healthy - # coprocessor: - # container_name: coprocessor - # image: ghcr.io/zama-ai/fhevm-coprocessor:latest - # environment: - # DATABASE_URL: postgresql://postgres:postgres@db:5432/postgres - # ports: - # - '50051:50051' - # volumes: - # - ./coprocessor.key:/usr/share/coprocessor.key - # command: - # - --run-bg-worker - # - --run-server - # - --server-addr=0.0.0.0:50051 - # - --coprocessor-private-key=/usr/share/coprocessor.key - # depends_on: - # db: - # condition: service_healthy + coprocessor: + container_name: coprocessor + image: ghcr.io/zama-ai/fhevm-coprocessor:v0.1.0-3 + environment: + DATABASE_URL: postgresql://postgres:postgres@db:5432/postgres + ports: + - '50051:50051' + volumes: + - ./coprocessor.key:/usr/share/coprocessor.key + command: + - --run-bg-worker + - --run-server + - --server-addr=0.0.0.0:50051 + - --coprocessor-private-key=/usr/share/coprocessor.key + depends_on: + migration: + condition: service_completed_successfully - # geth: - # container_name: geth - # image: ghcr.io/zama-ai/go-ethereum-coprocessor-dev:latest - # environment: - # FHEVM_COPROCESSOR_API_KEY: a1503fb6-d79b-4e9e-826d-44cf262f3e05 - # FHEVM_COPROCESSOR_URL: coprocessor:50051 - # COPROCESSOR_CONTRACT_ADDRESS: 0x6819e3aDc437fAf9D533490eD3a7552493fCE3B1 - # ACL_CONTRACT_ADDRESS: 0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 - # ports: - # - '8745:8545' + geth: + container_name: geth + image: ghcr.io/zama-ai/geth-coprocessor-devnode:v8 + environment: + FHEVM_COPROCESSOR_API_KEY: a1503fb6-d79b-4e9e-826d-44cf262f3e05 + FHEVM_COPROCESSOR_URL: coprocessor:50051 + COPROCESSOR_CONTRACT_ADDRESS: 0x6819e3aDc437fAf9D533490eD3a7552493fCE3B1 + ACL_CONTRACT_ADDRESS: 0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2 + volumes: + - ./geth:/geth + ports: + - '8745:8545' + depends_on: + coprocessor: + condition: service_started volumes: db: From ec7317be57e90b5102c50380098747ba2d97c73d Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Fri, 22 Nov 2024 10:13:00 +0100 Subject: [PATCH 6/8] fix: fix database --- fhevm-engine/coprocessor/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml index d1a4d249..da9e97ea 100644 --- a/fhevm-engine/coprocessor/docker-compose.yml +++ b/fhevm-engine/coprocessor/docker-compose.yml @@ -36,7 +36,7 @@ services: container_name: coprocessor image: ghcr.io/zama-ai/fhevm-coprocessor:v0.1.0-3 environment: - DATABASE_URL: postgresql://postgres:postgres@db:5432/postgres + DATABASE_URL: postgresql://postgres:postgres@db:5432/coprocessor ports: - '50051:50051' volumes: From 401103df0f895fb34f1f3416402af20e2909b073 Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Fri, 22 Nov 2024 15:05:02 +0100 Subject: [PATCH 7/8] feat: create Dockerfile for db migration --- fhevm-engine/coprocessor/Dockerfile.db | 19 +++++++++++++++++++ fhevm-engine/coprocessor/docker-compose.yml | 11 +++-------- fhevm-engine/coprocessor/initialize_db.sh | 18 ------------------ 3 files changed, 22 insertions(+), 26 deletions(-) create mode 100644 fhevm-engine/coprocessor/Dockerfile.db diff --git a/fhevm-engine/coprocessor/Dockerfile.db b/fhevm-engine/coprocessor/Dockerfile.db new file mode 100644 index 00000000..ba20ed47 --- /dev/null +++ b/fhevm-engine/coprocessor/Dockerfile.db @@ -0,0 +1,19 @@ +# Use the Rust image as the base +FROM rust:1.74 + +# Install dependencies and tools +RUN apt-get update && \ + apt-get install -y libpq-dev && \ + cargo install sqlx-cli --no-default-features --features postgres --locked && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +# Copy migrations and initialization script +COPY fhevm-engine/coprocessor/initialize_db.sh /initialize_db.sh +COPY fhevm-engine/coprocessor/migrations /migrations +COPY fhevm-engine/fhevm-keys /fhevm-keys + +# Make the script executable +RUN chmod +x /initialize_db.sh + +# Run the initialization script as the entrypoint +ENTRYPOINT ["/bin/bash", "/initialize_db.sh"] diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml index da9e97ea..81728d50 100644 --- a/fhevm-engine/coprocessor/docker-compose.yml +++ b/fhevm-engine/coprocessor/docker-compose.yml @@ -20,14 +20,9 @@ services: migration: container_name: migration - image: rust:1.74 - volumes: - - ./initialize_db.sh:/initialize_db.sh - - ./migrations:/migrations - - ../fhevm-keys:/fhevm-keys - entrypoint: - - /bin/bash - - /initialize_db.sh + image: migration:v1 + environment: + DATABASE_URL: postgres://postgres:postgres@db:5432/coprocessor depends_on: db: condition: service_healthy diff --git a/fhevm-engine/coprocessor/initialize_db.sh b/fhevm-engine/coprocessor/initialize_db.sh index 886f974a..731bbedf 100755 --- a/fhevm-engine/coprocessor/initialize_db.sh +++ b/fhevm-engine/coprocessor/initialize_db.sh @@ -1,23 +1,5 @@ #!/bin/bash -# Database configuration -TENANT_API_KEY="e5f23363-ada1-47ba-9a7b-2c33c6231ad8" -CHAIN_ID=11155111 -ACL_CONTRACT_ADDRESS="0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2" -VERIFYING_CONTRACT_ADDRESS="0x69dE3158643e738a0724418b21a35FAA20CBb1c5" -PKS_KEY="/fhevm-keys/pks" -SKS_KEY="/fhevm-keys/sks" -PUBLIC_PARAMS="/fhevm-keys/pp" -CKS_KEY="/fhevm-keys/cks" - -# 0. Setup environment -apt-get update && apt-get install -y libpq-dev && \ - cargo install sqlx-cli --no-default-features --features postgres --locked && \ - apt-get clean && rm -rf /var/lib/apt/lists/* - -echo "Setting up the database with sqlx..." -export DATABASE_URL="postgres://postgres:postgres@db:5432/coprocessor" - # 1: Create Database echo "Creating database..." sqlx database create From 862c0245cae4f5c86a934efd6b6f07accf63cf63 Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Fri, 22 Nov 2024 15:13:52 +0100 Subject: [PATCH 8/8] refactor: fhevm-db image --- fhevm-engine/coprocessor/docker-compose.yml | 2 +- fhevm-engine/fhevm-db/Dockerfile | 19 ++++++ fhevm-engine/fhevm-db/initialize_db.sh | 11 ++++ .../migrations/20240722111257_coprocessor.sql | 62 +++++++++++++++++++ ...20241120173212_insert_test_tenant.down.sql | 1 + .../20241120173212_insert_test_tenant.up.sql | 19 ++++++ 6 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 fhevm-engine/fhevm-db/Dockerfile create mode 100755 fhevm-engine/fhevm-db/initialize_db.sh create mode 100644 fhevm-engine/fhevm-db/migrations/20240722111257_coprocessor.sql create mode 100644 fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.down.sql create mode 100644 fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.up.sql diff --git a/fhevm-engine/coprocessor/docker-compose.yml b/fhevm-engine/coprocessor/docker-compose.yml index 81728d50..9365caf4 100644 --- a/fhevm-engine/coprocessor/docker-compose.yml +++ b/fhevm-engine/coprocessor/docker-compose.yml @@ -22,7 +22,7 @@ services: container_name: migration image: migration:v1 environment: - DATABASE_URL: postgres://postgres:postgres@db:5432/coprocessor + DATABASE_URL: postgresql://postgres:postgres@db:5432/coprocessor depends_on: db: condition: service_healthy diff --git a/fhevm-engine/fhevm-db/Dockerfile b/fhevm-engine/fhevm-db/Dockerfile new file mode 100644 index 00000000..8aa47cfb --- /dev/null +++ b/fhevm-engine/fhevm-db/Dockerfile @@ -0,0 +1,19 @@ +# Use the Rust image as the base +FROM rust:1.74 + +# Install dependencies and tools +RUN apt-get update && \ + apt-get install -y libpq-dev && \ + cargo install sqlx-cli --no-default-features --features postgres --locked && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +# Copy migrations and initialization script +COPY fhevm-engine/fhevm-db/initialize_db.sh /initialize_db.sh +COPY fhevm-engine/fhevm-db/migrations /migrations +COPY fhevm-engine/fhevm-keys /fhevm-keys + +# Make the script executable +RUN chmod +x /initialize_db.sh + +# Run the initialization script as the entrypoint +ENTRYPOINT ["/bin/bash", "/initialize_db.sh"] diff --git a/fhevm-engine/fhevm-db/initialize_db.sh b/fhevm-engine/fhevm-db/initialize_db.sh new file mode 100755 index 00000000..731bbedf --- /dev/null +++ b/fhevm-engine/fhevm-db/initialize_db.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# 1: Create Database +echo "Creating database..." +sqlx database create + +# 2: Run sqlx migrations +echo "Running migrations..." +sqlx migrate run --source /migrations || { echo "Failed to run migrations."; exit 1; } + +echo "Database initialization complete." diff --git a/fhevm-engine/fhevm-db/migrations/20240722111257_coprocessor.sql b/fhevm-engine/fhevm-db/migrations/20240722111257_coprocessor.sql new file mode 100644 index 00000000..25b2b94b --- /dev/null +++ b/fhevm-engine/fhevm-db/migrations/20240722111257_coprocessor.sql @@ -0,0 +1,62 @@ + +CREATE TABLE IF NOT EXISTS computations ( + tenant_id INT NOT NULL, + output_handle BYTEA NOT NULL, + output_type SMALLINT NOT NULL, + -- can be handle or scalar, depends on is_scalar field + -- only second dependency can ever be scalar + dependencies BYTEA[] NOT NULL, + fhe_operation SMALLINT NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT NOW(), + completed_at TIMESTAMP, + is_scalar BOOLEAN NOT NULL, + is_completed BOOLEAN NOT NULL DEFAULT 'f', + is_error BOOLEAN NOT NULL DEFAULT 'f', + error_message TEXT, + PRIMARY KEY (tenant_id, output_handle) +); + +CREATE TABLE IF NOT EXISTS ciphertexts ( + tenant_id INT NOT NULL, + handle BYTEA NOT NULL, + ciphertext BYTEA NOT NULL, + ciphertext_version SMALLINT NOT NULL, + ciphertext_type SMALLINT NOT NULL, + -- if ciphertext came from blob we have its reference + input_blob_hash BYTEA, + input_blob_index INT NOT NULL DEFAULT 0, + created_at TIMESTAMP DEFAULT NOW(), + PRIMARY KEY (tenant_id, handle, ciphertext_version) +); + +-- store for audits and historical reference +CREATE TABLE IF NOT EXISTS input_blobs ( + tenant_id INT NOT NULL, + blob_hash BYTEA NOT NULL, + blob_data BYTEA NOT NULL, + blob_ciphertext_count INT NOT NULL, + created_at TIMESTAMP DEFAULT NOW(), + PRIMARY KEY (tenant_id, blob_hash) +); + +CREATE TABLE IF NOT EXISTS tenants ( + tenant_id SERIAL PRIMARY KEY, + tenant_api_key UUID NOT NULL DEFAULT gen_random_uuid(), + -- for EIP712 signatures + chain_id INT NOT NULL, + -- for EIP712 signatures + verifying_contract_address TEXT NOT NULL, + acl_contract_address TEXT NOT NULL, + pks_key BYTEA NOT NULL, + sks_key BYTEA NOT NULL, + public_params BYTEA NOT NULL, + -- for debugging, can be null + cks_key BYTEA, + -- admin api key is allowed to create more tenants with their keys + is_admin BOOLEAN DEFAULT 'f' +); + +CREATE INDEX IF NOT EXISTS computations_dependencies_index ON computations USING GIN (dependencies); +CREATE INDEX IF NOT EXISTS computations_completed_index ON computations (is_completed); +CREATE INDEX IF NOT EXISTS computations_errors_index ON computations (is_error); +CREATE UNIQUE INDEX IF NOT EXISTS tenants_by_api_key ON tenants (tenant_api_key); \ No newline at end of file diff --git a/fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.down.sql b/fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.down.sql new file mode 100644 index 00000000..a35a24a9 --- /dev/null +++ b/fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.down.sql @@ -0,0 +1 @@ +DELETE FROM tenants WHERE tenant_api_key = 'a1503fb6-d79b-4e9e-826d-44cf262f3e05'; diff --git a/fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.up.sql b/fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.up.sql new file mode 100644 index 00000000..dfcdbe69 --- /dev/null +++ b/fhevm-engine/fhevm-db/migrations/20241120173212_insert_test_tenant.up.sql @@ -0,0 +1,19 @@ +INSERT INTO tenants ( + tenant_api_key, + chain_id, + acl_contract_address, + verifying_contract_address, + pks_key, + sks_key, + public_params, + cks_key +) VALUES ( + 'a1503fb6-d79b-4e9e-826d-44cf262f3e05', + 12345, + '0x339EcE85B9E11a3A3AA557582784a15d7F82AAf2', + '0x69dE3158643e738a0724418b21a35FAA20CBb1c5', + '/fhevm-keys/pks', + '/fhevm-keys/sks', + '/fhevm-keys/pp', + '/fhevm-keys/cks' +) ON CONFLICT DO NOTHING;