diff --git a/crates/threshold-signature-server/src/helpers/tests.rs b/crates/threshold-signature-server/src/helpers/tests.rs index b523072ab..abbbb9ffc 100644 --- a/crates/threshold-signature-server/src/helpers/tests.rs +++ b/crates/threshold-signature-server/src/helpers/tests.rs @@ -39,14 +39,17 @@ use crate::{ AppState, }; use axum::{routing::IntoMakeService, Router}; +use entropy_client::substrate::query_chain; use entropy_kvdb::{encrypted_sled::PasswordMethod, get_db_path, kv_manager::KvManager}; use entropy_protocol::PartyId; #[cfg(test)] use entropy_shared::EncodedVerifyingKey; -use entropy_shared::{EVE_VERIFYING_KEY, NETWORK_PARENT_KEY}; +use entropy_shared::{OcwMessageDkg, EVE_VERIFYING_KEY, NETWORK_PARENT_KEY}; +use futures::future::join_all; +use parity_scale_codec::Encode; use std::time::Duration; use subxt::{ - backend::legacy::LegacyRpcMethods, ext::sp_core::sr25519, tx::PairSigner, + backend::legacy::LegacyRpcMethods, events::EventsClient, ext::sp_core::sr25519, tx::PairSigner, utils::AccountId32 as SubxtAccountId32, Config, OnlineClient, }; use tokio::sync::OnceCell; @@ -355,3 +358,64 @@ pub async fn store_program_and_register( (verifying_key, program_hash) } + +/// Do a network jumpstart DKG +pub async fn do_jump_start( + api: &OnlineClient, + rpc: &LegacyRpcMethods, + pair: sr25519::Pair, +) { + let block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number + 1; + put_jumpstart_request_on_chain(api, rpc, pair).await; + + 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 }; + + let client = reqwest::Client::new(); + let response_results = join_all( + [3002, 3003, 3004] + .iter() + .map(|port| { + client + .post(format!("http://127.0.0.1:{}/generate_network_key", port)) + .body(onchain_user_request.clone().encode()) + .send() + }) + .collect::>(), + ) + .await; + for response_result in response_results { + assert_eq!(response_result.unwrap().text().await.unwrap(), ""); + } + + // Wait for jump start event + let mut got_jumpstart_event = false; + for _ in 0..75 { + std::thread::sleep(std::time::Duration::from_millis(1000)); + let block_hash = rpc.chain_get_block_hash(None).await.unwrap(); + let events = EventsClient::new(api.clone()).at(block_hash.unwrap()).await.unwrap(); + let jump_start_event = events.find::(); + if let Some(_event) = jump_start_event.flatten().next() { + got_jumpstart_event = true; + break; + }; + } + assert!(got_jumpstart_event); +} + +/// Submit a jumpstart extrinsic +async fn put_jumpstart_request_on_chain( + api: &OnlineClient, + rpc: &LegacyRpcMethods, + pair: sr25519::Pair, +) { + let account = PairSigner::::new(pair); + + let registering_tx = entropy::tx().registry().jump_start_network(); + submit_transaction(api, rpc, &account, ®istering_tx, None).await.unwrap(); +} diff --git a/crates/threshold-signature-server/src/user/tests.rs b/crates/threshold-signature-server/src/user/tests.rs index d82369f90..2379d3920 100644 --- a/crates/threshold-signature-server/src/user/tests.rs +++ b/crates/threshold-signature-server/src/user/tests.rs @@ -105,6 +105,7 @@ use tokio_tungstenite::{connect_async, tungstenite::Message}; use x25519_dalek::{PublicKey, StaticSecret}; use super::UserInputPartyInfo; +use crate::helpers::tests::do_jump_start; use crate::{ chain_api::{ entropy, entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec, @@ -944,8 +945,6 @@ async fn test_jumpstart_network() { initialize_test_logger().await; clean_tests(); - let alice = AccountKeyring::Alice; - let (_validator_ips, _validator_ids) = spawn_testing_validators(ChainSpecType::Integration).await; @@ -955,52 +954,9 @@ async fn test_jumpstart_network() { let api = get_api(&substrate_context.ws_url).await.unwrap(); let rpc = get_rpc(&substrate_context.ws_url).await.unwrap(); - let client = reqwest::Client::new(); - - let block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number + 1; - - put_jumpstart_request_on_chain(&api, &rpc, &alice).await; - - 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 }; - - // succeeds - let response_results = join_all( - vec![3002, 3003, 3004] - .iter() - .map(|port| { - client - .post(format!("http://127.0.0.1:{}/generate_network_key", port)) - .body(onchain_user_request.clone().encode()) - .send() - }) - .collect::>(), - ) - .await; - - for response_result in response_results { - assert_eq!(response_result.unwrap().text().await.unwrap(), ""); - } - - // wait for jump start event check that key exists in kvdb - let mut got_jumpstart_event = false; - for _ in 0..75 { - std::thread::sleep(std::time::Duration::from_millis(1000)); - let block_hash = rpc.chain_get_block_hash(None).await.unwrap(); - let events = EventsClient::new(api.clone()).at(block_hash.unwrap()).await.unwrap(); - let jump_start_event = events.find::(); - for _event in jump_start_event.flatten() { - got_jumpstart_event = true; - break; - } - } - assert!(got_jumpstart_event); + do_jump_start(&api, &rpc, AccountKeyring::Alice.pair()).await; + let client = reqwest::Client::new(); let response_key = unsafe_get(&client, hex::encode(NETWORK_PARENT_KEY), 3001).await; // check to make sure keyshare is correct @@ -1043,18 +999,6 @@ pub async fn put_register_request_on_chain( Ok(registered_event) } -pub async fn put_jumpstart_request_on_chain( - api: &OnlineClient, - rpc: &LegacyRpcMethods, - sig_req_keyring: &Sr25519Keyring, -) { - let sig_req_account = - PairSigner::::new(sig_req_keyring.pair()); - - let registering_tx = entropy::tx().registry().jump_start_network(); - submit_transaction(api, rpc, &sig_req_account, ®istering_tx, None).await.unwrap(); -} - #[tokio::test] async fn test_compute_hash() { initialize_test_logger().await; diff --git a/crates/threshold-signature-server/tests/register_and_sign.rs b/crates/threshold-signature-server/tests/register_and_sign.rs index c2d8353dd..2ecb2403a 100644 --- a/crates/threshold-signature-server/tests/register_and_sign.rs +++ b/crates/threshold-signature-server/tests/register_and_sign.rs @@ -17,7 +17,6 @@ use entropy_client::{ chain_api::{ entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec, entropy::runtime_types::pallet_registry::pallet::ProgramInstance, get_api, get_rpc, - EntropyConfig, }, client as test_client, Hasher, }; @@ -26,36 +25,36 @@ 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, ChainSpecType, + spawn_testing_validators, test_node_process_testing_state, ChainSpecType, }; -use entropy_tss::helpers::tests::initialize_test_logger; +use entropy_tss::helpers::tests::{do_jump_start, initialize_test_logger}; use serial_test::serial; -use sp_core::{sr25519, Pair}; +use sp_core::Pair; use sp_keyring::AccountKeyring; -use subxt::{tx::PairSigner, utils::AccountId32}; +use subxt::utils::AccountId32; use synedrion::k256::ecdsa::VerifyingKey; -#[ignore] #[tokio::test] #[serial] async fn integration_test_register_and_sign() { initialize_test_logger().await; clean_tests(); - let account_owner = AccountKeyring::Ferdie.pair(); - let signature_request_author = AccountKeyring::One; let (_validator_ips, _validator_ids) = spawn_testing_validators(ChainSpecType::Integration).await; let force_authoring = true; let substrate_context = test_node_process_testing_state(force_authoring).await; + let api = get_api(&substrate_context.ws_url).await.unwrap(); let rpc = get_rpc(&substrate_context.ws_url).await.unwrap(); - // Jumpstart the network - let alice = AccountKeyring::Alice; - let signer = PairSigner::::new(alice.clone().into()); - jump_start_network(&api, &rpc, &signer).await; + // First jumpstart the network + do_jump_start(&api, &rpc, AccountKeyring::Alice.pair()).await; + + // Now register an account + let account_owner = AccountKeyring::Ferdie.pair(); + let signature_request_author = AccountKeyring::One; // Store a program let program_pointer = test_client::store_program( diff --git a/crates/threshold-signature-server/tests/sign_eth_tx.rs b/crates/threshold-signature-server/tests/sign_eth_tx.rs index 6fb139ec4..58656be6f 100644 --- a/crates/threshold-signature-server/tests/sign_eth_tx.rs +++ b/crates/threshold-signature-server/tests/sign_eth_tx.rs @@ -17,7 +17,6 @@ use entropy_client::{ chain_api::{ entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec, entropy::runtime_types::pallet_registry::pallet::ProgramInstance, get_api, get_rpc, - EntropyConfig, }, client as test_client, Hasher, }; @@ -25,8 +24,9 @@ use entropy_kvdb::clean_tests; use entropy_protocol::{decode_verifying_key, RecoverableSignature}; use entropy_testing_utils::{ constants::{AUXILARY_DATA_SHOULD_SUCCEED, TEST_PROGRAM_WASM_BYTECODE}, - jump_start_network, spawn_testing_validators, test_node_process_testing_state, ChainSpecType, + spawn_testing_validators, test_node_process_testing_state, ChainSpecType, }; +use entropy_tss::helpers::tests::do_jump_start; use ethers_core::{ abi::ethabi::ethereum_types::{H160, H256}, types::{RecoveryMessage, Transaction, TransactionRequest, U256}, @@ -36,20 +36,17 @@ use ethers_core::{ }, }; use serial_test::serial; -use sp_core::{sr25519, Pair}; +use sp_core::Pair; use sp_keyring::AccountKeyring; -use subxt::{tx::PairSigner, utils::AccountId32}; +use subxt::utils::AccountId32; use synedrion::k256::ecdsa::VerifyingKey; const GOERLI_CHAIN_ID: u64 = 5; -#[ignore] #[tokio::test] #[serial] async fn integration_test_sign_eth_tx() { clean_tests(); - let account_owner = AccountKeyring::Ferdie.pair(); - let signature_request_author = AccountKeyring::One; let (_validator_ips, _validator_ids) = spawn_testing_validators(ChainSpecType::Integration).await; @@ -60,10 +57,11 @@ async fn integration_test_sign_eth_tx() { let api = get_api(&substrate_context.ws_url).await.unwrap(); let rpc = get_rpc(&substrate_context.ws_url).await.unwrap(); - // Jumpstart the network - let alice = AccountKeyring::Alice; - let signer = PairSigner::::new(alice.clone().into()); - jump_start_network(&api, &rpc, &signer).await; + // First jumpstart the network + do_jump_start(&api, &rpc, AccountKeyring::Alice.pair()).await; + + let account_owner = AccountKeyring::Ferdie.pair(); + let signature_request_author = AccountKeyring::One; // Store a program let program_pointer = test_client::store_program(