Skip to content

Commit 0cf5fbc

Browse files
committed
refactor(aggregator): make ctx signing config optional in sqlite database
Note: this commit compiles, but aggregator tests are still failing because `EpochService::inform_epoch` still need a ctx signing config.
1 parent 6638f0f commit 0cf5fbc

File tree

6 files changed

+60
-28
lines changed

6 files changed

+60
-28
lines changed

mithril-aggregator/src/database/migration.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,30 @@ alter table signed_entity add column epoch as (coalesce(json_extract(beacon, '$.
251251
create index signed_entity_epoch on signed_entity(epoch);
252252
"#,
253253
),
254+
// Migration 37
255+
// Update `epoch_setting` table to make `cardano_transactions_signing_config` optional.
256+
SqlMigration::new(
257+
37,
258+
r#"
259+
-- disable foreign keys since `signer_registration` has a foreign key constraint on `epoch_setting`
260+
pragma foreign_keys=false;
261+
262+
create table if not exists new_epoch_setting (
263+
epoch_setting_id integer not null,
264+
protocol_parameters json not null,
265+
cardano_transactions_signing_config json,
266+
primary key (epoch_setting_id)
267+
);
268+
insert into new_epoch_setting (epoch_setting_id, protocol_parameters, cardano_transactions_signing_config)
269+
select epoch_setting_id, protocol_parameters, cardano_transactions_signing_config
270+
from epoch_setting order by rowid asc;
271+
drop table epoch_setting;
272+
alter table new_epoch_setting rename to epoch_setting;
273+
274+
-- reenable foreign keys
275+
pragma foreign_key_check;
276+
pragma foreign_keys=true;
277+
"#,
278+
),
254279
]
255280
}

mithril-aggregator/src/database/query/epoch_settings/get_epoch_settings.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ mod tests {
6969
);
7070

7171
assert_eq!(
72-
CardanoTransactionsSigningConfig {
72+
Some(CardanoTransactionsSigningConfig {
7373
security_parameter: BlockNumber(10),
7474
step: BlockNumber(15)
75-
},
75+
}),
7676
epoch_settings_record.cardano_transactions_signing_config
7777
);
7878

@@ -86,10 +86,10 @@ mod tests {
8686
epoch_settings_record.protocol_parameters
8787
);
8888
assert_eq!(
89-
CardanoTransactionsSigningConfig {
89+
Some(CardanoTransactionsSigningConfig {
9090
security_parameter: BlockNumber(30),
9191
step: BlockNumber(15),
92-
},
92+
}),
9393
epoch_settings_record.cardano_transactions_signing_config
9494
);
9595

