mithril_aggregator/database/query/epoch_settings/
update_epoch_settings.rs1use sqlite::Value;
2
3use mithril_common::entities::Epoch;
4use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};
5
6use crate::database::record::EpochSettingsRecord;
7use crate::entities::AggregatorEpochSettings;
8
9pub struct UpdateEpochSettingsQuery {
11 condition: WhereCondition,
12}
13
14impl UpdateEpochSettingsQuery {
15 pub fn one(epoch: Epoch, epoch_settings: AggregatorEpochSettings) -> Self {
16 let epoch_settings_id: i64 = epoch.try_into().unwrap();
17
18 Self {
19 condition: WhereCondition::new(
20 "(epoch_setting_id, protocol_parameters, cardano_transactions_signing_config) values (?1, ?2, ?3)",
21 vec![
22 Value::Integer(epoch_settings_id),
23 Value::String(serde_json::to_string(&epoch_settings.protocol_parameters).unwrap()),
24 Value::String(
25 serde_json::to_string(&epoch_settings.cardano_transactions_signing_config).unwrap(),
26 ),
27 ],
28 ),
29 }
30 }
31}
32
33impl Query for UpdateEpochSettingsQuery {
34 type Entity = EpochSettingsRecord;
35
36 fn filters(&self) -> WhereCondition {
37 self.condition.clone()
38 }
39
40 fn get_definition(&self, condition: &str) -> String {
41 let projection = Self::Entity::get_projection()
44 .expand(SourceAlias::new(&[("{:epoch_setting:}", "epoch_setting")]));
45
46 format!("insert or replace into epoch_setting {condition} returning {projection}")
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use mithril_common::entities::{BlockNumber, CardanoTransactionsSigningConfig};
53 use mithril_common::test_utils::fake_data;
54 use mithril_persistence::sqlite::ConnectionExtensions;
55
56 use crate::database::query::GetEpochSettingsQuery;
57 use crate::database::test_helper::{insert_epoch_settings, main_db_connection};
58
59 use super::*;
60
61 #[test]
62 fn test_update_epoch_settings() {
63 let connection = main_db_connection().unwrap();
64 insert_epoch_settings(&connection, &[*Epoch(3)]).unwrap();
65
66 let epoch_settings_send_to_update = AggregatorEpochSettings {
67 protocol_parameters: fake_data::protocol_parameters(),
68 cardano_transactions_signing_config: CardanoTransactionsSigningConfig::new(
69 BlockNumber(24),
70 BlockNumber(62),
71 ),
72 };
73 let record_returned_by_update_query = connection
74 .fetch_first(UpdateEpochSettingsQuery::one(
75 Epoch(3),
76 epoch_settings_send_to_update.clone(),
77 ))
78 .unwrap()
79 .unwrap();
80
81 assert_eq!(Epoch(3), record_returned_by_update_query.epoch_settings_id);
82 assert_eq!(
83 epoch_settings_send_to_update.protocol_parameters,
84 record_returned_by_update_query.protocol_parameters
85 );
86 assert_eq!(
87 epoch_settings_send_to_update.cardano_transactions_signing_config,
88 record_returned_by_update_query.cardano_transactions_signing_config
89 );
90
91 let mut cursor = connection
92 .fetch(GetEpochSettingsQuery::by_epoch(Epoch(3)).unwrap())
93 .unwrap();
94 let epoch_settings_record = cursor
95 .next()
96 .expect("Should have an epoch settings for epoch 3.");
97
98 assert_eq!(record_returned_by_update_query, epoch_settings_record);
99 assert_eq!(0, cursor.count());
100 }
101}