Skip to content

Commit

Permalink
fix: random handles in smoke test on existing DB
Browse files Browse the repository at this point in the history
  • Loading branch information
dartdart26 committed Sep 12, 2024
1 parent bdb25cb commit 9ac06fd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
31 changes: 18 additions & 13 deletions fhevm-engine/coprocessor/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::str::FromStr;

use crate::server::common::FheOperation;
use crate::server::coprocessor::async_computation_input::Input;
use crate::server::coprocessor::fhevm_coprocessor_client::FhevmCoprocessorClient;
use crate::server::coprocessor::{
AsyncComputation, AsyncComputationInput, AsyncComputeRequest, DebugDecryptRequest,
DebugEncryptRequest, DebugEncryptRequestSingle,
};
use crate::server::common::FheOperation;
use tonic::metadata::MetadataValue;
use utils::{default_api_key, wait_until_all_ciphertexts_computed};
use utils::{default_api_key, random_handle, wait_until_all_ciphertexts_computed};

mod errors;
mod inputs;
mod operators;
mod utils;
mod inputs;
mod errors;

#[tokio::test]
async fn test_smoke() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -24,17 +24,22 @@ async fn test_smoke() -> Result<(), Box<dyn std::error::Error>> {
let api_key_header = format!("bearer {}", default_api_key());
let ct_type = 4; // i32

let h1 = random_handle().to_be_bytes();
let h2 = random_handle().to_be_bytes();
let h3 = random_handle().to_be_bytes();
let h4 = random_handle().to_be_bytes();

// encrypt two ciphertexts
{
let mut encrypt_request = tonic::Request::new(DebugEncryptRequest {
values: vec![
DebugEncryptRequestSingle {
handle: vec![0x0a, 0xbc],
handle: h1.to_vec(),
be_value: vec![123],
output_type: ct_type,
},
DebugEncryptRequestSingle {
handle: vec![0x0a, 0xbd],
handle: h2.to_vec(),
be_value: vec![124],
output_type: ct_type,
},
Expand All @@ -54,10 +59,10 @@ async fn test_smoke() -> Result<(), Box<dyn std::error::Error>> {
computations: vec![
AsyncComputation {
operation: FheOperation::FheAdd.into(),
output_handle: vec![0x0a, 0xbf],
output_handle: h3.to_vec(),
inputs: vec![
AsyncComputationInput {
input: Some(Input::InputHandle(vec![0x0a, 0xbe])),
input: Some(Input::InputHandle(h4.to_vec())),
},
AsyncComputationInput {
input: Some(Input::Scalar(vec![0x00, 0x10])),
Expand All @@ -66,13 +71,13 @@ async fn test_smoke() -> Result<(), Box<dyn std::error::Error>> {
},
AsyncComputation {
operation: FheOperation::FheAdd.into(),
output_handle: vec![0x0a, 0xbe],
output_handle: h4.to_vec(),
inputs: vec![
AsyncComputationInput {
input: Some(Input::InputHandle(vec![0x0a, 0xbc])),
input: Some(Input::InputHandle(h1.to_vec())),
},
AsyncComputationInput {
input: Some(Input::InputHandle(vec![0x0a, 0xbd])),
input: Some(Input::InputHandle(h2.to_vec())),
},
],
},
Expand All @@ -92,7 +97,7 @@ async fn test_smoke() -> Result<(), Box<dyn std::error::Error>> {
// decrypt values
{
let mut decrypt_request = tonic::Request::new(DebugDecryptRequest {
handles: vec![vec![0x0a, 0xbe], vec![0x0a, 0xbf]],
handles: vec![h4.to_vec(), h3.to_vec()],
});
decrypt_request.metadata_mut().append(
"authorization",
Expand All @@ -110,4 +115,4 @@ async fn test_smoke() -> Result<(), Box<dyn std::error::Error>> {
}

Ok(())
}
}
15 changes: 5 additions & 10 deletions fhevm-engine/coprocessor/src/tests/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::server::coprocessor::{
AsyncComputation, AsyncComputeRequest, DebugDecryptRequest, DebugEncryptRequest,
DebugEncryptRequestSingle,
};
use crate::tests::utils::wait_until_all_ciphertexts_computed;
use crate::tests::utils::{random_handle, wait_until_all_ciphertexts_computed};
use crate::{
server::coprocessor::{async_computation_input::Input, AsyncComputationInput},
tests::utils::{default_api_key, setup_test_app},
Expand All @@ -14,7 +14,6 @@ use fhevm_engine_common::tfhe_ops::{
does_fhe_operation_support_both_encrypted_operands, does_fhe_operation_support_scalar,
};
use fhevm_engine_common::types::{FheOperationType, SupportedFheOperations};
use rand::Rng;
use std::{ops::Not, str::FromStr};
use strum::IntoEnumIterator;
use tonic::metadata::MetadataValue;
Expand Down Expand Up @@ -75,17 +74,13 @@ fn supported_bits_to_bit_type_in_db(inp: i32) -> i32 {
}
}

fn random_handle_start() -> u64 {
rand::thread_rng().gen()
}

#[tokio::test]
async fn test_fhe_binary_operands() -> Result<(), Box<dyn std::error::Error>> {
let ops = generate_binary_test_cases();
let app = setup_test_app().await?;
let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?;

let mut handle_counter: u64 = random_handle_start();
let mut handle_counter: u64 = random_handle();
let mut next_handle = || {
let out: u64 = handle_counter;
handle_counter += 1;
Expand Down Expand Up @@ -223,7 +218,7 @@ async fn test_fhe_unary_operands() -> Result<(), Box<dyn std::error::Error>> {
let app = setup_test_app().await?;
let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?;

let mut handle_counter: u64 = random_handle_start();
let mut handle_counter: u64 = random_handle();
let mut next_handle = || {
let out: u64 = handle_counter;
handle_counter += 1;
Expand Down Expand Up @@ -336,7 +331,7 @@ async fn test_fhe_casts() -> Result<(), Box<dyn std::error::Error>> {
let app = setup_test_app().await?;
let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?;

let mut handle_counter = random_handle_start();
let mut handle_counter = random_handle();
let mut next_handle = || {
let out: u64 = handle_counter;
handle_counter += 1;
Expand Down Expand Up @@ -471,7 +466,7 @@ async fn test_fhe_if_then_else() -> Result<(), Box<dyn std::error::Error>> {
let app = setup_test_app().await?;
let mut client = FhevmCoprocessorClient::connect(app.app_url().to_string()).await?;

let mut handle_counter = random_handle_start();
let mut handle_counter = random_handle();
let mut next_handle = || {
let out: u64 = handle_counter;
handle_counter += 1;
Expand Down
7 changes: 6 additions & 1 deletion fhevm-engine/coprocessor/src/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cli::Args;
use rand::Rng;
use std::sync::atomic::{AtomicU16, Ordering};
use testcontainers::{core::WaitFor, runners::AsyncRunner, GenericImage, ImageExt};
use tokio::sync::watch::Receiver;
Expand Down Expand Up @@ -39,6 +40,10 @@ pub fn default_tenant_id() -> i32 {
1
}

pub fn random_handle() -> u64 {
rand::thread_rng().gen()
}

pub async fn setup_test_app() -> Result<TestInstance, Box<dyn std::error::Error>> {
if std::env::var("COPROCESSOR_TEST_LOCALHOST").is_ok() {
setup_test_app_existing_localhost().await
Expand All @@ -60,7 +65,7 @@ async fn setup_test_app_existing_localhost() -> Result<TestInstance, Box<dyn std
})
}

pub async fn setup_test_app_existing_db() -> Result<TestInstance, Box<dyn std::error::Error>> {
async fn setup_test_app_existing_db() -> Result<TestInstance, Box<dyn std::error::Error>> {
let app_port = get_app_port();
let (app_close_channel, rx) = tokio::sync::watch::channel(false);
start_coprocessor(rx, app_port, &LOCAL_DB_URL).await;
Expand Down

0 comments on commit 9ac06fd

Please sign in to comment.