Skip to content

Commit

Permalink
move get_signers_from_chain
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Sep 23, 2024
1 parent 961ae6a commit f6dc78a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 69 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.

62 changes: 0 additions & 62 deletions crates/client/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::{
substrate::query_chain,
};
use entropy_shared::{user::ValidatorInfo, BlockNumber, HashingAlgorithm};
use rand::prelude::SliceRandom;
use serde::{Deserialize, Serialize};
use subxt::{backend::legacy::LegacyRpcMethods, OnlineClient};

Expand Down Expand Up @@ -56,67 +55,6 @@ pub struct RelayerSignatureRequest {
pub validators_info: Vec<ValidatorInfo>,
}

/// Returns a threshold of signer's ValidatorInfo from the chain
pub async fn get_signers_from_chain(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
) -> Result<Vec<ValidatorInfo>, SubgroupGetError> {
let signer_query = entropy::storage().staking_extension().signers();
let signers = query_chain(api, rpc, signer_query, None)
.await?
.ok_or_else(|| SubgroupGetError::ChainFetch("Get all validators error"))?;

let key_info_query = entropy::storage().parameters().signers_info();
let threshold = query_chain(api, rpc, key_info_query, None)
.await?
.ok_or_else(|| SubgroupGetError::ChainFetch("Failed to get signers info"))?
.threshold;

let selected_signers: Vec<_> = {
let mut cloned_signers = signers.clone();
// TODO: temp remove dave for now until test dave is spun up correctly
cloned_signers.pop();
cloned_signers
.choose_multiple(&mut rand::thread_rng(), threshold as usize)
.cloned()
.collect()
};

let block_hash = rpc.chain_get_block_hash(None).await?;
let mut handles = Vec::new();

for signer in selected_signers {
let handle: tokio::task::JoinHandle<Result<ValidatorInfo, SubgroupGetError>> =
tokio::task::spawn({
let api = api.clone();
let rpc = rpc.clone();
async move {
let threshold_address_query =
entropy::storage().staking_extension().threshold_servers(signer);
let server_info = query_chain(&api, &rpc, threshold_address_query, block_hash)
.await?
.ok_or_else(|| {
SubgroupGetError::ChainFetch("threshold_servers query error")
})?;
Ok(ValidatorInfo {
x25519_public_key: server_info.x25519_public_key,
ip_address: std::str::from_utf8(&server_info.endpoint)?.to_string(),
tss_account: server_info.tss_account,
})
}
});

handles.push(handle);
}

let mut all_signers: Vec<ValidatorInfo> = vec![];
for handle in handles {
all_signers.push(handle.await??);
}

Ok(all_signers)
}

/// Gets a validator from chain to relay a message to the signers
/// Filters out all signers
pub async fn get_validators_not_signer_for_relay(
Expand Down
1 change: 1 addition & 0 deletions crates/threshold-signature-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ project-root={ version="0.2.2", optional=true }
tdx-quote={ git="https://github.com/entropyxyz/tdx-quote", rev="f7968ff", optional=true, features=[
"mock",
] }
rand={ version="0.8", default-features=false }

[dev-dependencies]
serial_test ="3.1.1"
Expand Down
59 changes: 59 additions & 0 deletions crates/threshold-signature-server/src/helpers/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
};
pub use entropy_client::substrate::{query_chain, submit_transaction};
use entropy_shared::user::ValidatorInfo;
use rand::prelude::SliceRandom;
use subxt::{backend::legacy::LegacyRpcMethods, utils::AccountId32, Config, OnlineClient};

/// Given a threshold server's account ID, return its corresponding stash (validator) address.
Expand Down Expand Up @@ -108,3 +109,61 @@ pub async fn get_validators_info(
}
Ok(all_signers)
}

/// Returns a threshold of signer's ValidatorInfo from the chain
pub async fn get_signers_from_chain(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
) -> Result<Vec<ValidatorInfo>, UserErr> {
let signer_query = entropy::storage().staking_extension().signers();
let signers = query_chain(api, rpc, signer_query, None)
.await?
.ok_or_else(|| UserErr::ChainFetch("Get all validators error"))?;

let key_info_query = entropy::storage().parameters().signers_info();
let threshold = query_chain(api, rpc, key_info_query, None)
.await?
.ok_or_else(|| UserErr::ChainFetch("Failed to get signers info"))?
.threshold;

let selected_signers: Vec<_> = {
let mut cloned_signers = signers.clone();
// TODO: temp remove dave for now until test dave is spun up correctly
cloned_signers.pop();
cloned_signers
.choose_multiple(&mut rand::thread_rng(), threshold as usize)
.cloned()
.collect()
};

let block_hash = rpc.chain_get_block_hash(None).await?;
let mut handles = Vec::new();

for signer in selected_signers {
let handle: tokio::task::JoinHandle<Result<ValidatorInfo, UserErr>> = tokio::task::spawn({
let api = api.clone();
let rpc = rpc.clone();
async move {
let threshold_address_query =
entropy::storage().staking_extension().threshold_servers(signer);
let server_info = query_chain(&api, &rpc, threshold_address_query, block_hash)
.await?
.ok_or_else(|| UserErr::ChainFetch("threshold_servers query error"))?;
Ok(ValidatorInfo {
x25519_public_key: server_info.x25519_public_key,
ip_address: std::str::from_utf8(&server_info.endpoint)?.to_string(),
tss_account: server_info.tss_account,
})
}
});

handles.push(handle);
}

let mut all_signers: Vec<ValidatorInfo> = vec![];
for handle in handles {
all_signers.push(handle.await??);
}

Ok(all_signers)
}
8 changes: 3 additions & 5 deletions crates/threshold-signature-server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ use crate::{
launch::LATEST_BLOCK_NUMBER_NEW_USER,
signing::{do_signing, Hasher},
substrate::{
get_oracle_data, get_program, get_stash_address, get_validators_info, query_chain,
submit_transaction,
get_oracle_data, get_program, get_signers_from_chain, get_stash_address,
get_validators_info, query_chain, submit_transaction,
},
user::{check_in_registration_group, compute_hash, do_dkg},
validator::{get_signer, get_signer_and_x25519_secret},
Expand All @@ -86,9 +86,7 @@ use crate::{
AppState, Configuration,
};

pub use entropy_client::user::{
get_signers_from_chain, RelayerSignatureRequest, UserSignatureRequest,
};
pub use entropy_client::user::{RelayerSignatureRequest, UserSignatureRequest};
pub const REQUEST_KEY_HEADER: &str = "REQUESTS";

/// Type for validators to send user key's back and forth
Expand Down
3 changes: 1 addition & 2 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use entropy_client::substrate::get_registered_details;
use entropy_client::{
client as test_client,
client::{sign, update_programs},
user::get_signers_from_chain,
};
use entropy_kvdb::{
clean_tests,
Expand Down Expand Up @@ -121,7 +120,7 @@ use crate::{
DEFAULT_ENDPOINT, DEFAULT_MNEMONIC,
},
signing::Hasher,
substrate::{get_oracle_data, query_chain, submit_transaction},
substrate::{get_oracle_data, get_signers_from_chain, query_chain, submit_transaction},
tests::{
create_clients, initialize_test_logger, jump_start_network_with_signer, remove_program,
run_to_block, setup_client, spawn_testing_validators, store_program_and_register,
Expand Down

0 comments on commit f6dc78a

Please sign in to comment.