From 4e3087605007671f763431f84fce0956384e1de3 Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Mon, 29 Jul 2024 11:43:22 -0400 Subject: [PATCH] add confirm reshare in tss --- .../src/validator/api.rs | 33 +++++++++++++++++-- .../src/validator/tests.rs | 8 ++--- node/cli/src/chain_spec/dev.rs | 1 + node/cli/src/chain_spec/integration_tests.rs | 1 + node/cli/src/chain_spec/testnet.rs | 1 + pallets/propagation/src/mock.rs | 1 + pallets/registry/src/mock.rs | 1 + pallets/staking/src/lib.rs | 9 +++++ pallets/staking/src/mock.rs | 1 + 9 files changed, 50 insertions(+), 6 deletions(-) diff --git a/crates/threshold-signature-server/src/validator/api.rs b/crates/threshold-signature-server/src/validator/api.rs index e7e6738b5..f83b1f4de 100644 --- a/crates/threshold-signature-server/src/validator/api.rs +++ b/crates/threshold-signature-server/src/validator/api.rs @@ -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, @@ -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, @@ -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, + rpc: &LegacyRpcMethods, + signer: &PairSigner, +) -> 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, diff --git a/crates/threshold-signature-server/src/validator/tests.rs b/crates/threshold-signature-server/src/validator/tests.rs index cc4bb3efd..28897d8a6 100644 --- a/crates/threshold-signature-server/src/validator/tests.rs +++ b/crates/threshold-signature-server/src/validator/tests.rs @@ -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; @@ -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; diff --git a/node/cli/src/chain_spec/dev.rs b/node/cli/src/chain_spec/dev.rs index 5db913f15..4db7b650a 100644 --- a/node/cli/src/chain_spec/dev.rs +++ b/node/cli/src/chain_spec/dev.rs @@ -276,6 +276,7 @@ pub fn development_genesis_config( }) .collect::>(), proactive_refresh_data: (vec![], vec![]), + mock_signer_rotate: false, inital_signers: initial_authorities.iter().map(|auth| { auth.0.clone() }) diff --git a/node/cli/src/chain_spec/integration_tests.rs b/node/cli/src/chain_spec/integration_tests.rs index 1761bff73..ba80280eb 100644 --- a/node/cli/src/chain_spec/integration_tests.rs +++ b/node/cli/src/chain_spec/integration_tests.rs @@ -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() }) diff --git a/node/cli/src/chain_spec/testnet.rs b/node/cli/src/chain_spec/testnet.rs index c304a555d..cecde11fa 100644 --- a/node/cli/src/chain_spec/testnet.rs +++ b/node/cli/src/chain_spec/testnet.rs @@ -414,6 +414,7 @@ pub fn testnet_genesis_config( }) .collect::>(), proactive_refresh_data: (vec![], vec![]), + mock_signer_rotate: false, inital_signers: initial_authorities.iter().map(|auth| { auth.0.clone() }) diff --git a/pallets/propagation/src/mock.rs b/pallets/propagation/src/mock.rs index 22e52f965..8cbcfa987 100644 --- a/pallets/propagation/src/mock.rs +++ b/pallets/propagation/src/mock.rs @@ -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], }; diff --git a/pallets/registry/src/mock.rs b/pallets/registry/src/mock.rs index 18a28acae..3f94d8c41 100644 --- a/pallets/registry/src/mock.rs +++ b/pallets/registry/src/mock.rs @@ -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], }; diff --git a/pallets/staking/src/lib.rs b/pallets/staking/src/lib.rs index ddc0ee4fb..9e1666820 100644 --- a/pallets/staking/src/lib.rs +++ b/pallets/staking/src/lib.rs @@ -200,6 +200,8 @@ pub mod pallet { pub inital_signers: Vec, /// validator info and accounts to take part in proactive refresh pub proactive_refresh_data: (Vec, Vec>), + /// validator info and account to take part in a reshare + pub mock_signer_rotate: bool, } #[pallet::genesis_build] @@ -229,6 +231,13 @@ pub mod pallet { proactive_refresh_keys: self.proactive_refresh_data.1.clone(), }; ProactiveRefresh::::put(refresh_info); + + if self.mock_signer_rotate { + NextSigners::::put(NextSignerInfo { + next_signers: self.inital_signers.clone(), + confirmations: vec![], + }); + } } } // Errors inform users that something went wrong. diff --git a/pallets/staking/src/mock.rs b/pallets/staking/src/mock.rs index ad34160d7..6f1cd56fb 100644 --- a/pallets/staking/src/mock.rs +++ b/pallets/staking/src/mock.rs @@ -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();