|
| 1 | +use catalyst_toolbox::stats::distribution::Stats; |
| 2 | +use catalyst_toolbox::stats::voters::calculate_active_wallet_distribution; |
| 3 | +use color_eyre::Report; |
| 4 | +use std::ops::Range; |
| 5 | +use std::path::PathBuf; |
| 6 | +use structopt::StructOpt; |
| 7 | +use thiserror::Error; |
| 8 | + |
| 9 | +#[derive(StructOpt, Debug)] |
| 10 | +pub struct ActiveVotersCommand { |
| 11 | + #[structopt(long = "support-lovelace")] |
| 12 | + pub support_lovelace: bool, |
| 13 | + #[structopt(long = "block0")] |
| 14 | + pub block0: String, |
| 15 | + #[structopt(long = "threshold")] |
| 16 | + pub threshold: u64, |
| 17 | + #[structopt(long = "votes-count-file")] |
| 18 | + pub votes_count_path: PathBuf, |
| 19 | + #[structopt(long = "votes-count-levels")] |
| 20 | + pub votes_count_levels: Option<PathBuf>, |
| 21 | + #[structopt(subcommand)] |
| 22 | + pub command: Command, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(StructOpt, Debug)] |
| 26 | +pub enum Command { |
| 27 | + Count, |
| 28 | + Ada, |
| 29 | + Votes, |
| 30 | +} |
| 31 | + |
| 32 | +impl ActiveVotersCommand { |
| 33 | + pub fn exec(&self) -> Result<(), Report> { |
| 34 | + match self.command { |
| 35 | + Command::Count => calculate_active_wallet_distribution( |
| 36 | + Stats::new(self.threshold)?, |
| 37 | + &self.block0, |
| 38 | + &self.votes_count_path, |
| 39 | + self.support_lovelace, |
| 40 | + |stats, value, _| stats.add(value), |
| 41 | + )? |
| 42 | + .print_count_per_level(), |
| 43 | + Command::Ada => calculate_active_wallet_distribution( |
| 44 | + Stats::new(self.threshold)?, |
| 45 | + &self.block0, |
| 46 | + &self.votes_count_path, |
| 47 | + self.support_lovelace, |
| 48 | + |stats, value, _| stats.add(value), |
| 49 | + )? |
| 50 | + .print_ada_per_level(), |
| 51 | + Command::Votes => calculate_active_wallet_distribution( |
| 52 | + Stats::new_with_levels(get_casted_votes_levels(&self.votes_count_levels)?), |
| 53 | + &self.block0, |
| 54 | + &self.votes_count_path, |
| 55 | + self.support_lovelace, |
| 56 | + |stats, _, weight| stats.add_with_weight(1, weight as u32), |
| 57 | + )? |
| 58 | + .print_count_per_level(), |
| 59 | + }; |
| 60 | + |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn get_casted_votes_levels(path: &Option<PathBuf>) -> Result<Vec<Range<u64>>, Error> { |
| 66 | + if let Some(path) = path { |
| 67 | + serde_json::from_reader(jcli_lib::utils::io::open_file_read(&Some(path))?) |
| 68 | + .map_err(Into::into) |
| 69 | + } else { |
| 70 | + Ok(default_casted_votes_levels()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn default_casted_votes_levels() -> Vec<Range<u64>> { |
| 75 | + vec![ |
| 76 | + (1..5), |
| 77 | + (5..10), |
| 78 | + (10..20), |
| 79 | + (20..50), |
| 80 | + (50..100), |
| 81 | + (100..200), |
| 82 | + (200..400), |
| 83 | + (400..800), |
| 84 | + (800..5_000), |
| 85 | + ] |
| 86 | +} |
| 87 | + |
| 88 | +#[allow(clippy::large_enum_variant)] |
| 89 | +#[derive(Error, Debug)] |
| 90 | +pub enum Error { |
| 91 | + #[error(transparent)] |
| 92 | + Io(#[from] std::io::Error), |
| 93 | + #[error(transparent)] |
| 94 | + Serde(#[from] serde_json::Error), |
| 95 | +} |
0 commit comments