forked from hyperledger-iroha/iroha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] hyperledger-iroha#2098: Block header queries (hyperledger-i…
…roha#2453) * [feature] hyperledger-iroha#2098: add block header queries Signed-off-by: Artemii Gerasimovich <[email protected]>
- Loading branch information
Showing
7 changed files
with
294 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
//! This module contains trait implementations related to block queries | ||
use eyre::Result; | ||
use eyre::{Result, WrapErr}; | ||
use iroha_data_model::query::block::FindBlockHeaderByHash; | ||
use iroha_telemetry::metrics; | ||
|
||
use super::*; | ||
|
||
impl ValidQuery for FindAllBlocks { | ||
#[metrics(+"find_all_blocks")] | ||
fn execute(&self, wsv: &WorldStateView) -> Result<Self::Output, query::Error> { | ||
let mut blocks: Vec<BlockValue> = wsv | ||
let blocks = wsv | ||
.blocks() | ||
.map(|blk| blk.clone()) | ||
.map(|block| block.clone()) | ||
.map(VersionedCommittedBlock::into_value) | ||
.rev() // Sorted by height desc. | ||
.collect(); | ||
blocks.reverse(); | ||
Ok(blocks) | ||
} | ||
} | ||
|
||
impl ValidQuery for FindAllBlockHeaders { | ||
#[metrics(+"find_all_block_headers")] | ||
fn execute(&self, wsv: &WorldStateView) -> Result<Self::Output, query::Error> { | ||
let block_headers = wsv | ||
.blocks() | ||
.map(|block| block.clone()) | ||
.map(VersionedCommittedBlock::into_value) | ||
.map(|block_value| block_value.header) | ||
.rev() // Sorted by height desc. | ||
.collect(); | ||
Ok(block_headers) | ||
} | ||
} | ||
|
||
impl ValidQuery for FindBlockHeaderByHash { | ||
#[metrics(+"find_block_header")] | ||
fn execute(&self, wsv: &WorldStateView) -> Result<Self::Output, query::Error> { | ||
let hash = self | ||
.hash | ||
.evaluate(wsv, &Context::default()) | ||
.wrap_err("Failed to evaluate hash") | ||
.map_err(|e| query::Error::Evaluate(e.to_string()))? | ||
.typed(); | ||
|
||
let block = wsv | ||
.blocks() | ||
.find(|block| block.hash() == hash) | ||
.ok_or_else(|| query::Error::Find(Box::new(FindError::Block(hash))))?; | ||
|
||
Ok(block.clone().into_value().header) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.