Skip to content

Commit

Permalink
[fix] #172: Query Block Headers
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <sam.henning.smith@protonmail.com>
  • Loading branch information
SamHSmith committed Apr 22, 2024
1 parent e91dd17 commit d0681ce
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/client.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -189,6 +190,24 @@ impl Client {
}
Ok(items)
}

fn query_all_block_headers(&self) -> PyResult<Vec<PyBlockHeader>> {
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 {
Expand Down
71 changes: 71 additions & 0 deletions 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<Self::Mirror> {
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::<PyBlockHeader>()?;
Ok(())
}
2 changes: 2 additions & 0 deletions src/data_model/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}

0 comments on commit d0681ce

Please sign in to comment.