Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow offchain worker requests to all TSS nodes in entropy-tss test environment #1147

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/testing-utils/src/substrate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ pub async fn test_node_process_testing_state(
"/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWM7EoKJfwgzAR1nAVmYRuuFq2f3GpJPLrdfhQaRsKjn38"
.to_string(),
);
// reduses message from chain to same TSS cleaning up a lot of logging
let fuck_off_tss_ip = Some("127.0.0.1:4010".to_string());
let result = test_node(
AccountKeyring::Alice,
"--chain=integration-tests".to_string(),
Expand All @@ -130,23 +128,23 @@ pub async fn test_node_process_testing_state(
"--chain=integration-tests".to_string(),
force_authoring,
alice_bootnode.clone(),
fuck_off_tss_ip.clone(),
Some("http://127.0.0.1:3002".into()),
)
.await;
let result_charlie = test_node_process_with(
AccountKeyring::Charlie,
"--chain=integration-tests".to_string(),
force_authoring,
alice_bootnode.clone(),
fuck_off_tss_ip.clone(),
Some("http://127.0.0.1:3003".into()),
)
.await;
let result_dave = test_node_process_with(
AccountKeyring::Dave,
"--chain=integration-tests".to_string(),
force_authoring,
alice_bootnode.clone(),
fuck_off_tss_ip.clone(),
Some("http://127.0.0.1:3004".into()),
)
.await;

Expand Down
29 changes: 1 addition & 28 deletions crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ use entropy_kvdb::{encrypted_sled::PasswordMethod, get_db_path, kv_manager::KvMa
use entropy_protocol::PartyId;
#[cfg(test)]
use entropy_shared::EncodedVerifyingKey;
use entropy_shared::{OcwMessageDkg, EVE_VERIFYING_KEY, NETWORK_PARENT_KEY};
use futures::future::join_all;
use parity_scale_codec::Encode;
use entropy_shared::{EVE_VERIFYING_KEY, NETWORK_PARENT_KEY};
use std::time::Duration;
use subxt::{
backend::legacy::LegacyRpcMethods, ext::sp_core::sr25519, tx::PairSigner,
Expand Down Expand Up @@ -371,28 +369,6 @@ pub async fn do_jump_start(

run_to_block(rpc, block_number + 1).await;

let selected_validators_query = entropy::storage().registry().jumpstart_dkg(block_number);
let validators_info =
query_chain(api, rpc, selected_validators_query, None).await.unwrap().unwrap();
let validators_info: Vec<_> = validators_info.into_iter().map(|v| v.0).collect();
let onchain_user_request =
OcwMessageDkg { block_number, validators_info: validators_info.clone() };

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

let mut results = vec![];
for validator_info in validators_info {
let url = format!(
"http://{}/generate_network_key",
std::str::from_utf8(&validator_info.ip_address.clone()).unwrap()
);
if url != *"http://127.0.0.1:3001/generate_network_key" {
results.push(client.post(url).body(onchain_user_request.clone().encode()).send())
}
}

let response_results = join_all(results).await;

let jump_start_status_query = entropy::storage().staking_extension().jump_start_progress();
let mut jump_start_status = query_chain(api, rpc, jump_start_status_query.clone(), None)
.await
Expand All @@ -414,9 +390,6 @@ pub async fn do_jump_start(
}

assert_eq!(format!("{:?}", jump_start_status), format!("{:?}", JumpStartStatus::Done));
for response_result in response_results {
assert_eq!(response_result.unwrap().text().await.unwrap(), "");
}
}

/// Submit a jumpstart extrinsic
Expand Down
56 changes: 29 additions & 27 deletions crates/threshold-signature-server/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ use entropy_testing_utils::{
substrate_context::{test_node_process_testing_state, testing_context},
test_context_stationary,
};
use futures::future::join_all;
use parity_scale_codec::Encode;
use serial_test::serial;
use sp_core::Pair;
Expand All @@ -67,7 +66,7 @@ use synedrion::k256::ecdsa::VerifyingKey;

#[tokio::test]
#[serial]
async fn test_reshare() {
async fn test_reshare_basic() {
initialize_test_logger().await;
clean_tests();

Expand All @@ -76,7 +75,7 @@ async fn test_reshare() {
let (_validator_ips, _validator_ids) =
spawn_testing_validators(ChainSpecType::Integration).await;

let validator_ports = vec![3002, 3003, 3004];
// let validator_ports = vec![3002, 3003, 3004];
let api = get_api(&cxt.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.ws_url).await.unwrap();

Expand Down Expand Up @@ -105,36 +104,36 @@ async fn test_reshare() {
}

// Get all validators
let validators_query = entropy::storage().session().validators();
let all_validators = query_chain(&api, &rpc, validators_query, None).await.unwrap().unwrap();
// let validators_query = entropy::storage().session().validators();
// let all_validators = query_chain(&api, &rpc, validators_query, None).await.unwrap().unwrap();

// Get stash account of a non-signer, to become the new signer
// Since we only have 4 nodes in our test setup, this will be the same one the chain chooses
let new_signer = all_validators.iter().find(|v| !signer_stash_accounts.contains(v)).unwrap();
// let new_signer = all_validators.iter().find(|v| !signer_stash_accounts.contains(v)).unwrap();

// let onchain_reshare_request = OcwMessageReshare {
// new_signers: vec![new_signer.0.to_vec()],
// block_number: block_number - 1,
// };
//
let block_number = TEST_RESHARE_BLOCK_NUMBER;
let onchain_reshare_request = OcwMessageReshare {
new_signers: vec![new_signer.0.to_vec()],
block_number: block_number - 1,
};

run_to_block(&rpc, block_number).await;
run_to_block(&rpc, block_number + 1).await;
// Send the OCW message to all TS servers who don't have a chain node
let response_results = join_all(
validator_ports
.iter()
.map(|port| {
client
.post(format!("http://127.0.0.1:{}/validator/reshare", port))
.body(onchain_reshare_request.clone().encode())
.send()
})
.collect::<Vec<_>>(),
)
.await;
for response_result in response_results {
assert_eq!(response_result.unwrap().text().await.unwrap(), "");
}
// let response_results = join_all(
// validator_ports
// .iter()
// .map(|port| {
// client
// .post(format!("http://127.0.0.1:{}/validator/reshare", port))
// .body(onchain_reshare_request.clone().encode())
// .send()
// })
// .collect::<Vec<_>>(),
// )
// .await;
// for response_result in response_results {
// assert_eq!(response_result.unwrap().text().await.unwrap(), "");
// }

for (tss_account, key_share_and_aux_before) in key_shares_before.iter() {
let (key_share_before, aux_info_before): KeyShareWithAuxInfo =
Expand Down Expand Up @@ -164,6 +163,9 @@ async fn test_reshare() {
signers
};

println!("Signers {:?}", signers);
println!("NEW Signers {:?}", new_signers);

for signer in new_signers {
let _ = client
.post(format!(
Expand Down
103 changes: 41 additions & 62 deletions crates/threshold-signature-server/tests/register_sign_reshare_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ use entropy_client::{
Hasher,
};
use entropy_kvdb::clean_tests;
use entropy_shared::{OcwMessageReshare, TEST_RESHARE_BLOCK_NUMBER};
use entropy_shared::TEST_RESHARE_BLOCK_NUMBER;
use entropy_testing_utils::{
constants::{
AUXILARY_DATA_SHOULD_SUCCEED, PREIMAGE_SHOULD_SUCCEED, TEST_PROGRAM_WASM_BYTECODE,
},
spawn_testing_validators, test_node_process_testing_state, ChainSpecType,
};
use entropy_tss::helpers::tests::{do_jump_start, initialize_test_logger, run_to_block};
use futures::future::join_all;
use serial_test::serial;
use sp_core::{Encode, Pair};
use sp_core::Pair;
use sp_keyring::AccountKeyring;
use subxt::{backend::legacy::LegacyRpcMethods, utils::AccountId32, OnlineClient};
use synedrion::k256::ecdsa::VerifyingKey;
Expand All @@ -59,6 +58,14 @@ async fn integration_test_register_sign_reshare_sign() {
// First jumpstart the network
do_jump_start(&api, &rpc, AccountKeyring::Alice.pair()).await;

let initial_signers = {
let signer_query = entropy::storage().staking_extension().signers();
query_chain(&api, &rpc, signer_query, None).await.unwrap().unwrap()
};

// Do a reshare
wait_for_reshare(&api, &rpc, initial_signers).await;

// Now register an account
let account_owner = AccountKeyring::Ferdie.pair();
let signature_request_author = AccountKeyring::One;
Expand Down Expand Up @@ -113,9 +120,6 @@ async fn integration_test_register_sign_reshare_sign() {
recovery_key_from_sig.to_encoded_point(true).to_bytes().to_vec()
);

// Do a reshare
do_reshare(&api, &rpc).await;

// Sign a message again
let recoverable_signature = test_client::sign(
&api,
Expand All @@ -142,76 +146,51 @@ async fn integration_test_register_sign_reshare_sign() {
);
}

async fn do_reshare(api: &OnlineClient<EntropyConfig>, rpc: &LegacyRpcMethods<EntropyConfig>) {
// Get current signers
let signer_query = entropy::storage().staking_extension().signers();
let signer_stash_accounts = query_chain(&api, &rpc, signer_query, None).await.unwrap().unwrap();
let mut signers = Vec::new();
for signer in signer_stash_accounts.iter() {
let query = entropy::storage().staking_extension().threshold_servers(signer);
let server_info = query_chain(&api, &rpc, query, None).await.unwrap().unwrap();
signers.push(server_info);
}

async fn wait_for_reshare(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
initial_signers: Vec<AccountId32>,
) {
let reshare_data_query = entropy::storage().staking_extension().reshare_data();
let reshare_data = query_chain(&api, &rpc, reshare_data_query, None).await.unwrap().unwrap();
let block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
dbg!(block_number);
println!("reshare_data {reshare_data:?}");

let block_number = TEST_RESHARE_BLOCK_NUMBER;
let onchain_reshare_request = OcwMessageReshare {
new_signers: reshare_data.new_signers.into_iter().map(|s| s.to_vec()).collect(),
block_number: block_number - 1,
};

let block_number = TEST_RESHARE_BLOCK_NUMBER + 20;
run_to_block(&rpc, block_number).await;
// Send the OCW message to all TS servers who don't have a chain node
let client = reqwest::Client::new();
let response_results = join_all(
[3002, 3003, 3004]
.iter()
.map(|port| {
client
.post(format!("http://127.0.0.1:{}/validator/reshare", port))
.body(onchain_reshare_request.clone().encode())
.send()
})
.collect::<Vec<_>>(),
)
.await;
for response_result in response_results {
assert_eq!(response_result.unwrap().text().await.unwrap(), "");
}

let new_signers = {
let signer_query = entropy::storage().staking_extension().signers();
let signer_ids = query_chain(&api, &rpc, signer_query, None).await.unwrap().unwrap();
let mut signers = Vec::new();
for signer in signer_ids {
let query = entropy::storage().staking_extension().threshold_servers(signer);
let server_info = query_chain(&api, &rpc, query, None).await.unwrap().unwrap();
signers.push(server_info);
}
signers
};
// let new_signers = {
// let signer_query = entropy::storage().staking_extension().signers();
// let signer_ids = query_chain(&api, &rpc, signer_query, None).await.unwrap().unwrap();
// let mut signers = Vec::new();
// for signer in signer_ids {
// let query = entropy::storage().staking_extension().threshold_servers(signer);
// let server_info = query_chain(&api, &rpc, query, None).await.unwrap().unwrap();
// signers.push(server_info);
// }
// signers
// };

// Tell TS servers who do not have an associated chain node to rotate their keyshare.
// This is called by the chain on getting confirmation of the reshare from all of the new
// signing group.
for signer in new_signers {
let _ = client
.post(format!(
"http://{}/rotate_network_key",
std::str::from_utf8(&signer.endpoint).unwrap()
))
.send()
.await
.unwrap();
}
// for signer in new_signers {
// let _ = client
// .post(format!(
// "http://{}/rotate_network_key",
// std::str::from_utf8(&signer.endpoint).unwrap()
// ))
// .send()
// .await
// .unwrap();
// }

// Check that the signers have changed since before the reshare
let signer_query = entropy::storage().staking_extension().signers();
let new_signer_stash_accounts =
query_chain(&api, &rpc, signer_query, None).await.unwrap().unwrap();
let old: HashSet<[u8; 32]> = signer_stash_accounts.iter().map(|s| s.0).collect();
let old: HashSet<[u8; 32]> = initial_signers.iter().map(|s| s.0).collect();
let new: HashSet<[u8; 32]> = new_signer_stash_accounts.iter().map(|s| s.0).collect();
assert_ne!(old, new);
}
Loading