Skip to content

Commit e808ce3

Browse files
authored
Merge pull request #1678 from input-output-hk/djo/common_ctx_block_range_roots_repository
Common Cardano transactions & Block range roots repository
2 parents dbe936c + bf20817 commit e808ce3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1046
-2380
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mithril-persistence/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-persistence"
3-
version = "0.1.10"
3+
version = "0.1.11"
44
description = "Common types, interfaces, and utilities to persist data for Mithril nodes."
55
authors = { workspace = true }
66
edition = { workspace = true }
@@ -16,7 +16,7 @@ anyhow = "1.0.79"
1616
async-trait = "0.1.77"
1717
chrono = { version = "0.4.33", features = ["serde"] }
1818
hex = "0.4.3"
19-
mithril-common = { path = "../../mithril-common" }
19+
mithril-common = { path = "../../mithril-common", features = ["fs"] }
2020
semver = "1.0.21"
2121
serde = { version = "1.0.196", features = ["derive"] }
2222
serde_json = "1.0.113"

mithril-signer/src/database/cardano_transaction_migration.rs renamed to internal/mithril-persistence/src/database/cardano_transaction_migration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Migration module for cardano transactions store
22
//!
3-
use mithril_persistence::database::SqlMigration;
3+
use crate::database::SqlMigration;
44

55
/// Get all the migrations required by this version of the software.
66
/// There shall be one migration per database version. There could be several

internal/mithril-persistence/src/database/signed_entity_hydrator.rs renamed to internal/mithril-persistence/src/database/hydrator.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1-
//! Signed Entity helpers for persistence
1+
//! Shared hydrator helpers for persistence
22
33
use mithril_common::entities::{
44
CardanoDbBeacon, Epoch, SignedEntityType, SignedEntityTypeDiscriminants,
55
};
66

77
use crate::sqlite::HydrationError;
88

