Skip to content

Commit 5661561

Browse files
committed
Manually implement display for entities in common that own ProtocolKeys
This allow to simplify the default display by removing all of those keys from the output. We can still show them by using the "alternate"/pretty-print debug display.
1 parent a5545ac commit 5661561

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

mithril-common/src/entities/certificate.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::entities::{
33
Beacon, CertificateMetadata, HexEncodedAgregateVerificationKey, ProtocolMessage,
44
};
55
use std::cmp::Ordering;
6+
use std::fmt::{Debug, Formatter};
67

78
use sha2::{Digest, Sha256};
89

@@ -19,7 +20,7 @@ pub enum CertificateSignature {
1920
}
2021

2122
/// Certificate represents a Mithril certificate embedding a Mithril STM multisignature
22-
#[derive(Clone, Debug)]
23+
#[derive(Clone)]
2324
pub struct Certificate {
2425
/// Hash of the current certificate
2526
/// Computed from the other fields of the certificate
@@ -133,6 +134,34 @@ impl PartialOrd for Certificate {
133134
}
134135
}
135136

137+
impl Debug for Certificate {
138+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
139+
let should_be_exhaustive = f.alternate();
140+
let mut debug = f.debug_struct("Certificate");
141+
debug
142+
.field("hash", &self.hash)
143+
.field("previous_hash", &self.previous_hash)
144+
.field("beacon", &format_args!("{:?}", self.beacon))
145+
.field("metadata", &format_args!("{:?}", self.metadata))
146+
.field(
147+
"protocol_message",
148+
&format_args!("{:?}", self.protocol_message),
149+
)
150+
.field("signed_message", &self.signed_message);
151+
152+
match should_be_exhaustive {
153+
true => debug
154+
.field(
155+
"aggregate_verification_key",
156+
&format_args!("{:?}", self.aggregate_verification_key),
157+
)
158+
.field("signature", &format_args!("{:?}", self.signature))
159+
.finish(),
160+
false => debug.finish_non_exhaustive(),
161+
}
162+
}
163+
}
164+
136165
#[cfg(test)]
137166
mod tests {
138167
use super::*;

mithril-common/src/entities/signer.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use crate::{
55
},
66
entities::{PartyId, Stake},
77
};
8+
use std::fmt::{Debug, Formatter};
89

910
use serde::{Deserialize, Serialize};
1011
use sha2::{Digest, Sha256};
1112

1213
/// Signer represents a signing participant in the network
13-
#[derive(Clone, Debug, Eq, Serialize, Deserialize)]
14+
#[derive(Clone, Eq, Serialize, Deserialize)]
1415
pub struct Signer {
1516
/// The unique identifier of the signer
1617
// TODO: Should be removed once the signer certification is fully deployed
@@ -75,6 +76,33 @@ impl Signer {
7576
}
7677
}
7778

79+
impl Debug for Signer {
80+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
81+
let should_be_exhaustive = f.alternate();
82+
let mut debug = f.debug_struct("Signer");
83+
debug.field("party_id", &self.party_id);
84+
85+
match should_be_exhaustive {
86+
true => debug
87+
.field(
88+
"verification_key",
89+
&format_args!("{:?}", self.verification_key),
90+
)
91+
.field(
92+
"verification_key_signature",
93+
&format_args!("{:?}", self.verification_key_signature),
94+
)
95+
.field(
96+
"operational_certificate",
97+
&format_args!("{:?}", self.operational_certificate),
98+
)
99+
.field("kes_period", &format_args!("{:?}", self.kes_period))
100+
.finish(),
101+
false => debug.finish_non_exhaustive(),
102+
}
103+
}
104+
}
105+
78106
impl From<SignerWithStake> for Signer {
79107
fn from(other: SignerWithStake) -> Self {
80108
Signer::new(
@@ -88,7 +116,7 @@ impl From<SignerWithStake> for Signer {
88116
}
89117

90118
/// Signer represents a signing party in the network (including its stakes)
91-
#[derive(Clone, Debug, Eq, Serialize, Deserialize)]
119+
#[derive(Clone, Eq, Serialize, Deserialize)]
92120
pub struct SignerWithStake {
93121
/// The unique identifier of the signer
94122
// TODO: Should be removed once the signer certification is fully deployed
@@ -184,6 +212,35 @@ impl SignerWithStake {
184212
}
185213
}
186214

215+
impl Debug for SignerWithStake {
216+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
217+
let should_be_exhaustive = f.alternate();
218+
let mut debug = f.debug_struct("SignerWithStake");
219+
debug
220+
.field("party_id", &self.party_id)
221+
.field("stake", &self.stake);
222+
223+
match should_be_exhaustive {
224+
true => debug
225+
.field(
226+
"verification_key",
227+
&format_args!("{:?}", self.verification_key),
228+
)
229+
.field(
230+
"verification_key_signature",
231+
&format_args!("{:?}", self.verification_key_signature),
232+
)
233+
.field(
234+
"operational_certificate",
235+
&format_args!("{:?}", self.operational_certificate),
236+
)
237+
.field("kes_period", &format_args!("{:?}", self.kes_period))
238+
.finish(),
239+
false => debug.finish_non_exhaustive(),
240+
}
241+
}
242+
}
243+
187244
#[cfg(test)]
188245
mod tests {
189246
use crate::test_utils::{fake_keys, MithrilFixtureBuilder};

mithril-common/src/entities/single_signatures.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use mithril_stm::stm::StmSig;
22
use serde::{Deserialize, Serialize};
3+
use std::fmt::{Debug, Formatter};
34

45
use crate::{
56
crypto_helper::ProtocolSingleSignature,
@@ -8,7 +9,7 @@ use crate::{
89

910
/// SingleSignatures represent single signatures originating from a participant in the network
1011
/// for a digest at won lottery indexes
11-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12+
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
1213
pub struct SingleSignatures {
1314
/// The unique identifier of the signer
1415
pub party_id: PartyId,
@@ -41,6 +42,23 @@ impl SingleSignatures {
4142
}
4243
}
4344

45+
impl Debug for SingleSignatures {
46+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47+
let is_pretty_printing = f.alternate();
48+
let mut debug = f.debug_struct("SingleSignatures");
49+
debug
50+
.field("party_id", &self.party_id)
51+
.field("won_indexes", &format_args!("{:?}", self.won_indexes));
52+
53+
match is_pretty_printing {
54+
true => debug
55+
.field("signature", &format_args!("{:?}", self.signature))
56+
.finish(),
57+
false => debug.finish_non_exhaustive(),
58+
}
59+
}
60+
}
61+
4462
#[cfg(test)]
4563
mod tests {
4664
use super::*;

0 commit comments

Comments
 (0)