Skip to content

Commit

Permalink
Move signed entity type beacon read from db to a commun methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Alenar committed Mar 27, 2024
1 parent 56f7a5c commit fb3da3f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
14 changes: 14 additions & 0 deletions mithril-aggregator/src/database/provider/mod.rs
Expand Up @@ -22,3 +22,17 @@ pub use single_signature::*;
pub use stake_pool::*;
#[cfg(test)]
pub use test_helper::*;

pub(crate) fn read_signed_entity_type_beacon_column<U: sqlite::RowIndex + Clone>(
row: &sqlite::Row,
column_index: U,
) -> String {
// TODO: We need to check first that the cell can be read as a string first
// (e.g. when beacon json is '{"network": "dev", "epoch": 1, "immutable_file_number": 2}').
// If it fails, we fallback on reading the cell as an integer (e.g. when beacon json is '5').
// Maybe there is a better way of doing this.
match row.try_read::<&str, _>(column_index.clone()) {
Ok(value) => value.to_string(),
Err(_) => (row.read::<i64, _>(column_index)).to_string(),
}
}
11 changes: 1 addition & 10 deletions mithril-aggregator/src/database/provider/open_message.rs
Expand Up @@ -103,16 +103,7 @@ impl SqLiteEntity for OpenMessageRecord {
let epoch_setting_id = row.read::<i64, _>(1);
let epoch_val = u64::try_from(epoch_setting_id)
.map_err(|e| panic!("Integer field open_message.epoch_setting_id (value={epoch_setting_id}) is incompatible with u64 Epoch representation. Error = {e}"))?;

// TODO: We need to check first that the cell can be read as a string first
// (e.g. when beacon json is '{"network": "dev", "epoch": 1, "immutable_file_number": 2}').
// If it fails, we fallback on readign the cell as an integer (e.g. when beacon json is '5').
// Maybe there is a better way of doing this.
let beacon_str = match row.try_read::<&str, _>(2) {
Ok(value) => value.to_string(),
Err(_) => (row.read::<i64, _>(2)).to_string(),
};

let beacon_str = super::read_signed_entity_type_beacon_column(&row, 2);
let signed_entity_type_id = usize::try_from(row.read::<i64, _>(3)).map_err(|e| {
panic!(
"Integer field open_message.signed_entity_type_id cannot be turned into usize: {e}"
Expand Down
9 changes: 1 addition & 8 deletions mithril-aggregator/src/database/provider/signed_entity.rs
Expand Up @@ -215,14 +215,7 @@ impl SqLiteEntity for SignedEntityRecord {
let signed_entity_id = row.read::<&str, _>(0).to_string();
let signed_entity_type_id_int = row.read::<i64, _>(1);
let certificate_id = row.read::<&str, _>(2).to_string();
// TODO: We need to check first that the cell can be read as a string first
// (e.g. when beacon json is '{"network": "dev", "epoch": 1, "immutable_file_number": 2}').
// If it fails, we fallback on readign the cell as an integer (e.g. when beacon json is '5').
// Maybe there is a better way of doing this.
let beacon_str = match row.try_read::<&str, _>(3) {
Ok(value) => value.to_string(),
Err(_) => (row.read::<i64, _>(3)).to_string(),
};
let beacon_str = super::read_signed_entity_type_beacon_column(&row, 3);
let artifact_str = row.read::<&str, _>(4).to_string();
let created_at = row.read::<&str, _>(5);

Expand Down

0 comments on commit fb3da3f

Please sign in to comment.