Skip to content

Fix master certificate retrieval #1007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2023
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
2 changes: 1 addition & 1 deletion 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.3.36"
version = "0.3.37"
description = "A Mithril Aggregator server"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
142 changes: 120 additions & 22 deletions mithril-aggregator/src/database/provider/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,34 @@ impl From<CertificateRecord> for Certificate {
other.sealed_at,
other.signers,
);
let signed_message = other.protocol_message.compute_hash();

if other.parent_certificate_id.is_none() {
// Genesis certificate
Certificate::new(
"".to_string(),
other.beacon,
certificate_metadata,
other.protocol_message,
other.aggregate_verification_key,
"".to_string(),
other.signature,
)
Certificate {
hash: other.certificate_id,
previous_hash: "".to_string(),
beacon: other.beacon,
metadata: certificate_metadata,
protocol_message: other.protocol_message,
signed_message,
aggregate_verification_key: other.aggregate_verification_key,
multi_signature: "".to_string(),
genesis_signature: other.signature,
}
} else {
// Multi-signature certificate
Certificate::new(
other.parent_certificate_id.unwrap(),
other.beacon,
certificate_metadata,
other.protocol_message,
other.aggregate_verification_key,
other.signature,
"".to_string(),
)
Certificate {
hash: other.certificate_id,
previous_hash: other.parent_certificate_id.unwrap(),
beacon: other.beacon,
metadata: certificate_metadata,
protocol_message: other.protocol_message,
signed_message,
aggregate_verification_key: other.aggregate_verification_key,
multi_signature: other.signature,
genesis_signature: "".to_string(),
}
}
}
}
Expand Down Expand Up @@ -447,6 +452,11 @@ impl<'conn> MasterCertificateProvider<'conn> {
"certificate.epoch between ?* and ?*",
vec![Value::Integer(epoch_i64 - 1), Value::Integer(epoch_i64)],
)
.and_where(
WhereCondition::new("certificate.parent_certificate_id is null", vec![]).or_where(
WhereCondition::new("certificate.epoch != parent_certificate.epoch", vec![]),
),
)
}
}

Expand All @@ -460,15 +470,19 @@ impl<'conn> Provider<'conn> for MasterCertificateProvider<'conn> {
fn get_definition(&self, condition: &str) -> String {
// it is important to alias the fields with the same name as the table
// since the table cannot be aliased in a RETURNING statement in SQLite.
let projection = Self::Entity::get_projection()
.expand(SourceAlias::new(&[("{:certificate:}", "certificate")]));
let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[
("{:certificate:}", "certificate"),
("{:parent_certificate:}", "parent_certificate"),
]));

format!(
r#"
select {projection}
from certificate
left join certificate as parent_certificate
on parent_certificate.certificate_id = certificate.parent_certificate_id
where {condition}
group by certificate.epoch order by certificate.epoch desc, certificate.ROWID asc"#
order by certificate.ROWID desc"#
)
}
}
Expand Down Expand Up @@ -738,6 +752,16 @@ mod tests {
assert_eq!(certificates, certificates_new);
}

#[test]
fn converting_certificate_record_to_certificate_should_not_recompute_hash() {
let expected_hash = "my_hash";
let record =
CertificateRecord::dummy_genesis(expected_hash, Beacon::new(String::new(), 1, 1));
let certificate: Certificate = record.into();

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

#[test]
fn projection() {
let projection = CertificateRecord::get_projection();
Expand Down Expand Up @@ -916,7 +940,7 @@ mod tests {
let (condition_str, parameters) = condition.expand();

assert_eq!(
"certificate.epoch between ?1 and ?2".to_string(),
"certificate.epoch between ?1 and ?2 and (certificate.parent_certificate_id is null or certificate.epoch != parent_certificate.epoch)".to_string(),
condition_str
);
assert_eq!(vec![Value::Integer(9), Value::Integer(10)], parameters);
Expand Down Expand Up @@ -1135,6 +1159,80 @@ mod tests {
assert_eq!(None, certificate);
}

#[tokio::test]
async fn get_master_certificate_second_genesis_after_multiple_cert_in_current_epoch_returns_last_genesis(
) {
let mut deps = DependenciesBuilder::new(Configuration::new_sample());
let connection = deps.get_sqlite_connection().await.unwrap();
let certificates = vec![
CertificateRecord::dummy_genesis("1", Beacon::new(String::new(), 1, 1)),
CertificateRecord::dummy("2", "1", Beacon::new(String::new(), 1, 2)),
CertificateRecord::dummy("3", "1", Beacon::new(String::new(), 1, 3)),
CertificateRecord::dummy_genesis("4", Beacon::new(String::new(), 1, 3)),
];
let expected_certificate: Certificate = certificates.last().unwrap().clone().into();
insert_certificate_records(connection.clone(), certificates).await;

let repository = CertificateRepository::new(connection);
let certificate = repository
.get_master_certificate_for_epoch(Epoch(2))
.await
.unwrap()
.expect("This should return a certificate.");

assert_eq!(expected_certificate, certificate);
}

#[tokio::test]
async fn get_master_certificate_second_genesis_after_multiple_cert_in_multiple_epochs_returns_last_genesis(
) {
let mut deps = DependenciesBuilder::new(Configuration::new_sample());
let connection = deps.get_sqlite_connection().await.unwrap();
let certificates = vec![
CertificateRecord::dummy_genesis("1", Beacon::new(String::new(), 1, 1)),
CertificateRecord::dummy("2", "1", Beacon::new(String::new(), 1, 2)),
CertificateRecord::dummy("3", "1", Beacon::new(String::new(), 1, 2)),
CertificateRecord::dummy("4", "1", Beacon::new(String::new(), 2, 4)),
CertificateRecord::dummy("5", "1", Beacon::new(String::new(), 2, 5)),
CertificateRecord::dummy_genesis("6", Beacon::new(String::new(), 2, 5)),
];
let expected_certificate: Certificate = certificates.last().unwrap().clone().into();
insert_certificate_records(connection.clone(), certificates).await;

let repository = CertificateRepository::new(connection);
let certificate = repository
.get_master_certificate_for_epoch(Epoch(2))
.await
.unwrap()
.expect("This should return a certificate.");

assert_eq!(expected_certificate, certificate);
}

#[tokio::test]
async fn get_master_certificate_new_genesis_after_multiple_cert_in_previous_epoch_returns_last_genesis(
) {
let mut deps = DependenciesBuilder::new(Configuration::new_sample());
let connection = deps.get_sqlite_connection().await.unwrap();
let certificates = vec![
CertificateRecord::dummy_genesis("1", Beacon::new(String::new(), 1, 1)),
CertificateRecord::dummy("2", "1", Beacon::new(String::new(), 1, 2)),
CertificateRecord::dummy("3", "1", Beacon::new(String::new(), 1, 3)),
CertificateRecord::dummy_genesis("4", Beacon::new(String::new(), 2, 3)),
];
let expected_certificate: Certificate = certificates.last().unwrap().clone().into();
insert_certificate_records(connection.clone(), certificates).await;

let repository = CertificateRepository::new(connection);
let certificate = repository
.get_master_certificate_for_epoch(Epoch(2))
.await
.unwrap()
.expect("This should return a certificate.");

assert_eq!(expected_certificate, certificate);
}

#[tokio::test]
async fn get_master_certificate_for_epoch() {
let (certificates, _) = setup_certificate_chain(3, 1);
Expand Down