From 441555e83f56ddedd9f91ec91ad8b18781087fab Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 12:12:40 +0530 Subject: [PATCH 01/46] remove unused image url param --- operator/control-plane/src/aws.rs | 15 ++------------- operator/control-plane/src/market.rs | 4 ---- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/operator/control-plane/src/aws.rs b/operator/control-plane/src/aws.rs index b0417942..0f67c719 100644 --- a/operator/control-plane/src/aws.rs +++ b/operator/control-plane/src/aws.rs @@ -852,7 +852,6 @@ EOF &self, job: &JobId, instance_type: InstanceType, - image_url: &str, family: &str, architecture: &str, region: &str, @@ -1343,7 +1342,6 @@ EOF pub async fn spin_up_instance( &self, - image_url: &str, job: &JobId, instance_type: &str, family: &str, @@ -1397,7 +1395,7 @@ EOF return Err(anyhow!("Required memory or vcpus are more than available")); } let instance = self - .launch_instance(job, instance_type, image_url, family, &architecture, region) + .launch_instance(job, instance_type, family, &architecture, region) .await .context("could not launch instance")?; sleep(Duration::from_secs(100)).await; @@ -1538,7 +1536,6 @@ EOF impl InfraProvider for Aws { async fn spin_up( &mut self, - eif_url: &str, job: &JobId, instance_type: &str, family: &str, @@ -1548,15 +1545,7 @@ impl InfraProvider for Aws { _bandwidth: u64, ) -> Result { let instance = self - .spin_up_instance( - eif_url, - job, - instance_type, - family, - region, - req_mem, - req_vcpu, - ) + .spin_up_instance(job, instance_type, family, region, req_mem, req_vcpu) .await .context("could not spin up instance")?; Ok(instance) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 3dd324f2..4ac4a915 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -34,7 +34,6 @@ pub struct JobId { pub trait InfraProvider { fn spin_up( &mut self, - eif_url: &str, job: &JobId, instance_type: &str, family: &str, @@ -100,7 +99,6 @@ where { async fn spin_up( &mut self, - eif_url: &str, job: &JobId, instance_type: &str, family: &str, @@ -111,7 +109,6 @@ where ) -> Result { (**self) .spin_up( - eif_url, job, instance_type, family, @@ -733,7 +730,6 @@ impl<'a> JobState<'a> { info!("Launching new instance"); let res = infra_provider .spin_up( - self.eif_url.as_str(), &self.job_id, self.instance_type.as_str(), self.family.as_str(), From a5745435387062c3ec17c49fc6aad55d0fceaf21 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 12:19:44 +0530 Subject: [PATCH 02/46] add a new outcome for enclave image updates and track directly --- operator/control-plane/src/test.rs | 65 +++++++++--------------------- 1 file changed, 20 insertions(+), 45 deletions(-) diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index b3fa176c..63fbed1f 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -54,12 +54,23 @@ pub struct RunEnclaveOutcome { pub debug: bool, } +#[cfg(test)] +#[derive(Clone, Debug)] +pub struct UpdateEnclaveImageOutcome { + pub instance_id: String, + pub region: String, + pub eif_url: String, + pub req_mem: i64, + pub req_vcpu: i32, +} + #[cfg(test)] #[derive(Clone, Debug)] pub enum TestAwsOutcome { SpinUp(SpinUpOutcome), SpinDown(SpinDownOutcome), RunEnclave(RunEnclaveOutcome), + UpdateEnclaveImage(UpdateEnclaveImageOutcome), } #[cfg(test)] @@ -229,51 +240,15 @@ impl InfraProvider for TestAws { req_vcpu: i32, req_mem: i64, ) -> Result<()> { - let job_id = self.instances.iter().find_map(|(key, val)| { - if val.instance_id == instance_id { - Some(key) - } else { - None - } - }); - if job_id.is_none() { - return Err(anyhow!( - "Instance not found for instance_id - {instance_id}" - )); - } - let job_id = job_id.unwrap(); - - let spin_up_outcome_index = - self.outcomes - .iter() - .enumerate() - .find_map(|(i, outcome)| match outcome { - TestAwsOutcome::SpinUp(spin_up) if spin_up.job == instance_id => Some(i), - _ => None, - }); - - if spin_up_outcome_index.is_none() { - return Err(anyhow!("Spin up outcome not found for job - {}", job_id)); - } - - let spin_up_outcome_index = spin_up_outcome_index.unwrap(); - - if let TestAwsOutcome::SpinUp(spin_up_outcome) = &mut self.outcomes[spin_up_outcome_index] { - if spin_up_outcome.region != region - || spin_up_outcome.req_vcpu != req_vcpu - || spin_up_outcome.req_mem != req_mem - { - return Err(anyhow!("Can only change EIF URL")); - } - - if spin_up_outcome.eif_url == eif_url { - return Err(anyhow!("Must input a different EIF URL")); - } - - eif_url.clone_into(&mut spin_up_outcome.eif_url); - } else { - panic!("Spin up outcome not found at proper index for the job.") - } + self.outcomes.push(TestAwsOutcome::UpdateEnclaveImage( + UpdateEnclaveImageOutcome { + instance_id: instance_id.to_owned(), + region: region.to_owned(), + eif_url: eif_url.to_owned(), + req_mem, + req_vcpu, + }, + )); Ok(()) } From c2510e0bb5f2087d41ab4ec605e87d0bf2c71500 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 12:22:10 +0530 Subject: [PATCH 03/46] clean up removed image url param --- operator/control-plane/src/market.rs | 10 ---------- operator/control-plane/src/test.rs | 4 ---- 2 files changed, 14 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 4ac4a915..2d6466d5 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1429,7 +1429,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -1513,7 +1512,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -1597,7 +1595,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); } else { panic!(); }; @@ -1682,7 +1679,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -1770,7 +1766,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -2164,7 +2159,6 @@ mod tests { && out.req_mem == 4096 && out.req_vcpu == 2 && out.bandwidth == 76 - && out.eif_url == "https://example.com/enclave.eif" && out.contract_address == "xyz" && out.chain_id == "123" ) @@ -2253,7 +2247,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -2339,7 +2332,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -2517,7 +2509,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { @@ -2800,7 +2791,6 @@ mod tests { assert!(out.req_mem == 4096); assert!(out.req_vcpu == 2); assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/updated-enclave.eif"); assert!(out.contract_address == "xyz"); assert!(out.chain_id == "123"); } else { diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 63fbed1f..7932e11d 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -24,7 +24,6 @@ pub struct SpinUpOutcome { pub req_mem: i64, pub req_vcpu: i32, pub bandwidth: u64, - pub eif_url: String, pub contract_address: String, pub chain_id: String, pub instance_id: String, @@ -113,7 +112,6 @@ pub struct TestAws { impl InfraProvider for TestAws { async fn spin_up( &mut self, - eif_url: &str, job: &JobId, instance_type: &str, family: &str, @@ -133,7 +131,6 @@ impl InfraProvider for TestAws { req_mem, req_vcpu, bandwidth, - eif_url: eif_url.to_owned(), contract_address: job.contract.clone(), chain_id: job.chain.clone(), instance_id: x.1.instance_id.clone(), @@ -155,7 +152,6 @@ impl InfraProvider for TestAws { req_mem, req_vcpu, bandwidth, - eif_url: eif_url.to_owned(), contract_address: job.contract.clone(), chain_id: job.chain.clone(), instance_id: instance_metadata.instance_id.clone(), From 6d6fcbfc16cf6c479d8ba34536cab62675a59c3c Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 13:11:07 +0530 Subject: [PATCH 04/46] add timestamp to update enclave outcome --- operator/control-plane/src/test.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 7932e11d..9a3af2f1 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -56,6 +56,7 @@ pub struct RunEnclaveOutcome { #[cfg(test)] #[derive(Clone, Debug)] pub struct UpdateEnclaveImageOutcome { + pub time: Instant, pub instance_id: String, pub region: String, pub eif_url: String, @@ -238,6 +239,7 @@ impl InfraProvider for TestAws { ) -> Result<()> { self.outcomes.push(TestAwsOutcome::UpdateEnclaveImage( UpdateEnclaveImageOutcome { + time: Instant::now(), instance_id: instance_id.to_owned(), region: region.to_owned(), eif_url: eif_url.to_owned(), From 18b0a26842699acd8bae4a6acdcc3631fdbfbcd7 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 13:11:22 +0530 Subject: [PATCH 05/46] implement equality so it can be checked directly --- operator/control-plane/src/test.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 9a3af2f1..d768a0c3 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -14,7 +14,7 @@ use tokio_stream::StreamExt; use crate::market::{GBRateCard, InfraProvider, JobId, LogsProvider, RateCard, RegionalRates}; #[cfg(test)] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct SpinUpOutcome { pub time: Instant, pub job: String, @@ -30,7 +30,7 @@ pub struct SpinUpOutcome { } #[cfg(test)] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct SpinDownOutcome { pub time: Instant, pub job: String, @@ -39,7 +39,7 @@ pub struct SpinDownOutcome { } #[cfg(test)] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct RunEnclaveOutcome { pub time: Instant, pub job: String, @@ -54,7 +54,7 @@ pub struct RunEnclaveOutcome { } #[cfg(test)] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub struct UpdateEnclaveImageOutcome { pub time: Instant, pub instance_id: String, @@ -65,7 +65,7 @@ pub struct UpdateEnclaveImageOutcome { } #[cfg(test)] -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] pub enum TestAwsOutcome { SpinUp(SpinUpOutcome), SpinDown(SpinDownOutcome), From aed254e266b1094fb1829934c2b47ddf97d8c86d Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 13:11:55 +0530 Subject: [PATCH 06/46] refactor out a common test runner --- operator/control-plane/src/market.rs | 58 +++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 2d6466d5..bbb3e6d9 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1366,8 +1366,9 @@ fn now_timestamp() -> Duration { #[cfg(test)] mod tests { use std::str::FromStr; + use std::time::UNIX_EPOCH; - use alloy::hex::ToHexExt; + use alloy::hex::{FromHex, ToHexExt}; use alloy::primitives::{Bytes, B256, U256}; use alloy::rpc::types::eth::Log; use alloy::sol_types::SolValue; @@ -1377,6 +1378,61 @@ mod tests { use crate::market; use crate::test::{self, Action, TestAws, TestAwsOutcome}; + struct JobManagerParams { + job_id: market::JobId, + allowed_regions: Vec, + address_whitelist: Vec, + address_blacklist: Vec, + } + + struct TestResults { + res: i8, + outcomes: Vec, + } + + async fn run_test( + start_time: Instant, + logs: Vec<(u64, Action, Bytes)>, + job_manager_params: JobManagerParams, + test_results: TestResults, + ) { + let _ = market::START.set(start_time); + + let job_num = B256::from_hex(&job_manager_params.job_id.id).unwrap(); + let job_logs: Vec<(u64, Log)> = logs + .into_iter() + .map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))) + .collect(); + + // pending stream appended so job stream never ends + let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) + .then(|(moment, log)| async move { + let delay = start_time + Duration::from_secs(moment) - Instant::now(); + sleep(delay).await; + log + }) + .chain(tokio_stream::pending())); + + let mut aws: TestAws = Default::default(); + let res = market::job_manager_once( + job_stream, + &mut aws, + job_manager_params.job_id, + &job_manager_params.allowed_regions, + 300, + &test::get_rates(), + &test::get_gb_rates(), + &job_manager_params.address_whitelist, + &job_manager_params.address_blacklist, + ) + .await; + + assert!(aws.instances.is_empty()); + + assert_eq!(res, test_results.res); + assert_eq!(aws.outcomes, test_results.outcomes); + } + #[tokio::test(start_paused = true)] async fn test_instance_launch_after_delay_on_spin_up() { let _ = market::START.set(Instant::now()); From b9b8ca712af491cb42950ab80da75065dd3fa17e Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 13:22:03 +0530 Subject: [PATCH 07/46] remove randomness and replace with deterministic hash of a counter --- operator/control-plane/src/server.rs | 4 +-- operator/control-plane/src/test.rs | 43 ++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/operator/control-plane/src/server.rs b/operator/control-plane/src/server.rs index 9560a9c9..9dafa931 100644 --- a/operator/control-plane/src/server.rs +++ b/operator/control-plane/src/server.rs @@ -172,7 +172,7 @@ mod tests { for id in 1..4 { let temp_job_id = U256::from(id).to_be_bytes::<32>().encode_hex_with_prefix(); - let instance_metadata = InstanceMetadata::new(None, None).await; + let instance_metadata = InstanceMetadata::new(0).await; aws.instances .insert(temp_job_id.clone(), instance_metadata.clone()); @@ -228,7 +228,7 @@ mod tests { for id in 1..4 { let temp_job_id = U256::from(id).to_be_bytes::<32>().encode_hex_with_prefix(); - let instance_metadata = InstanceMetadata::new(None, None).await; + let instance_metadata = InstanceMetadata::new(0).await; aws.instances .insert(temp_job_id.clone(), instance_metadata.clone()); diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index d768a0c3..2366c483 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::hash::{DefaultHasher, Hasher}; use std::str::FromStr; use alloy::hex::ToHexExt; @@ -73,6 +74,22 @@ pub enum TestAwsOutcome { UpdateEnclaveImage(UpdateEnclaveImageOutcome), } +pub fn compute_instance_id(counter: u64) -> u64 { + let mut hasher = DefaultHasher::new(); + hasher.write_u8(0); + hasher.write_u64(counter); + + hasher.finish() +} + +pub fn compute_instance_ip(counter: u64) -> u64 { + let mut hasher = DefaultHasher::new(); + hasher.write_u8(1); + hasher.write_u64(counter); + + hasher.finish() +} + #[cfg(test)] #[derive(Clone, Debug)] pub struct InstanceMetadata { @@ -82,17 +99,15 @@ pub struct InstanceMetadata { #[cfg(test)] impl InstanceMetadata { - pub async fn new(instance_id: Option, ip_address: Option) -> Self { - let instance_id = "i-".to_string() + &instance_id.unwrap_or(B128::random().encode_hex()); - - let ip_address = ip_address.unwrap_or( - B64::random() - .as_slice() - .iter() - .map(|x| x.to_string()) - .reduce(|a, b| a + "." + &b) - .unwrap(), - ); + pub async fn new(counter: u64) -> Self { + let instance_id = compute_instance_id(counter).to_string(); + let ip_address = compute_instance_ip(counter) + .to_le_bytes() + .iter() + .map(|x| x.to_string()) + .reduce(|a, b| a + "." + &b) + .unwrap(); + Self { instance_id, ip_address, @@ -107,6 +122,8 @@ pub struct TestAws { // HashMap format - (Job, InstanceMetadata) pub instances: HashMap, + + counter: u64, } #[cfg(test)] @@ -140,7 +157,9 @@ impl InfraProvider for TestAws { return Ok(x.1.instance_id.clone()); } - let instance_metadata: InstanceMetadata = InstanceMetadata::new(None, None).await; + let instance_metadata: InstanceMetadata = InstanceMetadata::new(self.counter).await; + self.counter += 1; + self.instances .insert(job.id.clone(), instance_metadata.clone()); From 49070c0c76d115b6b832f6354793f688711650c0 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 18:45:40 +0530 Subject: [PATCH 08/46] directly returns strings --- operator/control-plane/src/test.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 2366c483..56f1d069 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -74,20 +74,28 @@ pub enum TestAwsOutcome { UpdateEnclaveImage(UpdateEnclaveImageOutcome), } -pub fn compute_instance_id(counter: u64) -> u64 { +pub fn compute_instance_id(counter: u64) -> String { let mut hasher = DefaultHasher::new(); hasher.write_u8(0); hasher.write_u64(counter); - hasher.finish() + let hash = hasher.finish(); + + format!("{:x}", hash) } -pub fn compute_instance_ip(counter: u64) -> u64 { +pub fn compute_instance_ip(counter: u64) -> String { let mut hasher = DefaultHasher::new(); hasher.write_u8(1); hasher.write_u64(counter); - hasher.finish() + let hash = hasher.finish(); + + hash.to_le_bytes() + .iter() + .map(|x| x.to_string()) + .reduce(|a, b| a + "." + &b) + .unwrap() } #[cfg(test)] @@ -100,13 +108,8 @@ pub struct InstanceMetadata { #[cfg(test)] impl InstanceMetadata { pub async fn new(counter: u64) -> Self { - let instance_id = compute_instance_id(counter).to_string(); - let ip_address = compute_instance_ip(counter) - .to_le_bytes() - .iter() - .map(|x| x.to_string()) - .reduce(|a, b| a + "." + &b) - .unwrap(); + let instance_id = compute_instance_id(counter); + let ip_address = compute_instance_ip(counter); Self { instance_id, From 4deae8eeae3146fcc9a3c7d18ef28bada6b61024 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 19:02:49 +0530 Subject: [PATCH 09/46] convert the first test to use the new runner --- operator/control-plane/src/market.rs | 117 +++++++++++---------------- 1 file changed, 48 insertions(+), 69 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index bbb3e6d9..45b1c8c2 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1376,7 +1376,7 @@ mod tests { use tokio_stream::StreamExt; use crate::market; - use crate::test::{self, Action, TestAws, TestAwsOutcome}; + use crate::test::{self, compute_instance_id, Action, TestAws, TestAwsOutcome}; struct JobManagerParams { job_id: market::JobId, @@ -1392,7 +1392,7 @@ mod tests { async fn run_test( start_time: Instant, - logs: Vec<(u64, Action, Bytes)>, + logs: Vec<(u64, Action, Vec)>, job_manager_params: JobManagerParams, test_results: TestResults, ) { @@ -1435,85 +1435,64 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_instance_launch_after_delay_on_spin_up() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (301, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 1); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(301), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From a6785b9a91e30d825d30125c046dfd3f11fad9f5 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 19:07:06 +0530 Subject: [PATCH 10/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 113 +++++++++++---------------- 1 file changed, 46 insertions(+), 67 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 45b1c8c2..6ff54310 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1497,85 +1497,64 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_instance_launch_with_debug_mode_on_spin_up() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2,\"debug\":true}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (301, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == true); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 1); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: true, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(301), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 664bc7b5965738b6dec0bb0d5c5364320bfdfd24 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 19:09:59 +0530 Subject: [PATCH 11/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 111 +++++++++++---------------- 1 file changed, 46 insertions(+), 65 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 6ff54310..1d6fbd23 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1559,83 +1559,64 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_instance_launch_after_delay_on_spin_up_with_specific_family() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2,\"family\":\"tuna\"}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (301, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "tuna"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "tuna"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 1); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "tuna".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "tuna".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(301), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From cf1c242a47f7ff74719935b1f3318bbaafa2f0a1 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 28 Nov 2024 19:13:52 +0530 Subject: [PATCH 12/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 113 +++++++++++---------------- 1 file changed, 46 insertions(+), 67 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 1d6fbd23..fa1b53a1 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1621,88 +1621,67 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_deposit_withdraw_settle() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (40, Action::Deposit, 500.abi_encode()), (60, Action::Withdraw, 500.abi_encode()), (100, Action::Settle, (2, 6).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 205); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(505), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 53ab62e4fdd5ec04bc979911b99c464b4dde7355 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 10:55:59 +0530 Subject: [PATCH 13/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 113 +++++++++++---------------- 1 file changed, 46 insertions(+), 67 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index fa1b53a1..a494b72f 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1686,89 +1686,68 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_revise_rate_cancel() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (50, Action::ReviseRateInitiated, 32000000000000u64.abi_encode()), (100, Action::ReviseRateFinalized, 32000000000000u64.abi_encode()), (150, Action::ReviseRateInitiated, 60000000000000u64.abi_encode()), (200, Action::ReviseRateCancelled, [].into()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 205); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(505), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 391a0ccdb2e7ec399823a7cbdf9bee85495445aa Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:03:03 +0530 Subject: [PATCH 14/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index a494b72f..dad81f2a 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1752,46 +1752,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_unsupported_region() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-east-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From ed9c8c22d19e59fa020c03bd1af5cb7b20e7d54a Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:04:49 +0530 Subject: [PATCH 15/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index dad81f2a..66bce3d3 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1782,46 +1782,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_region_not_found() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From a0574bef63425e1a4f56fa316e8f79851f18811f Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:08:36 +0530 Subject: [PATCH 16/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 66bce3d3..35cc3a3a 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1812,46 +1812,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_instance_type_not_found() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From aa1df534d21d1623d230a0f6deab9bfb99c18f93 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:10:38 +0530 Subject: [PATCH 17/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 35cc3a3a..ef8bd55e 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1842,46 +1842,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_unsupported_instance() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.vsmall\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 586ad3fb33c4af693dc9a7518e9dc5962e0a0593 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:12:03 +0530 Subject: [PATCH 18/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index ef8bd55e..c2d77082 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1872,46 +1872,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_eif_url_not_found() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"instance\":\"c6a.vsmall\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 3b82d33257cd215100f033547f26c0d5af513b99 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:15:42 +0530 Subject: [PATCH 19/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index c2d77082..15ad1881 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1902,46 +1902,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_min_rate() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),29000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, 0); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: 0, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 31b23feb85fa631bc648c8f27c24bec7af5cf0e3 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:22:27 +0530 Subject: [PATCH 20/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 48 ++++++++++------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 15ad1881..fbcc3843 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1932,46 +1932,32 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_rate_exceed_balance() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,0u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, 0); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: 0, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 6108d013c06a4d481117016d129e40cf5d8d6926 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:32:15 +0530 Subject: [PATCH 21/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 118 +++++++++++---------------- 1 file changed, 48 insertions(+), 70 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index fbcc3843..fcb5b9be 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1960,91 +1960,69 @@ mod tests { run_test(start_time, logs, job_manager_params, test_results).await; } + // NOTE: This scenario should be impossible based on how the contract should be written + // Nevertheless, the cp should handle it to be defensive, so we test #[tokio::test(start_paused = true)] async fn test_withdrawal_exceed_rate() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (350, Action::Withdraw, 30000u64.abi_encode()), (500, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!( - B256::from_str(&out.job).unwrap() == job_num - && out.instance_type == "c6a.xlarge" - && out.family == "salmon" - && out.region == "ap-south-1" - && out.req_mem == 4096 - && out.req_vcpu == 2 - && out.bandwidth == 76 - && out.contract_address == "xyz" - && out.chain_id == "123" - ) - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!( - B256::from_str(&out.job).unwrap() == job_num - && out.instance_id == instance_id - && out.family == "salmon" - && out.region == "ap-south-1" - && out.req_mem == 4096 - && out.req_vcpu == 2 - && out.bandwidth == 76 - && out.eif_url == "https://example.com/enclave.eif" - && out.debug == false - ) - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 50); - assert!(B256::from_str(&out.job).unwrap() == job_num && out.region == *"ap-south-1") - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(350), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From dab3dde2c5d11ea30cb0c478b081677367be78f6 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:36:39 +0530 Subject: [PATCH 22/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 113 +++++++++++---------------- 1 file changed, 46 insertions(+), 67 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index fcb5b9be..d1d9fd40 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2027,88 +2027,67 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_revise_rate_lower_higher() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (350, Action::ReviseRateInitiated, 29000000000000u64.abi_encode()), (400, Action::ReviseRateFinalized, 29000000000000u64.abi_encode()), (450, Action::ReviseRateInitiated, 31000000000000u64.abi_encode()), (500, Action::ReviseRateFinalized, 31000000000000u64.abi_encode()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 50); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(350), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 171ef1b4efcb700d129bdbb67898481f49e06e6f Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:40:54 +0530 Subject: [PATCH 23/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 115 +++++++++++---------------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index d1d9fd40..97e3a8fe 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2092,87 +2092,66 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_address_whitelisted() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (500, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::from([ + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - ]), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + ], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 200); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(500), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From a34565e08aca0d0b115480b5b284794eecc43508 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:43:48 +0530 Subject: [PATCH 24/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 50 ++++++++++------------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 97e3a8fe..852daee4 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2156,48 +2156,34 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_address_not_whitelisted() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (500, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::from([ + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), - ]), - &Vec::new(), - ) - .await; + ], + address_blacklist: vec![], + }; - // job manager should have finished successfully - assert_eq!(res, 0); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: 0, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From f5d1791d20447c33df00e0ebdcb57fb55ba36694 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:45:20 +0530 Subject: [PATCH 25/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 50 ++++++++++------------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 852daee4..80afc2fb 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2188,48 +2188,34 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_address_blacklisted() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (500, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::from([ + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - ]), - ) - .await; + ], + }; - // job manager should have finished successfully - assert_eq!(res, 0); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: 0, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From 323f87ef3363981c882a0a84e6e17fe5c463ad0f Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:51:35 +0530 Subject: [PATCH 26/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 115 +++++++++++---------------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 80afc2fb..f0c77c2d 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2220,87 +2220,66 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_address_not_blacklisted() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ + let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), (500, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::from([ + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), - ]), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + ], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 200); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(300), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(500), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } // Tests for whitelist blacklist checks From 61ebdcc86c4250b744ab20eb1582a8f4a1734cb0 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 11:51:48 +0530 Subject: [PATCH 27/46] remove unnecessary START sets in some tests --- operator/control-plane/src/market.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index f0c77c2d..a6a24e97 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2285,8 +2285,6 @@ mod tests { // Tests for whitelist blacklist checks #[tokio::test] async fn test_whitelist_blacklist_check_no_list() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); @@ -2302,8 +2300,6 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_whitelisted() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); @@ -2322,8 +2318,6 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_not_whitelisted() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); @@ -2342,8 +2336,6 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_blacklisted() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); @@ -2362,8 +2354,6 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_not_blacklisted() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); @@ -2382,8 +2372,6 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_neither() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); @@ -2405,8 +2393,6 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_both() { - let _ = market::START.set(Instant::now()); - let log = test::get_log(Action::Open, Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), B256::ZERO); From 350787819e60bfed88543cb3f9d8e961581744c8 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:06:00 +0530 Subject: [PATCH 28/46] create a SystemContext trait to encapsulate simulatable behaviour --- operator/control-plane/src/market.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index a6a24e97..2c97fbe7 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -15,7 +15,12 @@ use tokio::time::{Duration, Instant}; use tokio_stream::StreamExt; use tracing::{error, info, info_span, Instrument}; -// IMPORTANT: do not import SystemTime, use the now_timestamp helper +// IMPORTANT: do not import SystemTime, use a SystemContext + +// Trait to encapsulate behaviour that should be simulated in tests +trait SystemContext { + fn now_timestamp() -> Duration; +} // Basic architecture: // One future listening to new jobs From 59d7a3e7475972b8e0392e080ea7af2c4f59e607 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:10:13 +0530 Subject: [PATCH 29/46] implement a "real" system context with actual implementations --- operator/control-plane/src/market.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 2c97fbe7..e9231933 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -22,6 +22,17 @@ trait SystemContext { fn now_timestamp() -> Duration; } +struct RealSystemContext {} + +impl SystemContext for RealSystemContext { + fn now_timestamp() -> Duration { + use std::time::SystemTime; + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + } +} + // Basic architecture: // One future listening to new jobs // Each job has its own future managing its lifetime From ee1c2c2eb20552f49c7c43d64d9ef1cf94e758fa Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:33:19 +0530 Subject: [PATCH 30/46] should have self param --- operator/control-plane/src/market.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index e9231933..b2cb4cc1 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -19,13 +19,13 @@ use tracing::{error, info, info_span, Instrument}; // Trait to encapsulate behaviour that should be simulated in tests trait SystemContext { - fn now_timestamp() -> Duration; + fn now_timestamp(&self) -> Duration; } struct RealSystemContext {} impl SystemContext for RealSystemContext { - fn now_timestamp() -> Duration { + fn now_timestamp(&self) -> Duration { use std::time::SystemTime; SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) From 3b079e56e2216ed9cf0d32d31e294b5a082ee969 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:33:43 +0530 Subject: [PATCH 31/46] store the context in JobState and use it for timestamps --- operator/control-plane/src/market.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index b2cb4cc1..e19b0405 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -524,6 +524,9 @@ fn whitelist_blacklist_check( } struct JobState<'a> { + // NOTE: not sure if dyn is a good idea, revisit later + context: &'a (dyn SystemContext + Send + Sync), + job_id: JobId, launch_delay: u64, allowed_regions: &'a [String], @@ -554,15 +557,21 @@ struct JobState<'a> { } impl<'a> JobState<'a> { - fn new(job_id: JobId, launch_delay: u64, allowed_regions: &[String]) -> JobState { + fn new( + context: &'a (dyn SystemContext + Send + Sync), + job_id: JobId, + launch_delay: u64, + allowed_regions: &'a [String], + ) -> JobState<'a> { // solvency metrics // default of 60s JobState { + context, job_id, launch_delay, allowed_regions, balance: U256::from(360), - last_settled: now_timestamp(), + last_settled: context.now_timestamp(), rate: U256::from(1), original_rate: U256::from(1), instance_id: String::new(), @@ -584,7 +593,7 @@ impl<'a> JobState<'a> { } fn insolvency_duration(&self) -> Duration { - let now_ts = now_timestamp(); + let now_ts = self.context.now_timestamp(); if self.rate == U256::ZERO { Duration::from_secs(0) From 57247ce8653f9e0fca71857e6608eede7d2fe53a Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:34:10 +0530 Subject: [PATCH 32/46] create a real context and pass it along --- operator/control-plane/src/market.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index e19b0405..fb2e49f9 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -468,6 +468,7 @@ async fn job_manager( let job_stream = std::pin::pin!(res.unwrap()); let res = job_manager_once( + RealSystemContext {}, job_stream, infra_provider.clone(), job_id.clone(), @@ -1239,6 +1240,7 @@ impl<'a> JobState<'a> { // manage the complete lifecycle of a job // returns true if "done" async fn job_manager_once( + context: impl SystemContext + Send + Sync, mut job_stream: impl StreamExt + Unpin, mut infra_provider: impl InfraProvider + Send + Sync, job_id: JobId, @@ -1249,7 +1251,7 @@ async fn job_manager_once( address_whitelist: &[String], address_blacklist: &[String], ) -> i8 { - let mut state = JobState::new(job_id, aws_delay_duration, allowed_regions); + let mut state = JobState::new(&context, job_id, aws_delay_duration, allowed_regions); let res = 'event: loop { // compute time to insolvency From 82d52e664bdb06468063caae847c27f6bf432033 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:34:28 +0530 Subject: [PATCH 33/46] create a test system context with a settable start time --- operator/control-plane/src/market.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index fb2e49f9..e588096d 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1405,6 +1405,18 @@ mod tests { use crate::market; use crate::test::{self, compute_instance_id, Action, TestAws, TestAwsOutcome}; + use super::SystemContext; + + struct TestSystemContext { + start: Instant, + } + + impl SystemContext for TestSystemContext { + fn now_timestamp(&self) -> Duration { + Instant::now() - self.start + } + } + struct JobManagerParams { job_id: market::JobId, allowed_regions: Vec, From 99682117cc8e7b09580b8a6474f56d1c983c60c1 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:36:12 +0530 Subject: [PATCH 34/46] replace now_timestamp call with plain 0, that is how it has more or less always worked --- operator/control-plane/src/market.rs | 50 ++++++++++++++-------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index e588096d..0efb191a 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1478,7 +1478,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (301, Action::Close, [].into()), ]; @@ -1540,7 +1540,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2,\"debug\":true}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2,\"debug\":true}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (301, Action::Close, [].into()), ]; @@ -1602,7 +1602,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2,\"family\":\"tuna\"}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2,\"family\":\"tuna\"}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (301, Action::Close, [].into()), ]; @@ -1664,7 +1664,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (40, Action::Deposit, 500.abi_encode()), (60, Action::Withdraw, 500.abi_encode()), (100, Action::Settle, (2, 6).abi_encode_sequence()), @@ -1729,7 +1729,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (50, Action::ReviseRateInitiated, 32000000000000u64.abi_encode()), (100, Action::ReviseRateFinalized, 32000000000000u64.abi_encode()), (150, Action::ReviseRateInitiated, 60000000000000u64.abi_encode()), @@ -1795,7 +1795,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-east-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-east-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -1825,7 +1825,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -1855,7 +1855,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -1885,7 +1885,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.vsmall\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.vsmall\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -1915,7 +1915,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"instance\":\"c6a.vsmall\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"instance\":\"c6a.vsmall\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -1945,7 +1945,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),29000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),29000000000000u64,31000u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -1975,7 +1975,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,0u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,0u64,0).abi_encode_sequence()), (505, Action::Close, [].into()), ]; @@ -2007,7 +2007,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (350, Action::Withdraw, 30000u64.abi_encode()), (500, Action::Close, [].into()), ]; @@ -2070,7 +2070,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (350, Action::ReviseRateInitiated, 29000000000000u64.abi_encode()), (400, Action::ReviseRateFinalized, 29000000000000u64.abi_encode()), (450, Action::ReviseRateInitiated, 31000000000000u64.abi_encode()), @@ -2135,7 +2135,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (500, Action::Close, [].into()), ]; @@ -2199,7 +2199,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (500, Action::Close, [].into()), ]; @@ -2231,7 +2231,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (500, Action::Close, [].into()), ]; @@ -2263,7 +2263,7 @@ mod tests { let job_id = format!("{:064x}", 1); let logs = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (500, Action::Close, [].into()), ]; @@ -2325,7 +2325,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_no_list() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![]; @@ -2340,7 +2340,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_whitelisted() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), @@ -2358,7 +2358,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_not_whitelisted() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49eb".to_string(), @@ -2376,7 +2376,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_blacklisted() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![ @@ -2394,7 +2394,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_not_blacklisted() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![ @@ -2412,7 +2412,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_neither() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49eb".to_string(), @@ -2433,7 +2433,7 @@ mod tests { #[tokio::test] async fn test_whitelist_blacklist_check_both() { let log = test::get_log(Action::Open, - Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), From c968a4795da6d0321bacb06e84026d1a95922b9a Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:36:28 +0530 Subject: [PATCH 35/46] create test system context and pass along --- operator/control-plane/src/market.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 0efb191a..eb0fba77 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1435,7 +1435,7 @@ mod tests { job_manager_params: JobManagerParams, test_results: TestResults, ) { - let _ = market::START.set(start_time); + let context = TestSystemContext { start: start_time }; let job_num = B256::from_hex(&job_manager_params.job_id.id).unwrap(); let job_logs: Vec<(u64, Log)> = logs @@ -1454,6 +1454,7 @@ mod tests { let mut aws: TestAws = Default::default(); let res = market::job_manager_once( + context, job_stream, &mut aws, job_manager_params.job_id, From 574859f363e4717f5b2137f89bb072434468b073 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:48:17 +0530 Subject: [PATCH 36/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 117 +++++++++++---------------- 1 file changed, 49 insertions(+), 68 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index eb0fba77..b5e1d34f 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1227,6 +1227,8 @@ impl<'a> JobState<'a> { } self.eif_url = url.to_string(); self.eif_update = true; + // WARN: this effectively delays the launch + // should revisit and see if it is desirable self.schedule_launch(self.launch_delay); } else { error!(topic = ?log.topics()[0], "Unknown event"); @@ -2508,86 +2510,65 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_eif_update_after_spin_up() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + let logs = vec![ + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), (100, Action::MetadataUpdated, "{\"region\":\"ap-south-1\",\"url\":\"https://example.com/updated-enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string().abi_encode()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; - - // job manager should have finished successfully - assert_eq!(res, 0); - let spin_up_tv_sec: Instant; - let instance_id: String; - if let TestAwsOutcome::SpinUp(out) = &aws.outcomes[0] { - spin_up_tv_sec = out.time; - instance_id = out.instance_id.clone(); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_type == "c6a.xlarge"); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.contract_address == "xyz"); - assert!(out.chain_id == "123"); - } else { - panic!(); - }; - - if let TestAwsOutcome::RunEnclave(out) = &aws.outcomes[1] { - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.instance_id == instance_id); - assert!(out.family == "salmon"); - assert!(out.region == "ap-south-1"); - assert!(out.req_mem == 4096); - assert!(out.req_vcpu == 2); - assert!(out.bandwidth == 76); - assert!(out.eif_url == "https://example.com/updated-enclave.eif"); - assert!(out.debug == false); - } else { - panic!(); + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], }; - if let TestAwsOutcome::SpinDown(out) = &aws.outcomes[2] { - assert_eq!((out.time - spin_up_tv_sec).as_secs(), 105); - assert!(B256::from_str(&out.job).unwrap() == job_num); - assert!(out.region == *"ap-south-1"); - } else { - panic!(); + let test_results = TestResults { + res: 0, + outcomes: vec![ + TestAwsOutcome::SpinUp(test::SpinUpOutcome { + time: start_time + Duration::from_secs(400), + job: job_id.clone(), + instance_type: "c6a.xlarge".into(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + contract_address: "xyz".into(), + chain_id: "123".into(), + instance_id: compute_instance_id(0), + }), + TestAwsOutcome::RunEnclave(test::RunEnclaveOutcome { + time: start_time + Duration::from_secs(400), + job: job_id.clone(), + family: "salmon".into(), + region: "ap-south-1".into(), + req_mem: 4096, + req_vcpu: 2, + bandwidth: 76, + instance_id: compute_instance_id(0), + eif_url: "https://example.com/updated-enclave.eif".into(), + debug: false, + }), + TestAwsOutcome::SpinDown(test::SpinDownOutcome { + time: start_time + Duration::from_secs(505), + job: job_id, + instance_id: compute_instance_id(0), + region: "ap-south-1".into(), + }), + ], }; - assert!(!aws.instances.contains_key(&job_num.to_string())) + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From c7ada6a23af3afcd35f544bf9e5864647f26a88e Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:51:43 +0530 Subject: [PATCH 37/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 50 ++++++++++------------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index b5e1d34f..e69c4615 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2573,48 +2573,34 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_other_metadata_update_after_spin_up() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + let logs = vec![ + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), // instance type has also been updated in the metadata. should fail this job. (100, Action::MetadataUpdated, "{\"region\":\"ap-south-1\",\"url\":\"https://example.com/updated-enclave.eif\",\"instance\":\"c6a.large\",\"memory\":4096,\"vcpu\":2}".to_string().abi_encode()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have errored out - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } #[tokio::test(start_paused = true)] From c36d49b881543238b147766913d1b1b29f83a24a Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:52:54 +0530 Subject: [PATCH 38/46] convert test to use the new runner --- operator/control-plane/src/market.rs | 50 ++++++++++------------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index e69c4615..ff33780d 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2605,47 +2605,33 @@ mod tests { #[tokio::test(start_paused = true)] async fn test_metadata_update_event_with_no_updates_after_spin_up() { - let _ = market::START.set(Instant::now()); + let start_time = Instant::now(); + let job_id = format!("{:064x}", 1); - let job_num = U256::from(1).into(); - let job_logs: Vec<(u64, Log)> = vec![ - (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,market::now_timestamp().as_secs()).abi_encode_sequence()), + let logs = vec![ + (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), // instance type has also been updated in the metadata. should fail this job. (100, Action::MetadataUpdated, "{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string().abi_encode()), (505, Action::Close, [].into()), - ].into_iter().map(|x| (x.0, test::get_log(x.1, Bytes::from(x.2), job_num))).collect(); + ]; - let start_time = Instant::now(); - // pending stream appended so job stream never ends - let job_stream = std::pin::pin!(tokio_stream::iter(job_logs.into_iter()) - .then(|(moment, log)| async move { - let delay = start_time + Duration::from_secs(moment) - Instant::now(); - sleep(delay).await; - log - }) - .chain(tokio_stream::pending())); - let mut aws: TestAws = Default::default(); - let res = market::job_manager_once( - job_stream, - &mut aws, - market::JobId { - id: job_num.encode_hex_with_prefix(), + let job_manager_params = JobManagerParams { + job_id: market::JobId { + id: job_id.clone(), operator: "abc".into(), contract: "xyz".into(), chain: "123".into(), }, - &["ap-south-1".into()], - 300, - &test::get_rates(), - &test::get_gb_rates(), - &Vec::new(), - &Vec::new(), - ) - .await; + allowed_regions: vec!["ap-south-1".to_owned()], + address_whitelist: vec![], + address_blacklist: vec![], + }; - // job manager should have errored out - assert_eq!(res, -2); - assert!(aws.outcomes.is_empty()); - assert!(!aws.instances.contains_key(&job_num.to_string())) + let test_results = TestResults { + res: -2, + outcomes: vec![], + }; + + run_test(start_time, logs, job_manager_params, test_results).await; } } From ca3b8e29fceaa51147047ae8d549893c0d091bef Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:53:46 +0530 Subject: [PATCH 39/46] clean up --- operator/control-plane/src/market.rs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index ff33780d..ee19c75d 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1371,23 +1371,6 @@ async fn job_logs( Ok(stream) } -#[cfg(not(test))] -fn now_timestamp() -> Duration { - // import here to ensure it is used only through this function - use std::time::SystemTime; - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() -} - -#[cfg(test)] -static START: std::sync::OnceLock = std::sync::OnceLock::new(); - -#[cfg(test)] -fn now_timestamp() -> Duration { - Instant::now() - *START.get().unwrap() -} - // -------------------------------------------------------------------------------------------------------------------------------------------------------- // TESTS // -------------------------------------------------------------------------------------------------------------------------------------------------------- From 471f2c46a98470de3d85d434e510821fa245765b Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Fri, 29 Nov 2024 12:55:47 +0530 Subject: [PATCH 40/46] clean up --- operator/control-plane/src/market.rs | 5 +---- operator/control-plane/src/test.rs | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index ee19c75d..31ea98bf 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1377,10 +1377,7 @@ async fn job_logs( #[cfg(test)] mod tests { - use std::str::FromStr; - use std::time::UNIX_EPOCH; - - use alloy::hex::{FromHex, ToHexExt}; + use alloy::hex::FromHex; use alloy::primitives::{Bytes, B256, U256}; use alloy::rpc::types::eth::Log; use alloy::sol_types::SolValue; diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 56f1d069..d2cd2a7c 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -2,9 +2,7 @@ use std::collections::HashMap; use std::hash::{DefaultHasher, Hasher}; use std::str::FromStr; -use alloy::hex::ToHexExt; -use alloy::primitives::B128; -use alloy::primitives::{keccak256, Address, Bytes, LogData, B256, B64, U256}; +use alloy::primitives::{keccak256, Address, Bytes, LogData, B256, U256}; use alloy::providers::Provider; use alloy::pubsub::PubSubFrontend; use alloy::rpc::types::eth::Log; From d5257796ceb7ce0d09d54434e0ce3ff37b4b9ef4 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 5 Dec 2024 16:24:06 +0530 Subject: [PATCH 41/46] remove incorrect comment --- operator/control-plane/src/market.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 31ea98bf..23043931 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2590,7 +2590,6 @@ mod tests { let logs = vec![ (0, Action::Open, ("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), - // instance type has also been updated in the metadata. should fail this job. (100, Action::MetadataUpdated, "{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string().abi_encode()), (505, Action::Close, [].into()), ]; From 0a53292213bc92c7accdbefe456bd57192abcbf8 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 5 Dec 2024 18:31:00 +0530 Subject: [PATCH 42/46] add function to deterministically generate addresses --- operator/control-plane/src/test.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index d2cd2a7c..286346f9 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::hash::{DefaultHasher, Hasher}; use std::str::FromStr; -use alloy::primitives::{keccak256, Address, Bytes, LogData, B256, U256}; +use alloy::primitives::{keccak256, Address, Bytes, FixedBytes, LogData, B256, U256}; use alloy::providers::Provider; use alloy::pubsub::PubSubFrontend; use alloy::rpc::types::eth::Log; @@ -96,6 +96,16 @@ pub fn compute_instance_ip(counter: u64) -> String { .unwrap() } +pub fn compute_address(salt: &str) -> FixedBytes<32> { + let mut hasher = DefaultHasher::new(); + hasher.write_u8(2); + hasher.write(salt.as_bytes()); + + let hash = hasher.finish(); + + FixedBytes::<32>::from_slice(hash.to_le_bytes().repeat(4).as_slice()) +} + #[cfg(test)] #[derive(Clone, Debug)] pub struct InstanceMetadata { From adeb04c85c6bca5cf9e3d229ea6cbff290e0c848 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 5 Dec 2024 18:31:37 +0530 Subject: [PATCH 43/46] use compute_address to get different addresses for each use instead of hardcoding the same one --- operator/control-plane/src/test.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 286346f9..416e3eda 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -357,7 +357,7 @@ pub fn get_gb_rates() -> Vec { pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { let mut log = Log { inner: alloy::primitives::Log { - address: Address::from_str("0x0F5F91BA30a00bD43Bd19466f020B3E5fc7a49ec").unwrap(), + address: Address::from_str("0x000000000000000000000000000000000000dead").unwrap(), data: LogData::new_unchecked(vec![], Bytes::new()), }, ..Default::default() @@ -368,8 +368,8 @@ pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { vec![ keccak256("JobOpened(bytes32,string,address,address,uint256,uint256,uint256)"), idx, - log.address().into_word(), - log.address().into_word(), + compute_address("owner"), + compute_address("provider"), ], data, ); @@ -389,7 +389,7 @@ pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { vec![ keccak256("JobDeposited(bytes32,address,uint256)"), idx, - log.address().into_word(), + compute_address("depositor"), ], data, ); @@ -399,7 +399,7 @@ pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { vec![ keccak256("JobWithdrew(bytes32,address,uint256)"), idx, - log.address().into_word(), + compute_address("withdrawer"), ], data, ); From b4f931f1ec1e39a6102bc56af96660338c502f53 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 5 Dec 2024 18:31:57 +0530 Subject: [PATCH 44/46] fix tests --- operator/control-plane/src/market.rs | 80 +++++++++++++++++----------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index 23043931..e4e7fd75 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1377,7 +1377,7 @@ async fn job_logs( #[cfg(test)] mod tests { - use alloy::hex::FromHex; + use alloy::hex::{FromHex, ToHexExt}; use alloy::primitives::{Bytes, B256, U256}; use alloy::rpc::types::eth::Log; use alloy::sol_types::SolValue; @@ -1385,7 +1385,9 @@ mod tests { use tokio_stream::StreamExt; use crate::market; - use crate::test::{self, compute_instance_id, Action, TestAws, TestAwsOutcome}; + use crate::test::{ + self, compute_address, compute_instance_id, Action, TestAws, TestAwsOutcome, + }; use super::SystemContext; @@ -2130,12 +2132,13 @@ mod tests { chain: "123".into(), }, allowed_regions: vec!["ap-south-1".to_owned()], - address_whitelist: vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - ], + address_whitelist: vec![compute_address("owner").encode_hex_with_prefix()], address_blacklist: vec![], }; + // real owner of the job is compute_address("owner") + // expected to deploy + let test_results = TestResults { res: 0, outcomes: vec![ @@ -2194,12 +2197,13 @@ mod tests { chain: "123".into(), }, allowed_regions: vec!["ap-south-1".to_owned()], - address_whitelist: vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), - ], + address_whitelist: vec![compute_address("notowner").encode_hex_with_prefix()], address_blacklist: vec![], }; + // real owner of the job is compute_address("owner") + // expected to not deploy + let test_results = TestResults { res: 0, outcomes: vec![], @@ -2227,11 +2231,12 @@ mod tests { }, allowed_regions: vec!["ap-south-1".to_owned()], address_whitelist: vec![], - address_blacklist: vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - ], + address_blacklist: vec![compute_address("owner").encode_hex_with_prefix()], }; + // real owner of the job is compute_address("owner") + // expected to not deploy + let test_results = TestResults { res: 0, outcomes: vec![], @@ -2259,11 +2264,12 @@ mod tests { }, allowed_regions: vec!["ap-south-1".to_owned()], address_whitelist: vec![], - address_blacklist: vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), - ], + address_blacklist: vec![compute_address("notowner").encode_hex_with_prefix()], }; + // real owner of the job is compute_address("owner") + // expected to deploy + let test_results = TestResults { res: 0, outcomes: vec![ @@ -2313,6 +2319,8 @@ mod tests { let address_whitelist = vec![]; let address_blacklist = vec![]; + // real owner of the job is compute_address("owner") + assert!(market::whitelist_blacklist_check( log.clone(), &address_whitelist, @@ -2326,11 +2334,13 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), + compute_address("owner").encode_hex_with_prefix(), + compute_address("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![]; + // real owner of the job is compute_address("owner") + assert!(market::whitelist_blacklist_check( log.clone(), &address_whitelist, @@ -2344,11 +2354,13 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49eb".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), + compute_address("notownereither").encode_hex_with_prefix(), + compute_address("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![]; + // real owner of the job is compute_address("owner") + assert!(!market::whitelist_blacklist_check( log.clone(), &address_whitelist, @@ -2363,10 +2375,12 @@ mod tests { B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), + compute_address("owner").encode_hex_with_prefix(), + compute_address("notowner").encode_hex_with_prefix(), ]; + // real owner of the job is compute_address("owner") + assert!(!market::whitelist_blacklist_check( log.clone(), &address_whitelist, @@ -2381,10 +2395,12 @@ mod tests { B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49eb".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), + compute_address("notownereither").encode_hex_with_prefix(), + compute_address("notowner").encode_hex_with_prefix(), ]; + // real owner of the job is compute_address("owner") + assert!(market::whitelist_blacklist_check( log.clone(), &address_whitelist, @@ -2398,14 +2414,16 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49eb".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), + compute_address("notownereither").encode_hex_with_prefix(), + compute_address("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ea".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ee".to_string(), + compute_address("definitelynotownereither").encode_hex_with_prefix(), + compute_address("definitelynotowner").encode_hex_with_prefix(), ]; + // real owner of the job is compute_address("owner") + assert!(!market::whitelist_blacklist_check( log.clone(), &address_whitelist, @@ -2419,14 +2437,16 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ed".to_string(), + compute_address("owner").encode_hex_with_prefix(), + compute_address("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![ - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49ec".to_string(), - "0x0000000000000000000000000f5f91ba30a00bd43bd19466f020b3e5fc7a49eb".to_string(), + compute_address("owner").encode_hex_with_prefix(), + compute_address("definitelynotowner").encode_hex_with_prefix(), ]; + // real owner of the job is compute_address("owner") + assert!(!market::whitelist_blacklist_check( log.clone(), &address_whitelist, From b9631cf97fdae384f5cbecda7ef883cfd92fa5e2 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 5 Dec 2024 18:33:44 +0530 Subject: [PATCH 45/46] add todo against unusual behaviour to revisit later --- operator/control-plane/src/market.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index e4e7fd75..d94737c6 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -2603,6 +2603,7 @@ mod tests { run_test(start_time, logs, job_manager_params, test_results).await; } + // TODO: Should this work like this? #[tokio::test(start_paused = true)] async fn test_metadata_update_event_with_no_updates_after_spin_up() { let start_time = Instant::now(); From b998397fc224e56797404e05034f8cf707dff142 Mon Sep 17 00:00:00 2001 From: Roshan Raghupathy Date: Thu, 5 Dec 2024 18:37:56 +0530 Subject: [PATCH 46/46] rename compute_address -> compute_address_word --- operator/control-plane/src/market.rs | 64 ++++++++++++++-------------- operator/control-plane/src/test.rs | 10 ++--- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/operator/control-plane/src/market.rs b/operator/control-plane/src/market.rs index d94737c6..d3d299ff 100644 --- a/operator/control-plane/src/market.rs +++ b/operator/control-plane/src/market.rs @@ -1386,7 +1386,7 @@ mod tests { use crate::market; use crate::test::{ - self, compute_address, compute_instance_id, Action, TestAws, TestAwsOutcome, + self, compute_address_word, compute_instance_id, Action, TestAws, TestAwsOutcome, }; use super::SystemContext; @@ -2132,11 +2132,11 @@ mod tests { chain: "123".into(), }, allowed_regions: vec!["ap-south-1".to_owned()], - address_whitelist: vec![compute_address("owner").encode_hex_with_prefix()], + address_whitelist: vec![compute_address_word("owner").encode_hex_with_prefix()], address_blacklist: vec![], }; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") // expected to deploy let test_results = TestResults { @@ -2197,11 +2197,11 @@ mod tests { chain: "123".into(), }, allowed_regions: vec!["ap-south-1".to_owned()], - address_whitelist: vec![compute_address("notowner").encode_hex_with_prefix()], + address_whitelist: vec![compute_address_word("notowner").encode_hex_with_prefix()], address_blacklist: vec![], }; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") // expected to not deploy let test_results = TestResults { @@ -2231,10 +2231,10 @@ mod tests { }, allowed_regions: vec!["ap-south-1".to_owned()], address_whitelist: vec![], - address_blacklist: vec![compute_address("owner").encode_hex_with_prefix()], + address_blacklist: vec![compute_address_word("owner").encode_hex_with_prefix()], }; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") // expected to not deploy let test_results = TestResults { @@ -2264,10 +2264,10 @@ mod tests { }, allowed_regions: vec!["ap-south-1".to_owned()], address_whitelist: vec![], - address_blacklist: vec![compute_address("notowner").encode_hex_with_prefix()], + address_blacklist: vec![compute_address_word("notowner").encode_hex_with_prefix()], }; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") // expected to deploy let test_results = TestResults { @@ -2319,7 +2319,7 @@ mod tests { let address_whitelist = vec![]; let address_blacklist = vec![]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(market::whitelist_blacklist_check( log.clone(), @@ -2334,12 +2334,12 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - compute_address("owner").encode_hex_with_prefix(), - compute_address("notowner").encode_hex_with_prefix(), + compute_address_word("owner").encode_hex_with_prefix(), + compute_address_word("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(market::whitelist_blacklist_check( log.clone(), @@ -2354,12 +2354,12 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - compute_address("notownereither").encode_hex_with_prefix(), - compute_address("notowner").encode_hex_with_prefix(), + compute_address_word("notownereither").encode_hex_with_prefix(), + compute_address_word("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(!market::whitelist_blacklist_check( log.clone(), @@ -2375,11 +2375,11 @@ mod tests { B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![ - compute_address("owner").encode_hex_with_prefix(), - compute_address("notowner").encode_hex_with_prefix(), + compute_address_word("owner").encode_hex_with_prefix(), + compute_address_word("notowner").encode_hex_with_prefix(), ]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(!market::whitelist_blacklist_check( log.clone(), @@ -2395,11 +2395,11 @@ mod tests { B256::ZERO); let address_whitelist = vec![]; let address_blacklist = vec![ - compute_address("notownereither").encode_hex_with_prefix(), - compute_address("notowner").encode_hex_with_prefix(), + compute_address_word("notownereither").encode_hex_with_prefix(), + compute_address_word("notowner").encode_hex_with_prefix(), ]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(market::whitelist_blacklist_check( log.clone(), @@ -2414,15 +2414,15 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - compute_address("notownereither").encode_hex_with_prefix(), - compute_address("notowner").encode_hex_with_prefix(), + compute_address_word("notownereither").encode_hex_with_prefix(), + compute_address_word("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![ - compute_address("definitelynotownereither").encode_hex_with_prefix(), - compute_address("definitelynotowner").encode_hex_with_prefix(), + compute_address_word("definitelynotownereither").encode_hex_with_prefix(), + compute_address_word("definitelynotowner").encode_hex_with_prefix(), ]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(!market::whitelist_blacklist_check( log.clone(), @@ -2437,15 +2437,15 @@ mod tests { Bytes::from(("{\"region\":\"ap-south-1\",\"url\":\"https://example.com/enclave.eif\",\"instance\":\"c6a.xlarge\",\"memory\":4096,\"vcpu\":2}".to_string(),31000000000000u64,31000u64,0).abi_encode_sequence()), B256::ZERO); let address_whitelist = vec![ - compute_address("owner").encode_hex_with_prefix(), - compute_address("notowner").encode_hex_with_prefix(), + compute_address_word("owner").encode_hex_with_prefix(), + compute_address_word("notowner").encode_hex_with_prefix(), ]; let address_blacklist = vec![ - compute_address("owner").encode_hex_with_prefix(), - compute_address("definitelynotowner").encode_hex_with_prefix(), + compute_address_word("owner").encode_hex_with_prefix(), + compute_address_word("definitelynotowner").encode_hex_with_prefix(), ]; - // real owner of the job is compute_address("owner") + // real owner of the job is compute_address_word("owner") assert!(!market::whitelist_blacklist_check( log.clone(), diff --git a/operator/control-plane/src/test.rs b/operator/control-plane/src/test.rs index 416e3eda..99f56d03 100644 --- a/operator/control-plane/src/test.rs +++ b/operator/control-plane/src/test.rs @@ -96,7 +96,7 @@ pub fn compute_instance_ip(counter: u64) -> String { .unwrap() } -pub fn compute_address(salt: &str) -> FixedBytes<32> { +pub fn compute_address_word(salt: &str) -> FixedBytes<32> { let mut hasher = DefaultHasher::new(); hasher.write_u8(2); hasher.write(salt.as_bytes()); @@ -368,8 +368,8 @@ pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { vec![ keccak256("JobOpened(bytes32,string,address,address,uint256,uint256,uint256)"), idx, - compute_address("owner"), - compute_address("provider"), + compute_address_word("owner"), + compute_address_word("provider"), ], data, ); @@ -389,7 +389,7 @@ pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { vec![ keccak256("JobDeposited(bytes32,address,uint256)"), idx, - compute_address("depositor"), + compute_address_word("depositor"), ], data, ); @@ -399,7 +399,7 @@ pub fn get_log(topic: Action, data: Bytes, idx: B256) -> Log { vec![ keccak256("JobWithdrew(bytes32,address,uint256)"), idx, - compute_address("withdrawer"), + compute_address_word("withdrawer"), ], data, );