Skip to content

Commit

Permalink
Fix for deadline issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Nov 25, 2024
1 parent 0204c83 commit 4381c76
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 195 deletions.
10 changes: 4 additions & 6 deletions crates/threshold-signature-server/src/helpers/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
// only compile when testing or when the test_helpers feature is enabled
#![cfg(any(test, feature = "test_helpers"))]

#[cfg(test)]
use crate::helpers::tests::entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec;
use crate::{
app,
chain_api::{
entropy::{
self, runtime_types::bounded_collections::bounded_vec::BoundedVec,
runtime_types::pallet_staking_extension::pallet::JumpStartStatus,
},
entropy::{self, runtime_types::pallet_staking_extension::pallet::JumpStartStatus},
EntropyConfig,
},
get_signer,
Expand All @@ -35,7 +34,6 @@ use crate::{
},
logger::{Instrumentation, Logger},
substrate::submit_transaction,
validator::get_signer_and_x25519_secret_from_mnemonic,
},
signing_client::ListenerState,
AppState,
Expand All @@ -46,7 +44,7 @@ use entropy_kvdb::{encrypted_sled::PasswordMethod, get_db_path, kv_manager::KvMa
use entropy_protocol::PartyId;
#[cfg(test)]
use entropy_shared::EncodedVerifyingKey;
use entropy_shared::{EVE_VERIFYING_KEY, NETWORK_PARENT_KEY};
use entropy_shared::NETWORK_PARENT_KEY;
use std::{fmt, time::Duration};
use subxt::{
backend::legacy::LegacyRpcMethods, ext::sp_core::sr25519, tx::PairSigner,
Expand Down
6 changes: 4 additions & 2 deletions crates/threshold-signature-server/src/signing_client/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ use sp_keyring::AccountKeyring;
async fn test_proactive_refresh() {
initialize_test_logger().await;
clean_tests();
let _cxt = &test_node_process_testing_state(ChainSpecType::Integration, false).await[0];
let _cxt =
&test_node_process_testing_state(ChainSpecType::IntegrationJumpStarted, false).await[0];

let (validator_ips, _ids) =
spawn_testing_validators(crate::helpers::tests::ChainSpecType::Integration).await;
spawn_testing_validators(crate::helpers::tests::ChainSpecType::IntegrationJumpStarted)
.await;
let signing_committee_ips = &validator_ips[..3].to_vec();

let client = reqwest::Client::new();
Expand Down
58 changes: 42 additions & 16 deletions crates/threshold-signature-server/src/validator/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use subxt::{
OnlineClient,
};
use synedrion::{KeyResharingInputs, NewHolder, OldHolder};
use x25519_dalek::StaticSecret;

/// HTTP POST endpoint called by the off-chain worker (propagation pallet) during network reshare.
///
Expand All @@ -61,20 +62,48 @@ pub async fn new_reshare(
let rpc = get_rpc(&app_state.configuration.endpoint).await?;
validate_new_reshare(&api, &rpc, &data, &app_state.kv_store).await?;

let (signer, x25519_secret_key) = get_signer_and_x25519_secret(&app_state.kv_store)
.await
.map_err(|e| ValidatorErr::UserError(e.to_string()))?;

let next_signers_query = entropy::storage().staking_extension().next_signers();
let next_signers = query_chain(&api, &rpc, next_signers_query, None)
.await?
.ok_or_else(|| ValidatorErr::ChainFetch("Error getting next signers"))?
.next_signers;

let validators_info = get_validators_info(&api, &rpc, next_signers)
.await
.map_err(|e| ValidatorErr::UserError(e.to_string()))?;

let (signer, x25519_secret_key) = get_signer_and_x25519_secret(&app_state.kv_store)
.await
.map_err(|e| ValidatorErr::UserError(e.to_string()))?;
let is_proper_signer = validators_info
.iter()
.any(|validator_info| validator_info.tss_account == *signer.account_id());

if !is_proper_signer {
return Ok(StatusCode::MISDIRECTED_REQUEST);
}

// Do reshare in a separate task so we can already respond
tokio::spawn(async move {
if let Err(err) =
do_reshare(api, &rpc, signer, &x25519_secret_key, data, validators_info, app_state)
.await
{
tracing::error!("Error during reshare: {err}");
}
});
Ok(StatusCode::OK)
}

async fn do_reshare(
api: OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
signer: PairSigner<EntropyConfig, sr25519::Pair>,
x25519_secret_key: &StaticSecret,
data: OcwMessageReshare,
validators_info: Vec<ValidatorInfo>,
app_state: AppState,
) -> Result<(), ValidatorErr> {
let verifying_key_query = entropy::storage().staking_extension().jump_start_progress();
let parent_key_details = query_chain(&api, &rpc, verifying_key_query, None)
.await?
Expand All @@ -92,15 +121,6 @@ pub async fn new_reshare(
.map_err(|_| ValidatorErr::Conversion("Verifying key conversion"))?,
)
.map_err(|e| ValidatorErr::VerifyingKeyError(e.to_string()))?;

let is_proper_signer = validators_info
.iter()
.any(|validator_info| validator_info.tss_account == *signer.account_id());

if !is_proper_signer {
return Ok(StatusCode::MISDIRECTED_REQUEST);
}

let my_stash_address = get_stash_address(&api, &rpc, signer.account_id())
.await
.map_err(|e| ValidatorErr::UserError(e.to_string()))?;
Expand Down Expand Up @@ -159,6 +179,7 @@ pub async fn new_reshare(
converted_validator_info.push(validator_info.clone());
tss_accounts.push(validator_info.tss_account.clone());
}

let channels = get_channels(
&app_state.listener_state,
converted_validator_info,
Expand All @@ -168,7 +189,6 @@ pub async fn new_reshare(
&x25519_secret_key,
)
.await?;

let (new_key_share, aux_info) =
execute_reshare(session_id.clone(), channels, signer.signer(), inputs, &new_holders, None)
.await?;
Expand All @@ -186,7 +206,7 @@ pub async fn new_reshare(

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

/// HTTP POST endpoint called by the off-chain worker (propagation pallet) after a network key reshare.
Expand All @@ -199,7 +219,10 @@ pub async fn rotate_network_key(
// validate from chain
let api = get_api(&app_state.configuration.endpoint).await?;
let rpc = get_rpc(&app_state.configuration.endpoint).await?;

tracing::info!("Rotate network key called");
validate_rotate_network_key(&api, &rpc).await?;
tracing::info!("Rotate network key validated");

let (signer, _) = get_signer_and_x25519_secret(&app_state.kv_store)
.await
Expand All @@ -221,10 +244,12 @@ pub async fn rotate_network_key(
)
.await?;

tracing::info!("Proper signer: {is_proper_signer}");

if !is_proper_signer {
return Ok(StatusCode::MISDIRECTED_REQUEST);
}

tracing::info!("Rotating network key");
let network_parent_key_heading = hex::encode(NETWORK_PARENT_KEY);
let next_network_parent_key_heading = hex::encode(NEXT_NETWORK_PARENT_KEY);

Expand All @@ -238,6 +263,7 @@ pub async fn rotate_network_key(

let reservation = app_state.kv_store.kv().reserve_key(network_parent_key_heading).await?;
app_state.kv_store.kv().put(reservation, new_parent_key).await?;
tracing::info!("Rotated network key");
Ok(StatusCode::OK)
}

Expand Down
Loading

0 comments on commit 4381c76

Please sign in to comment.