Skip to content
Snippets Groups Projects
Commit f05a4c70 authored by Éloïs's avatar Éloïs
Browse files

Merge branch 'gva-block' into 'dev'

[feat] gva: block

See merge request !1352
parents f1512300 f0da0fab
No related branches found
No related tags found
1 merge request!1352[feat] gva: block
// 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(())
}
}
......@@ -22,6 +22,7 @@
unused_import_braces
)]
pub mod block;
pub mod current_frame;
pub mod find_inputs;
pub mod idty;
......
......@@ -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,
......
......@@ -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,
......
// 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(())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment