Newer
Older
use std::ops::Deref;
type SessionKeys = [u8; 128];
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// define smith subcommands
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum Subcommand {
/// Emit a smith certification
Cert { to: u32 },
/// go online
GoOnline,
#[default]
/// go offline
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?.build_indexer().await?;
match command {
Subcommand::GoOnline => {
commands::smith::go_online(&data).await?;
}
Subcommand::GoOffline => {
commands::smith::go_offline(&data).await?;
}
Subcommand::Cert { to } => {
data = data.build_keypair().fetch_idty_index().await?;
commands::smith::cert(&data, to).await?
}
Subcommand::UpdateKeys => {
commands::smith::update_session_keys(&data).await?;
}
Subcommand::SudoSetKey { new_key } => {
data = data.build_keypair().build_client().await?;
commands::sudo::set_key(data.keypair(), data.client(), new_key).await?;
}
Subcommand::ShowExpire { blocks, sessions } => {
data = data.build_client().await?;
commands::expire::monitor_expirations(&data, blocks, sessions).await?
}
Subcommand::ShowOnline => {
data = data.build_client().await?;
commands::smith::online(&data).await?
}
};
Ok(())
}
pub async fn rotate_keys(client: &Client) -> Result<SessionKeys, anyhow::Error> {
client
.rpc()
.rotate_keys()
.await?
.deref()
.try_into()
.map_err(|e| anyhow!("Session keys have wrong length: {:?}", e))
.authority_members()
.set_session_keys(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?;
let _ = track_progress(progress).await?; // TODO
Ok(())
pub async fn go_online(data: &Data) -> Result<(), GcliError> {
if data
.client()
&runtime::storage().session().next_keys(data.address()),
return Err(GcliError::Logic(
"This account has not set session keys!".to_string(),
));
.await?;
let events = track_progress(progress).await?;
if let Some(e) = events.find_first::<runtime::authority_members::events::MemberGoOnline>()? {
println!("{e:?}");
}
Ok(())
pub async fn go_offline(data: &Data) -> Result<(), subxt::Error> {
let progress = data
.client()
.await?;
let events = track_progress(progress).await?;
if let Some(e) = events.find_first::<runtime::authority_members::events::MemberGoOffline>()? {
println!("{e:?}");
}
Ok(())
pub async fn online(data: &Data) -> Result<(), anyhow::Error> {
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(data: &Data, receiver: u32) -> Result<(), anyhow::Error> {
let progress = data
.client()
&runtime::tx().smith_cert().add_cert(data.idty_index(), receiver),
&PairSigner::new(data.keypair()),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
let events = track_progress(progress).await?;
let new_cert_event = events.find_first::<runtime::smith_cert::events::NewCert>()?;
let renew_cert_event = events.find_first::<runtime::smith_cert::events::RenewedCert>()?;