From d0681ce1e2c14296cb609b65819631c2833c7cfb Mon Sep 17 00:00:00 2001 From: "Sam H. Smith" Date: Mon, 22 Apr 2024 00:34:03 +0200 Subject: [PATCH] [fix] #172: Query Block Headers Signed-off-by: Sam H. Smith --- src/client.rs | 19 +++++++++++ src/data_model/block.rs | 71 +++++++++++++++++++++++++++++++++++++++++ src/data_model/mod.rs | 2 ++ 3 files changed, 92 insertions(+) create mode 100644 src/data_model/block.rs diff --git a/src/client.rs b/src/client.rs index 0e4427a5..e97d69a5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -11,6 +11,7 @@ use std::num::NonZeroU64; use std::str::FromStr; use crate::data_model::asset::{PyAsset, PyAssetDefinition, PyAssetDefinitionId, PyAssetId}; +use crate::data_model::block::*; use crate::data_model::crypto::*; use crate::data_model::PyMirror; use crate::{data_model::account::PyAccountId, isi::PyInstruction}; @@ -189,6 +190,24 @@ impl Client { } Ok(items) } + + fn query_all_block_headers(&self) -> PyResult> { + let query = iroha_data_model::query::prelude::FindAllBlockHeaders; + + let val = self + .client + .request(query) + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?; + + let mut items = Vec::new(); + for item in val { + items.push( + item.map(|d| d.into()) + .map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?, + ); + } + Ok(items) + } } macro_rules! register_query { diff --git a/src/data_model/block.rs b/src/data_model/block.rs new file mode 100644 index 00000000..13201ec9 --- /dev/null +++ b/src/data_model/block.rs @@ -0,0 +1,71 @@ +use pyo3::{ + exceptions::{PyRuntimeError, PyValueError}, + prelude::*, +}; + +use iroha_crypto::Hash; +use iroha_data_model::block::BlockHeader; + +use super::PyMirror; + +#[pyclass(name = "BlockHeader")] +#[derive(Clone, derive_more::From, derive_more::Into, derive_more::Deref)] +pub struct PyBlockHeader(pub BlockHeader); + +impl PyMirror for BlockHeader { + type Mirror = PyBlockHeader; + + fn mirror(self) -> PyResult { + Ok(PyBlockHeader(self)) + } +} + +#[pymethods] +impl PyBlockHeader { + #[getter] + fn get_height(&self) -> u64 { + self.0.height + } + + #[getter] + fn get_timestamp_ms(&self) -> u64 { + self.0.timestamp_ms + } + + #[getter] + fn get_consensus_previous_block_hash(&self) -> Option<[u8; Hash::LENGTH]> { + self.0 + .previous_block_hash + .as_ref() + .map(|previous_block_hash| previous_block_hash.as_ref()) + .copied() + } + + #[getter] + fn get_transactions_hash(&self) -> Option<[u8; Hash::LENGTH]> { + self.0 + .transactions_hash + .as_ref() + .map(|transactions_hash| transactions_hash.as_ref()) + .copied() + } + + #[getter] + fn get_view_change_index(&self) -> u64 { + self.0.view_change_index + } + + #[getter] + fn get_consensus_estimation_ms(&self) -> u64 { + self.0.consensus_estimation_ms + } + + fn __repr__(&self) -> String { + format!("{:?}", self.0) + } +} + +pub fn register_items(_py: Python<'_>, module: &PyModule) -> PyResult<()> { + module.add_class::()?; + Ok(()) +} diff --git a/src/data_model/mod.rs b/src/data_model/mod.rs index c1164ba7..16d64f8d 100644 --- a/src/data_model/mod.rs +++ b/src/data_model/mod.rs @@ -14,6 +14,7 @@ use self::domain::*; pub mod account; pub mod asset; +pub mod block; pub mod crypto; pub mod domain; pub mod role; @@ -127,5 +128,6 @@ pub fn register_items(py: Python<'_>, module: &PyModule) -> PyResult<()> { role::register_items(py, module)?; crypto::register_items(py, module)?; tx::register_items(py, module)?; + block::register_items(py, module)?; Ok(()) }