Skip to content

Commit d393eab

Browse files
authored
Merge pull request #1213 from input-output-hk/ensemble/798/implement-anyhow-for-aggregator-database
Implement anyhow context in mithril-aggregator database
2 parents 545bb7c + a3671ad commit d393eab

File tree

11 files changed

+157
-49
lines changed

11 files changed

+157
-49
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-aggregator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-aggregator"
3-
version = "0.3.84"
3+
version = "0.3.85"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ impl<'client> EpochSettingProvider<'client> {
8181
}
8282

8383
fn condition_by_epoch(&self, epoch: &Epoch) -> StdResult<WhereCondition> {
84-
let epoch_setting_id: i64 = epoch.try_into()?;
84+
let epoch_setting_id: i64 = epoch
85+
.try_into()
86+
.with_context(|| format!("Can not convert epoch: '{epoch}'"))?;
8587

8688
Ok(WhereCondition::new(
8789
"epoch_setting_id = ?*",

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use mithril_common::{
23
entities::{Epoch, ProtocolMessage, SignedEntityType, SingleSignatures},
34
sqlite::{HydrationError, Projection, Provider, SourceAlias, SqLiteEntity, WhereCondition},
@@ -170,15 +171,14 @@ impl<'client> OpenMessageProvider<'client> {
170171
fn get_signed_entity_type_condition(
171172
&self,
172173
signed_entity_type: &SignedEntityType,
173-
) -> WhereCondition {
174-
WhereCondition::new(
174+
) -> StdResult<WhereCondition> {
175+
Ok(WhereCondition::new(
175176
"signed_entity_type_id = ?* and beacon = ?*",
176177
vec![
177178
Value::Integer(signed_entity_type.index() as i64),
178-
// TODO!: Remove this ugly unwrap, should this method returns a result ?
179-
Value::String(signed_entity_type.get_json_beacon().unwrap()),
179+
Value::String(signed_entity_type.get_json_beacon()?),
180180
],
181-
)
181+
))
182182
}
183183

184184
// Useful in test and probably in the future.
@@ -268,7 +268,12 @@ signed_entity_type_id = ?*, protocol_message = ?*, is_certified = ?* \
268268
where open_message_id = ?*";
269269
let beacon_str = open_message.signed_entity_type.get_json_beacon()?;
270270
let parameters = vec![
271-
Value::Integer(open_message.epoch.try_into()?),
271+
Value::Integer(
272+
open_message
273+
.epoch
274+
.try_into()
275+
.with_context(|| format!("Can not convert epoch: '{}'", open_message.epoch))?,
276+
),
272277
Value::String(beacon_str),
273278
Value::Integer(open_message.signed_entity_type.index() as i64),
274279
Value::String(serde_json::to_string(&open_message.protocol_message)?),
@@ -434,14 +439,14 @@ impl<'client> OpenMessageWithSingleSignaturesProvider<'client> {
434439
fn get_signed_entity_type_condition(
435440
&self,
436441
signed_entity_type: &SignedEntityType,
437-
) -> WhereCondition {
438-
WhereCondition::new(
442+
) -> StdResult<WhereCondition> {
443+
Ok(WhereCondition::new(
439444
"signed_entity_type_id = ?* and beacon = ?*",
440445
vec![
441446
Value::Integer(signed_entity_type.index() as i64),
442-
Value::String(signed_entity_type.get_json_beacon().unwrap()),
447+
Value::String(signed_entity_type.get_json_beacon()?),
443448
],
444-
)
449+
))
445450
}
446451
}
447452

@@ -496,7 +501,7 @@ impl OpenMessageRepository {
496501
let provider = OpenMessageProvider::new(&lock);
497502
let filters = provider
498503
.get_epoch_condition(signed_entity_type.get_epoch())
499-
.and_where(provider.get_signed_entity_type_condition(signed_entity_type));
504+
.and_where(provider.get_signed_entity_type_condition(signed_entity_type)?);
500505
let mut messages = provider.find(filters)?;
501506

502507
Ok(messages.next())
@@ -511,7 +516,7 @@ impl OpenMessageRepository {
511516
let provider = OpenMessageWithSingleSignaturesProvider::new(&lock);
512517
let filters = provider
513518
.get_epoch_condition(signed_entity_type.get_epoch())
514-
.and_where(provider.get_signed_entity_type_condition(signed_entity_type));
519+
.and_where(provider.get_signed_entity_type_condition(signed_entity_type)?);
515520
let mut messages = provider.find(filters)?;
516521

517522
Ok(messages.next())
@@ -708,6 +713,7 @@ else json_group_array( \
708713
.get_signed_entity_type_condition(&SignedEntityType::CardanoImmutableFilesFull(
709714
beacon.clone(),
710715
))
716+
.unwrap()
711717
.expand();
712718

713719
assert_eq!(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ pub trait SignedEntityStorer: Sync + Send {
374374
) -> StdResult<Option<SignedEntityRecord>>;
375375

376376
/// Get signed entities type by certificates ids
377-
async fn get_signed_entity_by_certificates_ids<'a>(
377+
async fn get_signed_entities_by_certificates_ids<'a>(
378378
&self,
379379
certificates_ids: &[&'a str],
380380
) -> StdResult<Vec<SignedEntityRecord>>;
@@ -449,7 +449,7 @@ impl SignedEntityStorer for SignedEntityStoreAdapter {
449449
Ok(signed_entity)
450450
}
451451

452-
async fn get_signed_entity_by_certificates_ids<'a>(
452+
async fn get_signed_entities_by_certificates_ids<'a>(
453453
&self,
454454
certificates_ids: &[&'a str],
455455
) -> StdResult<Vec<SignedEntityRecord>> {
@@ -855,7 +855,7 @@ mod tests {
855855
.collect();
856856

857857
let queried_records = store
858-
.get_signed_entity_by_certificates_ids(&certificates_ids)
858+
.get_signed_entities_by_certificates_ids(&certificates_ids)
859859
.await
860860
.expect("querying signed entity record by certificates ids should not fail");
861861

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ impl VerificationKeyStorer for SignerRegistrationStore {
424424
let provider = InsertOrReplaceSignerRegistrationRecordProvider::new(connection);
425425
let existing_record = SignerRegistrationRecordProvider::new(connection)
426426
.get_by_signer_id_and_epoch(signer.party_id.clone(), &epoch)
427+
.with_context(|| {
428+
format!(
429+
"Get signer registration record failure with signer_id: '{}', epoch: '{}'",
430+
signer.party_id, epoch
431+
)
432+
})
427433
.map_err(AdapterError::QueryError)?
428434
.next();
429435

mithril-aggregator/src/services/certifier.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! single signatures and deal with the multi_signer for aggregate signature
66
//! creation.
77
8+
use anyhow::Context;
89
use async_trait::async_trait;
910
use chrono::Utc;
1011
use mithril_common::{
@@ -184,17 +185,27 @@ impl MithrilCertifierService {
184185
"CertifierService::get_open_message_record(signed_entity_type: {signed_entity_type:?})"
185186
);
186187

187-
self.open_message_repository
188+
let open_message_with_single_signatures = self
189+
.open_message_repository
188190
.get_open_message_with_single_signatures(signed_entity_type)
189191
.await
192+
.with_context(|| format!("Certifier can not get open message with single signatures for signed entity type: '{signed_entity_type}'"))?;
193+
194+
Ok(open_message_with_single_signatures)
190195
}
191196
}
192197

193198
#[async_trait]
194199
impl CertifierService for MithrilCertifierService {
195200
async fn inform_epoch(&self, epoch: Epoch) -> StdResult<()> {
196201
debug!("CertifierService::inform_epoch(epoch: {epoch:?})");
197-
let nb = self.open_message_repository.clean_epoch(epoch).await?;
202+
let nb = self
203+
.open_message_repository
204+
.clean_epoch(epoch)
205+
.await
206+
.with_context(|| {
207+
format!("Certifier can not clean open messages from epoch '{epoch}'")
208+
})?;
198209
info!("MithrilCertifierService: Informed of a new Epoch: {epoch:?}. Cleaned {nb} open messages along with their single signatures.");
199210

200211
Ok(())
@@ -229,8 +240,8 @@ impl CertifierService for MithrilCertifierService {
229240

230241
let single_signature = self
231242
.single_signature_repository
232-
.create_single_signature(signature, &open_message.into())
233-
.await?;
243+
.create_single_signature(signature, &open_message.clone().into())
244+
.await.with_context(|| format!("Certifier can not create the single signature from single_signature: '{signature:?}', open_message: '{open_message:?}'"))?;
234245
info!("CertifierService::register_single_signature: created pool '{}' single signature for {signed_entity_type:?}.", single_signature.signer_id);
235246
debug!("CertifierService::register_single_signature: created single signature for open message ID='{}'.", single_signature.open_message_id);
236247

@@ -250,7 +261,14 @@ impl CertifierService for MithrilCertifierService {
250261
signed_entity_type,
251262
protocol_message,
252263
)
253-
.await?;
264+
.await
265+
.with_context(|| {
266+
format!(
267+
"Certifier can not create open message from protocol_message: '{:?}, epoch: '{}''",
268+
protocol_message,
269+
signed_entity_type.get_epoch()
270+
)
271+
})?;
254272
info!("CertifierService::create_open_message: created open message for {signed_entity_type:?}");
255273
debug!(
256274
"CertifierService::create_open_message: created open message ID='{}'",
@@ -269,7 +287,8 @@ impl CertifierService for MithrilCertifierService {
269287
let open_message = self
270288
.open_message_repository
271289
.get_open_message_with_single_signatures(signed_entity_type)
272-
.await?
290+
.await
291+
.with_context(|| format!("Certifier can not get open message with single signatures for signed entity type: '{signed_entity_type}'"))?
273292
.map(|record| record.into());
274293

275294
Ok(open_message)
@@ -333,7 +352,13 @@ impl CertifierService for MithrilCertifierService {
333352
let parent_certificate_hash = self
334353
.certificate_repository
335354
.get_master_certificate_for_epoch(open_message.epoch)
336-
.await?
355+
.await
356+
.with_context(|| {
357+
format!(
358+
"Certifier can not get master certificate for epoch: '{}'",
359+
open_message.epoch
360+
)
361+
})?
337362
.map(|cert| cert.hash)
338363
.ok_or_else(|| Box::new(CertifierServiceError::NoParentCertificateFound))?;
339364

@@ -360,13 +385,18 @@ impl CertifierService for MithrilCertifierService {
360385
let certificate = self
361386
.certificate_repository
362387
.create_certificate(certificate)
363-
.await?;
388+
.await
389+
.with_context(|| {format!(
390+
"Certifier can not create certificate for signed entity type: '{signed_entity_type}'")
391+
})?;
364392

365393
let mut open_message_certified: OpenMessageRecord = open_message_record.into();
366394
open_message_certified.is_certified = true;
367395
self.open_message_repository
368396
.update_open_message(&open_message_certified)
369-
.await?;
397+
.await
398+
.with_context(|| format!("Certifier can not update open message for signed entity type: '{signed_entity_type}'"))
399+
?;
370400

371401
Ok(Some(certificate))
372402
}
@@ -379,6 +409,7 @@ impl CertifierService for MithrilCertifierService {
379409
self.certificate_repository
380410
.get_latest_certificates(last_n)
381411
.await
412+
.with_context(|| format!("Certifier can not get last '{last_n}' certificates"))
382413
}
383414

384415
async fn verify_certificate_chain(&self, epoch: Epoch) -> StdResult<()> {

mithril-aggregator/src/services/signed_entity.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! This service is responsible of dealing with [SignedEntity] type.
44
//! It creates [Artifact] that can be accessed by clients.
5+
use anyhow::Context;
56
use async_trait::async_trait;
67
use chrono::Utc;
78
use slog_scope::info;
@@ -136,15 +137,20 @@ impl SignedEntityService for MithrilSignedEntityService {
136137

137138
let signed_entity = SignedEntityRecord {
138139
signed_entity_id: artifact.get_id(),
139-
signed_entity_type,
140+
signed_entity_type: signed_entity_type.clone(),
140141
certificate_id: certificate.hash.clone(),
141142
artifact: serde_json::to_string(&artifact)?,
142143
created_at: Utc::now(),
143144
};
144145

145146
self.signed_entity_storer
146147
.store_signed_entity(&signed_entity)
147-
.await?;
148+
.await
149+
.with_context(|| {
150+
format!(
151+
"Signed Entity Service can not store signed entity with type: '{signed_entity_type}'"
152+
)
153+
})?;
148154

149155
Ok(())
150156
}
@@ -159,7 +165,13 @@ impl SignedEntityService for MithrilSignedEntityService {
159165
&SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
160166
total,
161167
)
162-
.await?;
168+
.await
169+
.with_context(|| {
170+
format!(
171+
"Signed Entity Service can not get last signed entities with type: '{:?}'",
172+
&SignedEntityTypeDiscriminants::CardanoImmutableFilesFull
173+
)
174+
})?;
163175
let mut signed_entities: Vec<SignedEntity<Snapshot>> = Vec::new();
164176

165177
for record in signed_entities_records {
@@ -179,7 +191,13 @@ impl SignedEntityService for MithrilSignedEntityService {
179191
&SignedEntityTypeDiscriminants::MithrilStakeDistribution,
180192
total,
181193
)
182-
.await?;
194+
.await
195+
.with_context(|| {
196+
format!(
197+
"Signed Entity Service can not get last signed entities with type: '{:?}'",
198+
&SignedEntityTypeDiscriminants::MithrilStakeDistribution
199+
)
200+
})?;
183201
let mut signed_entities: Vec<SignedEntity<MithrilStakeDistribution>> = Vec::new();
184202

185203
for record in signed_entities_records {
@@ -196,8 +214,12 @@ impl SignedEntityService for MithrilSignedEntityService {
196214
let entity: Option<SignedEntity<Snapshot>> = match self
197215
.signed_entity_storer
198216
.get_signed_entity(signed_entity_id)
199-
.await?
200-
{
217+
.await
218+
.with_context(|| {
219+
format!(
220+
"Signed Entity Service can not get signed entity with id: '{signed_entity_id}'"
221+
)
222+
})? {
201223
Some(entity) => Some(entity.try_into()?),
202224
None => None,
203225
};
@@ -212,8 +234,12 @@ impl SignedEntityService for MithrilSignedEntityService {
212234
let entity: Option<SignedEntity<MithrilStakeDistribution>> = match self
213235
.signed_entity_storer
214236
.get_signed_entity(signed_entity_id)
215-
.await?
216-
{
237+
.await
238+
.with_context(|| {
239+
format!(
240+
"Signed Entity Service can not get signed entity with id: '{signed_entity_id}'"
241+
)
242+
})? {
217243
Some(entity) => Some(entity.try_into()?),
218244
None => None,
219245
};

0 commit comments

Comments
 (0)