Skip to content

Commit

Permalink
Add helper to create 4 node test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Aug 26, 2024
1 parent dd6872e commit f393fd0
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,77 @@ pub async fn spawn_testing_validators(add_parent_key: bool) -> (Vec<String>, Vec
(ips, ids)
}

/// Spawn 4 TSS nodes with pre-stored keyshares
pub async fn spawn_four_testing_validators(add_parent_key: bool) -> (Vec<String>, Vec<PartyId>) {
// spawn threshold servers
let ports = [3001i64, 3002, 3003, 3004];

let (alice_axum, alice_kv) =
create_clients("validator1".to_string(), vec![], vec![], &Some(ValidatorName::Alice)).await;
let alice_id = PartyId::new(SubxtAccountId32(
*get_signer(&alice_kv).await.unwrap().account_id().clone().as_ref(),
));

let (bob_axum, bob_kv) =
create_clients("validator2".to_string(), vec![], vec![], &Some(ValidatorName::Bob)).await;
let bob_id = PartyId::new(SubxtAccountId32(
*get_signer(&bob_kv).await.unwrap().account_id().clone().as_ref(),
));

let (charlie_axum, charlie_kv) =
create_clients("validator3".to_string(), vec![], vec![], &Some(ValidatorName::Charlie))
.await;
let charlie_id = PartyId::new(SubxtAccountId32(
*get_signer(&charlie_kv).await.unwrap().account_id().clone().as_ref(),
));

let (dave_axum, dave_kv) =
create_clients("validator4".to_string(), vec![], vec![], &Some(ValidatorName::Dave)).await;
let dave_id = PartyId::new(SubxtAccountId32(
*get_signer(&dave_kv).await.unwrap().account_id().clone().as_ref(),
));

let ids = vec![alice_id, bob_id, charlie_id, dave_id];

put_keyshares_in_db("alice", alice_kv, add_parent_key).await;
put_keyshares_in_db("bob", bob_kv, add_parent_key).await;
put_keyshares_in_db("charlie", charlie_kv, add_parent_key).await;
// Don't give dave keyshares as dave is not initially in the signing committee

let listener_alice = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", ports[0]))
.await
.expect("Unable to bind to given server address.");
tokio::spawn(async move {
axum::serve(listener_alice, alice_axum).await.unwrap();
});

let listener_bob = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", ports[1]))
.await
.expect("Unable to bind to given server address.");
tokio::spawn(async move {
axum::serve(listener_bob, bob_axum).await.unwrap();
});

let listener_charlie = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", ports[2]))
.await
.expect("Unable to bind to given server address.");
tokio::spawn(async move {
axum::serve(listener_charlie, charlie_axum).await.unwrap();
});

let listener_dave = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", ports[3]))
.await
.expect("Unable to bind to given server address.");
tokio::spawn(async move {
axum::serve(listener_dave, dave_axum).await.unwrap();
});

tokio::time::sleep(Duration::from_secs(1)).await;

let ips = ports.iter().map(|port| format!("127.0.0.1:{port}")).collect();
(ips, ids)
}

/// Add the pre-generated test keyshares to a kvdb
async fn put_keyshares_in_db(holder_name: &str, kvdb: KvManager, add_parent_key: bool) {
let mut user_names_and_verifying_keys =
Expand Down

0 comments on commit f393fd0

Please sign in to comment.