Skip to content

Commit

Permalink
Unignore register and sign integration test, and do a non-mock jumpst…
Browse files Browse the repository at this point in the history
…art (#1070)

* Unignore register and sign test, and do a non-mock jumpstart

* Make jumpstart helper and unignore other test

* Clippy

* Comments, tidy
  • Loading branch information
ameba23 authored Sep 27, 2024
1 parent e1eaf12 commit 383ccfc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 84 deletions.
68 changes: 66 additions & 2 deletions crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
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::<Vec<_>>(),
)
.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::<entropy::registry::events::FinishedNetworkJumpStart>();
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<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
pair: sr25519::Pair,
) {
let account = PairSigner::<EntropyConfig, sp_core::sr25519::Pair>::new(pair);

let registering_tx = entropy::tx().registry().jump_start_network();
submit_transaction(api, rpc, &account, &registering_tx, None).await.unwrap();
}
62 changes: 3 additions & 59 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -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::<Vec<_>>(),
)
.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::<entropy::registry::events::FinishedNetworkJumpStart>();
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
Expand Down Expand Up @@ -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<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
sig_req_keyring: &Sr25519Keyring,
) {
let sig_req_account =
PairSigner::<EntropyConfig, sp_core::sr25519::Pair>::new(sig_req_keyring.pair());

let registering_tx = entropy::tx().registry().jump_start_network();
submit_transaction(api, rpc, &sig_req_account, &registering_tx, None).await.unwrap();
}

#[tokio::test]
async fn test_compute_hash() {
initialize_test_logger().await;
Expand Down
23 changes: 11 additions & 12 deletions crates/threshold-signature-server/tests/register_and_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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::<EntropyConfig, sr25519::Pair>::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(
Expand Down
20 changes: 9 additions & 11 deletions crates/threshold-signature-server/tests/sign_eth_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ 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,
};
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},
Expand All @@ -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;
Expand All @@ -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::<EntropyConfig, sr25519::Pair>::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(
Expand Down

0 comments on commit 383ccfc

Please sign in to comment.