mithril-aggregator/src/database/query/epoch_settings/insert_or_ignore_epoch_settings.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ impl InsertOrIgnoreEpochSettingsQuery {
1919
Value::String(
2020
serde_json::to_string(&epoch_settings.protocol_parameters).unwrap(),
2121
),
22-
Value::String(
23-
serde_json::to_string(&epoch_settings.cardano_transactions_signing_config)
24-
.unwrap(),
25-
),
22+
match &epoch_settings.cardano_transactions_signing_config {
23+
Some(config) => Value::String(serde_json::to_string(config).unwrap()),
24+
None => Value::Null,
25+
},
2626
],
2727
),
2828
}
@@ -64,10 +64,10 @@ mod tests {
6464
let expected_epoch_settings = EpochSettingsRecord {
6565
epoch_settings_id: Epoch(3),
6666
protocol_parameters: fake_data::protocol_parameters(),
67-
cardano_transactions_signing_config: CardanoTransactionsSigningConfig {
67+
cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
6868
security_parameter: BlockNumber(24),
6969
step: BlockNumber(62),
70-
},
70+
}),
7171
};
7272
let record = connection
7373
.fetch_first(InsertOrIgnoreEpochSettingsQuery::one(
@@ -76,6 +76,19 @@ mod tests {
7676
.unwrap();
7777

7878
assert_eq!(Some(expected_epoch_settings), record);
79+
80+
let expected_epoch_settings_without_ctx_config = EpochSettingsRecord {
81+
epoch_settings_id: Epoch(4),
82+
protocol_parameters: fake_data::protocol_parameters(),
83+
cardano_transactions_signing_config: None,
84+
};
85+
let record = connection
86+
.fetch_first(InsertOrIgnoreEpochSettingsQuery::one(
87+
expected_epoch_settings_without_ctx_config.clone(),
88+
))
89+
.unwrap();
90+
91+
assert_eq!(Some(expected_epoch_settings_without_ctx_config), record);
7992
}
8093

8194
#[test]
@@ -85,10 +98,10 @@ mod tests {
8598
let expected_epoch_settings = EpochSettingsRecord {
8699
epoch_settings_id: Epoch(3),
87100
protocol_parameters: fake_data::protocol_parameters(),
88-
cardano_transactions_signing_config: CardanoTransactionsSigningConfig {
101+
cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
89102
security_parameter: BlockNumber(24),
90103
step: BlockNumber(62),
91-
},
104+
}),
92105
};
93106
let record = connection
94107
.fetch_first(InsertOrIgnoreEpochSettingsQuery::one(
@@ -99,10 +112,10 @@ mod tests {
99112

100113
let record = connection
101114
.fetch_first(InsertOrIgnoreEpochSettingsQuery::one(EpochSettingsRecord {
102-
cardano_transactions_signing_config: CardanoTransactionsSigningConfig {
115+
cardano_transactions_signing_config: Some(CardanoTransactionsSigningConfig {
103116
security_parameter: BlockNumber(134),
104117
step: BlockNumber(872),
105-
},
118+
}),
106119
..expected_epoch_settings.clone()
107120
}))
108121
.unwrap();

mithril-aggregator/src/database/record/epoch_settings.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ pub struct EpochSettingsRecord {
1313
pub protocol_parameters: ProtocolParameters,
1414

1515
/// Cardano transactions signing configuration.
16-
pub cardano_transactions_signing_config: CardanoTransactionsSigningConfig,
16+
pub cardano_transactions_signing_config: Option<CardanoTransactionsSigningConfig>,
1717
}
1818

1919
impl From<EpochSettingsRecord> for AggregatorEpochSettings {
2020
fn from(other: EpochSettingsRecord) -> Self {
2121
Self {
2222
protocol_parameters: other.protocol_parameters,
23-
cardano_transactions_signing_config: Some(other.cardano_transactions_signing_config),
23+
cardano_transactions_signing_config: other.cardano_transactions_signing_config,
2424
}
2525
}
2626
}
@@ -32,7 +32,7 @@ impl SqLiteEntity for EpochSettingsRecord {
3232
{
3333
let epoch_settings_id_int = row.read::<i64, _>(0);
3434
let protocol_parameters_string = &row.read::<&str, _>(1);
35-
let cardano_transactions_signing_config_string = &row.read::<&str, _>(2);
35+
let cardano_transactions_signing_config_string = &row.read::<Option<&str>, _>(2);
3636

3737
let epoch_settings_record = Self {
3838
epoch_settings_id: Epoch(epoch_settings_id_int.try_into().map_err(|e| {
@@ -47,13 +47,13 @@ impl SqLiteEntity for EpochSettingsRecord {
4747
))
4848
},
4949
)?,
50-
cardano_transactions_signing_config: serde_json::from_str(cardano_transactions_signing_config_string).map_err(
50+
cardano_transactions_signing_config: cardano_transactions_signing_config_string.map(|config| serde_json::from_str(config).map_err(
5151
|e| {
5252
HydrationError::InvalidData(format!(
53-
"Could not turn string '{cardano_transactions_signing_config_string}' to CardanoTransactionsSigningConfig. Error: {e}"
53+
"Could not turn string '{config}' to CardanoTransactionsSigningConfig. Error: {e}"
5454
))
5555
},
56-
)?,
56+
)).transpose()?,
5757

5858
};
5959

mithril-aggregator/src/database/repository/epoch_settings_store.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ impl EpochSettingsStorer for EpochSettingsStore {
5353
) -> StdResult<Option<AggregatorEpochSettings>> {
5454
let record_to_insert = EpochSettingsRecord {
5555
epoch_settings_id: epoch,
56-
// Todo: make this field optional allowing removal of the unwrap()
57-
cardano_transactions_signing_config: epoch_settings
58-
.cardano_transactions_signing_config
59-
.unwrap(),
56+
cardano_transactions_signing_config: epoch_settings.cardano_transactions_signing_config,
6057
protocol_parameters: epoch_settings.protocol_parameters,
6158
};
6259
let epoch_settings_record = self

mithril-aggregator/src/database/test_helper.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,7 @@ pub fn insert_epoch_settings(
208208
let (sql_values, _) = InsertOrIgnoreEpochSettingsQuery::one(EpochSettingsRecord {
209209
epoch_settings_id: Epoch(1),
210210
protocol_parameters: ProtocolParameters::new(1, 2, 1.0),
211-
cardano_transactions_signing_config: CardanoTransactionsSigningConfig {
212-
security_parameter: BlockNumber(0),
213-
step: BlockNumber(0),
214-
},
211+
cardano_transactions_signing_config: None,
215212
})
216213
.filters()
217214
.expand();

0 commit comments

Comments
 (0)