Newer
Older
use crate::{cache, gdev, indexer::*, Args, Client};
2
3
4
5
6
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
70
71
72
73
74
75
76
77
78
use anyhow::{anyhow, Result};
use sp_core::{crypto::AccountId32, sr25519::Pair, Pair as _};
use std::ops::Deref;
use subxt::tx::{BaseExtrinsicParamsBuilder, PairSigner};
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(
&gdev::tx()
.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<()> {
let session_keys = rotate_keys(&client).await?;
set_session_keys(pair, client, session_keys).await
}
pub async fn go_online(pair: Pair, client: Client) -> Result<()> {
if client
.storage()
.fetch(
&gdev::storage()
.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(
&gdev::tx().authority_members().go_online(),
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
Ok(())
}
pub async fn go_offline(pair: Pair, client: Client) -> Result<()> {
client
.tx()
.sign_and_submit_then_watch(
&gdev::tx().authority_members().go_offline(),
&PairSigner::new(pair),
BaseExtrinsicParamsBuilder::new(),
)
.await?;
Ok(())
}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
pub async fn online(client: Client, args: &Args) -> Result<()> {
let parent_hash = client
.storage()
.fetch(&gdev::storage().system().parent_hash(), None)
.await?
.unwrap();
let gql_client = reqwest::Client::builder()
.user_agent("gcli/0.1.0")
.build()?;
let mut identity_cache = cache::IdentityCache::new(
&client,
if args.no_indexer {
None
} else {
Some(Indexer {
gql_client,
gql_url: &args.indexer,
})
},
);
let online_authorities = client
.storage()
.fetch(
&gdev::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(
&gdev::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(
&gdev::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(())
}