Skip to content

Commit c590faa

Browse files
authored
Merge pull request #1193 from input-output-hk/ensemble/798/errors-refactoring
Add common error: CodecError
2 parents 3673209 + aa1de1e commit c590faa

File tree

7 files changed

+59
-40
lines changed

7 files changed

+59
-40
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-common/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.2.98"
3+
version = "0.2.99"
44
authors = { workspace = true }
55
edition = { workspace = true }
66
documentation = { workspace = true }

mithril-common/src/crypto_helper/codec.rs

+33-7
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,55 @@ use crate::entities::{HexEncodedKey, HexEncodedKeySlice};
33
use hex::{FromHex, ToHex};
44
use serde::de::DeserializeOwned;
55
use serde::Serialize;
6+
use thiserror::Error;
7+
8+
/// Error raised when the encoding or decoding fails
9+
#[derive(Error, Debug)]
10+
#[error("Codec error: {msg}")]
11+
pub struct CodecError {
12+
msg: String,
13+
#[source]
14+
source: anyhow::Error,
15+
}
16+
17+
impl CodecError {
18+
/// [CodecError] factory.
19+
pub fn new(msg: &str, source: anyhow::Error) -> Self {
20+
Self {
21+
msg: msg.to_string(),
22+
source,
23+
}
24+
}
25+
}
626

727
/// Encode key to hex helper
8-
pub fn key_encode_hex<T>(from: T) -> Result<HexEncodedKey, String>
28+
pub fn key_encode_hex<T>(from: T) -> Result<HexEncodedKey, CodecError>
929
where
1030
T: Serialize,
1131
{
1232
Ok(serde_json::to_string(&from)
13-
.map_err(|e| format!("can't convert to hex: {e}"))?
33+
.map_err(|e| CodecError::new("Key encode hex: can not convert to hex", e.into()))?
1434
.encode_hex::<String>())
1535
}
1636

1737
/// Decode key from hex helper
18-
pub fn key_decode_hex<T>(from: HexEncodedKeySlice) -> Result<T, String>
38+
pub fn key_decode_hex<T>(from: HexEncodedKeySlice) -> Result<T, CodecError>
1939
where
2040
T: DeserializeOwned,
2141
{
2242
let from_vec = Vec::from_hex(from).map_err(|e| {
23-
format!("Key decode hex: can not turn hexadecimal value into bytes, error: {e}")
43+
CodecError::new(
44+
"Key decode hex: can not turn hexadecimal value into bytes",
45+
e.into(),
46+
)
2447
})?;
2548
serde_json::from_slice(from_vec.as_slice()).map_err(|e| {
26-
format!(
27-
"Key decode hex: can not deserialize to type '{}' from binary JSON: error {e}",
28-
std::any::type_name::<T>()
49+
CodecError::new(
50+
&format!(
51+
"Key decode hex: can not deserialize to type '{}' from binary JSON",
52+
std::any::type_name::<T>()
53+
),
54+
e.into(),
2955
)
3056
})
3157
}

mithril-common/src/crypto_helper/types/protocol_key.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{anyhow, Context};
1+
use anyhow::Context;
22
use serde::{de::DeserializeOwned, Deserialize, Serialize, Serializer};
33
use std::any::type_name;
44
use std::ops::Deref;
@@ -44,14 +44,12 @@ where
4444

4545
/// Create an instance from a JSON hex representation
4646
pub fn from_json_hex(hex_string: &str) -> StdResult<Self> {
47-
let key = key_decode_hex::<T>(hex_string)
48-
.map_err(|e| anyhow!(e))
49-
.with_context(|| {
50-
format!(
51-
"Could not deserialize a ProtocolKey from JSON hex string. Inner key type: {}",
52-
type_name::<T>()
53-
)
54-
})?;
47+
let key = key_decode_hex::<T>(hex_string).with_context(|| {
48+
format!(
49+
"Could not deserialize a ProtocolKey from JSON hex string. Inner key type: {}",
50+
type_name::<T>()
51+
)
52+
})?;
5553

5654
Ok(Self { key })
5755
}
@@ -63,14 +61,12 @@ where
6361

6462
/// Create a JSON hash representation of the given key
6563
pub fn key_to_json_hex(key: &T) -> StdResult<String> {
66-
key_encode_hex(key)
67-
.map_err(|e| anyhow!(e))
68-
.with_context(|| {
69-
format!(
70-
"Could not serialize a ProtocolKey to JSON hex key string. Inner key type: {}",
71-
type_name::<T>()
72-
)
73-
})
64+
key_encode_hex(key).with_context(|| {
65+
format!(
66+
"Could not serialize a ProtocolKey to JSON hex key string. Inner key type: {}",
67+
type_name::<T>()
68+
)
69+
})
7470
}
7571
}
7672

