Skip to content

Commit

Permalink
add cli command to purge community-ceremony (#192)
Browse files Browse the repository at this point in the history
* add cli command to purge community-ceremony

* review fixes
  • Loading branch information
brenzi authored Mar 19, 2022
1 parent af09619 commit 0fcae7e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "encointer-client-notee"
authors = ["encointer.org <alain@encointer.org>"]
edition = "2021"
#keep with node version
version = "0.8.3"
version = "0.8.4"

[dependencies]
clap = "2.33"
Expand Down
34 changes: 32 additions & 2 deletions client/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const SIGNER_ARG: &'static str = "signer";
const CID_ARG: &'static str = "cid";
const CLAIMS_ARG: &'static str = "claims";
const CEREMONY_INDEX_ARG: &'static str = "ceremony-index";
const IPFS_CID_ARG: &'static str = "ceremony-index";
const IPFS_CID_ARG: &'static str = "ipfs-cid";
const BOOTSTRAPPER_ARG: &'static str = "bootstrapper";
const FUNDEES_ARG: &'static str = "fundees";
const FROM_CINDEX_ARG: &'static str = "from-cindex";
const TO_CINDEX_ARG: &'static str = "to-cindex";
const ENDORSEES_ARG: &'static str = "endorsees";
const ALL_FLAG: &'static str = "all";

Expand All @@ -20,6 +22,8 @@ pub trait EncointerArgs<'b> {
fn ipfs_cid_arg(self) -> Self;
fn bootstrapper_arg(self) -> Self;
fn fundees_arg(self) -> Self;
fn from_cindex_arg(self) -> Self;
fn to_cindex_arg(self) -> Self;
fn endorsees_arg(self) -> Self;
fn all_flag(self) -> Self;
}
Expand All @@ -33,6 +37,8 @@ pub trait EncointerArgsExtractor {
fn ipfs_cid_arg(&self) -> Option<&str>;
fn bootstrapper_arg(&self) -> Option<&str>;
fn fundees_arg(&self) -> Option<Vec<&str>>;
fn from_cindex_arg(&self) -> Option<i32>;
fn to_cindex_arg(&self) -> Option<i32>;
fn endorsees_arg(&self) -> Option<Vec<&str>>;
fn all_flag(&self) -> bool;
}
Expand Down Expand Up @@ -123,7 +129,24 @@ impl<'a, 'b> EncointerArgs<'b> for App<'a, 'b> {
.help("Account(s) to be funded, ss58check encoded"),
)
}

fn from_cindex_arg(self) -> Self {
self.arg(
Arg::with_name(FROM_CINDEX_ARG)
.takes_value(true)
.required(true)
.value_name("FROM")
.help("first ceremony index to be purged"),
)
}
fn to_cindex_arg(self) -> Self {
self.arg(
Arg::with_name(TO_CINDEX_ARG)
.takes_value(true)
.required(true)
.value_name("TO")
.help("last ceremony index to be purged"),
)
}
fn endorsees_arg(self) -> Self {
self.arg(
Arg::with_name(ENDORSEES_ARG)
Expand Down Expand Up @@ -183,6 +206,13 @@ impl<'a> EncointerArgsExtractor for ArgMatches<'a> {
self.values_of(FUNDEES_ARG).map(|v| v.collect())
}

fn from_cindex_arg(&self) -> Option<i32> {
self.value_of(FROM_CINDEX_ARG).map(|v| v.parse().unwrap())
}
fn to_cindex_arg(&self) -> Option<i32> {
self.value_of(TO_CINDEX_ARG).map(|v| v.parse().unwrap())
}

fn endorsees_arg(&self) -> Option<Vec<&str>> {
self.values_of(ENDORSEES_ARG).map(|v| v.collect())
}
Expand Down
65 changes: 65 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,71 @@ fn main() {
Ok(())
}),
)
.add_cmd(
Command::new("purge-community-ceremony")
.description("purge all history within the provided ceremony index range for the specified community")
.options(|app| {
app.setting(AppSettings::ColoredHelp)
.from_cindex_arg()
.to_cindex_arg()

})
.runner(|_args: &str, matches: &ArgMatches<'_>| {
let sudoer = AccountKeyring::Alice.pair();
let api = get_chain_api(matches).set_signer(sudoer);

let current_ceremony_index = get_ceremony_index(&api);

let from_cindex_arg = matches.from_cindex_arg().unwrap_or(0);
let to_cindex_arg = matches.to_cindex_arg().unwrap_or(0);

let from_cindex = into_effective_cindex(from_cindex_arg, current_ceremony_index);
let to_cindex = into_effective_cindex(to_cindex_arg, current_ceremony_index);

if from_cindex > to_cindex {
panic!("'from' <= 'to' ceremony index violated");
}
let cid = verify_cid(&api,
matches
.cid_arg()
.expect("please supply argument --cid"),
);
println!("purging ceremony index range [{} {}] for community {}", from_cindex, to_cindex, cid);

let calls: Vec<_> = (from_cindex..=to_cindex)
.map(|idx| compose_call!(
api.metadata,
"EncointerCeremonies",
"purge_community_ceremony",
(cid, idx)
))
.collect();
let batch_call = compose_call!(
api.metadata,
"Utility",
"batch",
calls
);
let unsigned_sudo_call = compose_call!(
api.metadata,
"Sudo",
"sudo",
batch_call.clone()
);
info!("raw sudo batch call to sign with js/apps {}: 0x{}", cid, hex::encode(unsigned_sudo_call.encode()));
let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
api,
"Sudo",
"sudo",
batch_call
);
ensure_payment(&api, &xt.hex_encode());
let tx_hash = api.send_extrinsic(xt.hex_encode(), XtStatus::InBlock).unwrap();
info!("[+] Transaction got included. Hash: {:?}\n", tx_hash);
Ok(())
}),
)

// To handle when no subcommands match
.no_cmd(|_args, _matches| {
println!("No subcommand matched");
Expand Down
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "GPL-3.0"
name = "encointer-node-notee"
repository = "https://github.com/encointer/encointer-node"
#keep with client version
version = "0.8.3"
version = "0.8.4"

[[bin]]
name = "encointer-node-notee"
Expand Down

0 comments on commit 0fcae7e

Please sign in to comment.