mithril_aggregator/database/query/epoch_settings/
get_epoch_settings.rs1use anyhow::Context;
2use mithril_common::entities::Epoch;
3use mithril_common::StdResult;
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5use sqlite::Value;
6
7use crate::database::record::EpochSettingsRecord;
8
9pub struct GetEpochSettingsQuery {
11 condition: WhereCondition,
12}
13
14impl GetEpochSettingsQuery {
15 pub fn by_epoch(epoch: Epoch) -> StdResult<Self> {
16 let epoch_settings_id: i64 = epoch
17 .try_into()
18 .with_context(|| format!("Can not convert epoch: '{epoch}'"))?;
19
20 Ok(Self {
21 condition: WhereCondition::new(
22 "epoch_setting_id = ?*",
23 vec![Value::Integer(epoch_settings_id)],
24 ),
25 })
26 }
27}
28
29impl Query for GetEpochSettingsQuery {
30 type Entity = EpochSettingsRecord;
31
32 fn filters(&self) -> WhereCondition {
33 self.condition.clone()
34 }
35
36 fn get_definition(&self, condition: &str) -> String {
37 let aliases = SourceAlias::new(&[("{:epoch_setting:}", "es")]);
38 let projection = Self::Entity::get_projection().expand(aliases);
39 format!("select {projection} from epoch_setting as es where {condition} order by epoch_setting_id desc")
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use mithril_common::entities::{
46 BlockNumber, CardanoTransactionsSigningConfig, ProtocolParameters,
47 };
48 use mithril_persistence::sqlite::ConnectionExtensions;
49
50 use crate::database::test_helper::{insert_epoch_settings, main_db_connection};
51
52 use super::*;
53
54 #[test]
55 fn test_get_epoch_settings() {
56 let connection = main_db_connection().unwrap();
57 insert_epoch_settings(&connection, &[1, 2, 3]).unwrap();
58
59 let epoch_settings_record = connection
60 .fetch_first(GetEpochSettingsQuery::by_epoch(Epoch(1)).unwrap())
61 .unwrap()
62 .expect("Should have an epoch settings for epoch 1.");
63 assert_eq!(Epoch(1), epoch_settings_record.epoch_settings_id);
64 assert_eq!(
65 ProtocolParameters::new(1, 2, 1.0),
66 epoch_settings_record.protocol_parameters
67 );
68
69 assert_eq!(
70 CardanoTransactionsSigningConfig::new(BlockNumber(10), BlockNumber(15)),
71 epoch_settings_record.cardano_transactions_signing_config
72 );
73
74 let epoch_settings_record = connection
75 .fetch_first(GetEpochSettingsQuery::by_epoch(Epoch(3)).unwrap())
76 .unwrap()
77 .expect("Should have an epoch settings for epoch 3.");
78 assert_eq!(Epoch(3), epoch_settings_record.epoch_settings_id);
79 assert_eq!(
80 ProtocolParameters::new(3, 4, 1.0),
81 epoch_settings_record.protocol_parameters
82 );
83 assert_eq!(
84 CardanoTransactionsSigningConfig {
85 security_parameter: BlockNumber(30),
86 step: BlockNumber(15),
87 },
88 epoch_settings_record.cardano_transactions_signing_config
89 );
90
91 let cursor = connection
92 .fetch(GetEpochSettingsQuery::by_epoch(Epoch(5)).unwrap())
93 .unwrap();
94 assert_eq!(0, cursor.count());
95 }
96}