From 751a371a8540798ccf28e48fef74f1211ee464b0 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Wed, 12 Feb 2025 17:04:11 +0100 Subject: [PATCH] Create a `mithril-resource-pool` module --- Cargo.lock | 13 +++++++++ Cargo.toml | 1 + internal/mithril-persistence/Cargo.toml | 1 + .../src/sqlite/connection_pool.rs | 6 ++-- internal/mithril-resource-pool/Cargo.toml | 23 +++++++++++++++ internal/mithril-resource-pool/Makefile | 19 +++++++++++++ internal/mithril-resource-pool/README.md | 5 ++++ internal/mithril-resource-pool/src/lib.rs | 5 ++++ .../src/resource_pool.rs | 28 +++++++++---------- mithril-aggregator/Cargo.toml | 1 + mithril-aggregator/src/services/prover.rs | 2 +- .../src/crypto_helper/merkle_map.rs | 2 +- mithril-common/src/lib.rs | 14 +++++++++- 13 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 internal/mithril-resource-pool/Cargo.toml create mode 100644 internal/mithril-resource-pool/Makefile create mode 100644 internal/mithril-resource-pool/README.md create mode 100644 internal/mithril-resource-pool/src/lib.rs rename {mithril-common => internal/mithril-resource-pool}/src/resource_pool.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index 24271e1703..b0c80b85a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3658,6 +3658,7 @@ dependencies = [ "mithril-doc", "mithril-metric", "mithril-persistence", + "mithril-resource-pool", "mockall", "paste", "prometheus", @@ -3920,6 +3921,7 @@ dependencies = [ "chrono", "hex", "mithril-common", + "mithril-resource-pool", "mockall", "semver", "serde", @@ -3956,6 +3958,17 @@ dependencies = [ "warp", ] +[[package]] +name = "mithril-resource-pool" +version = "0.0.1" +dependencies = [ + "anyhow", + "mithril-common", + "mockall", + "thiserror 2.0.11", + "tokio", +] + [[package]] name = "mithril-signer" version = "0.2.228" diff --git a/Cargo.toml b/Cargo.toml index a75d9437ed..fabf2dfd6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "internal/mithril-doc-derive", "internal/mithril-metric", "internal/mithril-persistence", + "internal/mithril-resource-pool", "mithril-aggregator", "mithril-client", "mithril-client-cli", diff --git a/internal/mithril-persistence/Cargo.toml b/internal/mithril-persistence/Cargo.toml index 7e189ab706..53d34b852e 100644 --- a/internal/mithril-persistence/Cargo.toml +++ b/internal/mithril-persistence/Cargo.toml @@ -17,6 +17,7 @@ async-trait = "0.1.86" chrono = { version = "0.4.39", features = ["serde"] } hex = "0.4.3" mithril-common = { path = "../../mithril-common", features = ["fs"] } +mithril-resource-pool = { path = "../mithril-resource-pool" } semver = "1.0.25" serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.138" diff --git a/internal/mithril-persistence/src/sqlite/connection_pool.rs b/internal/mithril-persistence/src/sqlite/connection_pool.rs index 67c0b451ac..77120af8a8 100644 --- a/internal/mithril-persistence/src/sqlite/connection_pool.rs +++ b/internal/mithril-persistence/src/sqlite/connection_pool.rs @@ -1,9 +1,7 @@ use std::{ops::Deref, time::Duration}; -use mithril_common::{ - resource_pool::{Reset, ResourcePool, ResourcePoolItem}, - StdResult, -}; +use mithril_common::{Reset, StdResult}; +use mithril_resource_pool::resource_pool::{ResourcePool, ResourcePoolItem}; use crate::sqlite::SqliteConnection; diff --git a/internal/mithril-resource-pool/Cargo.toml b/internal/mithril-resource-pool/Cargo.toml new file mode 100644 index 0000000000..9ee27a412c --- /dev/null +++ b/internal/mithril-resource-pool/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "mithril-resource-pool" +version = "0.0.1" +description = "Common types, interfaces, and utilities to persist data for Mithril nodes." +authors = { workspace = true } +edition = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +repository = { workspace = true } + +[lib] +crate-type = ["lib", "cdylib", "staticlib"] + +[dependencies] +anyhow = "1.0.95" +mithril-common = { path = "../../mithril-common", features = ["fs"] } +thiserror = "2.0.11" +tokio = { version = "1.43.0", features = ["sync"] } + +[dev-dependencies] +mithril-common = { path = "../../mithril-common", features = ["test_tools"] } +mockall = "0.13.1" +tokio = { version = "1.43.0", features = ["macros", "time"] } diff --git a/internal/mithril-resource-pool/Makefile b/internal/mithril-resource-pool/Makefile new file mode 100644 index 0000000000..adc3e3d5c1 --- /dev/null +++ b/internal/mithril-resource-pool/Makefile @@ -0,0 +1,19 @@ +.PHONY: all build test check doc + +CARGO = cargo + +all: test build + +build: + ${CARGO} build --release + +test: + ${CARGO} test + +check: + ${CARGO} check --release --all-features --all-targets + ${CARGO} clippy --release --all-features --all-targets + ${CARGO} fmt --check + +doc: + ${CARGO} doc --no-deps --open --features full diff --git a/internal/mithril-resource-pool/README.md b/internal/mithril-resource-pool/README.md new file mode 100644 index 0000000000..3da6284567 --- /dev/null +++ b/internal/mithril-resource-pool/README.md @@ -0,0 +1,5 @@ +# Mithril-resource-pool + +**This is a work in progress** 🛠 + +This crate contains ... diff --git a/internal/mithril-resource-pool/src/lib.rs b/internal/mithril-resource-pool/src/lib.rs new file mode 100644 index 0000000000..64c5a7a0f4 --- /dev/null +++ b/internal/mithril-resource-pool/src/lib.rs @@ -0,0 +1,5 @@ +#![warn(missing_docs)] + +//! TODO +//! +pub mod resource_pool; diff --git a/mithril-common/src/resource_pool.rs b/internal/mithril-resource-pool/src/resource_pool.rs similarity index 96% rename from mithril-common/src/resource_pool.rs rename to internal/mithril-resource-pool/src/resource_pool.rs index 2e85cc8747..8b2ef9674e 100644 --- a/mithril-common/src/resource_pool.rs +++ b/internal/mithril-resource-pool/src/resource_pool.rs @@ -9,7 +9,7 @@ use std::{ }; use thiserror::Error; -use crate::StdResult; +use mithril_common::{Reset, StdResult}; /// [ResourcePool] related errors. #[derive(Error, Debug)] @@ -207,19 +207,19 @@ impl Drop for ResourcePoolItem<'_, T> { } } -/// Reset trait which is implemented by pooled resource items. -/// As pool resource items are mutable, this will guarantee that the pool stays consistent -/// and that acquired resource items do not depend from previous computations. -pub trait Reset { - /// Reset the resource - fn reset(&mut self) -> StdResult<()> { - Ok(()) - } -} - -cfg_test_tools! { - impl Reset for String {} -} +// /// Reset trait which is implemented by pooled resource items. +// /// As pool resource items are mutable, this will guarantee that the pool stays consistent +// /// and that acquired resource items do not depend from previous computations. +// pub trait Reset { +// /// Reset the resource +// fn reset(&mut self) -> StdResult<()> { +// Ok(()) +// } +// } + +// cfg_test_tools! { +// impl Reset for String {} +// } #[cfg(test)] mod tests { diff --git a/mithril-aggregator/Cargo.toml b/mithril-aggregator/Cargo.toml index bb81b187b0..243a220de8 100644 --- a/mithril-aggregator/Cargo.toml +++ b/mithril-aggregator/Cargo.toml @@ -30,6 +30,7 @@ mithril-common = { path = "../mithril-common", features = ["full"] } mithril-doc = { path = "../internal/mithril-doc" } mithril-metric = { path = "../internal/mithril-metric" } mithril-persistence = { path = "../internal/mithril-persistence" } +mithril-resource-pool = { path = "../internal/mithril-resource-pool" } paste = "1.0.15" prometheus = "0.13.4" rayon = "1.10.0" diff --git a/mithril-aggregator/src/services/prover.rs b/mithril-aggregator/src/services/prover.rs index 444c09b63a..52edb18787 100644 --- a/mithril-aggregator/src/services/prover.rs +++ b/mithril-aggregator/src/services/prover.rs @@ -13,10 +13,10 @@ use mithril_common::{ BlockNumber, BlockRange, CardanoTransaction, CardanoTransactionsSetProof, TransactionHash, }, logging::LoggerExtensions, - resource_pool::ResourcePool, signable_builder::BlockRangeRootRetriever, StdResult, }; +use mithril_resource_pool::resource_pool::ResourcePool; /// Prover service is the cryptographic engine in charge of producing cryptographic proofs for transactions #[cfg_attr(test, mockall::automock)] diff --git a/mithril-common/src/crypto_helper/merkle_map.rs b/mithril-common/src/crypto_helper/merkle_map.rs index 025722807e..ebc022aa6f 100644 --- a/mithril-common/src/crypto_helper/merkle_map.rs +++ b/mithril-common/src/crypto_helper/merkle_map.rs @@ -8,7 +8,7 @@ use std::{ sync::Arc, }; -use crate::{resource_pool::Reset, StdError, StdResult}; +use crate::{Reset, StdError, StdResult}; use super::{MKProof, MKTree, MKTreeNode, MKTreeStorer}; diff --git a/mithril-common/src/lib.rs b/mithril-common/src/lib.rs index e5b94fb110..420343c103 100644 --- a/mithril-common/src/lib.rs +++ b/mithril-common/src/lib.rs @@ -40,7 +40,6 @@ pub mod era; pub mod logging; pub mod messages; pub mod protocol; -pub mod resource_pool; pub mod signable_builder; pub mod signed_entity_type_lock; @@ -72,6 +71,19 @@ pub const MITHRIL_API_VERSION_HEADER: &str = "mithril-api-version"; /// Mithril Signer node version header name pub const MITHRIL_SIGNER_VERSION_HEADER: &str = "signer-node-version"; +/// Reset trait which is implemented by pooled resource items. +/// As pool resource items are mutable, this will guarantee that the pool stays consistent +/// and that acquired resource items do not depend from previous computations. +pub trait Reset { + /// Reset the resource + fn reset(&mut self) -> StdResult<()> { + Ok(()) + } +} + +cfg_test_tools! { + impl Reset for String {} +} #[cfg(test)] mod tests { #[cfg(feature = "apispec")]