Skip to content

Commit 7aab593

Browse files
committed
update libs
2 parents 4f40985 + ee9d0f3 commit 7aab593

File tree

32 files changed

+2097
-775
lines changed

32 files changed

+2097
-775
lines changed

Cargo.lock

Lines changed: 213 additions & 191 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

catalyst-toolbox/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jormungandr-integration-tests = { git = "https://github.com/input-output-hk/jorm
3333
jormungandr-automation = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
3434
thor = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
3535
jortestkit = { git = "https://github.com/input-output-hk/jortestkit.git", branch = "master" }
36+
rayon = "1.5"
3637
rust_decimal = "1.16"
3738
rust_decimal_macros = "1"
3839
futures = "0.3"
@@ -41,6 +42,7 @@ once_cell = "1.8"
4142
reqwest = { version = "0.11", features = ["blocking", "json"] }
4243
rand = "0.8.3"
4344
rand_chacha = "0.3"
45+
governor = { version = "0.4", features = ["std", "jitter"], default-features = false}
4446
regex = "1.5"
4547
serde = "1.0"
4648
serde_json = "1.0"
@@ -56,11 +58,12 @@ image = "0.23.12"
5658
qrcode = "0.12"
5759
quircs = "0.10.0"
5860
symmetric-cipher = { git = "https://github.com/input-output-hk/chain-wallet-libs.git", branch = "master" }
59-
graphql_client = "0.10"
61+
graphql_client = { version = "0.10" }
6062
gag = "1"
6163
vit-servicing-station-lib = { git = "https://github.com/input-output-hk/vit-servicing-station.git", branch = "master" }
6264
env_logger = "0.9"
6365
voting-hir = { path = "../voting-hir", features = ["serde"] }
66+
fraction = "0.10"
6467

6568
[dev-dependencies]
6669
rand_chacha = "0.3"
@@ -71,6 +74,7 @@ chain-vote = { git = "https://github.com/input-output-hk/chain-libs.git", branch
7174
proptest = { git = "https://github.com/input-output-hk/proptest", branch = "master" }
7275
test-strategy = "0.2"
7376
serde_test = "1"
77+
voting-hir = { path = "../voting-hir", features = ["serde", "proptest"] }
7478

7579
[build-dependencies]
7680
versionisator = "1.0.3"

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::collections::HashSet;
88

99
use structopt::StructOpt;
1010

11+
use catalyst_toolbox::http::default_http_client;
12+
1113
use serde::de::DeserializeOwned;
1214
use serde::Serialize;
1315
use std::path::{Path, PathBuf};
@@ -98,6 +100,8 @@ impl Import {
98100
stages_filters,
99101
} = self;
100102

103+
let client = default_http_client(Some(api_token));
104+
101105
let tags: CustomFieldTags = if let Some(tags_path) = tags {
102106
read_json_from_file(tags_path)?
103107
} else {
@@ -110,20 +114,15 @@ impl Import {
110114
Default::default()
111115
};
112116

113-
let runtime = tokio::runtime::Builder::new_multi_thread()
114-
.enable_io()
115-
.enable_time()
116-
.build()?;
117-
118117
let scores = read_scores_file(scores)?;
119118
let sponsors = read_sponsors_file(sponsors)?;
120-
let idescale_data = runtime.block_on(fetch_all(
119+
let idescale_data = fetch_all(
121120
*fund,
122121
&stage_label.to_lowercase(),
123122
&stages_filters.iter().map(AsRef::as_ref).collect::<Vec<_>>(),
124123
&excluded_proposals,
125-
api_token.clone(),
126-
))?;
124+
&client,
125+
)?;
127126

128127
let funds = build_fund(*fund as i32, fund_goal.clone(), *threshold);
129128
let challenges = build_challenges(*fund as i32, &idescale_data, sponsors);

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod community_advisors;
22
mod full;
3+
mod proposers;
34
mod veterans;
45
mod voters;
56

67
use std::path::PathBuf;
78

9+
use catalyst_toolbox::{http::default_http_client, rewards::proposers as proposers_lib};
810
use color_eyre::Report;
911
use structopt::StructOpt;
1012

@@ -22,6 +24,9 @@ pub enum Rewards {
2224

2325
/// Calculate full rewards based on a config file
2426
Full { path: PathBuf },
27+
28+
/// Calculate rewards for propsers
29+
Proposers(proposers_lib::ProposerRewards),
2530
}
2631

2732
impl Rewards {
@@ -31,6 +36,9 @@ impl Rewards {
3136
Rewards::CommunityAdvisors(cmd) => cmd.exec(),
3237
Rewards::Veterans(cmd) => cmd.exec(),
3338
Rewards::Full { path } => full::full_rewards(&path),
39+
Rewards::Proposers(proposers) => {
40+
proposers::rewards(&proposers, &default_http_client(None))
41+
}
3442
}
3543
}
3644
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::{collections::HashSet, fs::File};
2+
3+
use catalyst_toolbox::{
4+
http::HttpClient,
5+
rewards::proposers::{
6+
io::{load_data, write_results},
7+
proposer_rewards, ProposerRewards, ProposerRewardsInputs,
8+
},
9+
};
10+
use color_eyre::eyre::Result;
11+
12+
pub fn rewards(
13+
ProposerRewards {
14+
output,
15+
block0,
16+
proposals,
17+
excluded_proposals,
18+
active_voteplans,
19+
challenges,
20+
committee_keys,
21+
total_stake_threshold,
22+
approval_threshold,
23+
output_format,
24+
vit_station_url,
25+
}: &ProposerRewards,
26+
http: &impl HttpClient,
27+
) -> Result<()> {
28+
let (proposals, voteplans, challenges) = load_data(
29+
http,
30+
vit_station_url,
31+
proposals.as_deref(),
32+
active_voteplans.as_deref(),
33+
challenges.as_deref(),
34+
)?;
35+
36+
let block0_config = serde_yaml::from_reader(File::open(block0)?)?;
37+
38+
let excluded_proposals = match excluded_proposals {
39+
Some(path) => serde_json::from_reader(File::open(path)?)?,
40+
None => HashSet::new(),
41+
};
42+
let committee_keys = match committee_keys {
43+
Some(path) => serde_json::from_reader(File::open(path)?)?,
44+
None => vec![],
45+
};
46+
47+
let results = proposer_rewards(ProposerRewardsInputs {
48+
block0_config,
49+
proposals,
50+
voteplans,
51+
challenges,
52+
excluded_proposals,
53+
committee_keys,
54+
total_stake_threshold: *total_stake_threshold,
55+
approval_threshold: *approval_threshold,
56+
})?;
57+
58+
write_results(output, *output_format, results)?;
59+
60+
Ok(())
61+
}

catalyst-toolbox/src/bin/cli/rewards/proposers/util.rs

Whitespace-only changes.

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use catalyst_toolbox::rewards::voters::{calc_voter_rewards, Rewards, VoteCount};
2-
use catalyst_toolbox::snapshot::{registration::MainnetRewardAddress, Snapshot};
2+
use catalyst_toolbox::snapshot::{registration::MainnetRewardAddress, SnapshotInfo};
33
use catalyst_toolbox::utils::assert_are_close;
44

55
use color_eyre::eyre::eyre;
@@ -9,6 +9,8 @@ use jcli_lib::jcli_lib::block::{open_output, Common};
99
use jcli_lib::utils::io::open_file_read;
1010
use jormungandr_lib::interfaces::Block0Configuration;
1111

12+
use color_eyre::Report;
13+
use jcli_lib::jcli_lib::block::Common;
1214
use structopt::StructOpt;
1315

1416
use std::collections::BTreeMap;
@@ -23,15 +25,9 @@ pub struct VotersRewards {
2325
#[structopt(long)]
2426
total_rewards: u64,
2527

26-
/// Path to raw snapshot
28+
/// Path to a json encoded list of `SnapshotInfo`
2729
#[structopt(long)]
28-
snapshot_path: PathBuf,
29-
30-
/// Stake threshold to be able to participate in a Catalyst sidechain
31-
/// Registrations with less than the threshold associated to the stake address
32-
/// will be ignored
33-
#[structopt(long)]
34-
registration_threshold: u64,
30+
snapshot_info_path: PathBuf,
3531

3632
#[structopt(long)]
3733
votes_count_path: PathBuf,
@@ -63,8 +59,7 @@ impl VotersRewards {
6359
let VotersRewards {
6460
common,
6561
total_rewards,
66-
snapshot_path,
67-
registration_threshold,
62+
snapshot_info_path,
6863
votes_count_path,
6964
vote_threshold,
7065
} = self;

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use catalyst_toolbox::snapshot::{voting_group::RepsVotersAssigner, RawSnapshot, Snapshot};
22
use color_eyre::Report;
3+
use fraction::Fraction;
34
use jcli_lib::utils::{output_file::OutputFile, output_format::OutputFormat};
45
use jormungandr_lib::interfaces::Value;
56
use std::fs::File;
67
use std::io::Write;
78
use std::path::PathBuf;
89
use structopt::StructOpt;
910

10-
const DEFAULT_DIRECT_VOTER_GROUP: &str = "voter";
11+
const DEFAULT_DIRECT_VOTER_GROUP: &str = "direct";
1112
const DEFAULT_REPRESENTATIVE_GROUP: &str = "rep";
1213

1314
/// Process raw registrations into blockchain initials
@@ -19,7 +20,7 @@ pub struct SnapshotCmd {
1920
snapshot: PathBuf,
2021
/// Registrations voting power threshold for eligibility
2122
#[structopt(short, long)]
22-
threshold: Value,
23+
min_stake_threshold: Value,
2324

2425
/// Voter group to assign direct voters to.
2526
/// If empty, defaults to "voter"
@@ -35,6 +36,10 @@ pub struct SnapshotCmd {
3536
#[structopt(long)]
3637
reps_db_api_url: reqwest::Url,
3738

39+
/// Voting power cap for each account
40+
#[structopt(short, long)]
41+
voting_power_cap: Fraction,
42+
3843
#[structopt(flatten)]
3944
output: OutputFile,
4045

@@ -52,8 +57,13 @@ impl SnapshotCmd {
5257
.representatives_group
5358
.unwrap_or_else(|| DEFAULT_REPRESENTATIVE_GROUP.into());
5459
let assigner = RepsVotersAssigner::new(direct_voter, representative, self.reps_db_api_url)?;
55-
let initials =
56-
Snapshot::from_raw_snapshot(raw_snapshot, self.threshold).to_voter_hir(&assigner);
60+
let initials = Snapshot::from_raw_snapshot(
61+
raw_snapshot,
62+
self.min_stake_threshold,
63+
self.voting_power_cap,
64+
&assigner,
65+
)?
66+
.to_voter_hir();
5767
let mut out_writer = self.output.open()?;
5868
let content = self
5969
.output_format

catalyst-toolbox/src/http/mock.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use color_eyre::Result;
2+
use reqwest::StatusCode;
3+
4+
use super::{HttpClient, HttpResponse};
5+
6+
pub enum Method {
7+
Get,
8+
Post,
9+
Put,
10+
Delete,
11+
Head,
12+
Connect,
13+
Options,
14+
Trace,
15+
Path,
16+
}
17+
18+
#[non_exhaustive]
19+
pub struct Spec<'a> {
20+
pub method: Method,
21+
pub path: &'a str,
22+
}
23+
24+
pub struct MockClient {
25+
handler: fn(&Spec) -> (String, StatusCode),
26+
}
27+
28+
impl MockClient {
29+
pub fn new(handler: fn(&Spec) -> (String, StatusCode)) -> Self {
30+
Self { handler }
31+
}
32+
}
33+
34+
impl HttpClient for MockClient {
35+
fn get<T>(&self, path: &str) -> Result<super::HttpResponse<T>>
36+
where
37+
T: for<'a> serde::Deserialize<'a>,
38+
{
39+
let spec = Spec {
40+
method: Method::Get,
41+
path,
42+
};
43+
44+
let (body, status) = (self.handler)(&spec);
45+
Ok(HttpResponse {
46+
body,
47+
status,
48+
_marker: std::marker::PhantomData,
49+
})
50+
}
51+
}
52+
53+
#[test]
54+
fn example_usage() {
55+
fn function_that_calls_api<T: HttpClient>(client: &T) -> Result<i32> {
56+
let response = client.get("/example")?;
57+
response.json()
58+
}
59+
60+
let mock_client = MockClient::new(|spec| match spec {
61+
Spec {
62+
method: Method::Get,
63+
path: "/example",
64+
} => ("123".to_string(), StatusCode::OK),
65+
_ => ("not found".to_string(), StatusCode::NOT_FOUND),
66+
});
67+
68+
let response = function_that_calls_api(&mock_client).unwrap();
69+
assert_eq!(response, 123);
70+
}

0 commit comments

Comments
 (0)