Skip to content

Commit 0c5aff3

Browse files
committed
Fix master certificate retrieval
1 parent e4f72bf commit 0c5aff3

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

mithril-aggregator/src/database/provider/certificate.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use sqlite::{Connection, Value};
55
use async_trait::async_trait;
66

77
use mithril_common::{
8+
certificate_chain::{CertificateRetriever, CertificateRetrieverError},
89
entities::{
910
Beacon, Certificate, CertificateMetadata, Epoch, HexEncodedAgregateVerificationKey,
1011
HexEncodedKey, ProtocolMessage, ProtocolParameters, ProtocolVersion, SignerWithStake,
@@ -405,9 +406,10 @@ impl<'conn> MasterCertificateProvider<'conn> {
405406
WhereCondition::new("parent_certificate.epoch != certificate.epoch", Vec::new()),
406407
);
407408

409+
let epoch_i64: i64 = epoch.0.try_into().unwrap();
408410
WhereCondition::new(
409-
"certificate.epoch = ?*",
410-
vec![Value::Integer(epoch.0.try_into().unwrap())],
411+
"certificate.epoch between ?* and ?*",
412+
vec![Value::Integer(epoch_i64 - 1), Value::Integer(epoch_i64)],
411413
)
412414
.and_where(condition)
413415
}
@@ -488,6 +490,21 @@ impl CertificateRepository {
488490
}
489491
}
490492

493+
#[async_trait]
494+
impl CertificateRetriever for CertificateRepository {
495+
async fn get_certificate_details(
496+
&self,
497+
certificate_hash: &str,
498+
) -> Result<Certificate, CertificateRetrieverError> {
499+
self.get_certificate(certificate_hash)
500+
.await
501+
.map_err(|e| CertificateRetrieverError::General(e.to_string()))?
502+
.ok_or(CertificateRetrieverError::General(
503+
"certificate does not exist".to_string(),
504+
))
505+
}
506+
}
507+
491508
/// Service to deal with certificate (read & write).
492509
pub struct CertificateStoreAdapter {
493510
connection: Arc<Mutex<Connection>>,
@@ -855,8 +872,8 @@ mod tests {
855872
let condition = provider.get_master_certificate_condition(Epoch(10));
856873
let (condition_str, parameters) = condition.expand();
857874

858-
assert_eq!("certificate.epoch = ?1 and (certificate.parent_certificate_id is null or parent_certificate.epoch != certificate.epoch)".to_string(), condition_str);
859-
assert_eq!(Value::Integer(10), parameters[0]);
875+
assert_eq!("certificate.epoch between ?1 and ?2 and (certificate.parent_certificate_id is null or parent_certificate.epoch != certificate.epoch)".to_string(), condition_str);
876+
assert_eq!(vec![Value::Integer(9), Value::Integer(10)], parameters);
860877
}
861878

862879
#[tokio::test]
@@ -889,7 +906,7 @@ mod tests {
889906

890907
#[tokio::test]
891908
async fn get_master_certificate_for_epoch() {
892-
let (certificates, _) = setup_certificate_chain(5, 3);
909+
let (certificates, _) = setup_certificate_chain(14, 3);
893910
let expected_certificate_id = &certificates[2].hash;
894911
let epoch = &certificates[2].beacon.epoch;
895912
let mut deps = DependenciesBuilder::new(Configuration::new_sample());

0 commit comments

Comments
 (0)