-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into davidk/fhe-rand-generation
- Loading branch information
Showing
4 changed files
with
106 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
BSD 3-Clause Clear License | ||
|
||
Copyright © 2024 ZAMA. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, this | ||
list of conditions and the following disclaimer in the documentation and/or other | ||
materials provided with the distribution. | ||
|
||
3. Neither the name of ZAMA nor the names of its contributors may be used to endorse | ||
or promote products derived from this software without specific prior written permission. | ||
|
||
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE*. | ||
THIS SOFTWARE IS PROVIDED BY THE ZAMA AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | ||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
ZAMA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, | ||
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
*In addition to the rights carried by this license, ZAMA grants to the user a non-exclusive, | ||
free and non-commercial license on all patents filed in its name relating to the open-source | ||
code (the "Patents") for the sole purpose of evaluation, development, research, prototyping | ||
and experimentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
use crate::cli::Args; | ||
use fhevm_engine_common::tfhe_ops::{current_ciphertext_version, deserialize_fhe_ciphertext}; | ||
use rand::RngCore; | ||
use rand::{Rnd, RngCore}; | ||
use std::collections::BTreeMap; | ||
use std::sync::atomic::{AtomicU16, Ordering}; | ||
use testcontainers::{core::WaitFor, runners::AsyncRunner, GenericImage, ImageExt}; | ||
use tokio::sync::watch::Receiver; | ||
|
||
pub struct TestInstance { | ||
// just to destroy container | ||
|
@@ -41,32 +42,81 @@ 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 | ||
} else if std::env::var("COPROCESSOR_TEST_LOCAL_DB").is_ok() { | ||
setup_test_app_existing_db().await | ||
} else { | ||
setup_test_app_custom_docker().await | ||
} | ||
} | ||
|
||
pub async fn setup_test_app_existing_localhost() -> Result<TestInstance, Box<dyn std::error::Error>> | ||
{ | ||
pub async fn setup_test_app_existing_localhost() -> Result<TestInstance, Box<dyn std::error::Error>> { | ||
const LOCAL_DB_URL: &str = "postgresql://postgres:[email protected]:5432/coprocessor"; | ||
Ok(TestInstance { | ||
_container: None, | ||
app_close_channel: None, | ||
app_url: "http://127.0.0.1:50051".to_string(), | ||
db_url: "postgresql://postgres:[email protected]:5432/coprocessor".to_string(), | ||
db_url: LOCAL_DB_URL.to_string(), | ||
}) | ||
} | ||
|
||
pub async fn setup_test_app_custom_docker() -> 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; | ||
Ok(TestInstance { | ||
_container: None, | ||
app_close_channel: Some(app_close_channel), | ||
app_url: format!("http://127.0.0.1:{app_port}"), | ||
db_url: LOCAL_DB_URL.to_string(), | ||
}) | ||
} | ||
|
||
async fn start_coprocessor(rx: Receiver<bool>, app_port: u16, db_url: &str) { | ||
let args: Args = Args { | ||
run_bg_worker: true, | ||
run_server: true, | ||
generate_fhe_keys: false, | ||
server_maximum_ciphertexts_to_schedule: 5000, | ||
work_items_batch_size: 40, | ||
tenant_key_cache_size: 4, | ||
coprocessor_fhe_threads: 4, | ||
maximum_handles_per_input: 255, | ||
tokio_threads: 2, | ||
pg_pool_max_connections: 2, | ||
server_addr: format!("127.0.0.1:{app_port}"), | ||
database_url: Some(db_url.to_string()), | ||
maximimum_compact_inputs_upload: 10, | ||
coprocessor_private_key: "./coprocessor.key".to_string(), | ||
}; | ||
|
||
std::thread::spawn(move || { | ||
crate::start_runtime(args, Some(rx)); | ||
}); | ||
|
||
// wait until app port is opened | ||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; | ||
} | ||
|
||
fn get_app_port() -> u16 { | ||
static PORT_COUNTER: AtomicU16 = AtomicU16::new(10000); | ||
|
||
let app_port = PORT_COUNTER.fetch_add(1, Ordering::SeqCst); | ||
// wrap around, if we ever have that many tests? | ||
if app_port >= 50000 { | ||
PORT_COUNTER.store(10000, Ordering::SeqCst); | ||
} | ||
app_port | ||
} | ||
|
||
async fn setup_test_app_custom_docker() -> Result<TestInstance, Box<dyn std::error::Error>> { | ||
let app_port = get_app_port(); | ||
|
||
let container = GenericImage::new("postgres", "15.7") | ||
.with_wait_for(WaitFor::message_on_stderr( | ||
|
@@ -102,30 +152,7 @@ pub async fn setup_test_app_custom_docker() -> Result<TestInstance, Box<dyn std: | |
println!("DB prepared"); | ||
|
||
let (app_close_channel, rx) = tokio::sync::watch::channel(false); | ||
let args: Args = Args { | ||
run_bg_worker: true, | ||
run_server: true, | ||
generate_fhe_keys: false, | ||
server_maximum_ciphertexts_to_schedule: 5000, | ||
work_items_batch_size: 40, | ||
tenant_key_cache_size: 4, | ||
coprocessor_fhe_threads: 4, | ||
maximum_handles_per_input: 255, | ||
tokio_threads: 2, | ||
pg_pool_max_connections: 2, | ||
server_addr: format!("127.0.0.1:{app_port}"), | ||
database_url: Some(db_url.clone()), | ||
maximimum_compact_inputs_upload: 10, | ||
coprocessor_private_key: "./coprocessor.key".to_string(), | ||
}; | ||
|
||
std::thread::spawn(move || { | ||
crate::start_runtime(args, Some(rx)); | ||
}); | ||
|
||
// wait until app port is opened | ||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; | ||
|
||
start_coprocessor(rx, app_port, &db_url).await; | ||
Ok(TestInstance { | ||
_container: Some(container), | ||
app_close_channel: Some(app_close_channel), | ||
|