Skip to content

Commit

Permalink
build: add fhevm-db-migration image
Browse files Browse the repository at this point in the history
  • Loading branch information
0xawaz committed Nov 22, 2024
1 parent 2e11b11 commit ca3c48e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
19 changes: 19 additions & 0 deletions fhevm-engine/fhevm-db/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
11 changes: 11 additions & 0 deletions fhevm-engine/fhevm-db/initialize_db.sh
Original file line number Diff line number Diff line change
@@ -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."
62 changes: 62 additions & 0 deletions fhevm-engine/fhevm-db/migrations/20240722111257_coprocessor.sql
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM tenants WHERE tenant_api_key = 'a1503fb6-d79b-4e9e-826d-44cf262f3e05';
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ca3c48e

Please sign in to comment.