Skip to content

Commit

Permalink
Specify chainspec type when spawning TSS nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Aug 27, 2024
1 parent ee239e3 commit 8f081f6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 91 deletions.
2 changes: 1 addition & 1 deletion crates/testing-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod create_test_keyshares;
mod node_proc;
pub mod substrate_context;
pub use entropy_tss::helpers::tests::{
jump_start_network_with_signer as jump_start_network, spawn_testing_validators,
jump_start_network_with_signer as jump_start_network, spawn_testing_validators, ChainSpecType,
};
pub use node_proc::TestNodeProcess;
pub use substrate_context::*;
6 changes: 4 additions & 2 deletions crates/threshold-signature-server/src/attestation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
chain_api::{entropy, get_api, get_rpc},
helpers::{
substrate::query_chain,
tests::{initialize_test_logger, run_to_block, spawn_testing_validators},
tests::{initialize_test_logger, run_to_block, spawn_testing_validators, ChainSpecType},
},
};
use entropy_kvdb::clean_tests;
Expand All @@ -32,7 +32,9 @@ async fn test_attest() {
clean_tests();

let cxt = test_node_process_stationary().await;
let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Integration).await;

let api = get_api(&cxt.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.ws_url).await.unwrap();

Expand Down
107 changes: 36 additions & 71 deletions crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,67 +119,28 @@ pub async fn create_clients(
(app, kv_store)
}

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

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 ids = vec![alice_id, bob_id, charlie_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;

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();
});

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

let ips = ports.iter().map(|port| format!("127.0.0.1:{port}")).collect();
(ips, ids)
/// A way to specify whether the test environment uses the Development chainspec, which has 3 TSS
/// nodes, or the Integration test chainspec which has 4 TSS nodes
#[derive(PartialEq)]
pub enum ChainSpecType {
Development,
Integration,
}

/// Spawn 4 TSS nodes with pre-stored keyshares
pub async fn spawn_four_testing_validators(add_parent_key: bool) -> (Vec<String>, Vec<PartyId>) {
/// Spawn either 3 or 4 TSS nodes depending on chain configuration, adding pre-stored keyshares if
/// desired
pub async fn spawn_testing_validators(
add_parent_key: bool,
chain_spec_type: ChainSpecType,
) -> (Vec<String>, Vec<PartyId>) {
let add_fourth_server = chain_spec_type == ChainSpecType::Integration;

// spawn threshold servers
let ports = [3001i64, 3002, 3003, 3004];
let mut ports = vec![3001i64, 3002, 3003];

if add_fourth_server {
ports.push(3004);
}

let (alice_axum, alice_kv) =
create_clients("validator1".to_string(), vec![], vec![], &Some(ValidatorName::Alice)).await;
Expand All @@ -200,13 +161,7 @@ pub async fn spawn_four_testing_validators(add_parent_key: bool) -> (Vec<String>
*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];
let mut ids = vec![alice_id, bob_id, charlie_id];

put_keyshares_in_db("alice", alice_kv, add_parent_key).await;
put_keyshares_in_db("bob", bob_kv, add_parent_key).await;
Expand Down Expand Up @@ -234,12 +189,22 @@ pub async fn spawn_four_testing_validators(add_parent_key: bool) -> (Vec<String>
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();
});
if add_fourth_server {
let (dave_axum, dave_kv) =
create_clients("validator4".to_string(), vec![], vec![], &Some(ValidatorName::Dave))
.await;

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();
});
let dave_id = PartyId::new(SubxtAccountId32(
*get_signer(&dave_kv).await.unwrap().account_id().clone().as_ref(),
));
ids.push(dave_id);
}

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

