Skip to content

Commit 827216c

Browse files
authored
Merge pull request #1229 from input-output-hk/damien/798/implement-anyhow-for-common-protocol
Implement anyhow context for common/protocol
2 parents a6b8cc8 + a07c297 commit 827216c

File tree

9 files changed

+33
-17
lines changed

9 files changed

+33
-17
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.3.88"
3+
version = "0.3.89"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/multi_signer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use async_trait::async_trait;
23
use slog_scope::{debug, warn};
34
use std::sync::Arc;
@@ -457,6 +458,7 @@ impl MultiSigner for MultiSignerImpl {
457458

458459
protocol_multi_signer
459460
.verify_single_signature(message, single_signature)
461+
.with_context(|| "Multi Signer can not verify multi-signature")
460462
.map_err(|error| ProtocolError::Core(error.to_string()))
461463
}
462464

mithril-common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.2.111"
3+
version = "0.2.112"
44
authors = { workspace = true }
55
edition = { workspace = true }
66
documentation = { workspace = true }

mithril-common/src/protocol/multi_signer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{anyhow, Context, Result};
1+
use anyhow::{anyhow, Context};
22
use mithril_stm::stm::StmParameters;
33

44
use crate::{
@@ -7,6 +7,7 @@ use crate::{
77
ProtocolMultiSignature,
88
},
99
entities::{ProtocolMessage, SingleSignatures},
10+
StdResult,
1011
};
1112

1213
/// MultiSigner is the cryptographic engine in charge of producing multi-signatures from individual signatures
@@ -52,7 +53,7 @@ impl MultiSigner {
5253
&self,
5354
message: &ProtocolMessage,
5455
single_signature: &SingleSignatures,
55-
) -> Result<()> {
56+
) -> StdResult<()> {
5657
let protocol_signature = single_signature.to_protocol_signature();
5758

5859
let avk = self.compute_aggregate_verification_key();

mithril-common/src/protocol/signer_builder.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Context, Result};
1+
use anyhow::Context;
22
use rand_chacha::ChaCha20Rng;
33
use rand_core::{CryptoRng, RngCore, SeedableRng};
44
use std::path::Path;
@@ -11,6 +11,7 @@ use crate::{
1111
},
1212
entities::{PartyId, ProtocolParameters, SignerWithStake},
1313
protocol::MultiSigner,
14+
StdResult,
1415
};
1516

1617
use super::SingleSigner;
@@ -35,7 +36,7 @@ impl SignerBuilder {
3536
pub fn new(
3637
registered_signers: &[SignerWithStake],
3738
protocol_parameters: &ProtocolParameters,
38-
) -> Result<Self> {
39+
) -> StdResult<Self> {
3940
if registered_signers.is_empty() {
4041
return Err(SignerBuilderError::EmptySigners.into());
4142
}
@@ -91,7 +92,7 @@ impl SignerBuilder {
9192
signer_with_stake: SignerWithStake,
9293
kes_secret_key_path: Option<&Path>,
9394
rng: &mut R,
94-
) -> Result<(SingleSigner, ProtocolInitializer)> {
95+
) -> StdResult<(SingleSigner, ProtocolInitializer)> {
9596
let protocol_initializer = ProtocolInitializer::setup(
9697
self.protocol_parameters.clone().into(),
9798
kes_secret_key_path,
@@ -127,7 +128,7 @@ impl SignerBuilder {
127128
&self,
128129
signer_with_stake: SignerWithStake,
129130
kes_secret_key_path: Option<&Path>,
130-
) -> Result<(SingleSigner, ProtocolInitializer)> {
131+
) -> StdResult<(SingleSigner, ProtocolInitializer)> {
131132
self.build_single_signer_with_rng(
132133
signer_with_stake,
133134
kes_secret_key_path,
@@ -142,7 +143,7 @@ impl SignerBuilder {
142143
&self,
143144
signer_with_stake: SignerWithStake,
144145
kes_secret_key_path: Option<&Path>,
145-
) -> Result<(SingleSigner, ProtocolInitializer)> {
146+
) -> StdResult<(SingleSigner, ProtocolInitializer)> {
146147
let protocol_initializer_seed: [u8; 32] = signer_with_stake.party_id.as_bytes()[..32]
147148
.try_into()
148149
.unwrap();
@@ -169,7 +170,7 @@ impl SignerBuilder {
169170
&self,
170171
party_id: PartyId,
171172
protocol_initializer: ProtocolInitializer,
172-
) -> Result<SingleSigner> {
173+
) -> StdResult<SingleSigner> {
173174
let single_signer = protocol_initializer
174175
.new_signer(self.closed_key_registration.clone())
175176
.with_context(|| {

mithril-common/src/protocol/single_signer.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use anyhow::Result;
2-
31
use crate::{
42
crypto_helper::ProtocolSigner,
53
entities::{PartyId, ProtocolMessage, SingleSignatures},
4+
StdResult,
65
};
76

87
/// The SingleSigner is the structure responsible for issuing SingleSignatures.
@@ -23,7 +22,7 @@ impl SingleSigner {
2322
/// Issue a single signature for the given message.
2423
///
2524
/// If no lottery are won None will be returned.
26-
pub fn sign(&self, message: &ProtocolMessage) -> Result<Option<SingleSignatures>> {
25+
pub fn sign(&self, message: &ProtocolMessage) -> StdResult<Option<SingleSignatures>> {
2726
match self.protocol_signer.sign(message.compute_hash().as_bytes()) {
2827
Some(signature) => {
2928
let won_indexes = signature.indexes.clone();

mithril-signer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-signer"
3-
version = "0.2.77"
3+
version = "0.2.78"
44
description = "A Mithril Signer"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-signer/src/single_signer.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use hex::ToHex;
23
use slog_scope::{info, trace, warn};
34
use std::path::PathBuf;
@@ -102,8 +103,20 @@ impl SingleSigner for MithrilSingleSigner {
102103
info!("Signing protocol message"; "protocol_message" => #?protocol_message, "signed message" => protocol_message.compute_hash().encode_hex::<String>());
103104
let signatures = builder
104105
.restore_signer_from_initializer(self.party_id.clone(), protocol_initializer.clone())
106+
.with_context(|| {
107+
format!(
108+
"Mithril Single Signer can not restore signer with party_id: '{}'",
109+
self.party_id.clone()
110+
)
111+
})
105112
.map_err(|e| SingleSignerError::ProtocolSignerCreationFailure(e.to_string()))?
106113
.sign(protocol_message)
114+
.with_context(|| {
115+
format!(
116+
"Mithril Single Signer can not sign protocol_message: '{:?}'",
117+
protocol_message
118+
)
119+
})
107120
.map_err(SingleSignerError::SignatureFailed)?;
108121

109122
match &signatures {

0 commit comments

Comments
 (0)