Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Steiner committed Oct 25, 2020
1 parent 0bc6475 commit 5c14743
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
90 changes: 46 additions & 44 deletions rust/e2e/src/test_client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use xaynet_client::{
api::{ApiClient, HttpApiClient},
mobile_client::{
participant::{AggregationConfig, MaxMessageSize, ParticipantSettings},
ClientStateName,
MobileClient,
ClientStateName, MobileClient,
},
};
use xaynet_core::{
Expand Down Expand Up @@ -60,6 +59,28 @@ impl TestClientBuilder {
}
}

pub async fn generate_clients(
&mut self,
number: u64,
r#type: ClientStateName,
settings: TestClientSettings,
) -> anyhow::Result<Vec<TestClient>> {
let round_params = self.api_client.get_round_params().await?;
let mut container = Vec::with_capacity(number as usize);

for _ in 0..number {
let new_client = generate_client(
&round_params,
self.api_client.clone(),
settings.clone(),
&r#type,
)?;
container.push(new_client);
}

Ok(container)
}

pub async fn build_sum_clients(
&mut self,
) -> anyhow::Result<ConcurrentFutures<Pin<Box<dyn Future<Output = TestClient> + Send + 'static>>>>
Expand All @@ -69,10 +90,11 @@ impl TestClientBuilder {
Pin<Box<dyn Future<Output = TestClient> + Send + 'static>>,
>::new(100);
for _ in 0..self.settings.number_of_sum {
let sum_client = build_sum_client(
round_params.clone(),
let sum_client = generate_client(
&round_params,
self.api_client.clone(),
self.settings.test_client_settings.clone(),
&ClientStateName::Sum,
)?;
sum_clients.push(Box::pin(async {
let sum_client = sum_client.try_to_proceed().await;
Expand All @@ -95,10 +117,11 @@ impl TestClientBuilder {

let model = Model::from_primitives(vec![1; self.settings.model_size].into_iter())?;
for _ in 0..self.settings.number_of_update {
let mut update_client = build_update_client(
round_params.clone(),
let mut update_client = generate_client(
&round_params,
self.api_client.clone(),
self.settings.test_client_settings.clone(),
&ClientStateName::Update,
)?;
update_client.set_local_model(model.clone());
update_clients.push(Box::pin(async {
Expand Down Expand Up @@ -133,54 +156,33 @@ impl TestClientSettings {
}
}

fn new_client(round_params: &RoundParameters) -> (ClientStateName, ParticipantSecretKey) {
let secret_key = MobileClient::create_participant_secret_key();
let role = determine_role(
secret_key.clone(),
round_params.seed.as_slice(),
round_params.sum,
round_params.update,
);
(role, secret_key)
}

fn build_client(
participant_settings: ParticipantSettings,
api_client: HttpApiClient,
) -> anyhow::Result<TestClient> {
TestClient::new(api_client, participant_settings)
}

fn build_sum_client(
round_params: RoundParameters,
fn generate_client(
round_params: &RoundParameters,
api_client: HttpApiClient,
test_client_settings: TestClientSettings,
r#type: &ClientStateName,
) -> anyhow::Result<TestClient> {
loop {
if let (ClientStateName::Sum, secret_key) = new_client(&round_params) {
let sum_client = build_client(
test_client_settings.into_participant_settings(secret_key),
let (client_type, secret_key) = new_client(&round_params);
if client_type == *r#type {
let new_client = TestClient::new(
api_client,
test_client_settings.into_participant_settings(secret_key),
)?;
break Ok(sum_client);
break Ok(new_client);
}
}
}

fn build_update_client(
round_params: RoundParameters,
api_client: HttpApiClient,
test_client_settings: TestClientSettings,
) -> anyhow::Result<TestClient> {
loop {
if let (ClientStateName::Update, secret_key) = new_client(&round_params) {
let update_client = build_client(
test_client_settings.into_participant_settings(secret_key),
api_client,
)?;
break Ok(update_client);
}
}
fn new_client(round_params: &RoundParameters) -> (ClientStateName, ParticipantSecretKey) {
let secret_key = MobileClient::create_participant_secret_key();
let role = determine_role(
secret_key.clone(),
round_params.seed.as_slice(),
round_params.sum,
round_params.update,
);
(role, secret_key)
}

pub fn determine_role(
Expand Down
2 changes: 2 additions & 0 deletions rust/e2e/src/test_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ impl TestClient {
self.local_model.set_local_model(model);
}
}


2 changes: 1 addition & 1 deletion rust/xaynet-client/src/mobile_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl MobileClient {
}
}

#[derive(Debug)]
#[derive(Debug, Eq, PartialEq)]
pub enum ClientStateName {
Awaiting,
Sum,
Expand Down

0 comments on commit 5c14743

Please sign in to comment.