mithril-common/src/test_utils/fake_keys.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ mod test {
394394
encoded_types: &[&str],
395395
) {
396396
assert_decode_all(encoded_types, |encoded_type| {
397-
key_decode_hex::<T>(encoded_type).map(|_| ())
397+
key_decode_hex::<T>(encoded_type)
398+
.map(|_| ())
399+
.map_err(|e| format!("{e:?}"))
398400
})
399401
}
400402

mithril-signer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-signer"
3-
version = "0.2.71"
3+
version = "0.2.72"
44
description = "A Mithril Signer"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-signer/src/single_signer.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use mithril_common::entities::{
88
PartyId, ProtocolMessage, ProtocolParameters, SignerWithStake, SingleSignatures, Stake,
99
};
1010
use mithril_common::protocol::SignerBuilder;
11-
use mithril_common::StdError;
11+
use mithril_common::{StdError, StdResult};
1212

1313
#[cfg(test)]
1414
use mockall::automock;
@@ -46,14 +46,14 @@ pub trait SingleSigner: Sync + Send {
4646
protocol_message: &ProtocolMessage,
4747
signers_with_stake: &[SignerWithStake],
4848
protocol_initializer: &ProtocolInitializer,
49-
) -> Result<Option<SingleSignatures>, SingleSignerError>;
49+
) -> StdResult<Option<SingleSignatures>>;
5050

5151
/// Compute aggregate verification key from stake distribution
5252
fn compute_aggregate_verification_key(
5353
&self,
5454
signers_with_stake: &[SignerWithStake],
5555
protocol_initializer: &ProtocolInitializer,
56-
) -> Result<Option<String>, SingleSignerError>;
56+
) -> StdResult<Option<String>>;
5757

5858
/// Get party id
5959
fn get_party_id(&self) -> PartyId;
@@ -66,10 +66,6 @@ pub enum SingleSignerError {
6666
#[error("the protocol signer creation failed: `{0}`")]
6767
ProtocolSignerCreationFailure(String),
6868

69-
/// Encoding / Decoding error.
70-
#[error("codec error: '{0:?}'")]
71-
Codec(StdError),
72-
7369
/// Signature Error
7470
#[error("Signature Error: {0:?}")]
7571
SignatureFailed(StdError),
@@ -97,7 +93,7 @@ impl SingleSigner for MithrilSingleSigner {
9793
protocol_message: &ProtocolMessage,
9894
signers_with_stake: &[SignerWithStake],
9995
protocol_initializer: &ProtocolInitializer,
100-
) -> Result<Option<SingleSignatures>, SingleSignerError> {
96+
) -> StdResult<Option<SingleSignatures>> {
10197
let builder = SignerBuilder::new(
10298
signers_with_stake,
10399
&protocol_initializer.get_protocol_parameters().into(),
@@ -131,7 +127,7 @@ impl SingleSigner for MithrilSingleSigner {
131127
&self,
132128
signers_with_stake: &[SignerWithStake],
133129
protocol_initializer: &ProtocolInitializer,
134-
) -> Result<Option<String>, SingleSignerError> {
130+
) -> StdResult<Option<String>> {
135131
let signer_builder = SignerBuilder::new(
136132
signers_with_stake,
137133
&protocol_initializer.get_protocol_parameters().into(),
@@ -140,8 +136,7 @@ impl SingleSigner for MithrilSingleSigner {
140136

141137
let encoded_avk = signer_builder
142138
.compute_aggregate_verification_key()
143-
.to_json_hex()
144-
.map_err(SingleSignerError::Codec)?;
139+
.to_json_hex()?;
145140

146141
Ok(Some(encoded_avk))
147142
}

0 commit comments

Comments
 (0)