Skip to content

Commit

Permalink
add confirm reshare in tss
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Jul 29, 2024
1 parent 0e41d56 commit 4e30876
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 6 deletions.
33 changes: 31 additions & 2 deletions crates/threshold-signature-server/src/validator/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
get_signer_and_x25519_secret,
helpers::{
launch::FORBIDDEN_KEYS,
substrate::{get_stash_address, get_validators_info, query_chain},
substrate::{get_stash_address, get_validators_info, query_chain, submit_transaction},
},
signing_client::{protocol_transport::open_protocol_connections, ProtocolErr},
validator::errors::ValidatorErr,
Expand All @@ -40,7 +40,10 @@ use parity_scale_codec::{Decode, Encode};
use rand_core::OsRng;
use sp_core::Pair;
use std::{collections::BTreeSet, str::FromStr, time::Duration};
use subxt::{backend::legacy::LegacyRpcMethods, utils::AccountId32, OnlineClient};
use subxt::{
backend::legacy::LegacyRpcMethods, ext::sp_core::sr25519, tx::PairSigner, utils::AccountId32,
OnlineClient,
};
use synedrion::{
make_key_resharing_session, sessions::SessionId as SynedrionSessionId, KeyResharingInputs,
NewHolder, OldHolder,
Expand Down Expand Up @@ -203,9 +206,35 @@ pub async fn new_reshare(
let _serialized_key_share = key_serialize(&new_key_share)
.map_err(|_| ProtocolErr::KvSerialize("Kv Serialize Error".to_string()))?;
// TODO: do reshare call confirm_reshare (delete key when done) see #941

// TODO: Error handling really complex needs to be thought about.
confirm_key_reshare(&api, &rpc, &signer).await?;
Ok(StatusCode::OK)
}

/// Confirms that a address has finished registering on chain.
pub async fn confirm_key_reshare(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
signer: &PairSigner<EntropyConfig, sr25519::Pair>,
) -> Result<(), ValidatorErr> {
// TODO error handling + return error
// TODO fire and forget, or wait for in block maybe Ddos error
// TODO: Understand this better, potentially use sign_and_submit_default
// or other method under sign_and_*
let block_hash = rpc
.chain_get_block_hash(None)
.await?
.ok_or_else(|| ValidatorErr::OptionUnwrapError("Error getting block hash".to_string()))?;

let nonce_call = entropy::apis().account_nonce_api().account_nonce(signer.account_id().clone());
let nonce = api.runtime_api().at(block_hash).call(nonce_call).await?;

let confirm_key_reshare_request = entropy::tx().staking_extension().confirm_key_reshare();
submit_transaction(api, rpc, signer, &confirm_key_reshare_request, Some(nonce)).await?;
Ok(())
}

/// Validation for if an account can cover tx fees for a tx
pub async fn check_balance_for_fees(
api: &OnlineClient<EntropyConfig>,
Expand Down
8 changes: 4 additions & 4 deletions crates/threshold-signature-server/src/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use entropy_shared::{OcwMessageReshare, EVE_VERIFYING_KEY, MIN_BALANCE};
use entropy_testing_utils::{
constants::{ALICE_STASH_ADDRESS, RANDOM_ACCOUNT},
spawn_testing_validators,
substrate_context::testing_context,
substrate_context::{testing_context, test_node_process_testing_state},
test_context_stationary,
};
use futures::future::join_all;
Expand All @@ -50,10 +50,10 @@ async fn test_reshare() {

let alice = AccountKeyring::Alice;

let cxt = test_context_stationary().await;
let cxt = test_node_process_testing_state(true).await;
let (_validator_ips, _validator_ids) = spawn_testing_validators(true).await;
let api = get_api(&cxt.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();
let api = get_api(&cxt.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.ws_url).await.unwrap();

let client = reqwest::Client::new();
let block_number = rpc.chain_get_header(None).await.unwrap().unwrap().number + 1;
Expand Down
1 change: 1 addition & 0 deletions node/cli/src/chain_spec/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ pub fn development_genesis_config(
})
.collect::<Vec<_>>(),
proactive_refresh_data: (vec![], vec![]),
mock_signer_rotate: false,
inital_signers: initial_authorities.iter().map(|auth| {
auth.0.clone()
})
Expand Down
1 change: 1 addition & 0 deletions node/cli/src/chain_spec/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ pub fn integration_tests_genesis_config(
],
vec![EVE_VERIFYING_KEY.to_vec(), DAVE_VERIFYING_KEY.to_vec()],
),
mock_signer_rotate: true,
inital_signers: initial_authorities.iter().map(|auth| {
auth.0.clone()
})
Expand Down
1 change: 1 addition & 0 deletions node/cli/src/chain_spec/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ pub fn testnet_genesis_config(
})
.collect::<Vec<_>>(),
proactive_refresh_data: (vec![], vec![]),
mock_signer_rotate: false,
inital_signers: initial_authorities.iter().map(|auth| {
auth.0.clone()
})
Expand Down
1 change: 1 addition & 0 deletions pallets/propagation/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
(2, (4, NULL_ARR, vec![11])),
],
proactive_refresh_data: (vec![], vec![]),
mock_signer_rotate: false,
inital_signers: vec![5, 6],
};

Expand Down
1 change: 1 addition & 0 deletions pallets/registry/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
(7, (4, NULL_ARR, vec![50])),
],
proactive_refresh_data: (vec![], vec![]),
mock_signer_rotate: false,
inital_signers: vec![5, 6],
};

Expand Down
9 changes: 9 additions & 0 deletions pallets/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ pub mod pallet {
pub inital_signers: Vec<T::ValidatorId>,
/// validator info and accounts to take part in proactive refresh
pub proactive_refresh_data: (Vec<ValidatorInfo>, Vec<Vec<u8>>),
/// validator info and account to take part in a reshare
pub mock_signer_rotate: bool,
}

#[pallet::genesis_build]
Expand Down Expand Up @@ -229,6 +231,13 @@ pub mod pallet {
proactive_refresh_keys: self.proactive_refresh_data.1.clone(),
};
ProactiveRefresh::<T>::put(refresh_info);

if self.mock_signer_rotate {
NextSigners::<T>::put(NextSignerInfo {
next_signers: self.inital_signers.clone(),
confirmations: vec![],
});
}
}
}
// Errors inform users that something went wrong.
Expand Down
1 change: 1 addition & 0 deletions pallets/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
threshold_servers: vec![(5, (7, NULL_ARR, vec![20])), (6, (8, NULL_ARR, vec![40]))],
inital_signers: vec![5, 6],
proactive_refresh_data: (vec![], vec![]),
mock_signer_rotate: false,
};

pallet_balances.assimilate_storage(&mut t).unwrap();
Expand Down

0 comments on commit 4e30876

Please sign in to comment.