Skip to content

Commit

Permalink
Merge pull request #184 from input-output-hk/feature/new-registration…
Browse files Browse the repository at this point in the history
…-processing

fix: Voter rewards registration formating
  • Loading branch information
minikin authored Oct 11, 2023
2 parents 9c51c1f + 5a28fd1 commit 0188ab5
Show file tree
Hide file tree
Showing 22 changed files with 248 additions and 92 deletions.
2 changes: 1 addition & 1 deletion catalyst-toolbox/src/bin/cli/kedqr/decode/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::Path;
use std::path::PathBuf;

pub fn save_secret_from_qr(qr: PathBuf, output: Option<PathBuf>, pin: QrPin) -> Result<(), Report> {
let sk = secret_from_qr(&qr, pin)?;
let sk = secret_from_qr(qr, pin)?;
let hrp = Ed25519Extended::SECRET_BECH32_HRP;
let secret_key = bech32::encode(hrp, sk.leak_secret().to_base32(), Variant::Bech32)?;

Expand Down
2 changes: 1 addition & 1 deletion catalyst-toolbox/src/bin/cli/kedqr/encode/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn generate_qr(input: PathBuf, output: Option<PathBuf>, pin: QrPin) -> Resul
.read(true)
.write(false)
.append(false)
.open(&input)
.open(input)
.expect("Could not open input file.");

let mut reader = BufReader::new(key_file);
Expand Down
2 changes: 1 addition & 1 deletion catalyst-toolbox/src/bin/cli/kedqr/encode/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn generate_payload(input: PathBuf, output: Option<PathBuf>, pin: QrPin) ->
.read(true)
.write(false)
.append(false)
.open(&input)
.open(input)
.expect("Could not open input file.");

let mut reader = BufReader::new(key_file);
Expand Down
3 changes: 1 addition & 2 deletions catalyst-toolbox/src/bin/cli/kedqr/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ impl VerifyQrCodeCmd {
if let Some(file) = &self.file {
vec![file.to_path_buf()]
} else {
std::fs::read_dir(&self.folder.as_ref().unwrap())
std::fs::read_dir(self.folder.as_ref().unwrap())
.unwrap()
.into_iter()
.map(|x| x.unwrap().path())
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions catalyst-toolbox/src/bin/cli/rewards/veterans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ impl VeteransRewards {
min_rankings..=max_rankings_reputation,
rewards_agreement_rate_cutoffs
.into_iter()
.zip(rewards_agreement_rate_modifiers.into_iter())
.zip(rewards_agreement_rate_modifiers)
.collect(),
reputation_agreement_rate_cutoffs
.into_iter()
.zip(reputation_agreement_rate_modifiers.into_iter())
.zip(reputation_agreement_rate_modifiers)
.collect(),
);

Expand Down
6 changes: 3 additions & 3 deletions catalyst-toolbox/src/bin/cli/rewards/voters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use catalyst_toolbox::rewards::voters::{calc_voter_rewards, Rewards, VoteCount};
use catalyst_toolbox::snapshot::{MainnetRewardAddress, Snapshot};
use catalyst_toolbox::snapshot::{RewardAddress, Snapshot};
use catalyst_toolbox::utils::assert_are_close;

use color_eyre::Report;
Expand Down Expand Up @@ -40,12 +40,12 @@ pub struct VotersRewards {

fn write_rewards_results(
common: Common,
rewards: &BTreeMap<MainnetRewardAddress, Rewards>,
rewards: &BTreeMap<RewardAddress, Rewards>,
) -> Result<(), Report> {
let writer = common.open_output()?;
let header = ["Address", "Reward for the voter (lovelace)"];
let mut csv_writer = csv::Writer::from_writer(writer);
csv_writer.write_record(&header)?;
csv_writer.write_record(header)?;

for (address, rewards) in rewards.iter() {
let record = [address.to_string(), rewards.trunc().to_string()];
Expand Down
4 changes: 2 additions & 2 deletions catalyst-toolbox/src/bin/cli/stats/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ impl ArchiveCommand {
pub fn exec(self) -> Result<(), Report> {
let archiver: ArchiveStats = {
if let Some(csv) = &self.csv {
load_from_csv(&csv)?.into()
load_from_csv(csv)?.into()
} else if let Some(folder) = &self.folder {
load_from_folder(&folder)?.into()
load_from_folder(folder)?.into()
} else {
panic!("no csv nor folder defined");
}
Expand Down
2 changes: 1 addition & 1 deletion catalyst-toolbox/src/bin/cli/stats/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum Command {

impl SnapshotCommand {
pub fn exec(&self) -> Result<(), Report> {
let initials: Vec<Initial> = read_initials(&jortestkit::file::read_file(&self.snapshot)?)?;
let initials: Vec<Initial> = read_initials(jortestkit::file::read_file(&self.snapshot)?)?;

match self.command {
Command::Count => calculate_wallet_distribution_from_initials(
Expand Down
3 changes: 2 additions & 1 deletion catalyst-toolbox/src/ideascale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ pub fn build_challenges(
(
c.id,
models::se::Challenge {
#[allow(clippy::obfuscated_if_else)]
challenge_type: funnels
.get(&c.funnel_id)
.unwrap_or_else(|| panic!("A funnel with id {} wasn't found", c.funnel_id))
.is_community()
.then(|| "community-choice")
.then_some("community-choice")
.unwrap_or("simple")
.to_string(),
challenge_url: c.challenge_url.clone(),
Expand Down
5 changes: 1 addition & 4 deletions catalyst-toolbox/src/ideascale/models/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ fn deserialize_rewards<'de, D: Deserializer<'de>>(deserializer: D) -> Result<u64
})
.and_then(|s| s.parse().ok())
.ok_or_else(|| {
D::Error::custom(&format!(
"Unable to read malformed value: '{}'",
rewards_str
))
D::Error::custom(format!("Unable to read malformed value: '{}'", rewards_str))
})
}
2 changes: 1 addition & 1 deletion catalyst-toolbox/src/kedqr/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub enum QrDecodeError {
impl KeyQrCode {
pub fn generate(key: SecretKey<Ed25519Extended>, password: &[u8]) -> Self {
let enc_hex = payload::generate(key, password);
let inner = QrCode::with_error_correction_level(&enc_hex, EcLevel::H).unwrap();
let inner = QrCode::with_error_correction_level(enc_hex, EcLevel::H).unwrap();

KeyQrCode { inner }
}
Expand Down
2 changes: 1 addition & 1 deletion catalyst-toolbox/src/kedqr/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn decode<S: Into<String>>(
password: &[u8],
) -> Result<SecretKey<Ed25519Extended>, Error> {
let encrypted_bytes = hex::decode(payload.into())?;
let key = decrypt(password, &encrypted_bytes)?;
let key = decrypt(password, encrypted_bytes)?;
Ok(SecretKey::from_binary(&key)?)
}

Expand Down
8 changes: 2 additions & 6 deletions catalyst-toolbox/src/logs/sentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,9 @@ mod tests {
let successful_scan_log = serde_json::json!({ "message": REGISTERED_MESSAGE });
let unsuccessful_scan_log = serde_json::json!({ "message": MALFORMED_QR_MESSAGE });

let success_logs = (0..success)
.into_iter()
.map(|_| successful_scan_log.clone());
let success_logs = (0..success).map(|_| successful_scan_log.clone());

let unsuccessful_logs = (0..unssucess)
.into_iter()
.map(|_| unsuccessful_scan_log.clone());
let unsuccessful_logs = (0..unssucess).map(|_| unsuccessful_scan_log.clone());

success_logs.chain(unsuccessful_logs).collect()
}
Expand Down
3 changes: 2 additions & 1 deletion catalyst-toolbox/src/recovery/replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Replay {
}
}

#[allow(clippy::result_large_err)]
pub fn exec(self) -> Result<(), Error> {
let fragments = load_persistent_fragments_logs_from_folder_path(&self.logs_path)
.map_err(Error::PersistenLogsLoading)?;
Expand All @@ -53,7 +54,7 @@ impl Replay {
let mut out_writer = self.output.open()?;
let content = self
.output_format
.format_json(serde_json::to_value(&voteplan_status)?)?;
.format_json(serde_json::to_value(voteplan_status)?)?;
out_writer.write_all(content.as_bytes())?;
Ok(())
}
Expand Down
10 changes: 7 additions & 3 deletions catalyst-toolbox/src/recovery/tally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub struct VoteFragmentFilter<I: Iterator<Item = PersistentFragmentLog>> {
}

impl<I: Iterator<Item = PersistentFragmentLog>> VoteFragmentFilter<I> {
#[allow(clippy::result_large_err)]
pub fn new(block0: Block, range_check: Range<u32>, fragments: I) -> Result<Self, Error> {
let block0_configuration = Block0Configuration::from_block(&block0)?;
let fees = block0_configuration.blockchain_configuration.linear_fees;
Expand Down Expand Up @@ -347,6 +348,7 @@ impl<I: Iterator<Item = PersistentFragmentLog>> Iterator for VoteFragmentFilter<
}
}

#[allow(clippy::result_large_err)]
pub fn recover_ledger_from_logs(
block0: &Block,
fragment_logs: impl Iterator<Item = Result<PersistentFragmentLog, FragmentLogDeserializeError>>,
Expand Down Expand Up @@ -435,6 +437,7 @@ struct FragmentReplayer {

impl FragmentReplayer {
// build a new block0 with mirror accounts and same configuration as original one
#[allow(clippy::result_large_err)]
fn from_block0(block0: &Block) -> Result<(Self, Block), Error> {
let mut config =
Block0Configuration::from_block(block0).map_err(Error::Block0ConfigurationError)?;
Expand Down Expand Up @@ -502,6 +505,7 @@ impl FragmentReplayer {
))
}

#[allow(clippy::result_large_err)]
fn replay_votecast(&mut self, tx: TransactionSlice<VoteCast>) -> Result<Fragment, Error> {
let (vote_cast, identifier, _) = deconstruct_account_transaction(&tx)?;
let address =
Expand All @@ -526,6 +530,7 @@ impl FragmentReplayer {
Ok(res)
}

#[allow(clippy::result_large_err)]
fn replay_tx(&mut self, tx: TransactionSlice<NoExtra>) -> Result<Fragment, Error> {
let (_, identifier, _) = deconstruct_account_transaction(&tx)?;
let address =
Expand All @@ -544,9 +549,7 @@ impl FragmentReplayer {
self.non_voting_wallets
.entry(address.clone())
.or_insert_with(|| {
Wallet::new_from_key(<SecretKey<Ed25519Extended>>::generate(
&mut rand::thread_rng(),
))
Wallet::new_from_key(<SecretKey<Ed25519Extended>>::generate(rand::thread_rng()))
})
.account_id()
}
Expand Down Expand Up @@ -576,6 +579,7 @@ impl FragmentReplayer {
}

// rebuild a fragment to be used in the new ledger configuration with the account mirror account.
#[allow(clippy::result_large_err)]
fn replay(
&mut self,
original: ValidatedFragment,
Expand Down
14 changes: 7 additions & 7 deletions catalyst-toolbox/src/rewards/veterans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn calculate_veteran_advisors_incentives(

reputation_eligible_rankings
.into_iter()
.zip(rewards_eligible_rankings.into_iter())
.zip(rewards_eligible_rankings)
.map(|((vca, reputation), (_vca2, reward))| {
assert_eq!(vca, _vca2); // the use of BTreeMaps ensures iteration is consistent
(
Expand Down Expand Up @@ -225,11 +225,11 @@ mod tests {
2..=2,
THRESHOLDS
.into_iter()
.zip(REWARDS_DISAGREEMENT_MODIFIERS.into_iter())
.zip(REWARDS_DISAGREEMENT_MODIFIERS)
.collect(),
THRESHOLDS
.into_iter()
.zip(REPUTATION_DISAGREEMENT_MODIFIERS.into_iter())
.zip(REPUTATION_DISAGREEMENT_MODIFIERS)
.collect(),
);
assert!(results.get(VCA_1).is_none());
Expand All @@ -254,11 +254,11 @@ mod tests {
1..=1,
THRESHOLDS
.into_iter()
.zip(REWARDS_DISAGREEMENT_MODIFIERS.into_iter())
.zip(REWARDS_DISAGREEMENT_MODIFIERS)
.collect(),
THRESHOLDS
.into_iter()
.zip(REPUTATION_DISAGREEMENT_MODIFIERS.into_iter())
.zip(REPUTATION_DISAGREEMENT_MODIFIERS)
.collect(),
);
let res1 = results.get(VCA_1).unwrap();
Expand Down Expand Up @@ -304,11 +304,11 @@ mod tests {
1..=200,
THRESHOLDS
.into_iter()
.zip(REWARDS_DISAGREEMENT_MODIFIERS.into_iter())
.zip(REWARDS_DISAGREEMENT_MODIFIERS)
.collect(),
THRESHOLDS
.into_iter()
.zip(REPUTATION_DISAGREEMENT_MODIFIERS.into_iter())
.zip(REPUTATION_DISAGREEMENT_MODIFIERS)
.collect(),
);
let expected_reward_portion = agreement * Rewards::from(100) * reward_modifier;
Expand Down
10 changes: 4 additions & 6 deletions catalyst-toolbox/src/rewards/voters.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::snapshot::{MainnetRewardAddress, Snapshot};
use crate::snapshot::{RewardAddress, Snapshot};
use chain_addr::{Discrimination, Kind};
use chain_impl_mockchain::transaction::UnspecifiedAccountIdentifier;
use chain_impl_mockchain::vote::CommitteeId;
Expand Down Expand Up @@ -85,7 +85,7 @@ fn active_addresses(
// even if they didn't vote and the threshold is 0.
// Active accounts are overwritten with the correct count.
.map(|key| (key.to_hex(), 0))
.chain(vote_count.into_iter())
.chain(vote_count)
.filter_map(|(account_hex, count)| {
if count >= threshold {
Some(
Expand Down Expand Up @@ -120,7 +120,7 @@ fn account_hex_to_address(
fn rewards_to_mainnet_addresses(
rewards: HashMap<&'_ Address, Rewards>,
snapshot: Snapshot,
) -> BTreeMap<MainnetRewardAddress, Rewards> {
) -> BTreeMap<RewardAddress, Rewards> {
let mut res = BTreeMap::new();
for (addr, reward) in rewards {
let registrations = snapshot.registrations_for_voting_key(
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn calc_voter_rewards(
block0: &Block0Configuration,
snapshot: Snapshot,
total_rewards: Rewards,
) -> Result<BTreeMap<MainnetRewardAddress, Rewards>, Error> {
) -> Result<BTreeMap<RewardAddress, Rewards>, Error> {
let active_addresses = active_addresses(vote_count, block0, vote_threshold, &snapshot);

let committee_keys: HashSet<Address> = block0
Expand Down Expand Up @@ -219,7 +219,6 @@ mod tests {
fn test_all_active(snapshot: Snapshot) {
let votes_count = snapshot
.voting_keys()
.into_iter()
.map(|key| (key.to_hex(), 1))
.collect::<VoteCount>();
let n_voters = votes_count.len();
Expand Down Expand Up @@ -299,7 +298,6 @@ mod tests {

let votes_count = snapshot
.voting_keys()
.into_iter()
.map(|key| (key.to_hex(), 1))
.collect::<VoteCount>();

Expand Down
Loading

0 comments on commit 0188ab5

Please sign in to comment.