Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Minor tweaks to the README #44

Merged
merged 8 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
license = "GPLv3+"
default-run = "ledgeracio"

[dependencies]
substrate-subxt = { git = "https://github.com/paritytech/substrate-subxt.git", branch = "no-sync-required" }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ given index.
#### Announcing an intention to validate: `ledgeracio validator announce <index> [commission]`

This command announces that the controller account at `<index>` intends to
validate. An optional commission (in parts per billion) may also be provided. If none is supplied, it
defaults to 10⁹ parts per billion, or 100%.
validate. An optional commission (as a decimal between 0 and 1 inclusive) may also be provided. If none is supplied, it
DemiMarie marked this conversation as resolved.
Show resolved Hide resolved
defaults to 1, or 100%.

#### Cease validation: `ledgeracio validator chill`

Expand Down
39 changes: 39 additions & 0 deletions src/bin/ledgeracio/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,36 @@ pub fn pad(mut zeros: u8, value: u128) -> String {
value
}

pub fn parse_ppb(data: &str) -> Result<u32, Error> {
if data == "1" {
Ok(1_000_000_000)
} else if data == "0" {
Ok(0)
} else if !data.starts_with("0.") {
Err(
"Commission must start with 0. or be equal to 1 (the default) or 0"
.to_owned()
.into(),
)
} else if data.len() > 11 {
Err("Commission too long. Check for excess trailing zeroes."
.to_owned()
.into())
} else {
let mut len = data.len() - 2;
let mut res: u32 = str::parse(&data[2..])?;
while len < 9 {
res *= 10;
len += 1;
}
Ok(res)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn padding_works() {
assert_eq!(pad(0, 100), "100".to_owned());
Expand All @@ -154,4 +181,16 @@ mod tests {
assert_eq!(pad(3, 10001), "10.001".to_owned());
assert_eq!(pad(3, 10010), "10.01".to_owned());
}

#[test]
fn parse_ppb_works() {
assert_eq!(parse_ppb("1").unwrap(), 1_000_000_000);
assert_eq!(parse_ppb("0").unwrap(), 0);
assert!(parse_ppb("0.").is_err());
assert_eq!(parse_ppb("0.01").unwrap(), 10_000_000);
assert!(parse_ppb("0.0100000000").is_err());
assert_eq!(parse_ppb("0.010000000").unwrap(), 10_000_000);
assert_eq!(parse_ppb("0.000000000").unwrap(), 0);
assert_eq!(parse_ppb("0.999999999").unwrap(), 999_999_999);
}
}
9 changes: 7 additions & 2 deletions src/bin/ledgeracio/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//! e.g. rotating session keys, set payment target, announcing
//! intention to validate etc. Requires a network connection.

use super::{parse_reward_destination, AccountType, AddressSource, Error, LedgeracioPath, StructOpt};
use super::{common::parse_ppb, parse_reward_destination, AccountType, AddressSource, Error,
LedgeracioPath, StructOpt};
use codec::Decode;
use core::{future::Future, pin::Pin};
use ledgeracio::parse_address;
Expand All @@ -42,7 +43,11 @@ pub(crate) enum Validator {
/// specified.
Show { index: Option<u32> },
/// Announce intention to validate
Announce { index: u32, commission: Option<u32> },
Announce {
index: u32,
#[structopt(parse(try_from_str = parse_ppb))]
commission: Option<u32>,
},
/// Chill (announce intention to cease validation)
Chill { index: u32 },
/// Replace a session key
Expand Down