Skip to content

Commit 4ee02f6

Browse files
committed
Merge branch 'rewards-script-rewrite-it-in-rust' into full-rewards-script
2 parents 7aab593 + 4137dc1 commit 4ee02f6

File tree

1 file changed

+19
-6
lines changed
  • catalyst-toolbox/src/rewards/proposers

1 file changed

+19
-6
lines changed

catalyst-toolbox/src/rewards/proposers/mod.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
33
use chain_impl_mockchain::value::Value;
44
use color_eyre::{
55
eyre::{bail, eyre},
6-
Result,
6+
Help, Result,
77
};
88
use itertools::Itertools;
99
use jormungandr_lib::{
@@ -188,16 +188,19 @@ fn calculate_approval_threshold(
188188

189189
/// returns (yes, no)
190190
fn extract_yes_no_votes(proposal: &Proposal, voteplan: &VoteProposalStatus) -> Result<(u64, u64)> {
191+
const YES: &str = "yes";
192+
const NO: &str = "no";
193+
191194
let yes_index = proposal
192195
.chain_vote_options
193196
.0
194-
.get("yes")
195-
.ok_or(eyre!("missing `yes` field"))?;
197+
.get(YES)
198+
.ok_or(eyre!("missing `{YES}` field"))?;
196199
let no_index = proposal
197200
.chain_vote_options
198201
.0
199-
.get("no")
200-
.ok_or(eyre!("missing `no` field"))?;
202+
.get(NO)
203+
.ok_or(eyre!("missing `{NO}` field"))?;
201204

202205
let tally = match &voteplan.tally {
203206
Tally::Public { result } => result,
@@ -247,12 +250,22 @@ fn sanity_check_data(
247250
Ok(())
248251
}
249252

253+
const INVALID_CHAIN_ID_UTF8_MSG: &str = "chain_proposal_id's serde impls encode strings as `Vec<u8>`, panicking if non-UTF-8 is detected, so it should be safe to assume `chain_proposal_id` contains valid UTF-8 (probably a base16 string)";
254+
250255
fn filter_excluded_proposals(
251256
proposals: &HashMap<Hash, Proposal>,
252257
excluded_proposals: &HashSet<String>,
253258
) -> HashMap<Hash, Proposal> {
254259
let predicate = |prop: &Proposal| {
255-
let chain_proposal_id = String::from_utf8(prop.chain_proposal_id.clone()).unwrap();
260+
let chain_proposal_id = String::from_utf8(prop.chain_proposal_id.clone())
261+
.map_err(|_| {
262+
eyre!(
263+
"chain_proposal_id contained invalid UTF8 bytes, internal_id: {}",
264+
prop.internal_id
265+
)
266+
.with_note(|| INVALID_CHAIN_ID_UTF8_MSG)
267+
})
268+
.unwrap();
256269

257270
!excluded_proposals.contains(&prop.proposal_id)
258271
&& !excluded_proposals.contains(&chain_proposal_id)

0 commit comments

Comments
 (0)