Skip to content

Commit

Permalink
Select validators based on block number when doing initial DKG
Browse files Browse the repository at this point in the history
  • Loading branch information
ameba23 committed Sep 13, 2024
1 parent 815c356 commit c515a98
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/threshold-signature-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ thiserror ="1.0.63"
blake2 ="0.10.4"
x25519-dalek ={ version="2.0.1", features=["static_secrets"] }
rand_core ="0.6.4"
rand ="0.8.5"
zeroize ="1.8.1"
hex ="0.4.3"
reqwest-eventsource="0.6"
Expand Down
48 changes: 46 additions & 2 deletions crates/threshold-signature-server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ use futures::{
};
use num::{bigint::BigInt, FromPrimitive, Num, ToPrimitive};
use parity_scale_codec::{Decode, DecodeAll, Encode};
use rand::{seq::SliceRandom, SeedableRng};
use rand_core::OsRng;
use serde::{Deserialize, Serialize};
use sp_core::{crypto::AccountId32, H256};
use subxt::{
Expand All @@ -55,6 +57,7 @@ use subxt::{
Config, OnlineClient,
};
use synedrion::ThresholdKeyShare;
use tokio::select;
use tracing::instrument;
use x25519_dalek::StaticSecret;
use zeroize::Zeroize;
Expand Down Expand Up @@ -262,7 +265,7 @@ pub async fn generate_network_key(
State(app_state): State<AppState>,
encoded_data: Bytes,
) -> Result<StatusCode, UserErr> {
let data = OcwMessageDkg::decode(&mut encoded_data.as_ref())?;
let mut data = OcwMessageDkg::decode(&mut encoded_data.as_ref())?;
tracing::Span::current().record("block_number", data.block_number);

if data.sig_request_accounts.is_empty() {
Expand All @@ -273,8 +276,10 @@ pub async fn generate_network_key(
let rpc = get_rpc(&app_state.configuration.endpoint).await?;
let (signer, x25519_secret_key) = get_signer_and_x25519_secret(&app_state.kv_store).await?;

let selected_validators = get_jumpstart_validators(&api, &rpc, data.block_number).await?;
println!("selected_validators {:?}", selected_validators);
let in_registration_group =
check_in_registration_group(&data.validators_info, signer.account_id());
check_in_registration_group(&selected_validators, signer.account_id());

if in_registration_group.is_err() {
tracing::warn!(
Expand All @@ -286,6 +291,7 @@ pub async fn generate_network_key(
return Ok(StatusCode::MISDIRECTED_REQUEST);
}

data.validators_info = selected_validators;
validate_jump_start(&data, &api, &rpc, &app_state.kv_store).await?;

// Do the DKG protocol in another task, so we can already respond
Expand All @@ -300,6 +306,44 @@ pub async fn generate_network_key(
Ok(StatusCode::OK)
}

async fn get_jumpstart_validators(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
block_number: u32,
) -> Result<Vec<entropy_shared::ValidatorInfo>, UserErr> {
let total_signers_query = entropy::storage().parameters().signers_info();
let signers_info = query_chain(api, rpc, total_signers_query, None).await?.unwrap();
let n = signers_info.total_signers;

let mut validators_info: Vec<_> = vec![];

let validators_query = entropy::storage().session().validators();
let validators = query_chain(api, rpc, validators_query, None).await?.unwrap();

let block_number_bytes = block_number.to_le_bytes();
let mut seed = [0; 32];
seed[..4].copy_from_slice(&block_number_bytes);
let mut rng = rand::rngs::StdRng::from_seed(seed);

let selected_validators: Vec<_> =
validators.choose_multiple(&mut rng, n.into()).cloned().collect();
for validator_address in selected_validators {
let validator_query =
entropy::storage().staking_extension().threshold_servers(validator_address);
let server_info = query_chain(api, rpc, validator_query, None).await?.unwrap();
// .ok_or_else(|| {
// SubgroupGetError::ChainFetch("threshold_servers query error")
// })?;
validators_info.push(entropy_shared::ValidatorInfo {
x25519_public_key: server_info.x25519_public_key,
ip_address: server_info.endpoint,
tss_account: server_info.tss_account.0.to_vec(),
});
}

Ok(validators_info)
}

/// Setup and execute DKG.
///
/// Called internally by the [generate_network_key] function.
Expand Down
14 changes: 9 additions & 5 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,15 @@ async fn test_jumpstart_network() {

let alice = AccountKeyring::Alice;

let cxt = test_context_stationary().await;
let add_parent_key = false;
let (_validator_ips, _validator_ids) =
spawn_testing_validators(false, ChainSpecType::Development).await;
let api = get_api(&cxt.node_proc.ws_url).await.unwrap();
let rpc = get_rpc(&cxt.node_proc.ws_url).await.unwrap();
spawn_testing_validators(add_parent_key, ChainSpecType::Integration).await;

let force_authoring = true;
let substrate_context = test_node_process_testing_state(force_authoring).await;

let api = get_api(&substrate_context.ws_url).await.unwrap();
let rpc = get_rpc(&substrate_context.ws_url).await.unwrap();

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

Expand Down Expand Up @@ -753,7 +757,7 @@ async fn test_jumpstart_network() {

// succeeds
let response_results = join_all(
vec![3002, 3003]
vec![3002, 3003, 3004]
.iter()
.map(|port| {
client
Expand Down

0 comments on commit c515a98

Please sign in to comment.