Newer
Older
use anyhow::{anyhow, Result};
use sp_core::{crypto::AccountId32, sr25519::Pair, Pair as _};
use std::ops::Deref;
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner, TxStatus};
type SessionKeys = [u8; 128];
pub async fn rotate_keys(client: &Client) -> Result<SessionKeys> {
client
.rpc()
.rotate_keys()
.await?
.deref()
.try_into()
.map_err(|e| anyhow!("Session keys have wrong length: {:?}", e))
pub async fn set_session_keys(
pair: Pair,
client: &Client,
session_keys: SessionKeys,
) -> Result<()> {
client
.tx()
.sign_and_submit_then_watch(
.authority_members()
.set_session_keys(session_keys),
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
Ok(())
pub async fn update_session_keys(pair: Pair, client: &Client) -> Result<()> {
set_session_keys(pair, client, session_keys).await
pub async fn go_online(pair: Pair, client: &Client) -> Result<()> {
.session()
.next_keys(AccountId32::from(pair.public())),
None,
)
.await?
.is_none()
{
return Err(anyhow!("This account has not set session keys!"));
}
client
.tx()
.sign_and_submit_then_watch(
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
Ok(())
pub async fn go_offline(pair: Pair, client: &Client) -> Result<()> {
client
.tx()
.sign_and_submit_then_watch(
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
Ok(())
pub async fn online(data: &Data) -> Result<()> {
let client = data.client();
let indexer = data.indexer.clone();
let mut identity_cache = cache::IdentityCache::new(client.clone(), indexer);
let online_authorities = client
.storage()
.fetch(
Some(parent_hash),
)
.await?
.unwrap_or_default();
println!("Online:");
for identity_id in online_authorities {
println!(
" {}",
identity_cache
.fetch_identity(identity_id, parent_hash)
.await
.unwrap_or_else(|_| format!("{identity_id}"))
);
}
let incoming_authorities = client
.storage()
.fetch(
&runtime::storage()
.authority_members()
.incoming_authorities(),
Some(parent_hash),
)
.await?
.unwrap_or_default();
println!("Incoming:");
for identity_id in incoming_authorities {
println!(
" {}",
identity_cache
.fetch_identity(identity_id, parent_hash)
.await
.unwrap_or_else(|_| format!("{identity_id}"))
);
}
let outgoing_authorities = client
.storage()
.fetch(
&runtime::storage()
.authority_members()
.outgoing_authorities(),
Some(parent_hash),
)
.await?
.unwrap_or_default();
println!("Outgoing:");
for identity_id in outgoing_authorities {
println!(
" {}",
identity_cache
.fetch_identity(identity_id, parent_hash)
.await
.unwrap_or_else(|_| format!("{identity_id}"))
);
}
Ok(())
/// submit a certification and track progress
pub async fn cert(client: &Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> {
let mut progress = client
.tx()
.sign_and_submit_then_watch(
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
let in_block = loop {
if let Some(status) = progress.next_item().await {
match status? {
TxStatus::Ready => {
println!("transaction submitted to the network, waiting 6 seconds...");
}
TxStatus::InBlock(in_block) => break in_block,
TxStatus::Invalid => {
println!("Invalid");
}
_ => continue,
}
}
};
// get the block events and return if ExtrinsicFailed
let events = in_block.wait_for_success().await?;
// look for the expected event
let new_cert_event = events.find_first::<runtime::smith_cert::events::NewCert>()?;
let renew_cert_event = events.find_first::<runtime::smith_cert::events::RenewedCert>()?;