diff --git a/Cargo.lock b/Cargo.lock index 057d05b2854a8226f99a5ee248795bbd9d1e9a59..ab8a21db840eb74527a1d5d58c23dfbb2fb2646d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1069,7 +1069,6 @@ dependencies = [ "dubp", "duniter-dbs", "duniter-dbs-write-ops", - "duniter-gva", "duniter-gva-db-writer", "fast-threadpool", "flume", @@ -1155,7 +1154,6 @@ dependencies = [ "http", "log", "mockall", - "once_cell", "resiter", "serde", "serde_json", @@ -1173,6 +1171,7 @@ dependencies = [ "dubp", "duniter-dbs", "maplit", + "once_cell", "resiter", "smallvec", ] diff --git a/rust-bins/duniter-dbex/Cargo.toml b/rust-bins/duniter-dbex/Cargo.toml index 9cca136c239cc817011635e0b25026694382cedb..3c79fd4e24d8059fb0f028b010111328aae5112b 100644 --- a/rust-bins/duniter-dbex/Cargo.toml +++ b/rust-bins/duniter-dbex/Cargo.toml @@ -25,7 +25,6 @@ dirs = "3.0.1" dubp = { version = "0.34.0" } duniter-dbs = { path = "../../rust-libs/duniter-dbs", default-features = false, features = ["explorer", "leveldb_backend", "sled_backend"] } duniter-dbs-write-ops = { path = "../../rust-libs/duniter-dbs-write-ops", default-features = false, features = ["explorer", "leveldb_backend", "sled_backend"] } -duniter-gva = { path = "../../rust-libs/modules/gva" } duniter-gva-db-writer = { path = "../../rust-libs/modules/gva/db-writer" } fast-threadpool = "0.2.2" flume = "0.9.1" diff --git a/rust-bins/duniter-dbex/src/migrate.rs b/rust-bins/duniter-dbex/src/migrate.rs index 69699aad8ee66af6262b2655bf693e93927634ae..b7d249987b76757dedf86d6ec04ae27220c60275 100644 --- a/rust-bins/duniter-dbex/src/migrate.rs +++ b/rust-bins/duniter-dbex/src/migrate.rs @@ -27,7 +27,7 @@ const CHUNK_SIZE: usize = 250; pub(crate) fn migrate(profile_path: PathBuf) -> anyhow::Result<()> { let start_time = Instant::now(); let (bc_db, shared_dbs) = duniter_dbs::open_dbs(Some(profile_path.as_path())); - let gva_db = duniter_gva::GvaModule::get_gva_db_rw(Some(profile_path.as_path())); + let gva_db = duniter_gva_db_writer::get_gva_db_rw(Some(profile_path.as_path())); // Clear bc_db and gva_db bc_db.clear()?; diff --git a/rust-libs/modules/gva/Cargo.toml b/rust-libs/modules/gva/Cargo.toml index 5695a8cba0e3d18b9352e6ac4d304441dccedef4..f4b4175730bff55394ab9d1853b2bc21bc83c004 100644 --- a/rust-libs/modules/gva/Cargo.toml +++ b/rust-libs/modules/gva/Cargo.toml @@ -24,7 +24,6 @@ flume = "0.9.1" futures = "0.3.6" http = "0.2.1" log = "0.4.11" -once_cell = "1.5.2" resiter = "0.4.0" serde = { version = "1.0.105", features = ["derive"] } serde_urlencoded = "0.7.0" diff --git a/rust-libs/modules/gva/db-writer/Cargo.toml b/rust-libs/modules/gva/db-writer/Cargo.toml index 9c2e2ec0e114656626ce8d6d01ec5d2bfe675189..ff254bd64f42e024a559c39ada2ddf98a914b33f 100644 --- a/rust-libs/modules/gva/db-writer/Cargo.toml +++ b/rust-libs/modules/gva/db-writer/Cargo.toml @@ -15,6 +15,7 @@ path = "src/lib.rs" anyhow = "1.0.34" duniter-dbs = { path = "../../../duniter-dbs" } dubp = { version = "0.34.0" } +once_cell = "1.5.2" resiter = "0.4.0" [dev-dependencies] diff --git a/rust-libs/modules/gva/db-writer/src/lib.rs b/rust-libs/modules/gva/db-writer/src/lib.rs index 36df7c5a8ad531ef27f7c51f478e1d5d3feca01a..9ed890d4b7090abbe9a9cb44e518bc5f84a81863 100644 --- a/rust-libs/modules/gva/db-writer/src/lib.rs +++ b/rust-libs/modules/gva/db-writer/src/lib.rs @@ -37,10 +37,32 @@ use duniter_dbs::databases::gva_v1::*; use duniter_dbs::{ databases::gva_v1::{GvaV1Db, GvaV1DbReadable, GvaV1DbWritable}, kv_typed::prelude::*, - HashKeyV2, PubKeyKeyV2, SourceAmountValV2, TxDbV2, WalletConditionsV2, + prelude::*, + FileBackend, HashKeyV2, PubKeyKeyV2, SourceAmountValV2, TxDbV2, WalletConditionsV2, }; use resiter::filter::Filter; -use std::collections::{BTreeSet, HashMap}; +use std::{ + collections::{BTreeSet, HashMap}, + path::Path, +}; + +static GVA_DB_RO: once_cell::sync::OnceCell<GvaV1DbRo<FileBackend>> = + once_cell::sync::OnceCell::new(); +static GVA_DB_RW: once_cell::sync::OnceCell<GvaV1Db<FileBackend>> = + once_cell::sync::OnceCell::new(); + +pub fn get_gva_db_ro(profile_path_opt: Option<&Path>) -> &'static GvaV1DbRo<FileBackend> { + GVA_DB_RO.get_or_init(|| get_gva_db_rw(profile_path_opt).get_ro_handler()) +} +pub fn get_gva_db_rw(profile_path_opt: Option<&Path>) -> &'static GvaV1Db<FileBackend> { + GVA_DB_RW.get_or_init(|| { + duniter_dbs::databases::gva_v1::GvaV1Db::<FileBackend>::open(FileBackend::gen_backend_conf( + "gva_v1", + profile_path_opt, + )) + .expect("Fail to open GVA DB") + }) +} pub struct UtxoV10<'s> { pub id: UtxoIdV10, diff --git a/rust-libs/modules/gva/src/lib.rs b/rust-libs/modules/gva/src/lib.rs index 6a6767261fc60ae1a8d9679308d12bb552ee683d..1b02143b13df8d29f7e26d27e5b34a0803bfe8e2 100644 --- a/rust-libs/modules/gva/src/lib.rs +++ b/rust-libs/modules/gva/src/lib.rs @@ -60,11 +60,12 @@ use dubp::documents_parser::prelude::*; use dubp::wallet::prelude::*; use dubp::{block::DubpBlockV10, crypto::hashs::Hash}; use duniter_dbs::databases::{ - gva_v1::{GvaV1Db, GvaV1DbReadable, GvaV1DbRo, GvaV1DbWritable}, + gva_v1::{GvaV1DbReadable, GvaV1DbRo}, txs_mp_v2::TxsMpV2DbReadable, }; use duniter_dbs::prelude::*; use duniter_dbs::{kv_typed::prelude::*, FileBackend, TxDbV2}; +use duniter_gva_db_writer::{get_gva_db_ro, get_gva_db_rw}; #[cfg(not(test))] use duniter_gva_dbs_reader::create_dbs_reader; #[cfg(not(test))] @@ -80,11 +81,6 @@ use std::{ }; use warp::{http::Response as HttpResponse, Filter as _, Rejection, Stream}; -static GVA_DB_RO: once_cell::sync::OnceCell<GvaV1DbRo<FileBackend>> = - once_cell::sync::OnceCell::new(); -static GVA_DB_RW: once_cell::sync::OnceCell<GvaV1Db<FileBackend>> = - once_cell::sync::OnceCell::new(); - #[derive(Debug)] pub struct GvaModule { conf: Option<GvaConf>, @@ -104,7 +100,7 @@ impl duniter_module::DuniterModule for GvaModule { dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>, profile_path_opt: Option<&Path>, ) -> Result<Option<JoinHandle<KvResult<()>>>, ThreadPoolDisconnected> { - let gva_db = GvaModule::get_gva_db_rw(profile_path_opt); + let gva_db = get_gva_db_rw(profile_path_opt); if conf.gva.is_some() { Ok(Some(dbs_pool.launch(move |_| { duniter_gva_db_writer::apply_block(&block, gva_db) @@ -119,7 +115,7 @@ impl duniter_module::DuniterModule for GvaModule { dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>, profile_path_opt: Option<&Path>, ) -> Result<Option<JoinHandle<KvResult<()>>>, ThreadPoolDisconnected> { - let gva_db = GvaModule::get_gva_db_rw(profile_path_opt); + let gva_db = get_gva_db_rw(profile_path_opt); if conf.gva.is_some() { Ok(Some(dbs_pool.launch(move |_| { for block in blocks.deref() { @@ -137,7 +133,7 @@ impl duniter_module::DuniterModule for GvaModule { dbs_pool: &fast_threadpool::ThreadPoolSyncHandler<SharedDbs<FileBackend>>, profile_path_opt: Option<&Path>, ) -> Result<Option<JoinHandle<KvResult<()>>>, ThreadPoolDisconnected> { - let gva_db = GvaModule::get_gva_db_rw(profile_path_opt); + let gva_db = get_gva_db_rw(profile_path_opt); if conf.gva.is_some() { Ok(Some(dbs_pool.launch(move |_| { duniter_gva_db_writer::revert_block(&block, gva_db) @@ -185,7 +181,7 @@ impl duniter_module::DuniterModule for GvaModule { conf: conf.gva.to_owned(), currency: currency.to_owned(), dbs_pool: dbs_pool.to_owned(), - gva_db_ro: GvaModule::get_gva_db_ro(profile_path_opt), + gva_db_ro: get_gva_db_ro(profile_path_opt), mempools, self_pubkey: conf.self_key_pair.public_key(), software_version, @@ -228,7 +224,7 @@ impl duniter_module::DuniterModule for GvaModule { profile_path_opt: Option<&Path>, pubkey: PublicKey, ) -> KvResult<Option<duniter_module::TxsHistoryForBma>> { - let gva_db = GvaModule::get_gva_db_ro(profile_path_opt); + let gva_db = get_gva_db_ro(profile_path_opt); let duniter_gva_dbs_reader::txs_history::TxsHistory { sent, received, @@ -274,7 +270,7 @@ impl duniter_module::DuniterModule for GvaModule { hash: Hash, profile_path_opt: Option<&Path>, ) -> KvResult<Option<(TransactionDocumentV10, Option<BlockNumber>)>> { - let gva_db = GvaModule::get_gva_db_ro(profile_path_opt); + let gva_db = get_gva_db_ro(profile_path_opt); dbs_pool .execute(move |dbs| { if let Some(tx) = dbs.txs_mp_db.txs().get(&duniter_dbs::HashKeyV2(hash))? { @@ -290,18 +286,6 @@ impl duniter_module::DuniterModule for GvaModule { } impl GvaModule { - fn get_gva_db_ro(profile_path_opt: Option<&Path>) -> &'static GvaV1DbRo<FileBackend> { - use duniter_dbs::databases::gva_v1::GvaV1DbWritable as _; - GVA_DB_RO.get_or_init(|| GvaModule::get_gva_db_rw(profile_path_opt).get_ro_handler()) - } - pub fn get_gva_db_rw(profile_path_opt: Option<&Path>) -> &'static GvaV1Db<FileBackend> { - GVA_DB_RW.get_or_init(|| { - duniter_dbs::databases::gva_v1::GvaV1Db::<FileBackend>::open( - FileBackend::gen_backend_conf("gva_v1", profile_path_opt), - ) - .expect("Fail to open GVA DB") - }) - } async fn start_inner( conf: GvaConf, currency: String,