-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a
ProtocolInitializer
repository and migration to remove the…
… `ProtocolInitializerStore` that use the `StoreAdpater`
- Loading branch information
Showing
16 changed files
with
617 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
//! Signer related database queries | ||
mod protocol_initializer; | ||
mod signed_beacon; | ||
mod stake_pool; | ||
|
||
pub use protocol_initializer::*; | ||
pub use signed_beacon::*; | ||
pub use stake_pool::*; |
42 changes: 42 additions & 0 deletions
42
mithril-signer/src/database/query/protocol_initializer/delete_protocol_initializer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use sqlite::Value; | ||
|
||
use mithril_common::entities::Epoch; | ||
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition}; | ||
|
||
use crate::database::record::ProtocolInitializerRecord; | ||
|
||
/// Query to delete old [ProtocolInitializer] from the sqlite database | ||
pub struct DeleteProtocolInitializerQuery { | ||
condition: WhereCondition, | ||
} | ||
|
||
impl Query for DeleteProtocolInitializerQuery { | ||
type Entity = ProtocolInitializerRecord; | ||
|
||
fn filters(&self) -> WhereCondition { | ||
self.condition.clone() | ||
} | ||
|
||
fn get_definition(&self, condition: &str) -> String { | ||
// it is important to alias the fields with the same name as the table | ||
// since the table cannot be aliased in a RETURNING statement in SQLite. | ||
let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[( | ||
"{:protocol_initializer:}", | ||
"protocol_initializer", | ||
)])); | ||
|
||
format!("delete from protocol_initializer where {condition} returning {projection}") | ||
} | ||
} | ||
|
||
impl DeleteProtocolInitializerQuery { | ||
/// Create the SQL query to prune data older than the given Epoch. | ||
pub fn below_epoch_threshold(epoch_threshold: Epoch) -> Self { | ||
let condition = WhereCondition::new( | ||
"epoch < ?*", | ||
vec![Value::Integer(epoch_threshold.try_into().unwrap())], | ||
); | ||
|
||
Self { condition } | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
mithril-signer/src/database/query/protocol_initializer/get_protocol_initializer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use sqlite::Value; | ||
|
||
use mithril_common::entities::Epoch; | ||
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition}; | ||
|
||
use crate::database::record::ProtocolInitializerRecord; | ||
|
||
/// Simple queries to retrieve [ProtocolInitializer] from the sqlite database. | ||
pub struct GetProtocolInitializerQuery { | ||
condition: WhereCondition, | ||
limit: Option<usize>, | ||
} | ||
|
||
impl GetProtocolInitializerQuery { | ||
/// Get all signed beacons that match the given signed entity types. | ||
pub fn for_epoch(epoch: Epoch) -> Self { | ||
let epoch_i64: i64 = epoch.try_into().unwrap(); | ||
let condition = WhereCondition::new( | ||
"protocol_initializer.epoch = ?", | ||
vec![Value::Integer(epoch_i64)], | ||
); | ||
|
||
Self { | ||
condition, | ||
limit: None, | ||
} | ||
} | ||
|
||
pub fn last_n(limit: usize) -> Self { | ||
let condition = WhereCondition::default(); | ||
Self { | ||
condition, | ||
limit: Some(limit), | ||
} | ||
} | ||
} | ||
|
||
impl Query for GetProtocolInitializerQuery { | ||
type Entity = ProtocolInitializerRecord; | ||
|
||
fn filters(&self) -> WhereCondition { | ||
self.condition.clone() | ||
} | ||
|
||
fn get_definition(&self, condition: &str) -> String { | ||
let aliases = SourceAlias::new(&[("{:protocol_initializer:}", "protocol_initializer")]); | ||
let projection = Self::Entity::get_projection().expand(aliases); | ||
let limit = self | ||
.limit | ||
.map_or("".to_string(), |limit| format!(" limit {}", limit)); | ||
format!("select {projection} from protocol_initializer where {condition} order by rowid desc{limit}") | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
mithril-signer/src/database/query/protocol_initializer/insert_protocol_initializer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use sqlite::Value; | ||
|
||
use mithril_common::StdResult; | ||
use mithril_persistence::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition}; | ||
|
||
use crate::database::record::ProtocolInitializerRecord; | ||
|
||
/// Query to insert or replace [ProtocolInitializerRecord] in the sqlite database | ||
pub struct InsertOrReplaceProtocolInitializerQuery { | ||
condition: WhereCondition, | ||
} | ||
|
||
impl InsertOrReplaceProtocolInitializerQuery { | ||
pub fn one(record: ProtocolInitializerRecord) -> StdResult<Self> { | ||
let value = serde_json::to_string(&record.protocol_initializer).unwrap(); | ||
let condition = WhereCondition::new( | ||
"(epoch, protocol, created_at) values (?*, ?*, ?*)", | ||
vec![ | ||
Value::Integer(record.epoch.try_into()?), | ||
Value::String(value), | ||
Value::String(record.created_at.to_rfc3339()), | ||
], | ||
); | ||
|
||
Ok(Self { condition }) | ||
} | ||
} | ||
|
||
impl Query for InsertOrReplaceProtocolInitializerQuery { | ||
type Entity = ProtocolInitializerRecord; | ||
|
||
fn filters(&self) -> WhereCondition { | ||
self.condition.clone() | ||
} | ||
|
||
fn get_definition(&self, condition: &str) -> String { | ||
// it is important to alias the fields with the same name as the table | ||
// since the table cannot be aliased in a RETURNING statement in SQLite. | ||
let projection = Self::Entity::get_projection().expand(SourceAlias::new(&[( | ||
"{:protocol_initializer:}", | ||
"protocol_initializer", | ||
)])); | ||
|
||
format!("insert or replace into protocol_initializer {condition} returning {projection}") | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
mithril-signer/src/database/query/protocol_initializer/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod delete_protocol_initializer; | ||
mod get_protocol_initializer; | ||
mod insert_protocol_initializer; | ||
|
||
pub use delete_protocol_initializer::*; | ||
pub use get_protocol_initializer::*; | ||
pub use insert_protocol_initializer::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
//! Signer related database records | ||
mod protocol_initializer_record; | ||
mod signed_beacon_record; | ||
mod stake_pool; | ||
|
||
pub use protocol_initializer_record::*; | ||
pub use signed_beacon_record::*; | ||
pub use stake_pool::*; |
60 changes: 60 additions & 0 deletions
60
mithril-signer/src/database/record/protocol_initializer_record.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use chrono::{DateTime, Utc}; | ||
|
||
use mithril_common::{crypto_helper::ProtocolInitializer, entities::Epoch}; | ||
use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity}; | ||
|
||
/// Protocol initializer. | ||
#[derive(Debug)] | ||
pub struct ProtocolInitializerRecord { | ||
/// Epoch | ||
pub epoch: Epoch, | ||
|
||
/// Protocol Initializer | ||
pub protocol_initializer: ProtocolInitializer, | ||
|
||
/// DateTime of the record creation. | ||
pub created_at: DateTime<Utc>, | ||
} | ||
|
||
impl SqLiteEntity for ProtocolInitializerRecord { | ||
fn hydrate(row: sqlite::Row) -> Result<Self, HydrationError> | ||
where | ||
Self: Sized, | ||
{ | ||
let epoch_int = row.read::<i64, _>(0); | ||
let protocol = row.read::<&str, _>(1); | ||
let datetime = &row.read::<&str, _>(2); | ||
|
||
let record = Self { | ||
protocol_initializer: serde_json::from_str(protocol).map_err(|e| { | ||
HydrationError::InvalidData(format!( | ||
"Could not cast string ({}) to ProtocolInitializer. Error: '{e}'", | ||
protocol | ||
)) | ||
})?, | ||
epoch: Epoch(epoch_int.try_into().map_err(|e| { | ||
HydrationError::InvalidData(format!( | ||
"Could not cast i64 ({epoch_int}) to u64. Error: '{e}'" | ||
)) | ||
})?), | ||
created_at: DateTime::parse_from_rfc3339(datetime) | ||
.map_err(|e| { | ||
HydrationError::InvalidData(format!( | ||
"Could not turn string '{datetime}' to rfc3339 Datetime. Error: {e}" | ||
)) | ||
})? | ||
.with_timezone(&Utc), | ||
}; | ||
|
||
Ok(record) | ||
} | ||
|
||
fn get_projection() -> Projection { | ||
let mut projection = Projection::default(); | ||
projection.add_field("epoch", "{:protocol_initializer:}.epoch", "integer"); | ||
projection.add_field("protocol", "{:protocol_initializer:}.protocol", "integer"); | ||
projection.add_field("created_at", "{:protocol_initializer:}.created_at", "text"); | ||
|
||
projection | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
//! Signer related database repositories | ||
mod cardano_transaction_repository; | ||
mod protocol_initializer_repository; | ||
mod signed_beacon_repository; | ||
mod stake_pool_store; | ||
|
||
pub use protocol_initializer_repository::*; | ||
pub use signed_beacon_repository::*; | ||
pub use stake_pool_store::*; |
Oops, something went wrong.