#![allow(clippy::type_complexity)]
use crate::runtime;
use log::debug;
use sp_core::H256;
pub type Client = subxt::OnlineClient<crate::RuntimeConfig>;
pub type AccountId = subxt::utils::AccountId32;
pub type IdtyIndex = u32;
pub async fn client(rpc_url: String) -> Client {
Client::from_insecure_url(rpc_url)
.await
.expect("Cannot create RPC client")
}
pub async fn parent_hash(client: &Client) -> H256 {
client
.blocks()
.at_latest()
.await
.expect("Cannot fetch latest block hash")
.hash()
}
pub async fn current_pool_index(client: &Client, parent_hash: H256) -> u32 {
client
.storage()
.at(parent_hash)
.fetch(&runtime::storage().distance().current_pool_index())
.await
.expect("Cannot fetch current pool index")
.unwrap_or_default()
}
pub async fn current_pool(
client: &Client,
parent_hash: H256,
current_pool_index: u32,
) -> Option<runtime::runtime_types::pallet_distance::types::EvaluationPool<AccountId, IdtyIndex>> {
client
.storage()
.at(parent_hash)
.fetch(&match current_pool_index {
0 => {
debug!("Looking at Pool1 for pool index {}", current_pool_index);
runtime::storage().distance().evaluation_pool1()
}
1 => {
debug!("Looking at Pool2 for pool index {}", current_pool_index);
runtime::storage().distance().evaluation_pool2()
}
2 => {
debug!("Looking at Pool0 for pool index {}", current_pool_index);
runtime::storage().distance().evaluation_pool0()
}
_ => unreachable!("n<3"),
})
.await
.expect("Cannot fetch current pool")
}
pub async fn evaluation_block(client: &Client, parent_hash: H256) -> H256 {
client
.storage()
.at(parent_hash)
.fetch(&runtime::storage().distance().evaluation_block())
.await
.expect("Cannot fetch evaluation block")
.expect("No evaluation block")
}
pub async fn max_referee_distance(client: &Client) -> u32 {
client
.constants()
.at(&runtime::constants().distance().max_referee_distance())
.expect("Cannot fetch referee distance")
}
pub async fn member_iter(client: &Client, evaluation_block: H256) -> MemberIter {
MemberIter(
client
.storage()
.at(evaluation_block)
.iter(runtime::storage().membership().membership_iter())
.await
.expect("Cannot fetch memberships"),
)
}
pub struct MemberIter(
subxt::backend::StreamOfResults<
subxt::storage::StorageKeyValuePair<
subxt::storage::Address<
(),
runtime::runtime_types::sp_membership::MembershipData<u32>,
(),
(),
subxt::storage::address::Yes,
>,
>,
>,
);
impl MemberIter {
pub async fn next(&mut self) -> Result<Option<IdtyIndex>, subxt::error::Error> {
self.0
.next()
.await
.transpose()
.map(|i| i.map(|j| idty_id_from_storage_key(&j.key_bytes)))
}
}
pub async fn cert_iter(client: &Client, evaluation_block: H256) -> CertIter {
CertIter(
client
.storage()
.at(evaluation_block)
.iter(runtime::storage().certification().certs_by_receiver_iter())
.await
.expect("Cannot fetch certifications"),
)
}
pub struct CertIter(
subxt::backend::StreamOfResults<
subxt::storage::StorageKeyValuePair<
subxt::storage::Address<
(),
Vec<(u32, u32)>,
(),
subxt::storage::address::Yes,
subxt::storage::address::Yes,
>,
>,
>,
);
impl CertIter {
pub async fn next(
&mut self,
) -> Result<Option<(IdtyIndex, Vec<(IdtyIndex, u32)>)>, subxt::error::Error> {
self.0
.next()
.await
.transpose()
.map(|i| i.map(|j| (idty_id_from_storage_key(&j.key_bytes), j.value)))
}
}
fn idty_id_from_storage_key(storage_key: &[u8]) -> IdtyIndex {
u32::from_le_bytes(
storage_key[40..44]
.try_into()
.expect("Cannot convert StorageKey to IdtyIndex"),
)
}