From f0da0faba6d9c678264c9e0af8c8d099183d813d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= <tuxmain@zettascript.org>
Date: Tue, 23 Feb 2021 18:28:50 +0100
Subject: [PATCH] [feat] gva: block

---
 rust-libs/modules/gva/dbs-reader/src/block.rs | 52 +++++++++++++++
 rust-libs/modules/gva/dbs-reader/src/lib.rs   |  1 +
 rust-libs/modules/gva/gql/src/lib.rs          |  1 +
 rust-libs/modules/gva/gql/src/queries.rs      |  2 +
 .../modules/gva/gql/src/queries/block.rs      | 65 +++++++++++++++++++
 5 files changed, 121 insertions(+)
 create mode 100644 rust-libs/modules/gva/dbs-reader/src/block.rs
 create mode 100644 rust-libs/modules/gva/gql/src/queries/block.rs

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 000000000..01af7d7d1
--- /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 c2378e649..3ee4b4925 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 ddee2c171..4cb21cc97 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 3596943aa..f323b30d3 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 000000000..850aac755
--- /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(())
+    }
+}
-- 
GitLab