From ca3c48e0e9da64d57c2c31b932fbeb7322a96726 Mon Sep 17 00:00:00 2001 From: 0xawaz Date: Fri, 22 Nov 2024 15:33:31 +0100 Subject: [PATCH] build: add fhevm-db-migration image --- 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 ++++++ 5 files changed, 112 insertions(+) create mode 100644 fhevm-engine/fhevm-db/Dockerfile create mode 100644 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/fhevm-db/Dockerfile b/fhevm-engine/fhevm-db/Dockerfile new file mode 100644 index 00000000..a2bc3d97 --- /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"] \ No newline at end of file diff --git a/fhevm-engine/fhevm-db/initialize_db.sh b/fhevm-engine/fhevm-db/initialize_db.sh new file mode 100644 index 00000000..301f43c1 --- /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." \ No newline at end of file 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..0ae3b2a5 --- /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'; \ No newline at end of file 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..9fdef746 --- /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; \ No newline at end of file