Skip to content

Commit 08dd801

Browse files
committed
added voters active command for active voters calculation
1 parent 5748d63 commit 08dd801

File tree

10 files changed

+239
-44
lines changed

10 files changed

+239
-44
lines changed

catalyst-toolbox/src/bin/cli/rewards/voters.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ impl VotersRewards {
9090
entry.sort_by_key(|p| p.voteplan.chain_proposal_index);
9191
acc
9292
});
93-
9493
let vote_count = serde_json::from_reader::<_, HashMap<Identifier, Vec<AccountVotes>>>(
9594
jcli_lib::utils::io::open_file_read(&Some(votes_count_path))?,
9695
)?

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use color_eyre::Report;
88
use live::LiveStatsCommand;
99
use snapshot::SnapshotCommand;
1010
use structopt::StructOpt;
11-
use voters::InitialVotersCommand;
11+
use voters::VotersCommand;
1212

1313
#[derive(StructOpt, Debug)]
1414
pub enum Stats {
15-
Voters(InitialVotersCommand),
15+
Voters(VotersCommand),
1616
Live(LiveStatsCommand),
1717
Archive(ArchiveCommand),
1818
Snapshot(SnapshotCommand),

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use catalyst_toolbox::stats::distribution::Stats;
12
use catalyst_toolbox::stats::snapshot::read_initials;
23
use catalyst_toolbox::stats::voters::calculate_wallet_distribution_from_initials;
34
use color_eyre::Report;
@@ -28,17 +29,19 @@ impl SnapshotCommand {
2829

2930
match self.command {
3031
Command::Count => calculate_wallet_distribution_from_initials(
32+
Stats::new(self.threshold),
3133
initials,
3234
vec![],
33-
self.threshold,
3435
self.support_lovelace,
36+
|stats, _, _| stats.add(1),
3537
)?
3638
.print_count_per_level(),
3739
Command::Ada => calculate_wallet_distribution_from_initials(
40+
Stats::new(self.threshold),
3841
initials,
3942
vec![],
40-
self.threshold,
4143
self.support_lovelace,
44+
|stats, value, _| stats.add(value),
4245
)?
4346
.print_ada_per_level(),
4447
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use catalyst_toolbox::stats::distribution::Stats;
2+
use catalyst_toolbox::stats::voters::calculate_active_wallet_distribution;
3+
use std::ops::Range;
4+
use std::path::PathBuf;
5+
use structopt::StructOpt;
6+
7+
#[derive(StructOpt, Debug)]
8+
pub struct ActiveVotersCommand {
9+
#[structopt(long = "support-lovelace")]
10+
pub support_lovelace: bool,
11+
#[structopt(long = "block0")]
12+
pub block0: String,
13+
#[structopt(long = "threshold")]
14+
pub threshold: u64,
15+
#[structopt(long = "votes-count-file")]
16+
pub votes_count_path: PathBuf,
17+
#[structopt(subcommand)]
18+
pub command: Command,
19+
}
20+
21+
#[derive(StructOpt, Debug)]
22+
pub enum Command {
23+
Count,
24+
Ada,
25+
Votes,
26+
}
27+
28+
impl ActiveVotersCommand {
29+
pub fn exec(&self) -> Result<(), catalyst_toolbox::stats::Error> {
30+
match self.command {
31+
Command::Count => calculate_active_wallet_distribution(
32+
Stats::new(self.threshold),
33+
&self.block0,
34+
&self.votes_count_path,
35+
self.support_lovelace,
36+
|stats, value, _| stats.add(value),
37+
)?
38+
.print_count_per_level(),
39+
Command::Ada => calculate_active_wallet_distribution(
40+
Stats::new(self.threshold),
41+
&self.block0,
42+
&self.votes_count_path,
43+
self.support_lovelace,
44+
|stats, value, _| stats.add(value),
45+
)?
46+
.print_ada_per_level(),
47+
Command::Votes => calculate_active_wallet_distribution(
48+
Stats::new_with_levels(casted_votes_levels()),
49+
&self.block0,
50+
&self.votes_count_path,
51+
self.support_lovelace,
52+
|stats, _, weight| stats.add_with_weight(1, weight as u32),
53+
)?
54+
.print_count_per_level(),
55+
};
56+
57+
Ok(())
58+
}
59+
}
60+
61+
fn casted_votes_levels() -> Vec<Range<u64>> {
62+
vec![
63+
(1..5),
64+
(5..10),
65+
(10..20),
66+
(20..50),
67+
(50..100),
68+
(100..200),
69+
(200..400),
70+
(400..800),
71+
(800..5_000),
72+
]
73+
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use catalyst_toolbox::stats::distribution::Stats;
12
use catalyst_toolbox::stats::voters::calculate_wallet_distribution;
23
use color_eyre::Report;
34
use structopt::StructOpt;
@@ -23,14 +24,20 @@ pub enum Command {
2324
impl InitialVotersCommand {
2425
pub fn exec(&self) -> Result<(), Report> {
2526
match self.command {
26-
Command::Count => {
27-
calculate_wallet_distribution(&self.block0, self.threshold, self.support_lovelace)?
28-
.print_count_per_level()
29-
}
30-
Command::Ada => {
31-
calculate_wallet_distribution(&self.block0, self.threshold, self.support_lovelace)?
32-
.print_ada_per_level()
33-
}
27+
Command::Count => calculate_wallet_distribution(
28+
&self.block0,
29+
Stats::new(self.threshold),
30+
self.support_lovelace,
31+
|stats, value, _| stats.add(value),
32+
)?
33+
.print_count_per_level(),
34+
Command::Ada => calculate_wallet_distribution(
35+
&self.block0,
36+
Stats::new(self.threshold),
37+
self.support_lovelace,
38+
|stats, value, _| stats.add(value),
39+
)?
40+
.print_ada_per_level(),
3441
};
3542

3643
Ok(())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
mod active;
2+
mod initials;
3+
4+
use active::ActiveVotersCommand;
5+
use initials::InitialVotersCommand;
6+
use structopt::StructOpt;
7+
8+
#[derive(StructOpt, Debug)]
9+
pub enum VotersCommand {
10+
Initials(InitialVotersCommand),
11+
Active(ActiveVotersCommand),
12+
}
13+
14+
impl VotersCommand {
15+
pub fn exec(self) -> Result<(), catalyst_toolbox::stats::Error> {
16+
match self {
17+
Self::Initials(initials) => initials.exec(),
18+
Self::Active(active) => active.exec(),
19+
}
20+
}
21+
}

catalyst-toolbox/src/recovery/tally.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use chain_addr::{Discrimination, Kind};
22
use chain_core::property::Fragment as _;
33
use chain_crypto::{Ed25519Extended, SecretKey};
4-
use chain_impl_mockchain::accounting::account::SpendingCounterIncreasing;
54
use chain_impl_mockchain::{
65
account::{self, LedgerError, SpendingCounter},
76
block::{Block, BlockDate, HeaderId},
@@ -464,11 +463,8 @@ impl FragmentReplayer {
464463
value: utxo.value,
465464
};
466465
wallet
467-
.set_state(
468-
utxo.value.into(),
469-
SpendingCounterIncreasing::default().get_valid_counters(),
470-
)
471-
.unwrap();
466+
.set_state(utxo.value.into(), Default::default())
467+
.expect("cannot update wallet state");
472468
wallets.insert(utxo.address.clone(), wallet);
473469
if committee_members.contains(&utxo.address) {
474470
trace!("Committee account found {}", &utxo.address);

catalyst-toolbox/src/rewards/voters.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ fn filter_active_addresses(
108108
.collect()
109109
}
110110

111+
pub fn account_hex_to_address(
112+
account_hex: String,
113+
discrimination: Discrimination,
114+
) -> Result<Address, hex::FromHexError> {
115+
let mut buffer = [0u8; 32];
116+
hex::decode_to_slice(account_hex, &mut buffer)?;
117+
let identifier: UnspecifiedAccountIdentifier = UnspecifiedAccountIdentifier::from(buffer);
118+
Ok(Address::from(chain_addr::Address(
119+
discrimination,
120+
Kind::Account(
121+
identifier
122+
.to_single_account()
123+
.expect("Only single accounts are supported")
124+
.into(),
125+
),
126+
)))
127+
}
128+
111129
fn rewards_to_mainnet_addresses(
112130
rewards: HashMap<Identifier, Rewards>,
113131
voters: Vec<SnapshotInfo>,

catalyst-toolbox/src/stats/distribution.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ use std::collections::HashMap;
33

44
fn levels(threshold: u64) -> Vec<Range<u64>> {
55
vec![
6-
(0..threshold),
7-
(threshold..10_000),
6+
(0..450),
7+
(450..threshold),
8+
(500..1_000),
9+
(1_000..2_000),
10+
(2_000..5_000),
11+
(5_000..10_000),
812
(10_000..20_000),
913
(20_000..50_000),
1014
(50_000..100_000),
@@ -31,24 +35,32 @@ pub struct Stats {
3135

3236
impl Stats {
3337
pub fn new(threshold: u64) -> Self {
38+
Self::new_with_levels(levels(threshold))
39+
}
40+
41+
pub fn new_with_levels(levels: Vec<Range<u64>>) -> Self {
3442
Self {
35-
content: levels(threshold)
43+
content: levels
3644
.into_iter()
3745
.map(|range| (range, Default::default()))
3846
.collect(),
3947
}
4048
}
4149

42-
pub fn add(&mut self, value: u64) {
50+
pub fn add_with_weight(&mut self, value: u64, weight: u32) {
4351
for (range, record) in self.content.iter_mut() {
4452
if range.contains(&value) {
45-
record.count += 1;
53+
record.count += weight;
4654
record.total += value;
4755
return;
4856
}
4957
}
5058
}
5159

60+
pub fn add(&mut self, value: u64) {
61+
self.add_with_weight(value, 1);
62+
}
63+
5264
pub fn print_count_per_level(&self) {
5365
let mut keys = self.content.keys().cloned().collect::<Vec<Range<u64>>>();
5466
keys.sort_by_key(|x| x.start);

0 commit comments

Comments
 (0)