Skip to content

Commit

Permalink
Update tests and improve test helper fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Sep 2, 2024
1 parent c49f981 commit 8643c49
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 131 deletions.
6 changes: 2 additions & 4 deletions crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ pub async fn jump_start_network_with_signer(
}
}

/// Helper to store a program and register a user. Returns the verify key
/// Helper to store a program and register a user. Returns the verify key and program hash.
#[cfg(test)]
pub async fn store_program_and_register(
api: &OnlineClient<EntropyConfig>,
Expand All @@ -274,7 +274,7 @@ pub async fn store_program_and_register(
let program_hash = test_client::store_program(
&api,
&rpc,
deployer, // This is our program deployer
deployer,
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
Expand All @@ -283,8 +283,6 @@ pub async fn store_program_and_register(
.await
.unwrap();

println!("Program hash {:?}", program_hash);

let (verifying_key, _registered_info) = test_client::register(
&api,
&rpc,
Expand Down
164 changes: 37 additions & 127 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,37 +186,13 @@ async fn test_signature_requests_fail_on_different_conditions() {
// Register the user with a test program
let (verifying_key, program_hash) =
store_program_and_register(&entropy_api, &rpc, &one.pair(), &two.pair()).await;
// // We need to store a program in order to be able to register succesfully
// let program_hash = test_client::store_program(
// &entropy_api,
// &rpc,
// &two.pair(), // This is our program deployer
// TEST_PROGRAM_WASM_BYTECODE.to_owned(),
// vec![],
// vec![],
// vec![],
// )
// .await
// .unwrap();
//
// let (verifying_key, _registered_info) = test_client::register(
// &entropy_api,
// &rpc,
// one.clone().into(), // This is our program modification account
// subxtAccountId32(two.public().0), // This is our signature request account
// BoundedVec(vec![ProgramInstance { program_pointer: program_hash, program_config: vec![] }]),
// )
// .await
// .unwrap();

// Test: We check that an account with a program succeeds in submiting a signature request
let (validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED), verifying_key)
.await;

// The account we registered does have a program pointer, so this should succeed
signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
signature_request.signature_verifying_key = verifying_key.to_vec();

let test_user_res =
submit_transaction_requests(validator_ips_and_keys.clone(), signature_request.clone(), one)
.await;
Expand Down Expand Up @@ -343,35 +319,13 @@ async fn signature_request_with_derived_account_works() {
// for later
jump_start_network(&entropy_api, &rpc).await;

// We need to store a program in order to be able to register succesfully
let program_hash = test_client::store_program(
&entropy_api,
&rpc,
&bob.pair(), // This is our program deployer
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();

let (verifying_key, _registered_info) = test_client::register(
&entropy_api,
&rpc,
charlie.clone().into(), // This is our program modification account
subxtAccountId32(alice.public().0), // This is our signature request account
BoundedVec(vec![ProgramInstance { program_pointer: program_hash, program_config: vec![] }]),
)
.await
.unwrap();

let (validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;
// Register the user with a test program
let (verifying_key, _program_hash) =
store_program_and_register(&entropy_api, &rpc, &charlie.pair(), &bob.pair()).await;

// We'll use the actual verifying key we registered for the signature request
signature_request.signature_verifying_key = verifying_key.to_vec();
signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
let (validators_info, signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED), verifying_key)
.await;

let signature_request_responses = submit_transaction_requests(
validator_ips_and_keys.clone(),
Expand Down Expand Up @@ -412,8 +366,13 @@ async fn test_signing_fails_if_wrong_participants_are_used() {

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

let (_validators_info, signature_request, _validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;
let (_validators_info, signature_request, _validator_ips_and_keys) = get_sign_tx_data(
&entropy_api,
&rpc,
hex::encode(PREIMAGE_SHOULD_SUCCEED),
DAVE_VERIFYING_KEY,
)
.await;

// fails verification tests
// wrong key for wrong validator
Expand Down Expand Up @@ -484,36 +443,16 @@ async fn test_request_limit_are_updated_during_signing() {

jump_start_network(&entropy_api, &rpc).await;

let program_hash = test_client::store_program(
&entropy_api,
&rpc,
&two.pair(), // This is our program deployer
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();

let (verifying_key, _registered_info) = test_client::register(
&entropy_api,
&rpc,
one.clone().into(), // This is our program modification account
subxtAccountId32(two.public().0), // This is our signature request account
BoundedVec(vec![ProgramInstance { program_pointer: program_hash, program_config: vec![] }]),
)
.await
.unwrap();
// Register the user with a test program
let (verifying_key, _program_hash) =
store_program_and_register(&entropy_api, &rpc, &one.pair(), &two.pair()).await;

// Test: We check that the rate limiter changes as expected when signature requests are sent

// First we need to get a signature request to populate the KVDB for our verifying key
let (validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;

signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
signature_request.signature_verifying_key = verifying_key.to_vec();
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED), verifying_key)
.await;

let test_user_res =
submit_transaction_requests(validator_ips_and_keys.clone(), signature_request.clone(), one)
Expand Down Expand Up @@ -606,30 +545,13 @@ async fn test_fails_to_sign_if_non_signing_group_participants_are_used() {

jump_start_network(&entropy_api, &rpc).await;

let program_hash = test_client::store_program(
&entropy_api,
&rpc,
&two.pair(), // This is our program deployer
TEST_PROGRAM_WASM_BYTECODE.to_owned(),
vec![],
vec![],
vec![],
)
.await
.unwrap();

let (verifying_key, _registered_info) = test_client::register(
&entropy_api,
&rpc,
one.clone().into(), // This is our program modification account
subxtAccountId32(two.public().0), // This is our signature request account
BoundedVec(vec![ProgramInstance { program_pointer: program_hash, program_config: vec![] }]),
)
.await
.unwrap();
// Register the user with a test program
let (verifying_key, _program_hash) =
store_program_and_register(&entropy_api, &rpc, &one.pair(), &two.pair()).await;

let (_validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;
let (_validators_info, signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED), verifying_key)
.await;

let message_hash = Hasher::keccak(PREIMAGE_SHOULD_SUCCEED);
let signature_request_account = subxtAccountId32(one.pair().public().0);
Expand Down Expand Up @@ -675,12 +597,9 @@ async fn test_fails_to_sign_if_non_signing_group_participants_are_used() {
encrypted_connection.recv().await.is_err()
});

signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
signature_request.signature_verifying_key = verifying_key.to_vec();

let test_user_bad_connection_res = submit_transaction_requests(
vec![validator_ips_and_keys[0].clone()],
signature_request.clone(),
signature_request,
one,
)
.await;
Expand Down Expand Up @@ -765,12 +684,8 @@ async fn test_program_with_config() {
.unwrap();

// Now we'll send off a signature request using the new program
let (validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(message)).await;

// We'll use the actual verifying key we registered for the signature request
signature_request.signature_verifying_key = verifying_key.to_vec();
signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
let (validators_info, signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(message), verifying_key).await;

// Here we check that the signature request was indeed completed successfully
let signature_request_responses =
Expand Down Expand Up @@ -1039,12 +954,8 @@ async fn test_fail_infinite_program() {
.unwrap();

// Now we'll send off a signature request using the new program
let (_validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;

// We'll use the actual verifying key we registered for the signature request
signature_request.signature_verifying_key = verifying_key.to_vec();
signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
let (_validators_info, signature_request, validator_ips_and_keys) =
get_sign_tx_data(&api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED), verifying_key).await;

let test_infinite_loop =
submit_transaction_requests(validator_ips_and_keys.clone(), signature_request.clone(), one)
Expand Down Expand Up @@ -1181,11 +1092,9 @@ async fn test_device_key_proxy() {

// Now we'll send off a signature request using the new program with auxilary data
let (validators_info, mut signature_request, validator_ips_and_keys) =
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED)).await;
get_sign_tx_data(&entropy_api, &rpc, hex::encode(PREIMAGE_SHOULD_SUCCEED), verifying_key)
.await;

// We'll use the actual verifying key we registered for the signature request
signature_request.signature_verifying_key = verifying_key.to_vec();
signature_request.block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number;
signature_request.auxilary_data = auxilary_data;

let test_user_res =
Expand Down Expand Up @@ -1390,7 +1299,7 @@ async fn test_faucet() {

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

Expand Down Expand Up @@ -1587,6 +1496,7 @@ pub async fn get_sign_tx_data(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
message: String,
signature_verifying_key: [u8; 33],
) -> (Vec<ValidatorInfo>, UserSignatureRequest, Vec<(String, [u8; 32])>) {
let validators_info = get_signers_from_chain(api, rpc).await.unwrap();

Expand All @@ -1597,9 +1507,9 @@ pub async fn get_sign_tx_data(
Some(hex::encode(AUXILARY_DATA_SHOULD_SUCCEED)),
]),
validators_info: validators_info.clone(),
block_number: 0,
block_number: rpc.chain_get_header(None).await.unwrap().unwrap().number,
hash: HashingAlgorithm::Keccak,
signature_verifying_key: DAVE_VERIFYING_KEY.to_vec(),
signature_verifying_key: signature_verifying_key.to_vec(),
};

let validator_ips_and_keys =
Expand Down

0 comments on commit 8643c49

Please sign in to comment.