Skip to content

Commit

Permalink
Move get_registered_details fully into client
Browse files Browse the repository at this point in the history
  • Loading branch information
HCastano committed Aug 20, 2024
1 parent 75833d6 commit 837467c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 86 deletions.
40 changes: 2 additions & 38 deletions crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
EntropyConfig,
},
client::entropy::staking_extension::events::{EndpointChanged, ThresholdAccountChanged},
substrate::{query_chain, submit_transaction_with_pair},
substrate::{get_registered_details, query_chain, submit_transaction_with_pair},
user::{get_signers_from_chain, UserSignatureRequest},
Hasher,
};
Expand Down Expand Up @@ -359,43 +359,6 @@ pub async fn put_old_register_request_on_chain(
Err(ClientError::RegistrationTimeout)
}

/// Returns a registered user's key visibility
///
/// TODO (Nando): This was copied from `entropy-tss::helpers::substrate`
///
/// What's the best place for this? Ideally here, and then we import this into the entropy-tss, but
/// we need the `full-client` feature enabled...
#[tracing::instrument(skip_all, fields(verifying_key))]
pub async fn get_registered_details(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
verifying_key: Vec<u8>,
) -> Result<RegisteredInfo, ClientError> {
tracing::info!("Querying chain for registration info.");

let registered_info_query =
entropy::storage().registry().registered(BoundedVec(verifying_key.clone()));
let registered_result = query_chain(api, rpc, registered_info_query, None).await?;

let registration_info = if let Some(old_registration_info) = registered_result {
tracing::debug!("Found user in old `Registered` struct.");

old_registration_info
} else {
// We failed with the old registration path, let's try the new one
tracing::warn!("Didn't find user in old `Registered` struct, trying new one.");

let registered_info_query =
entropy::storage().registry().registered_on_chain(BoundedVec(verifying_key));

query_chain(api, rpc, registered_info_query, None)
.await?
.ok_or_else(|| ClientError::NotRegistered)?
};

Ok(registration_info)
}

