diff --git a/rust-libs/modules/gva/dbs-reader/src/block.rs b/rust-libs/modules/gva/dbs-reader/src/block.rs new file mode 100644 index 0000000000000000000000000000000000000000..01af7d7d1e2b531a7825d0343999cabf41aaa994 --- /dev/null +++ b/rust-libs/modules/gva/dbs-reader/src/block.rs @@ -0,0 +1,52 @@ +// Copyright (C) 2021 Pascal Engélibert +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use crate::*; + +impl DbsReader { + pub fn block( + &self, + bc_db: &BcV2DbRo<FileBackend>, + number: U32BE, + ) -> KvResult<Option<duniter_dbs::BlockMetaV2>> { + bc_db.blocks_meta().get(&number) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use duniter_dbs::databases::bc_v2::BcV2DbWritable; + use duniter_gva_db::GvaV1DbWritable; + + #[test] + fn test_block() -> KvResult<()> { + let bc_db = duniter_dbs::databases::bc_v2::BcV2Db::<Mem>::open(MemConf::default())?; + let gva_db = duniter_gva_db::GvaV1Db::<Mem>::open(MemConf::default())?; + let bc_db_ro = bc_db.get_ro_handler(); + let db_reader = create_dbs_reader(unsafe { std::mem::transmute(&gva_db.get_ro_handler()) }); + + bc_db + .blocks_meta_write() + .upsert(U32BE(0), duniter_dbs::BlockMetaV2::default())?; + + assert_eq!( + db_reader.block(&bc_db_ro, U32BE(0))?, + Some(duniter_dbs::BlockMetaV2::default()) + ); + + Ok(()) + } +} diff --git a/rust-libs/modules/gva/dbs-reader/src/lib.rs b/rust-libs/modules/gva/dbs-reader/src/lib.rs index c2378e649e92d2e50240ca3c6e9c3b0856c4f55f..3ee4b49259ed2fbfb13a1ffe77b9b22c3337c67a 100644 --- a/rust-libs/modules/gva/dbs-reader/src/lib.rs +++ b/rust-libs/modules/gva/dbs-reader/src/lib.rs @@ -22,6 +22,7 @@ unused_import_braces )] +pub mod block; pub mod current_frame; pub mod find_inputs; pub mod idty; diff --git a/rust-libs/modules/gva/gql/src/lib.rs b/rust-libs/modules/gva/gql/src/lib.rs index ddee2c171146705fab463a14c64ceb77599fd89d..4cb21cc97eb1cdde29bd41dc0f26a8075816d76c 100644 --- a/rust-libs/modules/gva/gql/src/lib.rs +++ b/rust-libs/modules/gva/gql/src/lib.rs @@ -93,6 +93,7 @@ mod tests { pubkey: PublicKey, page_info: PageInfo<BlockNumber>, ) -> KvResult<PagedData<duniter_gva_dbs_reader::uds_of_pubkey::UdsWithSum>>; + fn block(&self, bc_db: &BcV2DbRo<FileBackend>, number: U32BE) -> KvResult<Option<BlockMetaV2>>; fn find_inputs<BcDb: 'static + BcV2DbReadable, TxsMpDb: 'static + TxsMpV2DbReadable>( &self, bc_db: &BcDb, diff --git a/rust-libs/modules/gva/gql/src/queries.rs b/rust-libs/modules/gva/gql/src/queries.rs index 3596943aa114f1e3c75b5100b5b7b5b67efe8d0e..f323b30d3e87acf36760412fc96ead1b04163402 100644 --- a/rust-libs/modules/gva/gql/src/queries.rs +++ b/rust-libs/modules/gva/gql/src/queries.rs @@ -14,6 +14,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. pub mod account_balance; +pub mod block; pub mod current_block; pub mod current_frame; pub mod gen_tx; @@ -29,6 +30,7 @@ use duniter_dbs::databases::cm_v1::CmV1DbReadable as _; pub struct QueryRoot( queries::NodeQuery, queries::account_balance::AccountBalanceQuery, + queries::block::BlockQuery, queries::current_block::CurrentBlockQuery, queries::current_frame::CurrentFrameQuery, queries::gen_tx::GenTxsQuery, diff --git a/rust-libs/modules/gva/gql/src/queries/block.rs b/rust-libs/modules/gva/gql/src/queries/block.rs new file mode 100644 index 0000000000000000000000000000000000000000..850aac75568b75d27fe97ec6118351ec22f11d67 --- /dev/null +++ b/rust-libs/modules/gva/gql/src/queries/block.rs @@ -0,0 +1,65 @@ +// Copyright (C) 2021 Pascal Engélibert +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +use crate::*; + +#[derive(Default)] +pub(crate) struct BlockQuery; +#[async_graphql::Object] +impl BlockQuery { + /// Get block by number + async fn block_by_number( + &self, + ctx: &async_graphql::Context<'_>, + #[graphql(desc = "block number")] number: u32, + ) -> async_graphql::Result<Option<BlockMeta>> { + let data = ctx.data::<GvaSchemaData>()?; + let dbs_reader = data.dbs_reader(); + + let block = data + .dbs_pool + .execute(move |dbs| dbs_reader.block(&dbs.bc_db_ro, U32BE(number))) + .await??; + + Ok(block.map(|block| BlockMeta::from(&block))) + } +} + +#[cfg(test)] +mod tests { + use crate::tests::*; + + #[tokio::test] + async fn test_block() -> anyhow::Result<()> { + let mut dbs_reader = MockDbsReader::new(); + dbs_reader + .expect_block() + .withf(|_, s| s.0 == 0) + .times(1) + .returning(|_, _| Ok(Some(duniter_dbs::BlockMetaV2::default()))); + let schema = create_schema(dbs_reader)?; + assert_eq!( + exec_graphql_request(&schema, r#"{ blockByNumber(number: 0) {number} }"#).await?, + serde_json::json!({ + "data": { + "blockByNumber": { + "number": duniter_dbs::BlockMetaV2::default().number, + } + } + }) + ); + Ok(()) + } +}