9-
/// Helper struct to hydrate [SignedEntityType].
10-
pub struct SignedEntityTypeHydrator {}
9+
/// Helper struct to hydrate common data.
10+
pub struct Hydrator;
11+
12+
impl Hydrator {
13+
/// Read a signed entity beacon column from the database
14+
pub fn read_signed_entity_beacon_column<U: sqlite::RowIndex + Clone>(
15+
row: &sqlite::Row,
16+
column_index: U,
17+
) -> String {
18+
// We need to check first that the cell can be read as a string first
19+
// (e.g. when beacon json is '{"network": "dev", "epoch": 1, "immutable_file_number": 2}').
20+
// If it fails, we fallback on reading the cell as an integer (e.g. when beacon json is '5').
21+
// TODO: Maybe there is a better way of doing this.
22+
match row.try_read::<&str, _>(column_index.clone()) {
23+
Ok(value) => value.to_string(),
24+
Err(_) => (row.read::<i64, _>(column_index)).to_string(),
25+
}
26+
}
27+
28+
/// Try to convert an i64 field from the database to a u64
29+
pub fn try_to_u64(field: &str, value: i64) -> Result<u64, HydrationError> {
30+
u64::try_from(value)
31+
.map_err(|e|
32+
HydrationError::InvalidData(
33+
format!("Integer field {field} (value={value}) is incompatible with u64 representation. Error = {e}")
34+
)
35+
)
36+
}
1137

12-
impl SignedEntityTypeHydrator {
1338
/// Create a [SignedEntityType] from data coming from the database
14-
pub fn hydrate(
39+
pub fn hydrate_signed_entity_type(
1540
signed_entity_type_id: usize,
1641
beacon_str: &str,
1742
) -> Result<SignedEntityType, HydrationError> {
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
//! database module.
22
//! This module contains providers and entities shared between all application types.
33
4+
pub mod cardano_transaction_migration;
45
mod db_version;
5-
mod signed_entity_hydrator;
6+
mod hydrator;
7+
pub(crate) mod provider;
8+
pub mod record;
9+
pub mod repository;
610
mod version_checker;
711

12+
pub use db_version::*;
13+
pub use hydrator::Hydrator;
14+
pub use version_checker::{DatabaseVersionChecker, SqlMigration};
15+
816
/// Database version.
917
pub type DbVersion = i64;
1018

11-
pub use db_version::*;
12-
pub use signed_entity_hydrator::SignedEntityTypeHydrator;
13-
pub use version_checker::{DatabaseVersionChecker, SqlMigration};
19+
#[cfg(test)]
20+
pub mod test_helper {
21+
use sqlite::ConnectionThreadSafe;
22+
23+
use mithril_common::StdResult;
24+
25+
use crate::sqlite::{ConnectionBuilder, ConnectionOptions};
26+
27+
/// In-memory sqlite database without foreign key support with cardano db migrations applied
28+
pub fn cardano_tx_db_connection() -> StdResult<ConnectionThreadSafe> {
29+
let connection = ConnectionBuilder::open_memory()
30+
.with_options(&[ConnectionOptions::ForceDisableForeignKeys])
31+
.with_migrations(crate::database::cardano_transaction_migration::get_migrations())
32+
.build()?;
33+
Ok(connection)
34+
}
35+
}

mithril-signer/src/database/provider/block_range_root/get_block_range_root.rs renamed to internal/mithril-persistence/src/database/provider/block_range_root/get_block_range_root.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use mithril_common::entities::BlockNumber;
2-
use mithril_persistence::sqlite::{
3-
Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
4-
};
52
use sqlite::Value;
63

74
use crate::database::record::BlockRangeRootRecord;
5+
use crate::sqlite::{Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition};
86

97
/// Simple queries to retrieve [BlockRangeRootRecord] from the sqlite database.
108
pub struct GetBlockRangeRootProvider<'client> {
@@ -22,8 +20,7 @@ impl<'client> GetBlockRangeRootProvider<'client> {
2220
}
2321
}
2422

25-
#[cfg(test)]
26-
impl mithril_persistence::sqlite::GetAllCondition for GetBlockRangeRootProvider<'_> {}
23+
impl crate::sqlite::GetAllCondition for GetBlockRangeRootProvider<'_> {}
2724

2825
impl<'client> Provider<'client> for GetBlockRangeRootProvider<'client> {
2926
type Entity = BlockRangeRootRecord;

mithril-signer/src/database/provider/block_range_root/get_interval_without_block_range_provider.rs renamed to internal/mithril-persistence/src/database/provider/block_range_root/get_interval_without_block_range_provider.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use mithril_persistence::sqlite::{
2-
Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
3-
};
4-
51
use crate::database::record::IntervalWithoutBlockRangeRootRecord;
2+
use crate::sqlite::{Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition};
63

74
/// Query that return the interval of block numbers that does not have a block range root.
85
pub struct GetIntervalWithoutBlockRangeRootProvider<'client> {

mithril-aggregator/src/database/provider/block_range_root/insert_block_range.rs renamed to internal/mithril-persistence/src/database/provider/block_range_root/insert_block_range.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ use std::iter::repeat;
33
use sqlite::Value;
44

55
use mithril_common::StdResult;
6-
use mithril_persistence::sqlite::{
7-
Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
8-
};
96

107
use crate::database::record::BlockRangeRootRecord;
8+
use crate::sqlite::{Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition};
119

1210
/// Query to insert [BlockRangeRootRecord] in the sqlite database
1311
pub struct InsertBlockRangeRootProvider<'client> {

mithril-aggregator/src/database/provider/cardano_transaction/get_cardano_transaction.rs renamed to internal/mithril-persistence/src/database/provider/cardano_transaction/get_cardano_transaction.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ use std::ops::Range;
33
use sqlite::Value;
44

55
use mithril_common::entities::{BlockNumber, BlockRange, TransactionHash};
6-
#[cfg(test)]
7-
use mithril_persistence::sqlite::GetAllCondition;
8-
use mithril_persistence::sqlite::{
9-
Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
10-
};
116

127
use crate::database::record::CardanoTransactionRecord;
8+
use crate::sqlite::{
9+
GetAllCondition, Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
10+
};
1311

1412
/// Simple queries to retrieve [CardanoTransaction] from the sqlite database.
1513
pub struct GetCardanoTransactionProvider<'client> {
@@ -90,5 +88,4 @@ impl<'client> Provider<'client> for GetCardanoTransactionProvider<'client> {
9088
}
9189
}
9290

93-
#[cfg(test)]
9491
impl GetAllCondition for GetCardanoTransactionProvider<'_> {}

0 commit comments

Comments
 (0)