/// Check that the verfiying key from a new signature matches that in the from the
/// on-chain registration info for a given account
pub async fn check_verifying_key(
Expand All @@ -410,6 +373,7 @@ pub async fn check_verifying_key(
entropy::storage().registry().registered(BoundedVec(verifying_key_serialized));
let query_registered_status = query_chain(api, rpc, registered_query, None).await;
query_registered_status?.ok_or(ClientError::NotRegistered)?;

Ok(())
}

Expand Down
4 changes: 3 additions & 1 deletion crates/client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum SubstrateError {
GenericSubstrate(#[from] subxt::error::Error),
#[error("Could not sumbit transaction {0}")]
BadEvent(String),
#[error("User is not registered on-chain")]
NotRegistered,
}

/// An error on getting the current subgroup signers
Expand Down Expand Up @@ -92,7 +94,7 @@ pub enum ClientError {
BadRecoveryId,
#[error("Cannot parse chain query response: {0}")]
TryFromSlice(#[from] std::array::TryFromSliceError),
#[error("User not registered")]
#[error("User is not registered on-chain")]
NotRegistered,
#[error("No synced validators")]
NoSyncedValidators,
Expand Down
35 changes: 35 additions & 0 deletions crates/client/src/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! For interacting with the substrate chain node
use crate::chain_api::entropy::runtime_types::bounded_collections::bounded_vec::BoundedVec;
use crate::chain_api::entropy::runtime_types::pallet_registry::pallet::RegisteredInfo;
use crate::chain_api::{entropy, EntropyConfig};

use entropy_shared::MORTALITY_BLOCKS;
use sp_core::{sr25519, Pair};
use subxt::{
Expand Down Expand Up @@ -107,6 +110,38 @@ where
Ok(result)
}

/// Returns a registered user's key visibility
#[tracing::instrument(skip_all, fields(verifying_key))]
pub async fn get_registered_details(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
verifying_key: Vec<u8>,
) -> Result<RegisteredInfo, SubstrateError> {
tracing::info!("Querying chain for registration info.");

let registered_info_query =
entropy::storage().registry().registered(BoundedVec(verifying_key.clone()));
let registered_result = query_chain(api, rpc, registered_info_query, None).await?;

let registration_info = if let Some(old_registration_info) = registered_result {
tracing::debug!("Found user in old `Registered` struct.");

old_registration_info
} else {
// We failed with the old registration path, let's try the new one
tracing::warn!("Didn't find user in old `Registered` struct, trying new one.");

let registered_info_query =
entropy::storage().registry().registered_on_chain(BoundedVec(verifying_key));

query_chain(api, rpc, registered_info_query, None)
.await?
.ok_or_else(|| SubstrateError::NotRegistered)?
};

Ok(registration_info)
}

/// A wrapper around [sr25519::Pair] which implements [Signer]
/// This is needed because on wasm we cannot use the generic `subxt::tx::PairSigner`
#[derive(Clone)]
Expand Down
33 changes: 0 additions & 33 deletions crates/threshold-signature-server/src/helpers/substrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::{
self,
runtime_types::{
bounded_collections::bounded_vec::BoundedVec, pallet_programs::pallet::ProgramInfo,
pallet_registry::pallet::RegisteredInfo,
},
},
EntropyConfig,
Expand Down Expand Up @@ -72,38 +71,6 @@ pub async fn get_oracle_data(
Ok(oracle_info.0)
}

/// Returns a registered user's key visibility
#[tracing::instrument(skip_all, fields(verifying_key))]
pub async fn get_registered_details(
api: &OnlineClient<EntropyConfig>,
rpc: &LegacyRpcMethods<EntropyConfig>,
verifying_key: Vec<u8>,
) -> Result<RegisteredInfo, UserErr> {
tracing::info!("Querying chain for registration info.");

let registered_info_query =
entropy::storage().registry().registered(BoundedVec(verifying_key.clone()));
let registered_result = query_chain(api, rpc, registered_info_query, None).await?;

let registration_info = if let Some(old_registration_info) = registered_result {
tracing::debug!("Found user in old `Registered` struct.");

old_registration_info
} else {
// We failed with the old registration path, let's try the new one
tracing::warn!("Didn't find user in old `Registered` struct, trying new one.");

let registered_info_query =
entropy::storage().registry().registered_on_chain(BoundedVec(verifying_key));

query_chain(api, rpc, registered_info_query, None)
.await?
.ok_or_else(|| UserErr::ChainFetch("Not Registering error: Register Onchain first"))?
};

Ok(registration_info)
}

/// Takes Stash keys and returns validator info from chain
pub async fn get_validators_info(
api: &OnlineClient<EntropyConfig>,
Expand Down
4 changes: 2 additions & 2 deletions crates/threshold-signature-server/src/user/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use axum::{
use base64::prelude::{Engine, BASE64_STANDARD};
use bip39::{Language, Mnemonic};
use blake2::{Blake2s256, Digest};
use entropy_client::substrate::get_registered_details;
use entropy_kvdb::kv_manager::{
error::{InnerKvError, KvError},
helpers::serialize as key_serialize,
Expand Down Expand Up @@ -68,8 +69,7 @@ use crate::{
launch::LATEST_BLOCK_NUMBER_NEW_USER,
signing::{do_signing, Hasher},
substrate::{
get_oracle_data, get_program, get_registered_details, get_stash_address, query_chain,
submit_transaction,
get_oracle_data, get_program, get_stash_address, query_chain, submit_transaction,
},
user::{check_in_registration_group, compute_hash, do_dkg},
validator::{get_signer, get_signer_and_x25519_secret},
Expand Down
17 changes: 5 additions & 12 deletions crates/threshold-signature-server/src/user/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use axum::http::StatusCode;
use base64::prelude::{Engine, BASE64_STANDARD};
use bip39::{Language, Mnemonic};
use blake3::hash;
use entropy_client::substrate::get_registered_details;
use entropy_client::{
client::{sign, store_program, update_programs},
user::get_signers_from_chain,
Expand Down Expand Up @@ -430,12 +431,8 @@ async fn signature_request_with_derived_account_works() {
let actual_verifying_key = actual_verifying_key.0;

// Next we want to check that the info that's on-chain is what we actually expect
let registered_info = crate::helpers::substrate::get_registered_details(
&entropy_api,
&rpc,
actual_verifying_key.to_vec(),
)
.await;
let registered_info =
get_registered_details(&entropy_api, &rpc, actual_verifying_key.to_vec()).await;

assert!(
matches!(registered_info, Ok(_)),
Expand Down Expand Up @@ -1526,12 +1523,8 @@ async fn test_new_registration_flow() {
let actual_verifying_key = actual_verifying_key.0;

// Next we want to check that the info that's on-chain is what we actually expect
let registered_info = crate::helpers::substrate::get_registered_details(
&entropy_api,
&rpc,
actual_verifying_key.to_vec(),
)
.await;
let registered_info =
get_registered_details(&entropy_api, &rpc, actual_verifying_key.to_vec()).await;

assert!(
matches!(registered_info, Ok(_)),
Expand Down

0 comments on commit 837467c

Please sign in to comment.