Skip to content

Commit ee4f537

Browse files
committed
fix: better error handling
1 parent c3c48e2 commit ee4f537

3 files changed

Lines changed: 30 additions & 43 deletions

File tree

crates/charon/src/obolapi/client.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ impl Client {
5757

5858
let req_timeout = options.timeout.unwrap_or(DEFAULT_TIMEOUT);
5959

60-
let http_client = reqwest::Client::builder()
61-
.timeout(req_timeout)
62-
.build()
63-
.map_err(Error::Reqwest)?;
60+
let http_client = reqwest::Client::builder().timeout(req_timeout).build()?;
6461

6562
Ok(Self {
6663
base_url: url_str.to_string(),

crates/charon/src/obolapi/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ pub enum Error {
6969
/// Epoch parsing error.
7070
#[error("epoch parsing error: {0}")]
7171
EpochParse(#[from] std::num::ParseIntError),
72+
73+
/// SSZ hasher error.
74+
#[error("SSZ hasher error: {0}")]
75+
HasherError(#[from] charon_cluster::ssz_hasher::HasherError),
7276
}

crates/charon/src/obolapi/exit.rs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
1010

1111
use charon_cluster::{
1212
helpers::left_pad,
13-
ssz_hasher::{HashWalker, Hasher, HasherError},
13+
ssz_hasher::{HashWalker, Hasher},
1414
};
1515
use eth2api::types::{
1616
GetPoolVoluntaryExitsResponseResponseDatum, Phase0SignedVoluntaryExitMessage,
@@ -28,43 +28,40 @@ pub type SignedVoluntaryExit = GetPoolVoluntaryExitsResponseResponseDatum;
2828
/// Trait for types that can be hashed using SSZ hash tree root.
2929
pub trait SszHashable {
3030
/// Hashes this value into the provided hasher.
31-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()>;
31+
fn hash_with(&self, hh: &mut Hasher) -> Result<()>;
3232

3333
/// Computes the SSZ hash tree root of this value.
3434
fn hash_tree_root(&self) -> Result<[u8; 32]> {
3535
let mut hh = Hasher::default();
3636
self.hash_with(&mut hh)?;
37-
hh.hash_root().map_err(map_hasher_error)
37+
Ok(hh.hash_root()?)
3838
}
3939
}
4040

4141
impl SszHashable for SignedVoluntaryExit {
42-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()> {
42+
fn hash_with(&self, hh: &mut Hasher) -> Result<()> {
4343
let index = hh.index();
4444

4545
self.message.hash_with(hh)?;
4646
let sig_bytes = from_0x(&self.signature, SSZ_LEN_BLS_SIG)?;
4747
put_bytes_n(hh, &sig_bytes, SSZ_LEN_BLS_SIG)?;
4848

49-
hh.merkleize(index).map_err(map_walker_error)?;
49+
hh.merkleize(index)?;
5050
Ok(())
5151
}
5252
}
5353

5454
impl SszHashable for Phase0SignedVoluntaryExitMessage {
55-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()> {
55+
fn hash_with(&self, hh: &mut Hasher) -> Result<()> {
5656
let index = hh.index();
5757

58-
let epoch = self.epoch.parse::<u64>().map_err(Error::EpochParse)?;
59-
let validator_index = self
60-
.validator_index
61-
.parse::<u64>()
62-
.map_err(Error::EpochParse)?;
58+
let epoch = self.epoch.parse::<u64>()?;
59+
let validator_index = self.validator_index.parse::<u64>()?;
6360

64-
hh.put_uint64(epoch).map_err(map_walker_error)?;
65-
hh.put_uint64(validator_index).map_err(map_walker_error)?;
61+
hh.put_uint64(epoch)?;
62+
hh.put_uint64(validator_index)?;
6663

67-
hh.merkleize(index).map_err(map_walker_error)?;
64+
hh.merkleize(index)?;
6865
Ok(())
6966
}
7067
}
@@ -81,7 +78,7 @@ pub struct ExitBlob {
8178
}
8279

8380
impl SszHashable for ExitBlob {
84-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()> {
81+
fn hash_with(&self, hh: &mut Hasher) -> Result<()> {
8582
let index = hh.index();
8683

8784
let pk = self.public_key.as_ref().ok_or_else(|| {
@@ -91,11 +88,11 @@ impl SszHashable for ExitBlob {
9188
))
9289
})?;
9390
let pk_bytes = from_0x(pk, SSZ_LEN_PUB_KEY)?;
94-
hh.put_bytes(&pk_bytes).map_err(map_walker_error)?;
91+
hh.put_bytes(&pk_bytes)?;
9592

9693
self.signed_exit_message.hash_with(hh)?;
9794

98-
hh.merkleize(index).map_err(map_walker_error)?;
95+
hh.merkleize(index)?;
9996
Ok(())
10097
}
10198
}
@@ -106,16 +103,15 @@ impl SszHashable for ExitBlob {
106103
pub struct PartialExits(pub Vec<ExitBlob>);
107104

108105
impl SszHashable for PartialExits {
109-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()> {
106+
fn hash_with(&self, hh: &mut Hasher) -> Result<()> {
110107
let index = hh.index();
111108
let num = self.0.len();
112109

113110
for exit_blob in &self.0 {
114111
exit_blob.hash_with(hh)?;
115112
}
116113

117-
hh.merkleize_with_mixin(index, num, SSZ_MAX_EXITS)
118-
.map_err(map_walker_error)?;
114+
hh.merkleize_with_mixin(index, num, SSZ_MAX_EXITS)?;
119115
Ok(())
120116
}
121117
}
@@ -139,13 +135,13 @@ pub struct UnsignedPartialExitRequest {
139135
}
140136

141137
impl SszHashable for UnsignedPartialExitRequest {
142-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()> {
138+
fn hash_with(&self, hh: &mut Hasher) -> Result<()> {
143139
let index = hh.index();
144140

145141
self.partial_exits.hash_with(hh)?;
146-
hh.put_uint64(self.share_idx).map_err(map_walker_error)?;
142+
hh.put_uint64(self.share_idx)?;
147143

148-
hh.merkleize(index).map_err(map_walker_error)?;
144+
hh.merkleize(index)?;
149145
Ok(())
150146
}
151147
}
@@ -232,14 +228,14 @@ pub struct FullExitAuthBlob {
232228
}
233229

234230
impl SszHashable for FullExitAuthBlob {
235-
fn hash_with<H: HashWalker>(&self, hh: &mut H) -> Result<()> {
231+
fn hash_with(&self, hh: &mut Hasher) -> Result<()> {
236232
let index = hh.index();
237233

238-
hh.put_bytes(&self.lock_hash).map_err(map_walker_error)?;
234+
hh.put_bytes(&self.lock_hash)?;
239235
put_bytes_n(hh, &self.validator_pubkey, SSZ_LEN_PUB_KEY)?;
240-
hh.put_uint64(self.share_index).map_err(map_walker_error)?;
236+
hh.put_uint64(self.share_index)?;
241237

242-
hh.merkleize(index).map_err(map_walker_error)?;
238+
hh.merkleize(index)?;
243239
Ok(())
244240
}
245241
}
@@ -439,17 +435,7 @@ fn fetch_full_exit_url(val_pubkey: &str, lock_hash: &str, share_index: u64) -> S
439435
.replace(SHARE_INDEX_PATH, &share_index.to_string())
440436
}
441437

442-
fn map_hasher_error(err: HasherError) -> Error {
443-
use charon_cluster::ssz::SSZError;
444-
Error::Ssz(SSZError::HashWalkerError(err))
445-
}
446-
447-
fn map_walker_error<E: std::error::Error>(err: E) -> Error {
448-
use charon_cluster::ssz::SSZError;
449-
Error::Ssz(SSZError::UnsupportedVersion(err.to_string()))
450-
}
451-
452-
fn put_bytes_n<H: HashWalker>(hh: &mut H, bytes: &[u8], expected_len: usize) -> Result<()> {
438+
fn put_bytes_n(hh: &mut Hasher, bytes: &[u8], expected_len: usize) -> Result<()> {
453439
if bytes.len() > expected_len {
454440
use charon_cluster::ssz::SSZError;
455441
return Err(Error::Ssz(SSZError::UnsupportedVersion(format!(
@@ -459,7 +445,7 @@ fn put_bytes_n<H: HashWalker>(hh: &mut H, bytes: &[u8], expected_len: usize) ->
459445
))));
460446
}
461447
let padded: Vec<u8> = left_pad(bytes, expected_len);
462-
hh.put_bytes(&padded).map_err(map_walker_error)
448+
Ok(hh.put_bytes(&padded)?)
463449
}
464450

465451
#[cfg(test)]

0 commit comments

Comments
 (0)