diff --git a/Cargo.lock b/Cargo.lock index dc959cf6c68125eab1e3e61444f9e855a2a02c82..9eba35da14339e19f2dfd3ad0800903394e74d85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ "juniper-from-schema 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "mockall 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "once_cell 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2029,6 +2030,11 @@ name = "numtoa" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "once_cell" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "opaque-debug" version = "0.2.3" @@ -3348,6 +3354,7 @@ dependencies = [ "checksum num-traits 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c81ffc11c212fa327657cb19dd85eb7419e163b5b076bede2bdb5c974c07e4" "checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72" "checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" +"checksum once_cell 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b1c601810575c99596d4afc46f78a678c80105117c379eb3650cf99b8a21ce5b" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" "checksum openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3a3cc5799d98e1088141b8e01ff760112bbd9f19d850c124500566ca6901a585" "checksum openssl-sys 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)" = "465d16ae7fc0e313318f7de5cecf57b2fbe7511fd213978b457e1c96ff46736f" diff --git a/lib/modules-lib/bc-db-reader/src/lib.rs b/lib/modules-lib/bc-db-reader/src/lib.rs index 7a0f0d271c1c56b9bec59c341f9d3850569ee215..7ac5131612988de39f85bc177deaa9efd1450746 100644 --- a/lib/modules-lib/bc-db-reader/src/lib.rs +++ b/lib/modules-lib/bc-db-reader/src/lib.rs @@ -85,6 +85,15 @@ where pub r: Reader<'r>, } +impl<'r, 'db: 'r, DB> std::fmt::Debug for BcDbWithReaderStruct<'r, 'db, DB> +where + DB: DbReadable, +{ + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "BcDbWithReaderStruct {{}}") + } +} + pub type BcDbRoWithReader<'r, 'db> = BcDbWithReaderStruct<'r, 'db, BcDbRo>; impl<'r, 'db: 'r, DB> BcDbWithReader for BcDbWithReaderStruct<'r, 'db, DB> diff --git a/lib/modules-lib/bc-db-reader/src/traits.rs b/lib/modules-lib/bc-db-reader/src/traits.rs index 06e4ecc773b44eca8017f01af5ae7126516c340e..5eda50b6a841a2a223c0b5d576a7ff16a5da5fa1 100644 --- a/lib/modules-lib/bc-db-reader/src/traits.rs +++ b/lib/modules-lib/bc-db-reader/src/traits.rs @@ -50,7 +50,7 @@ where } } -pub trait BcDbWithReader { +pub trait BcDbWithReader: std::fmt::Debug { type DB: DbReadable; type R: DbReader; @@ -71,6 +71,13 @@ impl<'a> BcDbWithReader for MockBcDbInReadTx { } } +#[cfg(feature = "mock")] +impl std::fmt::Debug for MockBcDbInReadTx { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "MockBcDbInReadTx {{}}") + } +} + #[cfg_attr(feature = "mock", automock)] pub trait BcDbInReadTx: BcDbWithReader { fn get_current_blockstamp(&self) -> Result<Option<Blockstamp>, DbError>; diff --git a/lib/modules/blockchain/bc-db-writer/src/lib.rs b/lib/modules/blockchain/bc-db-writer/src/lib.rs index 853291e68a84ab70e4ca76ffa51dbcf756598a25..5924e4bd325d43a3dc210b16c67d1523ba832d13 100644 --- a/lib/modules/blockchain/bc-db-writer/src/lib.rs +++ b/lib/modules/blockchain/bc-db-writer/src/lib.rs @@ -81,6 +81,12 @@ pub struct BcDbRwWithWriter<'w, 'db: 'w> { pub w: &'w DbWriter<'w>, } +impl<'w, 'db: 'w> std::fmt::Debug for BcDbRwWithWriter<'w, 'db> { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "BcDbRwWithWriter {{}}") + } +} + impl<'w, 'db: 'w> durs_bc_db_reader::BcDbWithReader for BcDbRwWithWriter<'w, 'db> { type DB = Db; type R = DbWriter<'w>; diff --git a/lib/modules/gva/Cargo.toml b/lib/modules/gva/Cargo.toml index de838d2dce4e402fa04bccfc8346cc2eb8ce93dc..db767b05101d50b4ca0a821aafd1853ceddffc33 100644 --- a/lib/modules/gva/Cargo.toml +++ b/lib/modules/gva/Cargo.toml @@ -29,6 +29,7 @@ failure = "0.1.5" juniper = "0.14.1" juniper-from-schema = "0.5.0" log = "0.4.8" +once_cell = "1.3.1" serde = "1.0.102" serde_derive = "1.0.102" serde_json = "1.0.41" diff --git a/lib/modules/gva/src/context.rs b/lib/modules/gva/src/context.rs index 306f68903773001610d1327f08d748ae235b93c1..f6e68fbc9cc47c2cb3dfdee0877d1b8e5bb4d971 100644 --- a/lib/modules/gva/src/context.rs +++ b/lib/modules/gva/src/context.rs @@ -17,9 +17,10 @@ use crate::db::BcDbRo; use crate::schema::Schema; +use once_cell::sync::OnceCell; pub struct GlobalContext { - db: &'static BcDbRo, + db: &'static OnceCell<BcDbRo>, pub(crate) schema: Schema, software_name: &'static str, software_version: &'static str, @@ -27,7 +28,7 @@ pub struct GlobalContext { impl GlobalContext { pub(crate) fn new( - db: &'static BcDbRo, + db: &'static OnceCell<BcDbRo>, schema: Schema, software_name: &'static str, software_version: &'static str, @@ -42,7 +43,7 @@ impl GlobalContext { } pub struct QueryContext { - db: &'static BcDbRo, + db: &'static OnceCell<BcDbRo>, software_name: &'static str, software_version: &'static str, } @@ -52,7 +53,7 @@ impl juniper::Context for QueryContext {} impl From<&GlobalContext> for QueryContext { fn from(global_context: &GlobalContext) -> Self { QueryContext { - db: global_context.db, + db: &global_context.db, software_name: global_context.software_name, software_version: global_context.software_version, } @@ -61,7 +62,7 @@ impl From<&GlobalContext> for QueryContext { impl QueryContext { pub(crate) fn get_db(&self) -> &BcDbRo { - &self.db + &self.db.get().expect("DB not initialised.") } pub fn get_software_name(&self) -> &'static str { diff --git a/lib/modules/gva/src/lib.rs b/lib/modules/gva/src/lib.rs index 16666c20f51c5753d29e14dec0b9e47a35e24d0b..5404c506827197fe48742eb46af059741c3ff9e4 100644 --- a/lib/modules/gva/src/lib.rs +++ b/lib/modules/gva/src/lib.rs @@ -30,6 +30,7 @@ missing_copy_implementations, trivial_casts, trivial_numeric_casts, + unsafe_code, unstable_features, unused_import_braces, unused_qualifications diff --git a/lib/modules/gva/src/schema/queries.rs b/lib/modules/gva/src/schema/queries.rs index 812bf8bbb02631eb3dc1812aad53590b8ea029e6..d888abba4781b2753c8dc3d900800a6e17203a8a 100644 --- a/lib/modules/gva/src/schema/queries.rs +++ b/lib/modules/gva/src/schema/queries.rs @@ -31,18 +31,19 @@ mod tests { use actix_web::web; use assert_json_diff::assert_json_eq; use juniper::http::GraphQLRequest; + use once_cell::sync::OnceCell; use std::sync::Arc; pub(crate) fn setup( mock_db: BcDbRo, - db_container: &'static mut Option<BcDbRo>, + db_container: &'static OnceCell<BcDbRo>, ) -> web::Data<Arc<GlobalContext>> { // Give a static lifetime to the DB - let db = durs_common_tools::fns::r#static::to_static_ref(mock_db, db_container); + db_container.set(mock_db).expect("DB already initialized."); // Init global context web::Data::new(std::sync::Arc::new(GlobalContext::new( - db, + &db_container, create_schema(), "soft_name", "soft_version", diff --git a/lib/modules/gva/src/schema/queries/block.rs b/lib/modules/gva/src/schema/queries/block.rs index 48278ce323200fcbca51f315e02e6f5f5bb0209d..8746aa5092b701f1d4707c5f48f6707e6093227f 100644 --- a/lib/modules/gva/src/schema/queries/block.rs +++ b/lib/modules/gva/src/schema/queries/block.rs @@ -48,9 +48,10 @@ mod tests { use dup_crypto_tests_tools::mocks::{hash, pubkey}; use durs_bc_db_reader::blocks::BlockDb; use mockall::predicate::eq; + use once_cell::sync::OnceCell; use serde_json::json; - static mut DB_BLOCK_1: Option<BcDbRo> = None; + static DB_BLOCK_1: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_block() { @@ -82,7 +83,7 @@ mod tests { .with(eq(pubkey('B'))) .returning(|_| Ok(Some("issuerName".to_owned()))); - let schema = tests::setup(mock_db, unsafe { &mut DB_BLOCK_1 }); + let schema = tests::setup(mock_db, &DB_BLOCK_1); tests::test_gql_query( schema.clone(), diff --git a/lib/modules/gva/src/schema/queries/blocks.rs b/lib/modules/gva/src/schema/queries/blocks.rs index d2bdb99795187f6e3659a832aa866a645ba902bc..9e7a25c5c08b275251218fd882bd0f66a8c3ac5a 100644 --- a/lib/modules/gva/src/schema/queries/blocks.rs +++ b/lib/modules/gva/src/schema/queries/blocks.rs @@ -108,6 +108,7 @@ mod tests { use dup_crypto_tests_tools::mocks::{hash, pubkey}; use durs_bc_db_reader::blocks::BlockDb; use mockall::predicate::eq; + use once_cell::sync::OnceCell; use serde_json::json; fn block_0() -> BlockDocumentV10 { @@ -225,7 +226,7 @@ mod tests { }) } - static mut DB_TEST_BLOCKS_FROM_2: Option<BcDbRo> = None; + static DB_TEST_BLOCKS_FROM_2: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_blocks_from_2() { @@ -261,7 +262,7 @@ mod tests { ]) }); - let schema = tests::setup(mock_db, unsafe { &mut DB_TEST_BLOCKS_FROM_2 }); + let schema = tests::setup(mock_db, &DB_TEST_BLOCKS_FROM_2); tests::test_gql_query( schema, @@ -288,7 +289,7 @@ mod tests { ); } - static mut DB_TEST_BLOCKS_STEP_2: Option<BcDbRo> = None; + static DB_TEST_BLOCKS_STEP_2: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_blocks_with_step_2() { @@ -319,7 +320,7 @@ mod tests { ]) }); - let schema = tests::setup(mock_db, unsafe { &mut DB_TEST_BLOCKS_STEP_2 }); + let schema = tests::setup(mock_db, &DB_TEST_BLOCKS_STEP_2); tests::test_gql_query( schema, @@ -357,7 +358,7 @@ mod tests { ); } - static mut DB_TEST_BLOCKS_DESC: Option<BcDbRo> = None; + static DB_TEST_BLOCKS_DESC: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_blocks_order_desc() { @@ -393,7 +394,7 @@ mod tests { ]) }); - let global_context = tests::setup(mock_db, unsafe { &mut DB_TEST_BLOCKS_DESC }); + let global_context = tests::setup(mock_db, &DB_TEST_BLOCKS_DESC); tests::test_gql_query( global_context, @@ -420,7 +421,7 @@ mod tests { ); } - static mut DB_TEST_BLOCKS: Option<BcDbRo> = None; + static DB_TEST_BLOCKS: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_blocks() { @@ -456,7 +457,7 @@ mod tests { ]) }); - let schema = tests::setup(mock_db, unsafe { &mut DB_TEST_BLOCKS }); + let schema = tests::setup(mock_db, &DB_TEST_BLOCKS); tests::test_gql_query( schema, diff --git a/lib/modules/gva/src/schema/queries/current.rs b/lib/modules/gva/src/schema/queries/current.rs index 02757c23b9d0bd294106c8b4b7158b5618e2ea75..8ced281c0796d75a98630186463a8e685f8c8484 100644 --- a/lib/modules/gva/src/schema/queries/current.rs +++ b/lib/modules/gva/src/schema/queries/current.rs @@ -40,9 +40,10 @@ mod tests { use dup_crypto_tests_tools::mocks::{hash, pubkey}; use durs_bc_db_reader::blocks::BlockDb; use mockall::predicate::eq; + use once_cell::sync::OnceCell; use serde_json::json; - static mut DB_TEST_CURRENT_1: Option<BcDbRo> = None; + static DB_TEST_CURRENT_1: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_current() { @@ -70,7 +71,7 @@ mod tests { .with(eq(pubkey('B'))) .returning(|_| Ok(Some("issuerName".to_owned()))); - let schema = tests::setup(mock_db, unsafe { &mut DB_TEST_CURRENT_1 }); + let schema = tests::setup(mock_db, &DB_TEST_CURRENT_1); tests::test_gql_query( schema, diff --git a/lib/modules/gva/src/schema/queries/current_ud.rs b/lib/modules/gva/src/schema/queries/current_ud.rs index b885e1512a4e98675f20ad63532f35b382788776..a553b2bb03933dc26d4c7503883b8271d888b21c 100644 --- a/lib/modules/gva/src/schema/queries/current_ud.rs +++ b/lib/modules/gva/src/schema/queries/current_ud.rs @@ -31,9 +31,10 @@ mod tests { use crate::schema::queries::tests; use dubp_common_doc::BlockNumber; use durs_bc_db_reader::current_metadata::current_ud::CurrentUdDb; + use once_cell::sync::OnceCell; use serde_json::json; - static mut DB_TEST_CURRENT_UD_1: Option<BcDbRo> = None; + static DB_TEST_CURRENT_UD_1: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_current_ud() { @@ -52,7 +53,7 @@ mod tests { })) }); - let schema = tests::setup(mock_db, unsafe { &mut DB_TEST_CURRENT_UD_1 }); + let schema = tests::setup(mock_db, &DB_TEST_CURRENT_UD_1); tests::test_gql_query( schema, diff --git a/lib/modules/gva/src/schema/queries/node.rs b/lib/modules/gva/src/schema/queries/node.rs index c57bd1d715b27d385c100cffc118c9ddcab74b52..e9d5c2d72977730104e78d23c058157245a916d0 100644 --- a/lib/modules/gva/src/schema/queries/node.rs +++ b/lib/modules/gva/src/schema/queries/node.rs @@ -36,13 +36,14 @@ pub(crate) fn execute( mod tests { use crate::db::BcDbRo; use crate::schema::queries::tests; + use once_cell::sync::OnceCell; use serde_json::json; - static mut DB_TEST_NODE_SUMMARY: Option<BcDbRo> = None; + static DB_TEST_NODE_SUMMARY: OnceCell<BcDbRo> = OnceCell::new(); #[test] fn test_graphql_node_summary() { - let schema = tests::setup(BcDbRo::new(), unsafe { &mut DB_TEST_NODE_SUMMARY }); + let schema = tests::setup(BcDbRo::new(), &DB_TEST_NODE_SUMMARY); tests::test_gql_query( schema, diff --git a/lib/modules/gva/src/webserver.rs b/lib/modules/gva/src/webserver.rs index 70ae86729e62f07f396d742513549b0b244f9ba5..fa82ef4a5a498f13f66c7fc3f0bdaa58f06c3fdd 100644 --- a/lib/modules/gva/src/webserver.rs +++ b/lib/modules/gva/src/webserver.rs @@ -27,10 +27,11 @@ use durs_module::SoftwareMetaDatas; use durs_network_documents::host::Host; use durs_network_documents::url::Url; use juniper::http::graphiql::graphiql_source; +use once_cell::sync::OnceCell; use std::net::SocketAddr; /// Database readonly handler (access to database) -static mut DB_RO_HANDLER: Option<BcDbRo> = None; +static DB_RO_HANDLER: OnceCell<BcDbRo> = OnceCell::new(); async fn graphiql() -> HttpResponse { let html = graphiql_source("/graphql"); @@ -64,11 +65,13 @@ pub fn start_web_server( let db = BcDbRo::new(); // Give a static lifetime to the DB - let db = durs_common_tools::fns::r#static::to_static_ref(db, unsafe { &mut DB_RO_HANDLER }); + DB_RO_HANDLER + .set(db) + .expect("DB_RO_HANDLER already initialized !"); // Create global context let global_context = std::sync::Arc::new(GlobalContext::new( - db, + &DB_RO_HANDLER, create_schema(), soft_meta_datas.soft_name, soft_meta_datas.soft_version,