Select Git revision
-
Hugo Trentesaux authoredHugo Trentesaux authored
smith.rs 8.05 KiB
use crate::*;
use std::ops::Deref;
type SessionKeys = [u8; 128];
#[cfg(feature = "gdev")]
type SmithMembershipMetaData =
runtime::runtime_types::common_runtime::entities::SmithMembershipMetaData<SessionKeys>;
/// define smith subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
/// Request smith membership
Request { endpoint: String },
/// Emit a smith certification
Cert { to: u32 },
/// Claim smith membership
Claim,
/// go online
GoOnline,
/// go offline
#[default]
GoOffline,
/// Rotate and set session keys
UpdateKeys,
/// set sudo keys
SudoSetKey { new_key: AccountId },
/// List upcoming expirations that require an action
ShowExpire {
/// Show certs that expire within less than this number of blocks
#[clap(short, long, default_value_t = 100800)]
blocks: u32,
/// Show authorities that should rotate keys within less than this number of sessions
#[clap(short, long, default_value_t = 100)]
sessions: u32,
},
/// List online authorities
ShowOnline,
}
/// handle smith commands
pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<()> {
let mut data = data.build_client().await?;
match command {
Subcommand::Request { endpoint } => {
request_smith_membership(&data, endpoint).await?;
}
Subcommand::Claim => {
claim_smith_membership(&data).await?;
}
Subcommand::GoOnline => {
go_online(&data).await?;
}
Subcommand::GoOffline => {
go_offline(&data).await?;
}
Subcommand::Cert { to } => {
data = data.fetch_idty_index().await?;
cert(&data, to).await?
}
Subcommand::UpdateKeys => {
update_session_keys(&data).await?;
}
Subcommand::SudoSetKey { new_key } => {
data = data.build_client().await?;
commands::sudo::set_key(&data, new_key).await?;
}
Subcommand::ShowExpire { blocks, sessions } => {
data = data.build_client().await?.build_indexer().await?;
commands::expire::monitor_expirations(&data, blocks, sessions).await?
}
Subcommand::ShowOnline => {
data = data.build_client().await?;
online(&data).await?
}
};
Ok(())
}
/// rotate session keys
/// (needs to be connected to unsafe RPC)
pub async fn rotate_keys(client: &Client) -> Result<SessionKeys, anyhow::Error> {
client
.rpc()
.rotate_keys()
.await.map_err(|e| anyhow!("Please make sure you are connected to your validator node with the unsafe RPC API enabled {e}"))?
.deref()
.try_into()
.map_err(|e| anyhow!("Session keys have wrong length: {:?}", e))
}
/// request smith membership
pub async fn request_smith_membership(data: &Data, endpoint: String) -> Result<(), anyhow::Error> {
let session_keys = rotate_keys(data.client()).await?;
let metadata = SmithMembershipMetaData {
session_keys,
owner_key: data.address(),
p2p_endpoint: endpoint,
};
let progress = data
.client()
.tx()
.sign_and_submit_then_watch(
&runtime::tx()
.smith_membership()
.request_membership(metadata),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).await?;
let request = events.find_first::<runtime::smith_membership::events::MembershipRequested>()?;
if let Some(event) = request {
println!("{event:?}");
}
Ok(())
}
/// set session keys
pub async fn set_session_keys(
data: &Data,
session_keys: SessionKeys,
) -> Result<TxProgress, subxt::Error> {
data.client()
.tx()
.sign_and_submit_then_watch(
&runtime::tx()
.authority_members()
.set_session_keys(session_keys),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await
}
/// update session keys
pub async fn update_session_keys(data: &Data) -> Result<(), GcliError> {
let session_keys = rotate_keys(data.client()).await?;
let progress = set_session_keys(data, session_keys).await?;
if data.args.no_wait {
return Ok(());
}
let _ = track_progress(progress).await?; // TODO
Ok(())
}
/// submit go_online
pub async fn go_online(data: &Data) -> Result<(), GcliError> {
if data
.client()
.storage()
.fetch(
&runtime::storage().session().next_keys(data.address()),
None,
)
.await?
.is_none()
{
return Err(GcliError::Logic(
"This account has not set session keys!".to_string(),
));
}
let progress = data
.client()
.tx()
.sign_and_submit_then_watch(
&runtime::tx().authority_members().go_online(),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).await?;
if let Some(e) = events.find_first::<runtime::authority_members::events::MemberGoOnline>()? {
println!("{e:?}");
}
Ok(())
}
/// submit go_offline
pub async fn claim_smith_membership(data: &Data) -> Result<(), subxt::Error> {
let progress = data
.client()
.tx()
.sign_and_submit_then_watch(
&runtime::tx().smith_membership().claim_membership(),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).await?;
if let Some(e) = events.find_first::<runtime::smith_membership::events::MembershipAcquired>()? {
println!("{e:?}");
}
Ok(())
}
/// submit go_offline
pub async fn go_offline(data: &Data) -> Result<(), subxt::Error> {
let progress = data
.client()
.tx()
.sign_and_submit_then_watch(
&runtime::tx().authority_members().go_offline(),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).await?;
if let Some(e) = events.find_first::<runtime::authority_members::events::MemberGoOffline>()? {
println!("{e:?}");
}
Ok(())
}
/// get online authorities
pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
let client = data.client();
let indexer = data.indexer.clone();
let parent_hash = client
.clone()
.storage()
.fetch(&runtime::storage().system().parent_hash(), None)
.await?
.unwrap();
let mut identity_cache = cache::IdentityCache::new(client.clone(), indexer);
let online_authorities = client
.storage()
.fetch(
&runtime::storage().authority_members().online_authorities(),
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(data: &Data, receiver: u32) -> Result<(), anyhow::Error> {
let progress = data
.client()
.tx()
.sign_and_submit_then_watch(
&runtime::tx()
.smith_cert()
.add_cert(data.idty_index(), receiver),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
if data.args.no_wait {
return Ok(());
}
let events = track_progress(progress).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>()?;
if let Some(event) = new_cert_event {
println!("{event:?}");
}
if let Some(event) = renew_cert_event {
println!("{event:?}");
}
Ok(())
}