Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Aug 8, 2024
1 parent 3522f67 commit 25ad441
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
9 changes: 5 additions & 4 deletions crates/threshold-signature-server/src/validator/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub async fn new_reshare(
validators_info.iter().cloned().map(|x| PartyId::new(x.tss_account)).collect();
// let mut new_signer_address = data.new_signer;
let pruned_old_holders =
prune_old_holders(&api, &rpc, data.new_signer, &validators_info).await?;
prune_old_holders(&api, &rpc, data.new_signer, validators_info.clone()).await?;

let old_holders: BTreeSet<PartyId> =
pruned_old_holders.into_iter().map(|x| PartyId::new(x.tss_account)).collect();
Expand Down Expand Up @@ -317,22 +317,23 @@ pub fn check_forbidden_key(key: &str) -> Result<(), ValidatorErr> {
Ok(())
}

/// Filters out new signer from next signers to get old holders
pub async fn prune_old_holders(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
new_signer: Vec<u8>,
validators_info: &Vec<ValidatorInfo>,
validators_info: Vec<ValidatorInfo>,
) -> Result<Vec<ValidatorInfo>, ValidatorErr> {
Ok(if !new_signer.is_empty() {
let address_slice: &[u8; 32] = &new_signer.clone().try_into().unwrap();
let new_signer_address = AccountId32(*address_slice);
let new_signer_info = &get_validators_info(&api, &rpc, vec![new_signer_address])
let new_signer_info = &get_validators_info(api, rpc, vec![new_signer_address])
.await
.map_err(|e| ValidatorErr::UserError(e.to_string()))?[0];
validators_info
.iter()
.cloned()
.filter(|x| x.tss_account != new_signer_info.tss_account)
.cloned()
.collect()
} else {
validators_info.clone()
Expand Down
22 changes: 20 additions & 2 deletions crates/threshold-signature-server/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use crate::{
},
validator::get_signer_and_x25519_secret_from_mnemonic,
},
validator::{api::validate_new_reshare, errors::ValidatorErr},
validator::{
api::{prune_old_holders, validate_new_reshare},
errors::ValidatorErr,
},
};
use entropy_kvdb::clean_tests;
use entropy_shared::{
Expand Down Expand Up @@ -151,7 +154,7 @@ async fn test_reshare_validation_fail_not_in_reshare() {
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();
let kv = setup_client().await;

let mut block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number + 1;
let block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number + 1;
let ocw_message = OcwMessageReshare { new_signer: alice.public().encode(), block_number };

run_to_block(&rpc, block_number + 1).await;
Expand All @@ -163,6 +166,21 @@ async fn test_reshare_validation_fail_not_in_reshare() {
clean_tests();
}

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

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

assert!(prune_old_holders(&api, &rpc, vec![], vec![]).await.is_ok());

clean_tests();
}

async fn setup_for_reshare(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
Expand Down

0 comments on commit 25ad441

Please sign in to comment.