Skip to content

Commit 99ea7e5

Browse files
committed
done, leaving recovery struct for pattern matching
1 parent baa65e4 commit 99ea7e5

File tree

11 files changed

+40
-110
lines changed

11 files changed

+40
-110
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ impl VotersRewards {
6565
votes_count_path,
6666
vote_threshold,
6767
} = self;
68-
std::fs::read("doesnt_exist")?;
69-
7068
let block = common.input.load_block()?;
7169
let block0 = Block0Configuration::from_block(&block)?;
7270

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ pub struct BatchSizeByCaster {
8787

8888
impl BatchSizeByCaster {
8989
pub fn exec(&self, archiver: ArchiveStats) -> Result<BTreeMap<String, usize>, Report> {
90-
Ok(archiver.max_batch_size_per_caster(self.slots_in_epoch)?)
90+
archiver.max_batch_size_per_caster(self.slots_in_epoch)
9191
}
9292
}

catalyst-toolbox/src/kedqr/img.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,11 @@ use std::fmt;
1010
use std::fs::File;
1111
use std::io::prelude::*;
1212
use std::path::Path;
13-
use thiserror::Error;
1413

1514
pub struct KeyQrCode {
1615
inner: QrCode,
1716
}
1817

19-
20-
#[derive(Error, Debug)]
21-
pub enum QrDecodeError {
22-
#[error("couldn't decode QR code")]
23-
DecodeError(#[from] quircs::DecodeError),
24-
#[error("couldn't extract QR code")]
25-
ExtractError(#[from] quircs::ExtractError),
26-
#[error("QR code payload is not valid uf8")]
27-
NonUtf8Payload,
28-
}
29-
3018
impl KeyQrCode {
3119
pub fn generate(key: SecretKey<Ed25519Extended>, password: &[u8]) -> Self {
3220
let enc_hex = payload::generate(key, password);
@@ -67,14 +55,11 @@ impl KeyQrCode {
6755

6856
codes
6957
.map(|code| -> Result<_, Report> {
70-
let decoded = code
71-
.map_err(QrDecodeError::ExtractError)
72-
.and_then(|c| c.decode().map_err(QrDecodeError::DecodeError))?;
58+
let decoded = code?.decode()?;
7359

7460
// TODO: I actually don't know if this can fail
75-
let h = std::str::from_utf8(&decoded.payload)
76-
.map_err(|_| QrDecodeError::NonUtf8Payload)?;
77-
payload::decode(h, password).map_err(Into::into)
61+
let h = std::str::from_utf8(&decoded.payload)?;
62+
payload::decode(h, password)
7863
})
7964
.collect()
8065
}

catalyst-toolbox/src/logs/compare.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use crate::logs::sentry::{Error, SentryFragmentLog};
1+
use crate::logs::sentry::SentryFragmentLog;
22
use crate::recovery::tally::{deconstruct_account_transaction, ValidationError};
33

44
use chain_core::property::Fragment as _;
55
use chain_impl_mockchain::fragment::Fragment;
66
use chain_impl_mockchain::vote::Payload;
7+
use color_eyre::eyre::bail;
8+
use color_eyre::Report;
79
use jormungandr_lib::interfaces::PersistentFragmentLog;
810

911
use std::collections::HashSet;
@@ -19,7 +21,7 @@ pub struct LogCmpFields {
1921

2022
pub fn persistent_fragment_log_to_log_cmp_fields(
2123
fragment: &PersistentFragmentLog,
22-
) -> Result<LogCmpFields, Error> {
24+
) -> Result<LogCmpFields, Report> {
2325
if let Fragment::VoteCast(ref transaction) = fragment.fragment.clone() {
2426
let (vote_cast, identifier, choice) = deconstruct_account_transaction(
2527
&transaction.as_slice(),
@@ -39,9 +41,7 @@ pub fn persistent_fragment_log_to_log_cmp_fields(
3941
voteplan_id: vote_cast.vote_plan().to_string(),
4042
})
4143
} else {
42-
Err(Error::NotVoteCastTransaction {
43-
fragment_id: fragment.fragment.id().to_string(),
44-
})
44+
bail!("not vote cast transaction: {}", fragment.fragment.id())
4545
}
4646
}
4747

@@ -51,7 +51,7 @@ pub struct LogCmpStats {
5151
pub duplicated_sentry_logs: usize,
5252
pub duplicated_fragment_logs: usize,
5353
pub fragment_ids_differ: HashSet<String>,
54-
pub unhandled_fragment_logs: Vec<(Fragment, Error)>,
54+
pub unhandled_fragment_logs: Vec<(Fragment, Report)>,
5555
}
5656

5757
pub fn compare_logs(
@@ -62,7 +62,7 @@ pub fn compare_logs(
6262
let fragment_logs_size = fragment_logs.len();
6363
let sentry_cmp: Vec<LogCmpFields> = sentry_logs.iter().cloned().map(Into::into).collect();
6464

65-
let (fragments_cmp, unhandled_fragment_logs): (Vec<LogCmpFields>, Vec<(Fragment, Error)>) =
65+
let (fragments_cmp, unhandled_fragment_logs): (Vec<LogCmpFields>, Vec<(Fragment, Report)>) =
6666
fragment_logs.iter().fold(
6767
(Vec::new(), Vec::new()),
6868
|(mut success, mut errored), log| {

catalyst-toolbox/src/logs/sentry.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::logs::compare::LogCmpFields;
2-
use crate::recovery::tally::ValidationError;
32

3+
use color_eyre::Report;
44
use regex::Regex;
55
use reqwest::{blocking::Client, Method, Url};
66

@@ -12,24 +12,6 @@ const MALFORMED_QR_MESSAGE: &str = "malformed encryption or decryption payload";
1212

1313
pub type RawLog = serde_json::Value;
1414

15-
#[derive(Debug, thiserror::Error)]
16-
pub enum Error {
17-
#[error(transparent)]
18-
RequestError(#[from] reqwest::Error),
19-
20-
#[error("Unable to parse log from: '{0}'")]
21-
LogParseError(String),
22-
23-
#[error("Not a vote cast transaction: {fragment_id}")]
24-
NotVoteCastTransaction { fragment_id: String },
25-
26-
#[error(transparent)]
27-
UrlParseError(#[from] url::ParseError),
28-
29-
#[error(transparent)]
30-
ValidationError(#[from] ValidationError),
31-
}
32-
3315
pub struct SentryLogClient {
3416
client: Client,
3517
api_url: Url,
@@ -46,33 +28,33 @@ impl SentryLogClient {
4628
}
4729
}
4830

49-
pub fn get_raw_logs(&self) -> Result<String, Error> {
50-
self.client
31+
pub fn get_raw_logs(&self) -> Result<String, Report> {
32+
Ok(self
33+
.client
5134
.request(Method::GET, self.api_url.clone())
5235
.bearer_auth(&self.auth_token)
5336
.send()?
5437
.bytes()
55-
.map(|b| std::str::from_utf8(&b).unwrap().to_string())
56-
.map_err(Error::RequestError)
38+
.map(|b| std::str::from_utf8(&b).unwrap().to_string())?)
5739
}
5840

59-
pub fn get_json_logs(&self) -> Result<Vec<RawLog>, Error> {
60-
self.client
41+
pub fn get_json_logs(&self) -> Result<Vec<RawLog>, Report> {
42+
Ok(self
43+
.client
6144
.request(Method::GET, self.api_url.clone())
6245
.bearer_auth(&self.auth_token)
6346
.send()?
64-
.json()
65-
.map_err(Error::RequestError)
47+
.json()?)
6648
}
6749

68-
pub fn get_json_logs_chunks(&self, chunk: usize) -> Result<Vec<RawLog>, Error> {
50+
pub fn get_json_logs_chunks(&self, chunk: usize) -> Result<Vec<RawLog>, Report> {
6951
let api_url = self.api_url.join(&format!("?&cursor=0:{}:0", chunk))?;
70-
self.client
52+
Ok(self
53+
.client
7154
.request(Method::GET, api_url)
7255
.bearer_auth(&self.auth_token)
7356
.send()?
74-
.json()
75-
.map_err(Error::RequestError)
57+
.json()?)
7658
}
7759
}
7860

@@ -286,7 +268,7 @@ impl From<SentryFragmentLog> for LogCmpFields {
286268
}
287269

288270
impl FromStr for SentryFragmentLog {
289-
type Err = Error;
271+
type Err = Report;
290272

291273
fn from_str(s: &str) -> Result<Self, Self::Err> {
292274
let parsed = sscanf::scanf!(
@@ -320,7 +302,7 @@ impl FromStr for SentryFragmentLog {
320302
fragment_id,
321303
},
322304
)
323-
.ok_or_else(|| Error::LogParseError(s.to_string()))
305+
.ok_or_else(|| color_eyre::eyre::eyre!("log parse error: {s}"))
324306
}
325307
}
326308

catalyst-toolbox/src/stats/archive/calculator.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::loader::ArchiverRecord;
2-
use chain_impl_mockchain::block::BlockDateParseError;
32
use chain_time::TimeEra;
3+
use color_eyre::Report;
44
use itertools::Itertools;
55
use jormungandr_lib::interfaces::BlockDate;
66
use std::collections::BTreeMap;
77
use std::collections::BTreeSet;
8-
use thiserror::Error;
98

109
pub struct ArchiveStats {
1110
records: Vec<ArchiverRecord>,
@@ -59,7 +58,7 @@ impl ArchiveStats {
5958
pub fn max_batch_size_per_caster(
6059
&self,
6160
slots_in_epoch: u32,
62-
) -> Result<BTreeMap<String, usize>, ArchiveCalculatorError> {
61+
) -> Result<BTreeMap<String, usize>, Report> {
6362
let time_era = self.records[0].time.time_era(slots_in_epoch);
6463

6564
Ok(self
@@ -96,14 +95,3 @@ fn are_equal_or_adjacent(left: &BlockDate, right: &BlockDate, time_era: &TimeEra
9695
left == right || left.clone().shift_slot(1, time_era) == *right
9796
}
9897

99-
#[derive(Debug, Error)]
100-
pub enum ArchiveCalculatorError {
101-
#[error("general error")]
102-
General(#[from] std::io::Error),
103-
#[error("cannot calculate distribution: cannot calculate max element result is empty")]
104-
EmptyResult,
105-
#[error("csv error")]
106-
Csv(#[from] csv::Error),
107-
#[error("block date error")]
108-
BlockDateParse(#[from] BlockDateParseError),
109-
}

catalyst-toolbox/src/stats/archive/loader.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use color_eyre::eyre::bail;
2+
use color_eyre::Report;
13
use csv;
24
use jormungandr_lib::interfaces::BlockDate;
35
use serde::Deserialize;
46
use std::{ffi::OsStr, fmt, path::Path};
5-
use thiserror::Error;
67

78
#[derive(Debug, Deserialize)]
89
pub struct ArchiverRecord {
@@ -42,9 +43,7 @@ where
4243
deserializer.deserialize_f64(VoteOptionsDeserializer())
4344
}
4445

45-
pub fn load_from_csv<P: AsRef<Path>>(
46-
csv_path: P,
47-
) -> Result<Vec<ArchiverRecord>, ArchiveReaderError> {
46+
pub fn load_from_csv<P: AsRef<Path>>(csv_path: P) -> Result<Vec<ArchiverRecord>, Report> {
4847
let csv_path = csv_path.as_ref();
4948

5049
let mut reader = csv::ReaderBuilder::new()
@@ -61,15 +60,13 @@ pub fn load_from_csv<P: AsRef<Path>>(
6160
Ok(data) => {
6261
results.push(data);
6362
}
64-
Err(e) => return Err(ArchiveReaderError::Csv(e)),
63+
Err(e) => bail!("bad csv: {e}"),
6564
}
6665
}
6766
Ok(results)
6867
}
6968

70-
pub fn load_from_folder<P: AsRef<Path>>(
71-
folder_path: P,
72-
) -> Result<Vec<ArchiverRecord>, ArchiveReaderError> {
69+
pub fn load_from_folder<P: AsRef<Path>>(folder_path: P) -> Result<Vec<ArchiverRecord>, Report> {
7370
let mut records = Vec::new();
7471

7572
for entry in std::fs::read_dir(folder_path)? {
@@ -83,11 +80,3 @@ pub fn load_from_folder<P: AsRef<Path>>(
8380
}
8481
Ok(records)
8582
}
86-
87-
#[derive(Debug, Error)]
88-
pub enum ArchiveReaderError {
89-
#[error("general error")]
90-
General(#[from] std::io::Error),
91-
#[error("csv error")]
92-
Csv(#[from] csv::Error),
93-
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod calculator;
22
mod loader;
33

4-
pub use calculator::{ArchiveCalculatorError, ArchiveStats};
5-
pub use loader::{load_from_csv, load_from_folder, ArchiveReaderError};
4+
pub use calculator::ArchiveStats;
5+
pub use loader::{load_from_csv, load_from_folder};

catalyst-toolbox/src/stats/live/harvester.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use color_eyre::Report;
12
use jormungandr_automation::jormungandr::JormungandrRest;
23
use jormungandr_lib::interfaces::FragmentLog;
34
use serde_json;
@@ -20,7 +21,7 @@ impl Harvester {
2021
}
2122
}
2223

23-
pub fn harvest(&self) -> Result<Snapshot, super::Error> {
24+
pub fn harvest(&self) -> Result<Snapshot, Report> {
2425
let mut votes_count: usize = 0;
2526

2627
for vote_status in self.rest.vote_plan_statuses()? {
@@ -41,7 +42,7 @@ impl Harvester {
4142
})
4243
}
4344

44-
pub fn fragment_logs(&self) -> Result<Vec<FragmentLog>, super::Error> {
45+
pub fn fragment_logs(&self) -> Result<Vec<FragmentLog>, Report> {
4546
let client = reqwest::blocking::Client::builder()
4647
.timeout(self.timeout)
4748
.build()?;

catalyst-toolbox/src/stats/live/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,3 @@ pub use harvester::Harvester;
66
pub use monitor::start;
77
pub use settings::Settings;
88

9-
use thiserror::Error;
10-
11-
#[derive(Debug, Error)]
12-
pub enum Error {
13-
#[error(transparent)]
14-
Rest(#[from] jormungandr_automation::jormungandr::RestError),
15-
#[error(transparent)]
16-
Reqwest(#[from] reqwest::Error),
17-
#[error(transparent)]
18-
Serde(#[from] serde_json::Error),
19-
#[error(transparent)]
20-
Io(#[from] std::io::Error),
21-
}

0 commit comments

Comments
 (0)