Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-aggregator"
version = "0.7.76"
version = "0.7.77"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests {
let expected_certificate_records: Vec<CertificateRecord> = certificates
.reversed_chain()
.into_iter()
.filter_map(|c| (c.epoch == Epoch(1)).then_some(c.to_owned().into()))
.filter_map(|c| (c.epoch == Epoch(1)).then_some(c.to_owned().try_into().unwrap()))
.collect();
assert_eq!(expected_certificate_records, certificate_records);

Expand All @@ -92,7 +92,7 @@ mod tests {
let expected_certificate_records: Vec<CertificateRecord> = certificates
.reversed_chain()
.into_iter()
.filter_map(|c| (c.epoch == Epoch(3)).then_some(c.to_owned().into()))
.filter_map(|c| (c.epoch == Epoch(3)).then_some(c.to_owned().try_into().unwrap()))
.collect();
assert_eq!(expected_certificate_records, certificate_records);

Expand All @@ -105,8 +105,11 @@ mod tests {
#[test]
fn test_get_all_certificate_records() {
let certificates = setup_certificate_chain(5, 2);
let expected_certificate_records: Vec<CertificateRecord> =
certificates.reversed_chain().into_iter().map(Into::into).collect();
let expected_certificate_records: Vec<CertificateRecord> = certificates
.reversed_chain()
.into_iter()
.map(|c| c.try_into().unwrap())
.collect();

let connection = main_db_connection().unwrap();
insert_certificate_records(&connection, certificates.certificates_chained.clone());
Expand All @@ -127,8 +130,11 @@ mod tests {
phi_f: 0.65,
})
.build();
let first_chain_genesis: CertificateRecord =
first_certificates_chain.genesis_certificate().clone().into();
let first_chain_genesis: CertificateRecord = first_certificates_chain
.genesis_certificate()
.clone()
.try_into()
.unwrap();
let second_certificates_chain = CertificateChainBuilder::new()
.with_total_certificates(2)
.with_protocol_parameters(ProtocolParameters {
Expand All @@ -137,8 +143,11 @@ mod tests {
phi_f: 0.65,
})
.build();
let second_chain_genesis: CertificateRecord =
second_certificates_chain.genesis_certificate().clone().into();
let second_chain_genesis: CertificateRecord = second_certificates_chain
.genesis_certificate()
.clone()
.try_into()
.unwrap();
assert_ne!(first_chain_genesis, second_chain_genesis);

let connection = main_db_connection().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod tests {
let connection = main_db_connection().unwrap();

for certificate in certificates.certificates_chained {
let certificate_record: CertificateRecord = certificate.into();
let certificate_record: CertificateRecord = certificate.try_into().unwrap();
let certificate_record_saved = connection
.fetch_first(InsertCertificateRecordQuery::one(
certificate_record.clone(),
Expand All @@ -67,7 +67,7 @@ mod tests {
#[test]
fn test_insert_many_certificates_records() {
let certificates = setup_certificate_chain(5, 2);
let certificates_records: Vec<CertificateRecord> = certificates.into();
let certificates_records: Vec<CertificateRecord> = certificates.try_into().unwrap();

let connection = main_db_connection().unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mod tests {
#[test]
fn test_insert_many_certificates_records_in_empty_db() {
let certificates = setup_certificate_chain(5, 2);
let certificates_records: Vec<CertificateRecord> = certificates.into();
let certificates_records: Vec<CertificateRecord> = certificates.try_into().unwrap();

let connection = main_db_connection().unwrap();

Expand All @@ -75,7 +75,7 @@ mod tests {
fn test_replace_one_certificate_record() {
let certificate_record = CertificateRecord {
epoch: Epoch(12),
..fake_data::certificate("hash").into()
..fake_data::certificate("hash").try_into().unwrap()
};

let connection = main_db_connection().unwrap();
Expand Down Expand Up @@ -106,26 +106,26 @@ mod tests {
let tested_records: HashMap<_, CertificateRecord> = HashMap::from([
(
"cert1-genesis",
fake_data::genesis_certificate("genesis").into(),
fake_data::genesis_certificate("genesis").try_into().unwrap(),
),
("cert2", fake_data::certificate("cert2").into()),
("cert2", fake_data::certificate("cert2").try_into().unwrap()),
(
"cert2-modified",
CertificateRecord {
epoch: Epoch(14),
..fake_data::certificate("cert2").into()
..fake_data::certificate("cert2").try_into().unwrap()
},
),
("cert3", fake_data::certificate("cert3").into()),
("cert4", fake_data::certificate("cert4").into()),
("cert3", fake_data::certificate("cert3").try_into().unwrap()),
("cert4", fake_data::certificate("cert4").try_into().unwrap()),
(
"cert4-modified",
CertificateRecord {
epoch: Epoch(32),
..fake_data::certificate("cert4").into()
..fake_data::certificate("cert4").try_into().unwrap()
},
),
("cert5", fake_data::certificate("cert5").into()),
("cert5", fake_data::certificate("cert5").try_into().unwrap()),
]);
let connection = main_db_connection().unwrap();

Expand Down
45 changes: 26 additions & 19 deletions mithril-aggregator/src/database/record/certificate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::{DateTime, Utc};

use mithril_common::StdError;
use mithril_common::entities::{
Certificate, CertificateMetadata, CertificateSignature, Epoch,
HexEncodedAggregateVerificationKey, HexEncodedKey, ProtocolMessage, ProtocolParameters,
Expand Down Expand Up @@ -127,24 +128,24 @@ impl CertificateRecord {
}
}

impl From<Certificate> for CertificateRecord {
fn from(other: Certificate) -> Self {
impl TryFrom<Certificate> for CertificateRecord {
type Error = StdError;

fn try_from(other: Certificate) -> Result<Self, Self::Error> {
let signed_entity_type = other.signed_entity_type();
let (signature, parent_certificate_id) = match other.signature {
CertificateSignature::GenesisSignature(signature) => {
(signature.to_bytes_hex().unwrap(), None)
}
CertificateSignature::GenesisSignature(signature) => (signature.to_bytes_hex()?, None),
CertificateSignature::MultiSignature(_, signature) => {
(signature.to_json_hex().unwrap(), Some(other.previous_hash))
(signature.to_json_hex()?, Some(other.previous_hash))
}
};

CertificateRecord {
let certificate_record = CertificateRecord {
certificate_id: other.hash,
parent_certificate_id,
message: other.signed_message,
signature,
aggregate_verification_key: other.aggregate_verification_key.to_json_hex().unwrap(),
aggregate_verification_key: other.aggregate_verification_key.to_json_hex()?,
epoch: other.epoch,
network: other.metadata.network,
signed_entity_type,
Expand All @@ -154,12 +155,16 @@ impl From<Certificate> for CertificateRecord {
signers: other.metadata.signers,
initiated_at: other.metadata.initiated_at,
sealed_at: other.metadata.sealed_at,
}
};

Ok(certificate_record)
}
}

impl From<CertificateRecord> for Certificate {
fn from(other: CertificateRecord) -> Self {
impl TryFrom<CertificateRecord> for Certificate {
type Error = StdError;

fn try_from(other: CertificateRecord) -> Result<Self, Self::Error> {
let certificate_metadata = CertificateMetadata::new(
other.network,
other.protocol_version,
Expand All @@ -171,27 +176,29 @@ impl From<CertificateRecord> for Certificate {
let (previous_hash, signature) = match other.parent_certificate_id {
None => (
String::new(),
CertificateSignature::GenesisSignature(other.signature.try_into().unwrap()),
CertificateSignature::GenesisSignature(other.signature.try_into()?),
),
Some(parent_certificate_id) => (
parent_certificate_id,
CertificateSignature::MultiSignature(
other.signed_entity_type,
other.signature.try_into().unwrap(),
other.signature.try_into()?,
),
),
};

Certificate {
let certificate = Certificate {
hash: other.certificate_id,
previous_hash,
epoch: other.epoch,
metadata: certificate_metadata,
signed_message: other.protocol_message.compute_hash(),
protocol_message: other.protocol_message,
aggregate_verification_key: other.aggregate_verification_key.try_into().unwrap(),
aggregate_verification_key: other.aggregate_verification_key.try_into()?,
signature,
}
};

Ok(certificate)
}
}

Expand Down Expand Up @@ -393,11 +400,11 @@ mod tests {
let certificates = setup_certificate_chain(20, 3);
let mut certificate_records: Vec<CertificateRecord> = Vec::new();
for certificate in certificates.certificates_chained.clone() {
certificate_records.push(certificate.into());
certificate_records.push(certificate.try_into().unwrap());
}
let mut certificates_new: Vec<Certificate> = Vec::new();
for certificate_record in certificate_records {
certificates_new.push(certificate_record.into());
certificates_new.push(certificate_record.try_into().unwrap());
}
assert_eq!(certificates.certificates_chained, certificates_new);
}
Expand All @@ -406,7 +413,7 @@ mod tests {
fn converting_certificate_record_to_certificate_should_not_recompute_hash() {
let expected_hash = "my_hash";
let record = CertificateRecord::dummy_genesis(expected_hash, Epoch(1));
let certificate: Certificate = record.into();
let certificate: Certificate = record.try_into().unwrap();

assert_eq!(expected_hash, &certificate.hash);
}
Expand Down
Loading