diff --git a/fhevm-engine/.sqlx/query-af5a9e817d8447b94c957f34070db77805483ee92daa64b0b2a78134d702eaf3.json b/fhevm-engine/.sqlx/query-af5a9e817d8447b94c957f34070db77805483ee92daa64b0b2a78134d702eaf3.json new file mode 100644 index 00000000..bc5870cf --- /dev/null +++ b/fhevm-engine/.sqlx/query-af5a9e817d8447b94c957f34070db77805483ee92daa64b0b2a78134d702eaf3.json @@ -0,0 +1,20 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO tenants(\n tenant_api_key,\n chain_id,\n acl_contract_address,\n verifying_contract_address,\n pks_key,\n sks_key,\n public_params\n )\n VALUES (\n $1,\n $2,\n $3,\n $4,\n $5,\n $6,\n $7\n )\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Uuid", + "Int4", + "Text", + "Text", + "Bytea", + "Bytea", + "Bytea" + ] + }, + "nullable": [] + }, + "hash": "af5a9e817d8447b94c957f34070db77805483ee92daa64b0b2a78134d702eaf3" +} diff --git a/fhevm-engine/coprocessor/Cargo.toml b/fhevm-engine/coprocessor/Cargo.toml index a2c6cf11..7904cd6a 100644 --- a/fhevm-engine/coprocessor/Cargo.toml +++ b/fhevm-engine/coprocessor/Cargo.toml @@ -2,6 +2,7 @@ name = "coprocessor" version = "0.1.0" edition = "2021" +default-run = "coprocessor" [dependencies] # workspace dependencies @@ -52,3 +53,11 @@ testcontainers = "0.21" [build-dependencies] tonic-build = "0.12" + +[[bin]] +name = "coprocessor" +path = "src/main.rs" + +[[bin]] +name = "cli" +path = "src/tenant_cli.rs" diff --git a/fhevm-engine/coprocessor/Dockerfile b/fhevm-engine/coprocessor/Dockerfile index a4a60291..5afcd530 100644 --- a/fhevm-engine/coprocessor/Dockerfile +++ b/fhevm-engine/coprocessor/Dockerfile @@ -22,10 +22,13 @@ FROM debian:bullseye-slim RUN useradd -m zama COPY --from=build /app/fhevm-engine/target/release/coprocessor /usr/local/bin/ +COPY --from=build /app/fhevm-engine/target/release/cli /usr/local/bin/coprocessor-cli RUN chown zama:zama /usr/local/bin/coprocessor && \ chmod 500 /usr/local/bin/coprocessor +RUN chown zama:zama /usr/local/bin/coprocessor-cli && \ + chmod 500 /usr/local/bin/coprocessor-cli USER zama -ENTRYPOINT ["/usr/local/bin/coprocessor"] \ No newline at end of file +ENTRYPOINT ["/usr/local/bin/coprocessor"] diff --git a/fhevm-engine/coprocessor/migrations/20240722111257_coprocessor.sql b/fhevm-engine/coprocessor/migrations/20240722111257_coprocessor.sql index 116cb88e..25b2b94b 100644 --- a/fhevm-engine/coprocessor/migrations/20240722111257_coprocessor.sql +++ b/fhevm-engine/coprocessor/migrations/20240722111257_coprocessor.sql @@ -59,4 +59,4 @@ CREATE TABLE IF NOT EXISTS tenants ( 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 INDEX IF NOT EXISTS tenants_by_api_key ON tenants (tenant_api_key); \ No newline at end of file +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/coprocessor/src/cli.rs b/fhevm-engine/coprocessor/src/daemon_cli.rs similarity index 100% rename from fhevm-engine/coprocessor/src/cli.rs rename to fhevm-engine/coprocessor/src/daemon_cli.rs diff --git a/fhevm-engine/coprocessor/src/main.rs b/fhevm-engine/coprocessor/src/main.rs index bb0f4732..3c30ffee 100644 --- a/fhevm-engine/coprocessor/src/main.rs +++ b/fhevm-engine/coprocessor/src/main.rs @@ -4,7 +4,7 @@ use ::tracing::{error, info}; use fhevm_engine_common::keys::{FhevmKeys, SerializedFhevmKeys}; use tokio::task::JoinSet; -mod cli; +mod daemon_cli; mod db_queries; mod metrics; mod server; @@ -16,7 +16,7 @@ mod types; mod utils; fn main() { - let args = crate::cli::parse_args(); + let args = crate::daemon_cli::parse_args(); assert!( args.work_items_batch_size < args.tenant_key_cache_size, "Work items batch size must be less than tenant key cache size" @@ -31,7 +31,7 @@ fn main() { // separate function for testing pub fn start_runtime( - args: crate::cli::Args, + args: crate::daemon_cli::Args, close_recv: Option>, ) { tokio::runtime::Builder::new_multi_thread() @@ -65,7 +65,7 @@ pub fn start_runtime( static TRACING_INIT: Once = Once::new(); async fn async_main( - args: crate::cli::Args, + args: crate::daemon_cli::Args, ) -> Result<(), Box> { TRACING_INIT.call_once(|| { tracing_subscriber::fmt().json().with_level(true).init(); diff --git a/fhevm-engine/coprocessor/src/metrics.rs b/fhevm-engine/coprocessor/src/metrics.rs index a9422696..cad77540 100644 --- a/fhevm-engine/coprocessor/src/metrics.rs +++ b/fhevm-engine/coprocessor/src/metrics.rs @@ -13,7 +13,7 @@ async fn healthcheck() -> impl actix_web::Responder { } pub async fn run_metrics_server( - args: crate::cli::Args, + args: crate::daemon_cli::Args, ) -> Result<(), Box> { info!("metrics server listening at {}", args.metrics_addr); let _ = actix_web::HttpServer::new(|| { diff --git a/fhevm-engine/coprocessor/src/server.rs b/fhevm-engine/coprocessor/src/server.rs index 3e19dc7c..fc6bf1cb 100644 --- a/fhevm-engine/coprocessor/src/server.rs +++ b/fhevm-engine/coprocessor/src/server.rs @@ -87,13 +87,13 @@ lazy_static! { pub struct CoprocessorService { pool: sqlx::Pool, - args: crate::cli::Args, + args: crate::daemon_cli::Args, tenant_key_cache: std::sync::Arc>>, signer: PrivateKeySigner, } pub async fn run_server( - args: crate::cli::Args, + args: crate::daemon_cli::Args, ) -> Result<(), Box> { loop { if let Err(e) = run_server_iteration(args.clone()).await { @@ -105,7 +105,7 @@ pub async fn run_server( } pub async fn run_server_iteration( - args: crate::cli::Args, + args: crate::daemon_cli::Args, ) -> Result<(), Box> { let addr = args .server_addr diff --git a/fhevm-engine/coprocessor/src/tenant_cli.rs b/fhevm-engine/coprocessor/src/tenant_cli.rs new file mode 100644 index 00000000..03da4e27 --- /dev/null +++ b/fhevm-engine/coprocessor/src/tenant_cli.rs @@ -0,0 +1,99 @@ +use std::str::FromStr; + +use clap::Parser; +use sqlx::types::Uuid; + +#[derive(Parser, Debug, Clone)] +#[command(version, about, long_about = None)] +pub enum Args { + /// Inserts tenant into specified database + InsertTenant { + /// PKS file path + #[arg(long)] + pks_file: String, + /// SKS file path + #[arg(long)] + sks_file: String, + /// Public params file path + #[arg(long)] + public_params_file: String, + /// Tenant api key + #[arg(long)] + tenant_api_key: String, + /// ACL contract address + #[arg(long)] + acl_contract_address: String, + /// Input verifier address + #[arg(long)] + verifying_contract_address: String, + /// Chain id + #[arg(long)] + chain_id: u32, + }, +} + +fn main() { + let args = Args::parse(); + match args { + Args::InsertTenant { pks_file, sks_file, public_params_file, tenant_api_key, acl_contract_address, verifying_contract_address, chain_id } => { + let db_url = std::env::var("DATABASE_URL") + .expect("DATABASE_URL environment variable is undefined"); + let pks_file = std::fs::read(&pks_file) + .expect("Can't read pks file"); + let sks_file = std::fs::read(&sks_file) + .expect("Can't read pks file"); + let public_params_file = std::fs::read(&public_params_file) + .expect("Can't read public params file"); + let _ = alloy::primitives::Address::from_str(&acl_contract_address) + .expect("Can't parse acl contract adddress"); + let _ = alloy::primitives::Address::from_str(&verifying_contract_address) + .expect("Can't parse input verifier adddress"); + let tenant_api_key = Uuid::from_str(&tenant_api_key).expect("Can't parse tenant api key"); + + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(async move { + let pool = sqlx::postgres::PgPoolOptions::new() + .max_connections(1) + .connect(&db_url) + .await.expect("Can't connect to postgres instance"); + + + sqlx::query!( + " + INSERT INTO tenants( + tenant_api_key, + chain_id, + acl_contract_address, + verifying_contract_address, + pks_key, + sks_key, + public_params + ) + VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7 + ) + ", + tenant_api_key, + chain_id as i32, + &acl_contract_address, + &verifying_contract_address, + &pks_file, + &sks_file, + &public_params_file + ) + .execute(&pool) + .await + .expect("Can't insert new tenant"); + }); + }, + } +} \ No newline at end of file diff --git a/fhevm-engine/coprocessor/src/tests/utils.rs b/fhevm-engine/coprocessor/src/tests/utils.rs index ca1b0c01..0cfa0c76 100644 --- a/fhevm-engine/coprocessor/src/tests/utils.rs +++ b/fhevm-engine/coprocessor/src/tests/utils.rs @@ -1,4 +1,4 @@ -use crate::cli::Args; +use crate::daemon_cli::Args; use fhevm_engine_common::tfhe_ops::current_ciphertext_version; use fhevm_engine_common::types::SupportedFheCiphertexts; use fhevm_engine_common::utils::{safe_deserialize, safe_deserialize_key}; @@ -214,10 +214,9 @@ pub async fn setup_test_user(pool: &sqlx::PgPool) -> Result<(), Box Result<(), Box> { loop { // here we log the errors and make sure we retry @@ -59,7 +59,7 @@ pub async fn run_tfhe_worker( } async fn tfhe_worker_cycle( - args: &crate::cli::Args, + args: &crate::daemon_cli::Args, ) -> Result<(), Box> { let tracer = opentelemetry::global::tracer("tfhe_worker"); diff --git a/fhevm-engine/coprocessor/src/utils.rs b/fhevm-engine/coprocessor/src/utils.rs index 4bcbe762..4e47373e 100644 --- a/fhevm-engine/coprocessor/src/utils.rs +++ b/fhevm-engine/coprocessor/src/utils.rs @@ -168,7 +168,7 @@ pub fn sort_computations_by_dependencies<'a>( Ok((res, handles_to_check_in_db)) } -pub fn db_url(args: &crate::cli::Args) -> String { +pub fn db_url(args: &crate::daemon_cli::Args) -> String { if let Some(db_url) = &args.database_url { return db_url.clone(); }