Expand Down
4 changes: 2 additions & 2 deletions crates/threshold-signature-server/src/signing_client/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
launch::LATEST_BLOCK_NUMBER_PROACTIVE_REFRESH,
tests::{
initialize_test_logger, run_to_block, setup_client, spawn_testing_validators,
unsafe_get,
unsafe_get, ChainSpecType,
},
},
};
Expand All @@ -45,7 +45,7 @@ async fn test_proactive_refresh() {
clean_tests();
let _cxt = test_node_process_testing_state(false).await;

let (validator_ips, _ids) = spawn_testing_validators(false).await;
let (validator_ips, _ids) = spawn_testing_validators(false, ChainSpecType::Integration).await;

let client = reqwest::Client::new();

Expand Down
32 changes: 21 additions & 11 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ use crate::{
substrate::{get_oracle_data, query_chain, submit_transaction},
tests::{
create_clients, initialize_test_logger, jump_start_network_with_signer, remove_program,
run_to_block, setup_client, spawn_testing_validators, unsafe_get,
run_to_block, setup_client, spawn_testing_validators, unsafe_get, ChainSpecType,
},
user::compute_hash,
validator::get_signer_and_x25519_secret_from_mnemonic,
Expand Down Expand Up @@ -169,7 +169,8 @@ async fn test_sign_tx_no_chain() {
let one = AccountKeyring::Dave;
let two = AccountKeyring::Two;

let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_context_stationary().await;
let entropy_api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down Expand Up @@ -376,7 +377,8 @@ async fn signature_request_with_derived_account_works() {
let charlie = AccountKeyring::Charlie;

let add_parent_key_to_kvdb = true;
let (_validator_ips, _validator_ids) = spawn_testing_validators(add_parent_key_to_kvdb).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(add_parent_key_to_kvdb, ChainSpecType::Development).await;

// Here we need to use `--chain=integration-tests` and force authoring otherwise we won't be
// able to get our chain in the right state to be jump started.
Expand Down Expand Up @@ -498,7 +500,8 @@ async fn test_sign_tx_no_chain_fail() {

let one = AccountKeyring::Dave;

let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_context_stationary().await;
let entropy_api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down Expand Up @@ -625,7 +628,8 @@ async fn test_program_with_config() {
let one = AccountKeyring::Dave;
let two = AccountKeyring::Two;

let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_context_stationary().await;
let entropy_api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down Expand Up @@ -694,7 +698,8 @@ async fn test_store_share() {
let program_manager = AccountKeyring::Dave;

let cxt = test_context_stationary().await;
let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let api = get_api(&cxt.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();

Expand Down Expand Up @@ -864,7 +869,8 @@ async fn test_jumpstart_network() {
let alice = AccountKeyring::Alice;

let cxt = test_context_stationary().await;
let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let api = get_api(&cxt.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();

Expand Down Expand Up @@ -1091,7 +1097,8 @@ async fn test_fail_infinite_program() {
let one = AccountKeyring::Dave;
let two = AccountKeyring::Two;

let (validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_context_stationary().await;
let entropy_api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down Expand Up @@ -1191,7 +1198,8 @@ async fn test_device_key_proxy() {

let one = AccountKeyring::Dave;

let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_context_stationary().await;
let entropy_api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down Expand Up @@ -1301,7 +1309,8 @@ async fn test_faucet() {
let two = AccountKeyring::Eve;
let alice = AccountKeyring::Alice;

let (validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_node_process_testing_state(true).await;
let entropy_api = get_api(&substrate_context.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.ws_url).await.unwrap();
Expand Down Expand Up @@ -1472,7 +1481,8 @@ async fn test_new_registration_flow() {
let charlie = AccountKeyring::Charlie;

let add_parent_key_to_kvdb = true;
let (_validator_ips, _validator_ids) = spawn_testing_validators(add_parent_key_to_kvdb).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(add_parent_key_to_kvdb, ChainSpecType::Development).await;

// Here we need to use `--chain=integration-tests` and force authoring otherwise we won't be
// able to get our chain in the right state to be jump started.
Expand Down
5 changes: 3 additions & 2 deletions crates/threshold-signature-server/tests/register_and_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use entropy_testing_utils::{
constants::{
AUXILARY_DATA_SHOULD_SUCCEED, PREIMAGE_SHOULD_SUCCEED, TEST_PROGRAM_WASM_BYTECODE,
},
jump_start_network, spawn_testing_validators, test_node_process_testing_state,
jump_start_network, spawn_testing_validators, test_node_process_testing_state, ChainSpecType,
};
use serial_test::serial;
use sp_core::{sr25519, Pair};
Expand All @@ -42,7 +42,8 @@ async fn integration_test_register_and_sign() {
let signature_request_author = AccountKeyring::One;

let add_parent_key = true;
let (_validator_ips, _validator_ids) = spawn_testing_validators(add_parent_key).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(add_parent_key, ChainSpecType::Integration).await;

let force_authoring = true;
let substrate_context = test_node_process_testing_state(force_authoring).await;
Expand Down
4 changes: 3 additions & 1 deletion crates/threshold-signature-server/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use entropy_testing_utils::{
},
spawn_testing_validators,
substrate_context::test_context_stationary,
ChainSpecType,
};
use serial_test::serial;
use sp_keyring::AccountKeyring;
Expand All @@ -41,7 +42,8 @@ async fn integration_test_sign_public() {
let eve = AccountKeyring::Eve;
let request_author = AccountKeyring::One;

let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;

let substrate_context = test_context_stationary().await;
let api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down
4 changes: 3 additions & 1 deletion crates/threshold-signature-server/tests/sign_eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use entropy_testing_utils::{
constants::{AUXILARY_DATA_SHOULD_SUCCEED, TEST_PROGRAM_WASM_BYTECODE},
spawn_testing_validators,
substrate_context::test_context_stationary,
ChainSpecType,
};
use ethers_core::{
abi::ethabi::ethereum_types::{H160, H256},
Expand All @@ -49,7 +50,8 @@ async fn integration_test_sign_eth_tx() {
clean_tests();
let pre_registered_user = AccountKeyring::Eve;

let (_validator_ips, _validator_ids) = spawn_testing_validators(false).await;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let substrate_context = test_context_stationary().await;
let api = get_api(&substrate_context.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.node_proc.ws_url).await.unwrap();
Expand Down

0 comments on commit 8f081f6

Please sign in to comment.