Skip to content

Commit

Permalink
refactor: rename AsMessage to ToMessage
Browse files Browse the repository at this point in the history
And the trait method from `message_string` to `to_message`.
  • Loading branch information
Alenar committed Sep 19, 2024
1 parent eeb23cf commit d06bb6e
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
12 changes: 6 additions & 6 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod tests {

use mithril_common::crypto_helper::tests_setup::*;
use mithril_common::entities::{CardanoDbBeacon, Epoch, SignedEntityType, SignerWithStake};
use mithril_common::protocol::AsMessage;
use mithril_common::protocol::ToMessage;
use mithril_common::test_utils::{fake_data, MithrilFixtureBuilder};

use crate::services::FakeEpochService;
Expand Down Expand Up @@ -186,11 +186,11 @@ mod tests {
let signature = fixture.signers_fixture()[0].sign(&message).unwrap();

multi_signer
.verify_single_signature(&message.message_string(), &signature)
.verify_single_signature(&message.to_message(), &signature)
.await
.unwrap();

multi_signer.verify_single_signature_for_next_epoch(&message.message_string(), &signature).await.expect_err(
multi_signer.verify_single_signature_for_next_epoch(&message.to_message(), &signature).await.expect_err(
"single signature issued in the current epoch should not be valid for the next epoch",
);
}
Expand All @@ -200,13 +200,13 @@ mod tests {

multi_signer
.verify_single_signature_for_next_epoch(
&message.message_string(),
&message.to_message(),
&next_epoch_signature,
)
.await
.unwrap();

multi_signer.verify_single_signature(&message.message_string(), &next_epoch_signature).await.expect_err(
multi_signer.verify_single_signature(&message.to_message(), &next_epoch_signature).await.expect_err(
"single signature issued in the next epoch should not be valid for the current epoch",
);
}
Expand Down Expand Up @@ -235,7 +235,7 @@ mod tests {

for signature in &signatures {
multi_signer
.verify_single_signature(&message.message_string(), signature)
.verify_single_signature(&message.to_message(), signature)
.await
.expect("single signature should be valid");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mithril_common::entities::{
Certificate, CertificateMetadata, CertificateSignature, Epoch, ProtocolMessage,
SignedEntityType, SingleSignatures, StakeDistributionParty,
};
use mithril_common::protocol::AsMessage;
use mithril_common::protocol::ToMessage;
use mithril_common::{CardanoNetwork, StdResult, TickerService};

use crate::database::record::{OpenMessageRecord, OpenMessageWithSingleSignaturesRecord};
Expand Down Expand Up @@ -130,7 +130,7 @@ impl CertifierService for MithrilCertifierService {
}

self.multi_signer
.verify_single_signature(&open_message.protocol_message.message_string(), signature)
.verify_single_signature(&open_message.protocol_message.to_message(), signature)
.await
.map_err(|err| {
CertifierServiceError::InvalidSingleSignature(signed_entity_type.clone(), err)
Expand Down
6 changes: 3 additions & 3 deletions mithril-common/src/certificate_chain/certificate_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
ProtocolMessage, ProtocolMessagePartKey, ProtocolParameters,
},
era_deprecate,
protocol::AsMessage,
protocol::ToMessage,
StdResult,
};

Expand Down Expand Up @@ -53,15 +53,15 @@ impl CertificateGenesisProducer {
}

/// Sign the Genesis protocol message (test only)
pub fn sign_genesis_protocol_message<T: AsMessage>(
pub fn sign_genesis_protocol_message<T: ToMessage>(
&self,
genesis_message: T,
) -> Result<ProtocolGenesisSignature, CertificateGenesisProducerError> {
Ok(self
.genesis_signer
.as_ref()
.ok_or_else(CertificateGenesisProducerError::MissingGenesisSigner)?
.sign(genesis_message.message_string().as_bytes()))
.sign(genesis_message.to_message().as_bytes()))
}

era_deprecate!("Remove immutable_file_number");
Expand Down
6 changes: 3 additions & 3 deletions mithril-common/src/entities/protocol_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{collections::BTreeMap, fmt::Display};

use crate::protocol::AsMessage;
use crate::protocol::ToMessage;

/// The key of a ProtocolMessage
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -97,8 +97,8 @@ impl ProtocolMessage {
}
}

impl AsMessage for ProtocolMessage {
fn message_string(&self) -> String {
impl ToMessage for ProtocolMessage {
fn to_message(&self) -> String {
self.compute_hash()
}
}
Expand Down
12 changes: 6 additions & 6 deletions mithril-common/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ pub use signer_builder::{SignerBuilder, SignerBuilderError};
pub use single_signer::SingleSigner;

/// Trait to convert a type to a message that can be signed or verified by the Mithril protocol.
pub trait AsMessage: Sync + Send {
pub trait ToMessage: Sync + Send {
/// Return a String representation of the message.
fn message_string(&self) -> String;
fn to_message(&self) -> String;
}

impl AsMessage for String {
fn message_string(&self) -> String {
impl ToMessage for String {
fn to_message(&self) -> String {
self.clone()
}
}

impl AsMessage for &str {
fn message_string(&self) -> String {
impl ToMessage for &str {
fn to_message(&self) -> String {
self.to_string()
}
}
10 changes: 5 additions & 5 deletions mithril-common/src/protocol/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
ProtocolMultiSignature,
},
entities::SingleSignatures,
protocol::AsMessage,
protocol::ToMessage,
StdResult,
};

Expand All @@ -26,7 +26,7 @@ impl MultiSigner {
}

/// Aggregate the given single signatures into a multi-signature
pub fn aggregate_single_signatures<T: AsMessage>(
pub fn aggregate_single_signatures<T: ToMessage>(
&self,
single_signatures: &[SingleSignatures],
message: &T,
Expand All @@ -37,7 +37,7 @@ impl MultiSigner {
.collect();

self.protocol_clerk
.aggregate(&protocol_signatures, message.message_string().as_bytes())
.aggregate(&protocol_signatures, message.to_message().as_bytes())
.map(|multi_sig| multi_sig.into())
}

Expand All @@ -47,7 +47,7 @@ impl MultiSigner {
}

/// Verify a single signature
pub fn verify_single_signature<T: AsMessage>(
pub fn verify_single_signature<T: ToMessage>(
&self,
message: &T,
single_signature: &SingleSignatures,
Expand All @@ -74,7 +74,7 @@ impl MultiSigner {
&vk,
&stake,
&avk,
message.message_string().as_bytes(),
message.to_message().as_bytes(),
)
.with_context(|| {
format!(
Expand Down
6 changes: 3 additions & 3 deletions mithril-common/src/protocol/single_signer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
crypto_helper::ProtocolSigner,
entities::{PartyId, SingleSignatures},
protocol::AsMessage,
protocol::ToMessage,
StdResult,
};

Expand All @@ -23,8 +23,8 @@ impl SingleSigner {
/// Issue a single signature for the given message.
///
/// If no lottery are won None will be returned.
pub fn sign<T: AsMessage>(&self, message: &T) -> StdResult<Option<SingleSignatures>> {
let signed_message = message.message_string();
pub fn sign<T: ToMessage>(&self, message: &T) -> StdResult<Option<SingleSignatures>> {
let signed_message = message.to_message();
match self.protocol_signer.sign(signed_message.as_bytes()) {
Some(signature) => {
let won_indexes = signature.indexes.clone();
Expand Down
8 changes: 4 additions & 4 deletions mithril-common/src/test_utils/mithril_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
ProtocolParameters, Signer, SignerWithStake, SingleSignatures, Stake, StakeDistribution,
StakeDistributionParty,
},
protocol::{AsMessage, SignerBuilder},
protocol::{SignerBuilder, ToMessage},
};

/// A fixture of Mithril data types.
Expand Down Expand Up @@ -199,7 +199,7 @@ impl MithrilFixture {

/// Make all underlying signers sign the given message, filter the resulting list to remove
/// the signers that did not sign because they loosed the lottery.
pub fn sign_all<T: AsMessage>(&self, message: &T) -> Vec<SingleSignatures> {
pub fn sign_all<T: ToMessage>(&self, message: &T) -> Vec<SingleSignatures> {
self.signers
.par_iter()
.filter_map(|s| s.sign(message))
Expand Down Expand Up @@ -227,8 +227,8 @@ impl From<MithrilFixture> for Vec<SignerFixture> {

impl SignerFixture {
/// Sign the given protocol message.
pub fn sign<T: AsMessage>(&self, message: &T) -> Option<SingleSignatures> {
let message = message.message_string();
pub fn sign<T: ToMessage>(&self, message: &T) -> Option<SingleSignatures> {
let message = message.to_message();
self.protocol_signer
.sign(message.as_bytes())
.map(|signature| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Context;
use mithril_common::entities::{ProtocolMessage, SignedEntityType, SingleSignatures};
use mithril_common::messages::{RegisterSignatureMessage, TryToMessageAdapter};
use mithril_common::protocol::AsMessage;
use mithril_common::protocol::ToMessage;
use mithril_common::StdResult;

pub struct ToRegisterSignatureMessageAdapter;
Expand All @@ -26,7 +26,7 @@ impl
"'ToRegisterSignatureMessageAdapter' can not convert the single signature"
})?,
won_indexes: single_signature.won_indexes,
signed_message: Some(protocol_message.message_string()),
signed_message: Some(protocol_message.to_message()),
};

Ok(message)
Expand All @@ -50,7 +50,7 @@ mod tests {

assert_eq!("party_id".to_string(), message.party_id);
assert_eq!(
Some(ProtocolMessage::default().message_string()),
Some(ProtocolMessage::default().to_message()),
message.signed_message
);
}
Expand Down

0 comments on commit d06bb6e

Please sign in to comment.