Skip to content
Snippets Groups Projects
Commit 8eda68de authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

wip refac smith cert

parent 79bf0446
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !5. Comments created here will be created in the context of that merge request.
use crate::*;
use crate::indexer::*;
use crate::*;
use anyhow::{anyhow, Result};
use std::collections::{hash_map, HashMap};
......
use crate::*;
use crate::indexer::*;
use crate::*;
use anyhow::Result;
use sp_core::{sr25519::Pair, H256};
......
use crate::*;
use crate::indexer::*;
use crate::*;
use anyhow::Result;
use futures::join;
......@@ -102,7 +102,9 @@ pub async fn monitor_expirations(
let mut smith_certs_iter = client
.storage()
.iter(
runtime::storage().smith_cert().storage_certs_removable_on(0),
runtime::storage()
.smith_cert()
.storage_certs_removable_on(0),
10,
Some(parent_hash),
)
......@@ -163,7 +165,9 @@ pub async fn monitor_expirations(
let mut smith_membership_iter = client
.storage()
.iter(
runtime::storage().smith_membership().memberships_expire_on(0),
runtime::storage()
.smith_membership()
.memberships_expire_on(0),
10,
Some(parent_hash),
)
......
use crate::*;
use crate::indexer::*;
use crate::*;
use crate::runtime::runtime_types::common_runtime::entities::IdtyData;
use crate::runtime::runtime_types::pallet_identity::types::*;
......@@ -101,7 +101,11 @@ pub async fn get_identity_by_index(
.await?)
}
pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) -> Result<TxProgress, subxt::Error> {
pub async fn create_identity(
pair: Pair,
client: Client,
target: AccountId32,
) -> Result<TxProgress, subxt::Error> {
Ok(client
.tx()
.sign_and_submit_then_watch(
......@@ -112,7 +116,11 @@ pub async fn create_identity(pair: Pair, client: Client, target: AccountId32) ->
.await?)
}
pub async fn confirm_identity(pair: Pair, client: Client, name: String) -> Result<TxProgress, subxt::Error> {
pub async fn confirm_identity(
pair: Pair,
client: Client,
name: String,
) -> Result<TxProgress, subxt::Error> {
Ok(client
.tx()
.sign_and_submit_then_watch(
......
......@@ -50,8 +50,7 @@ pub async fn repart(
pub async fn spam_roll(pair: Pair, client: Client, actual_repart: usize) -> Result<()> {
let mut nonce = 0;
let mut pairs =
Vec::<(PairSigner<Runtime, Pair>, AccountId32)>::with_capacity(actual_repart);
let mut pairs = Vec::<(PairSigner<Runtime, Pair>, AccountId32)>::with_capacity(actual_repart);
for i in 0..actual_repart {
let pair_i = pair
.derive(std::iter::once(DeriveJunction::hard::<u32>(i as u32)), None)
......
......@@ -45,7 +45,9 @@ pub async fn consume_oneshot_account(
dest.into(),
)
} else {
runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(dest.into())
runtime::runtime_types::pallet_oneshot_account::types::Account::Normal(
dest.into(),
)
},
),
&PairSigner::new(pair),
......@@ -111,7 +113,9 @@ pub async fn oneshot_account_balance(client: Client, account: AccountId32) -> Re
client
.storage()
.fetch(
&runtime::storage().oneshot_account().oneshot_accounts(&account),
&runtime::storage()
.oneshot_account()
.oneshot_accounts(&account),
None
)
.await?
......
......@@ -124,7 +124,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let incoming_authorities = client
.storage()
.fetch(
&runtime::storage().authority_members().incoming_authorities(),
&runtime::storage()
.authority_members()
.incoming_authorities(),
Some(parent_hash),
)
.await?
......@@ -144,7 +146,9 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
let outgoing_authorities = client
.storage()
.fetch(
&runtime::storage().authority_members().outgoing_authorities(),
&runtime::storage()
.authority_members()
.outgoing_authorities(),
Some(parent_hash),
)
.await?
......@@ -164,37 +168,8 @@ pub async fn online(client: Client, args: &Args) -> Result<()> {
Ok(())
}
/// emit a new smith cert from signer's identity to target identity
pub async fn emit_cert(args: Args, receiver: u32) -> Result<()> {
// issuer key
let pair = get_keys(
args.secret_format,
&args.address,
&args.secret,
NeededKeys::Secret,
)?
.1
.unwrap();
// connect to client
let client = Client::from_url(&args.url).await.unwrap();
// get issuer index
let issuer = commands::identity::get_idty_index_by_account_id(
client.clone(),
&AccountId32::from(pair.public()),
)
.await?
.ok_or(anyhow!("can not certify if not member"))?;
// submit and track certification
cert(client, pair, issuer, receiver).await?;
Ok(())
}
/// submit a certification and track progress
async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> {
pub async fn cert(client: Client, pair: Pair, issuer: u32, receiver: u32) -> Result<()> {
let mut progress = client
.tx()
.sign_and_submit_then_watch(
......
......@@ -14,11 +14,12 @@ pub enum NeededKeys {
Secret,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
pub enum SecretFormat {
/// Raw 32B seed
Seed,
/// Substrate secret key or BIP39 mnemonic (optionally followed by derivation path)
#[default]
Substrate,
}
......
......@@ -52,7 +52,7 @@ impl From<u64> for Tip {
}
}
#[derive(Clone, Parser, Debug)]
#[derive(Clone, Parser, Debug, Default)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
#[clap(subcommand)]
......@@ -81,11 +81,13 @@ pub struct Args {
/// Data of current command
/// can also include fetched information
#[derive(Default)]
pub struct Data {
pub args: Args,
pub client: Option<Client>,
pub address: Option<AccountId>,
pub keypair: Option<Pair>,
pub idty_index: Option<u32>,
}
// implement helper functions for Data
......@@ -94,12 +96,11 @@ impl Data {
pub fn new(args: Args) -> Self {
Self {
args,
client: None,
address: None,
keypair: None,
..Default::default()
}
}
// --- getters ---
// the "unwrap" should not fail if data is well prepared
pub fn client(&self) -> Client {
self.client.clone().unwrap()
}
......@@ -109,7 +110,11 @@ impl Data {
pub fn keypair(&self) -> Pair {
self.keypair.clone().unwrap()
}
pub fn idty_index(&self) -> u32 {
self.idty_index.clone().unwrap()
}
// --- mutators ---
/// force an address if needed
pub fn build_address(mut self) -> Self {
self.address = Some(
get_keys(
......@@ -124,24 +129,38 @@ impl Data {
);
self
}
/// force a keypair if needed
pub fn build_keypair(mut self) -> Self {
self.keypair = Some(
get_keys(
let (address, keypair) = get_keys(
self.args.secret_format,
&self.args.address,
&self.args.secret,
NeededKeys::Secret,
)
.expect("needed")
.1
.expect("needed"),
);
.expect("needed");
self.address = address;
self.keypair = keypair;
self
}
/// build a client from url
// TODO get client from a pre-defined list
pub async fn build_client(mut self) -> Self {
self.client = Some(Client::from_url(&self.args.url).await.expect("needed"));
self
}
/// get issuer index
/// needs address and client first
pub async fn fetch_idty_index(mut self) -> Result<Self, anyhow::Error> {
self.idty_index = Some(
commands::identity::get_idty_index_by_account_id(
self.client().clone(),
&self.address(),
)
.await?
.ok_or(anyhow::anyhow!("needs to be member to use this command"))?,
);
Ok(self)
}
}
/// track progress of transaction on the network
......@@ -181,8 +200,11 @@ impl From<anyhow::Error> for GcliError {
}
}
#[derive(Clone, Debug, clap::Subcommand)]
#[derive(Clone, Debug, clap::Subcommand, Default)]
pub enum Subcommand {
// TODO flodef
#[default]
GetBalance,
/// Create and certify an identity
///
/// Caller must be member, and the target account must exist.
......@@ -301,11 +323,11 @@ async fn main() -> Result<(), GcliError> {
let mut data = Data::new(args.clone());
match args.subcommand {
Subcommand::GetBalance => {}
Subcommand::CreateIdentity { target } => {
data = data.build_client().await.build_keypair();
let progress =
commands::identity::create_identity(data.keypair(), data.client(), target)
.await?;
commands::identity::create_identity(data.keypair(), data.client(), target).await?;
track_progress(progress).await?
}
Subcommand::ConfirmIdentity { name } => {
......@@ -495,7 +517,15 @@ async fn main() -> Result<(), GcliError> {
)
.await?
}
Subcommand::SmithCert { to } => commands::smith::emit_cert(args, to).await?,
Subcommand::SmithCert { to } => {
data = data
.build_client()
.await
.build_keypair()
.fetch_idty_index()
.await?;
commands::smith::cert(data.client(), data.keypair(), data.idty_index(), to).await?
}
Subcommand::TechMembers => {
commands::collective::technical_committee_members(
Client::from_url(&args.url).await.unwrap(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment