Skip to content

Commit a24b47c

Browse files
authored
Merge pull request #1126 from input-output-hk/djo/epoch_deref_to_u64
Simplify epoch type by implementing Deref & TryInto<i64>
2 parents 894501d + 39ef55e commit a24b47c

File tree

16 files changed

+74
-61
lines changed

16 files changed

+74
-61
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.63"
3+
version = "0.3.64"
44
description = "A Mithril Aggregator server"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-aggregator/src/artifact_builder/cardano_immutable_files_full.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl CardanoImmutableFilesFullArtifactBuilder {
5858
})?;
5959
let snapshot_name = format!(
6060
"{}-e{}-i{}.{}.tar.gz",
61-
beacon.network, beacon.epoch.0, beacon.immutable_file_number, snapshot_digest
61+
beacon.network, *beacon.epoch, beacon.immutable_file_number, snapshot_digest
6262
);
6363
// spawn a separate thread to prevent blocking
6464
let ongoing_snapshot =
@@ -237,7 +237,7 @@ mod tests {
237237
Path::new(
238238
format!(
239239
"{}-e{}-i{}.{}.tar.gz",
240-
beacon.network, beacon.epoch.0, beacon.immutable_file_number, "test+digest"
240+
beacon.network, *beacon.epoch, beacon.immutable_file_number, "test+digest"
241241
)
242242
.as_str()
243243
),

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,9 @@ impl<'client> CertificateRecordProvider<'client> {
296296
}
297297

298298
fn condition_by_epoch(&self, epoch: &Epoch) -> StdResult<WhereCondition> {
299-
let epoch: i64 = i64::try_from(epoch.0)?;
300-
301299
Ok(WhereCondition::new(
302300
"epoch = ?*",
303-
vec![Value::Integer(epoch)],
301+
vec![Value::Integer(epoch.try_into()?)],
304302
))
305303
}
306304

@@ -385,7 +383,7 @@ protocol_message, signers, initiated_at, sealed_at)";
385383
Value::String(certificate_record.message.to_owned()),
386384
Value::String(certificate_record.signature.to_owned()),
387385
Value::String(certificate_record.aggregate_verification_key.to_owned()),
388-
Value::Integer(i64::try_from(certificate_record.epoch.0).unwrap()),
386+
Value::Integer(certificate_record.epoch.try_into().unwrap()),
389387
Value::String(serde_json::to_string(&certificate_record.beacon).unwrap()),
390388
Value::String(certificate_record.protocol_version.to_owned()),
391389
Value::String(
@@ -456,7 +454,7 @@ impl<'conn> MasterCertificateProvider<'conn> {
456454
}
457455

458456
pub fn get_master_certificate_condition(&self, epoch: Epoch) -> WhereCondition {
459-
let epoch_i64: i64 = epoch.0.try_into().unwrap();
457+
let epoch_i64: i64 = epoch.try_into().unwrap();
460458
WhereCondition::new(
461459
"certificate.epoch between ?* and ?*",
462460
vec![Value::Integer(epoch_i64 - 1), Value::Integer(epoch_i64)],
@@ -685,7 +683,7 @@ mod tests {
685683
(3, certificate_record.message.into()),
686684
(4, certificate_record.signature.into()),
687685
(5, certificate_record.aggregate_verification_key.into()),
688-
(6, i64::try_from(certificate_record.epoch.0).unwrap().into()),
686+
(6, Value::Integer(*certificate_record.epoch as i64)),
689687
(
690688
7,
691689
serde_json::to_string(&certificate_record.beacon)
@@ -878,7 +876,7 @@ mod tests {
878876
Value::String(certificate_record.message),
879877
Value::String(certificate_record.signature),
880878
Value::String(certificate_record.aggregate_verification_key),
881-
Value::Integer(i64::try_from(certificate_record.epoch.0).unwrap()),
879+
Value::Integer(*certificate_record.epoch as i64),
882880
Value::String(serde_json::to_string(&certificate_record.beacon).unwrap()),
883881
Value::String(certificate_record.protocol_version),
884882
Value::String(
@@ -925,7 +923,7 @@ protocol_message, signers, initiated_at, sealed_at) values \
925923
Value::String(certificate_record.message),
926924
Value::String(certificate_record.signature),
927925
Value::String(certificate_record.aggregate_verification_key),
928-
Value::Integer(i64::try_from(certificate_record.epoch.0).unwrap()),
926+
Value::Integer(*certificate_record.epoch as i64),
929927
Value::String(serde_json::to_string(&certificate_record.beacon).unwrap()),
930928
Value::String(certificate_record.protocol_version),
931929
Value::String(

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'client> EpochSettingProvider<'client> {
8282
}
8383

8484
fn condition_by_epoch(&self, epoch: &Epoch) -> Result<WhereCondition, StdError> {
85-
let epoch_setting_id: i64 = i64::try_from(epoch.0)?;
85+
let epoch_setting_id: i64 = epoch.try_into()?;
8686

8787
Ok(WhereCondition::new(
8888
"epoch_setting_id = ?*",
@@ -140,7 +140,7 @@ impl<'conn> UpdateEpochSettingProvider<'conn> {
140140
epoch: Epoch,
141141
protocol_parameters: ProtocolParameters,
142142
) -> WhereCondition {
143-
let epoch_setting_id = i64::try_from(epoch.0).unwrap();
143+
let epoch_setting_id: i64 = epoch.try_into().unwrap();
144144

145145
WhereCondition::new(
146146
"(epoch_setting_id, protocol_parameters) values (?1, ?2)",
@@ -214,7 +214,7 @@ impl<'conn> DeleteEpochSettingProvider<'conn> {
214214

215215
/// Create the SQL condition to delete a record given the Epoch.
216216
fn get_delete_condition_by_epoch(&self, epoch: Epoch) -> WhereCondition {
217-
let epoch_setting_id_value = Value::Integer(i64::try_from(epoch.0).unwrap());
217+
let epoch_setting_id_value = Value::Integer(epoch.try_into().unwrap());
218218

219219
WhereCondition::new("epoch_setting_id = ?*", vec![epoch_setting_id_value])
220220
}
@@ -228,7 +228,7 @@ impl<'conn> DeleteEpochSettingProvider<'conn> {
228228

229229
/// Create the SQL condition to prune data older than the given Epoch.
230230
fn get_prune_condition(&self, epoch_threshold: Epoch) -> WhereCondition {
231-
let epoch_setting_id_value = Value::Integer(i64::try_from(epoch_threshold.0).unwrap());
231+
let epoch_setting_id_value = Value::Integer(epoch_threshold.try_into().unwrap());
232232

233233
WhereCondition::new("epoch_setting_id < ?*", vec![epoch_setting_id_value])
234234
}

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ impl<'client> OpenMessageProvider<'client> {
164164
}
165165

166166
fn get_epoch_condition(&self, epoch: Epoch) -> WhereCondition {
167-
WhereCondition::new(
168-
"epoch_setting_id = ?*",
169-
vec![Value::Integer(epoch.0 as i64)],
170-
)
167+
WhereCondition::new("epoch_setting_id = ?*", vec![Value::Integer(*epoch as i64)])
171168
}
172169

173170
fn get_signed_entity_type_condition(
@@ -231,7 +228,7 @@ impl<'client> InsertOpenMessageProvider<'client> {
231228
let beacon_str = signed_entity_type.get_json_beacon()?;
232229
let parameters = vec![
233230
Value::String(Uuid::new_v4().to_string()),
234-
Value::Integer(epoch.0 as i64),
231+
Value::Integer(epoch.try_into()?),
235232
Value::String(beacon_str),
236233
Value::Integer(signed_entity_type.index() as i64),
237234
Value::String(serde_json::to_string(protocol_message)?),
@@ -271,7 +268,7 @@ signed_entity_type_id = ?*, protocol_message = ?*, is_certified = ?* \
271268
where open_message_id = ?*";
272269
let beacon_str = open_message.signed_entity_type.get_json_beacon()?;
273270
let parameters = vec![
274-
Value::Integer(open_message.epoch.0 as i64),
271+
Value::Integer(open_message.epoch.try_into()?),
275272
Value::String(beacon_str),
276273
Value::Integer(open_message.signed_entity_type.index() as i64),
277274
Value::String(serde_json::to_string(&open_message.protocol_message)?),
@@ -308,10 +305,7 @@ impl<'client> DeleteOpenMessageProvider<'client> {
308305
}
309306

310307
fn get_epoch_condition(&self, epoch: Epoch) -> WhereCondition {
311-
WhereCondition::new(
312-
"epoch_setting_id < ?*",
313-
vec![Value::Integer(epoch.0 as i64)],
314-
)
308+
WhereCondition::new("epoch_setting_id < ?*", vec![Value::Integer(*epoch as i64)])
315309
}
316310
}
317311

@@ -434,10 +428,7 @@ impl<'client> OpenMessageWithSingleSignaturesProvider<'client> {
434428
}
435429

436430
fn get_epoch_condition(&self, epoch: Epoch) -> WhereCondition {
437-
WhereCondition::new(
438-
"epoch_setting_id = ?*",
439-
vec![Value::Integer(epoch.0 as i64)],
440-
)
431+
WhereCondition::new("epoch_setting_id = ?*", vec![Value::Integer(*epoch as i64)])
441432
}
442433

443434
fn get_signed_entity_type_condition(
@@ -448,7 +439,6 @@ impl<'client> OpenMessageWithSingleSignaturesProvider<'client> {
448439
"signed_entity_type_id = ?* and beacon = ?*",
449440
vec![
450441
Value::Integer(signed_entity_type.index() as i64),
451-
// TODO!: Remove this ugly unwrap, should this method returns a result ?
452442
Value::String(signed_entity_type.get_json_beacon().unwrap()),
453443
],
454444
)
@@ -807,7 +797,7 @@ else json_group_array( \
807797
);
808798
assert_eq!(
809799
vec![
810-
Value::Integer(open_message.epoch.0 as i64),
800+
Value::Integer(*open_message.epoch as i64),
811801
Value::String(open_message.signed_entity_type.get_json_beacon().unwrap()),
812802
Value::Integer(open_message.signed_entity_type.index() as i64),
813803
Value::String(serde_json::to_string(&open_message.protocol_message).unwrap()),

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'client> SignerRegistrationRecordProvider<'client> {
194194
}
195195

196196
fn condition_by_epoch(&self, epoch: &Epoch) -> Result<WhereCondition, StdError> {
197-
let epoch: i64 = i64::try_from(epoch.0)?;
197+
let epoch: i64 = epoch.try_into()?;
198198

199199
Ok(WhereCondition::new(
200200
"epoch_setting_id = ?*",
@@ -270,7 +270,7 @@ impl<'conn> InsertOrReplaceSignerRegistrationRecordProvider<'conn> {
270270
vec![
271271
Value::String(signer_registration_record.signer_id),
272272
Value::Integer(
273-
i64::try_from(signer_registration_record.epoch_setting_id.0).unwrap(),
273+
signer_registration_record.epoch_setting_id.try_into().unwrap(),
274274
),
275275
Value::String(signer_registration_record.verification_key),
276276
signer_registration_record
@@ -361,7 +361,7 @@ impl<'conn> DeleteSignerRegistrationRecordProvider<'conn> {
361361

362362
/// Create the SQL condition to delete a record given the Epoch.
363363
fn get_delete_condition_by_epoch(&self, epoch: Epoch) -> WhereCondition {
364-
let epoch_threshold = Value::Integer(i64::try_from(epoch.0).unwrap());
364+
let epoch_threshold = Value::Integer(epoch.try_into().unwrap());
365365

366366
WhereCondition::new("epoch_setting_id = ?*", vec![epoch_threshold])
367367
}
@@ -375,7 +375,7 @@ impl<'conn> DeleteSignerRegistrationRecordProvider<'conn> {
375375

376376
/// Create the SQL condition to prune data older than the given Epoch.
377377
fn get_prune_condition(&self, epoch_threshold: Epoch) -> WhereCondition {
378-
let epoch_threshold = Value::Integer(i64::try_from(epoch_threshold.0).unwrap());
378+
let epoch_threshold = Value::Integer(epoch_threshold.try_into().unwrap());
379379

380380
WhereCondition::new("epoch_setting_id < ?*", vec![epoch_threshold])
381381
}
@@ -512,9 +512,7 @@ mod tests {
512512
(1, signer_registration_record.signer_id.into()),
513513
(
514514
2,
515-
i64::try_from(signer_registration_record.epoch_setting_id.0)
516-
.unwrap()
517-
.into(),
515+
Value::Integer(*signer_registration_record.epoch_setting_id as i64),
518516
),
519517
(3, signer_registration_record.verification_key.into()),
520518
(
@@ -632,9 +630,7 @@ mod tests {
632630
assert_eq!(
633631
vec![
634632
Value::String(signer_registration_record.signer_id),
635-
Value::Integer(
636-
i64::try_from(signer_registration_record.epoch_setting_id.0).unwrap(),
637-
),
633+
Value::Integer(*signer_registration_record.epoch_setting_id as i64),
638634
Value::String(signer_registration_record.verification_key),
639635
signer_registration_record
640636
.verification_key_signature

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'client> SingleSignatureRecordProvider<'client> {
174174
&self,
175175
registration_epoch: &Epoch,
176176
) -> Result<WhereCondition, StdError> {
177-
let epoch: i64 = i64::try_from(registration_epoch.0)?;
177+
let epoch: i64 = registration_epoch.try_into()?;
178178

179179
Ok(WhereCondition::new(
180180
"registration_epoch_setting_id = ?*",
@@ -237,7 +237,7 @@ impl<'conn> UpdateSingleSignatureRecordProvider<'conn> {
237237
Value::String(single_signature_record.open_message_id.to_string()),
238238
Value::String(single_signature_record.signer_id.to_owned()),
239239
Value::Integer(
240-
i64::try_from(single_signature_record.registration_epoch_setting_id.0).unwrap(),
240+
single_signature_record.registration_epoch_setting_id.try_into().unwrap(),
241241
),
242242
Value::String(serde_json::to_string(&single_signature_record.lottery_indexes).unwrap()),
243243
Value::String(single_signature_record.signature.to_owned()),
@@ -413,7 +413,7 @@ mod tests {
413413
vec![
414414
Value::String(single_signature_record.open_message_id.to_string()),
415415
Value::String(single_signature_record.signer_id),
416-
Value::Integer(single_signature_record.registration_epoch_setting_id.0 as i64),
416+
Value::Integer(*single_signature_record.registration_epoch_setting_id as i64),
417417
Value::String(
418418
serde_json::to_string(&single_signature_record.lottery_indexes).unwrap()
419419
),

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ impl<'client> StakePoolProvider<'client> {
8989
}
9090

9191
fn condition_by_epoch(&self, epoch: &Epoch) -> Result<WhereCondition, StdError> {
92-
let epoch: i64 = i64::try_from(epoch.0)?;
93-
9492
Ok(WhereCondition::new(
9593
"epoch = ?*",
96-
vec![Value::Integer(epoch)],
94+
vec![Value::Integer(epoch.try_into()?)],
9795
))
9896
}
9997

@@ -138,7 +136,7 @@ impl<'conn> InsertOrReplaceStakePoolProvider<'conn> {
138136
epoch: Epoch,
139137
stake: Stake,
140138
) -> WhereCondition {
141-
let epoch = i64::try_from(epoch.0).unwrap();
139+
let epoch = epoch.try_into().unwrap();
142140
let stake = i64::try_from(stake).unwrap();
143141

144142
WhereCondition::new(
@@ -215,9 +213,10 @@ impl<'conn> DeleteStakePoolProvider<'conn> {
215213

216214
/// Create the SQL condition to prune data older than the given Epoch.
217215
fn get_prune_condition(&self, epoch_threshold: Epoch) -> WhereCondition {
218-
let epoch_value = Value::Integer(i64::try_from(epoch_threshold.0).unwrap());
219-
220-
WhereCondition::new("epoch < ?*", vec![epoch_value])
216+
WhereCondition::new(
217+
"epoch < ?*",
218+
vec![Value::Integer(epoch_threshold.try_into().unwrap())],
219+
)
221220
}
222221

223222
/// Prune the stake pools data older than the given epoch.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ pub fn insert_single_signatures_in_db(
8282
(2, single_signature_record.signer_id.into()),
8383
(
8484
3,
85-
i64::try_from(single_signature_record.registration_epoch_setting_id.0)
86-
.unwrap()
87-
.into(),
85+
Value::Integer(*single_signature_record.registration_epoch_setting_id as i64),
8886
),
8987
(
9088
4,

0 commit comments

Comments
 (0)