Skip to content

Commit ddf7d44

Browse files
committed
review fix vol. 2
1 parent 312ae77 commit ddf7d44

File tree

7 files changed

+70
-30
lines changed

7 files changed

+70
-30
lines changed

catalyst-toolbox/src/bin/cli/stats/snapshot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ impl SnapshotCommand {
2929

3030
match self.command {
3131
Command::Count => calculate_wallet_distribution_from_initials(
32-
Stats::new(self.threshold),
32+
Stats::new(self.threshold)?,
3333
initials,
3434
vec![],
3535
self.support_lovelace,
3636
|stats, _, _| stats.add(1),
3737
)?
3838
.print_count_per_level(),
3939
Command::Ada => calculate_wallet_distribution_from_initials(
40-
Stats::new(self.threshold),
40+
Stats::new(self.threshold)?,
4141
initials,
4242
vec![],
4343
self.support_lovelace,

catalyst-toolbox/src/bin/cli/stats/voters/active.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use color_eyre::Report;
44
use std::ops::Range;
55
use std::path::PathBuf;
66
use structopt::StructOpt;
7+
use thiserror::Error;
78

89
#[derive(StructOpt, Debug)]
910
pub struct ActiveVotersCommand {
@@ -15,6 +16,8 @@ pub struct ActiveVotersCommand {
1516
pub threshold: u64,
1617
#[structopt(long = "votes-count-file")]
1718
pub votes_count_path: PathBuf,
19+
#[structopt(long = "votes-count-levels")]
20+
pub votes_count_levels: Option<PathBuf>,
1821
#[structopt(subcommand)]
1922
pub command: Command,
2023
}
@@ -30,23 +33,23 @@ impl ActiveVotersCommand {
3033
pub fn exec(&self) -> Result<(), Report> {
3134
match self.command {
3235
Command::Count => calculate_active_wallet_distribution(
33-
Stats::new(self.threshold),
36+
Stats::new(self.threshold)?,
3437
&self.block0,
3538
&self.votes_count_path,
3639
self.support_lovelace,
3740
|stats, value, _| stats.add(value),
3841
)?
3942
.print_count_per_level(),
4043
Command::Ada => calculate_active_wallet_distribution(
41-
Stats::new(self.threshold),
44+
Stats::new(self.threshold)?,
4245
&self.block0,
4346
&self.votes_count_path,
4447
self.support_lovelace,
4548
|stats, value, _| stats.add(value),
4649
)?
4750
.print_ada_per_level(),
4851
Command::Votes => calculate_active_wallet_distribution(
49-
Stats::new_with_levels(casted_votes_levels()),
52+
Stats::new_with_levels(get_casted_votes_levels(&self.votes_count_levels)?),
5053
&self.block0,
5154
&self.votes_count_path,
5255
self.support_lovelace,
@@ -59,7 +62,16 @@ impl ActiveVotersCommand {
5962
}
6063
}
6164

62-
fn casted_votes_levels() -> Vec<Range<u64>> {
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>> {
6375
vec![
6476
(1..5),
6577
(5..10),
@@ -72,3 +84,12 @@ fn casted_votes_levels() -> Vec<Range<u64>> {
7284
(800..5_000),
7385
]
7486
}
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+
}

catalyst-toolbox/src/bin/cli/stats/voters/initials.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ impl InitialVotersCommand {
2626
match self.command {
2727
Command::Count => calculate_wallet_distribution(
2828
&self.block0,
29-
Stats::new(self.threshold),
29+
Stats::new(self.threshold)?,
3030
self.support_lovelace,
3131
|stats, value, _| stats.add(value),
3232
)?
3333
.print_count_per_level(),
3434
Command::Ada => calculate_wallet_distribution(
3535
&self.block0,
36-
Stats::new(self.threshold),
36+
Stats::new(self.threshold)?,
3737
self.support_lovelace,
3838
|stats, value, _| stats.add(value),
3939
)?

catalyst-toolbox/src/rewards/voters.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
use crate::snapshot::{registration::MainnetRewardAddress, SnapshotInfo};
12
use jormungandr_lib::crypto::{account::Identifier, hash::Hash};
3+
use chain_addr::{Discrimination, Kind};
4+
use chain_impl_mockchain::transaction::UnspecifiedAccountIdentifier;
5+
use jormungandr_lib::{crypto::account::Identifier, interfaces::Address};
26
use rust_decimal::Decimal;
37
use snapshot_lib::{registration::MainnetRewardAddress, SnapshotInfo};
48
use std::collections::{BTreeMap, HashMap, HashSet};

catalyst-toolbox/src/stats/distribution.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
use core::ops::Range;
22
use std::collections::HashMap;
3+
use thiserror::Error;
34

4-
fn levels(threshold: u64) -> Vec<Range<u64>> {
5-
vec![
5+
fn levels(threshold: u64) -> Result<Vec<Range<u64>>, Error> {
6+
if !(450..=1_000).contains(&threshold) {
7+
return Err(Error::InvalidThreshold(threshold));
8+
}
9+
10+
Ok(vec![
611
(0..450),
712
(450..threshold),
813
(threshold..1_000),
@@ -20,7 +25,7 @@ fn levels(threshold: u64) -> Vec<Range<u64>> {
2025
(10_000_000..25_000_000),
2126
(25_000_000..50_000_000),
2227
(50_000_000..32_000_000_000),
23-
]
28+
])
2429
}
2530

2631
#[derive(Default)]
@@ -34,8 +39,8 @@ pub struct Stats {
3439
}
3540

3641
impl Stats {
37-
pub fn new(threshold: u64) -> Self {
38-
Self::new_with_levels(levels(threshold))
42+
pub fn new(threshold: u64) -> Result<Self, Error> {
43+
Ok(Self::new_with_levels(levels(threshold)?))
3944
}
4045

4146
pub fn new_with_levels(levels: Vec<Range<u64>>) -> Self {
@@ -110,3 +115,10 @@ fn format_big_number(n: u64) -> String {
110115
n.to_string()
111116
}
112117
}
118+
119+
#[allow(clippy::large_enum_variant)]
120+
#[derive(Error, Debug)]
121+
pub enum Error {
122+
#[error("invalid threshold for distribution levels ({0}). It should be more than 450 and less that 1000")]
123+
InvalidThreshold(u64),
124+
}

catalyst-toolbox/src/stats/voters.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use crate::rewards::voters::{account_hex_to_address, VoteCount};
1+
use crate::rewards::voters::VoteCount;
22
use crate::stats::distribution::Stats;
33
use chain_addr::{Discrimination, Kind};
44
use chain_crypto::Ed25519;
55
use chain_impl_mockchain::vote::CommitteeId;
66
use jormungandr_automation::testing::block0::get_block;
7-
use jormungandr_lib::interfaces::Address;
8-
use jormungandr_lib::interfaces::Block0Configuration;
9-
use jormungandr_lib::interfaces::Initial;
10-
use jormungandr_lib::interfaces::InitialUTxO;
7+
use jormungandr_lib::{
8+
crypto::account::Identifier,
9+
interfaces::{Address, Block0Configuration, Initial, InitialUTxO},
10+
};
1111
use std::path::Path;
1212

1313
fn blacklist_addresses(genesis: &Block0Configuration) -> Vec<Address> {
@@ -35,7 +35,7 @@ fn blacklist_addresses(genesis: &Block0Configuration) -> Vec<Address> {
3535
.collect()
3636
}
3737

38-
fn vote_counts_as_addresses(
38+
fn vote_counts_as_utxo(
3939
votes_count: VoteCount,
4040
genesis: &Block0Configuration,
4141
) -> Vec<(InitialUTxO, u32)> {
@@ -45,15 +45,8 @@ fn vote_counts_as_addresses(
4545
.filter_map(|initials| {
4646
if let Initial::Fund(funds) = initials {
4747
for utxo in funds {
48-
if let Some((_, votes_count)) = votes_count.iter().find(|(address, _)| {
49-
account_hex_to_address(
50-
address.to_string(),
51-
genesis.blockchain_configuration.discrimination,
52-
)
53-
.unwrap()
54-
== utxo.address
55-
}) {
56-
return Some((utxo.clone(), *votes_count as u32));
48+
if let Some(vote_count) = votes_count.get(&addr_to_hex(&utxo.address)) {
49+
return Some((utxo.clone(), *vote_count as u32));
5750
}
5851
}
5952
}
@@ -62,6 +55,16 @@ fn vote_counts_as_addresses(
6255
.collect()
6356
}
6457

58+
pub fn addr_to_hex(address: &Address) -> String {
59+
match &address.1 .1 {
60+
Kind::Account(pk) => {
61+
let id: Identifier = pk.clone().into();
62+
id.to_hex()
63+
}
64+
_ => unimplemented!(),
65+
}
66+
}
67+
6568
pub fn calculate_active_wallet_distribution<S: Into<String>, P: AsRef<Path>>(
6669
stats: Stats,
6770
block0: S,
@@ -77,7 +80,7 @@ pub fn calculate_active_wallet_distribution<S: Into<String>, P: AsRef<Path>>(
7780
)?)?;
7881

7982
let blacklist = blacklist_addresses(&genesis);
80-
let initials = vote_counts_as_addresses(vote_count, &genesis);
83+
let initials = vote_counts_as_utxo(vote_count, &genesis);
8184

8285
calculate_wallet_distribution_from_initials_utxo(
8386
stats,

catalyst-toolbox/tests/tally/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn account_from_slice<P>(
4545
}
4646
}
4747

48-
impl<'a> VoteRoundGenerator {
48+
impl VoteRoundGenerator {
4949
pub fn new(blockchain: TestBlockchain) -> Self {
5050
let TestBlockchain {
5151
config,

0 commit comments

Comments
 (0)