Skip to content

Commit

Permalink
refactor: use HashOf<BlockHeader> as the type of the block hash (#4998
Browse files Browse the repository at this point in the history
)

Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic authored Aug 23, 2024
1 parent 9e8c35d commit a27bc2e
Show file tree
Hide file tree
Showing 24 changed files with 152 additions and 159 deletions.
6 changes: 3 additions & 3 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
config::Config,
crypto::{HashOf, KeyPair},
data_model::{
block::SignedBlock,
block::{BlockHeader, SignedBlock},
events::pipeline::{
BlockEventFilter, BlockStatus, PipelineEventBox, PipelineEventFilterBox,
TransactionEventFilter, TransactionStatus,
Expand Down Expand Up @@ -951,7 +951,7 @@ mod blocks_api {
pub struct Events;

impl FlowEvents for Events {
type Event = crate::data_model::block::SignedBlock;
type Event = SignedBlock;

fn message(&self, message: Vec<u8>) -> Result<Self::Event> {
Ok(BlockMessage::decode_all(&mut message.as_slice()).map(Into::into)?)
Expand Down Expand Up @@ -1012,7 +1012,7 @@ pub mod block {
}

/// Construct a query to find block header by hash
pub fn header_by_hash(hash: HashOf<SignedBlock>) -> FindBlockHeaderByHash {
pub fn header_by_hash(hash: HashOf<BlockHeader>) -> FindBlockHeaderByHash {
FindBlockHeaderByHash::new(hash)
}
}
Expand Down
10 changes: 5 additions & 5 deletions client/tests/integration/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ fn limits_should_work() -> Result<()> {

#[test]
fn fetch_size_should_work() -> Result<()> {
let (_rt, _peer, client) = <PeerBuilder>::new().with_port(11_120).start_with_runtime();
wait_for_genesis_committed(&vec![client.clone()], 0);

register_assets(&client)?;

// use the lower-level API to inspect the batch size
use iroha_data_model::query::{
builder::QueryExecutor as _,
parameters::{FetchSize, QueryParams, Sorting},
QueryWithFilter, QueryWithParams,
};

let (_rt, _peer, client) = <PeerBuilder>::new().with_port(11_120).start_with_runtime();
wait_for_genesis_committed(&vec![client.clone()], 0);

register_assets(&client)?;

let query = QueryWithParams::new(
QueryWithFilter::new(asset::all_definitions(), CompoundPredicate::PASS).into(),
QueryParams::new(
Expand Down
8 changes: 3 additions & 5 deletions core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pub enum BlockValidationError {
/// Mismatch between the actual and expected hashes of the previous block. Expected: {expected:?}, actual: {actual:?}
PrevBlockHashMismatch {
/// Expected value
expected: Option<HashOf<SignedBlock>>,
expected: Option<HashOf<BlockHeader>>,
/// Actual value
actual: Option<HashOf<SignedBlock>>,
actual: Option<HashOf<BlockHeader>>,
},
/// Mismatch between the actual and expected height of the previous block. Expected: {expected}, actual: {actual}
PrevBlockHeightMismatch {
Expand Down Expand Up @@ -146,7 +146,7 @@ mod pending {

fn make_header(
prev_height: usize,
prev_block_hash: Option<HashOf<SignedBlock>>,
prev_block_hash: Option<HashOf<BlockHeader>>,
view_change_index: usize,
transactions: &[CommittedTransaction],
consensus_estimation: Duration,
Expand Down Expand Up @@ -1009,7 +1009,6 @@ mod event {

let block_event = core::iter::once(BlockEvent {
header: self.as_ref().header().clone(),
hash: self.as_ref().hash(),
status: BlockStatus::Approved,
});

Expand All @@ -1023,7 +1022,6 @@ mod event {
fn produce_events(&self) -> impl Iterator<Item = PipelineEventBox> {
let block_event = core::iter::once(BlockEvent {
header: self.as_ref().header().clone(),
hash: self.as_ref().hash(),
status: BlockStatus::Committed,
});

Expand Down
25 changes: 14 additions & 11 deletions core/src/block_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use std::{

use iroha_config::parameters::actual::BlockSync as Config;
use iroha_crypto::HashOf;
use iroha_data_model::{block::SignedBlock, prelude::*};
use iroha_data_model::{
block::{BlockHeader, SignedBlock},
prelude::*,
};
use iroha_logger::prelude::*;
use iroha_macro::*;
use iroha_p2p::Post;
Expand Down Expand Up @@ -50,7 +53,7 @@ pub struct BlockSynchronizer {
gossip_size: NonZeroU32,
network: IrohaNetwork,
state: Arc<State>,
seen_blocks: BTreeSet<(NonZeroUsize, HashOf<SignedBlock>)>,
seen_blocks: BTreeSet<(NonZeroUsize, HashOf<BlockHeader>)>,
latest_height: usize,
}

Expand Down Expand Up @@ -161,20 +164,20 @@ pub mod message {
/// Peer id
pub peer_id: PeerId,
/// Hash of second to latest block
pub prev_hash: Option<HashOf<SignedBlock>>,
pub prev_hash: Option<HashOf<BlockHeader>>,
/// Hash of latest available block
pub latest_hash: Option<HashOf<SignedBlock>>,
pub latest_hash: Option<HashOf<BlockHeader>>,
/// The block hashes already seen
pub seen_blocks: BTreeSet<HashOf<SignedBlock>>,
pub seen_blocks: BTreeSet<HashOf<BlockHeader>>,
}

impl GetBlocksAfter {
/// Construct [`GetBlocksAfter`].
pub const fn new(
peer_id: PeerId,
prev_hash: Option<HashOf<SignedBlock>>,
latest_hash: Option<HashOf<SignedBlock>>,
seen_blocks: BTreeSet<HashOf<SignedBlock>>,
prev_hash: Option<HashOf<BlockHeader>>,
latest_hash: Option<HashOf<BlockHeader>>,
seen_blocks: BTreeSet<HashOf<BlockHeader>>,
) -> Self {
Self {
peer_id,
Expand Down Expand Up @@ -304,9 +307,9 @@ pub mod message {
#[derive(Decode)]
struct GetBlocksAfterCandidate {
peer: PeerId,
prev_hash: Option<HashOf<SignedBlock>>,
latest_hash: Option<HashOf<SignedBlock>>,
seen_blocks: BTreeSet<HashOf<SignedBlock>>,
prev_hash: Option<HashOf<BlockHeader>>,
latest_hash: Option<HashOf<BlockHeader>>,
seen_blocks: BTreeSet<HashOf<BlockHeader>>,
}

#[derive(Decode)]
Expand Down
20 changes: 10 additions & 10 deletions core/src/kura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{

use iroha_config::{kura::InitMode, parameters::actual::Kura as Config};
use iroha_crypto::{Hash, HashOf};
use iroha_data_model::block::SignedBlock;
use iroha_data_model::block::{BlockHeader, SignedBlock};
use iroha_logger::prelude::*;
use iroha_version::scale::{DecodeVersioned, EncodeVersioned};
use parity_scale_codec::DecodeAll;
Expand All @@ -35,7 +35,7 @@ pub struct Kura {
block_store: Mutex<BlockStore>,
/// The array of block hashes and a slot for an arc of the block. This is normally recovered from the index file.
#[allow(clippy::type_complexity)]
block_data: Mutex<Vec<(HashOf<SignedBlock>, Option<Arc<SignedBlock>>)>>,
block_data: Mutex<Vec<(HashOf<BlockHeader>, Option<Arc<SignedBlock>>)>>,
/// Path to file for plain text blocks.
block_plain_text_path: Option<PathBuf>,
}
Expand Down Expand Up @@ -134,7 +134,7 @@ impl Kura {
fn init_fast_mode(
block_store: &BlockStore,
block_index_count: usize,
) -> Result<Vec<HashOf<SignedBlock>>, Error> {
) -> Result<Vec<HashOf<BlockHeader>>, Error> {
let block_hashes_count = block_store
.read_hashes_count()?
.try_into()
Expand All @@ -149,7 +149,7 @@ impl Kura {
fn init_strict_mode(
block_store: &mut BlockStore,
block_index_count: usize,
) -> Result<Vec<HashOf<SignedBlock>>, Error> {
) -> Result<Vec<HashOf<BlockHeader>>, Error> {
let mut block_hashes = Vec::with_capacity(block_index_count);

let mut block_indices = vec![BlockIndex::default(); block_index_count];
Expand Down Expand Up @@ -271,7 +271,7 @@ impl Kura {
}

/// Get the hash of the block at the provided height.
pub fn get_block_hash(&self, block_height: NonZeroUsize) -> Option<HashOf<SignedBlock>> {
pub fn get_block_hash(&self, block_height: NonZeroUsize) -> Option<HashOf<BlockHeader>> {
let hash_data_guard = self.block_data.lock();

let block_height = block_height.get();
Expand All @@ -284,7 +284,7 @@ impl Kura {
}

/// Search through blocks for the height of the block with the given hash.
pub fn get_block_height_by_hash(&self, hash: HashOf<SignedBlock>) -> Option<NonZeroUsize> {
pub fn get_block_height_by_hash(&self, hash: HashOf<BlockHeader>) -> Option<NonZeroUsize> {
self.block_data
.lock()
.iter()
Expand Down Expand Up @@ -463,7 +463,7 @@ impl BlockStore {
&self,
start_block_height: u64,
block_count: usize,
) -> Result<Vec<HashOf<SignedBlock>>> {
) -> Result<Vec<HashOf<BlockHeader>>> {
let path = self.path_to_blockchain.join(HASHES_FILE_NAME);
let mut hashes_file = std::fs::OpenOptions::new()
.read(true)
Expand Down Expand Up @@ -497,7 +497,7 @@ impl BlockStore {

/// Get the number of hashes in the hashes file, which is
/// calculated as the size of the hashes file in bytes divided by
/// `size_of(HashOf<SignedBlock>)`.
/// `size_of(HashOf<BlockHeader>)`.
///
/// # Errors
/// IO Error.
Expand Down Expand Up @@ -631,7 +631,7 @@ impl BlockStore {
///
/// # Errors
/// IO Error.
pub fn write_block_hash(&mut self, block_height: u64, hash: HashOf<SignedBlock>) -> Result<()> {
pub fn write_block_hash(&mut self, block_height: u64, hash: HashOf<BlockHeader>) -> Result<()> {
let path = self.path_to_blockchain.join(HASHES_FILE_NAME);
let mut hashes_file = std::fs::OpenOptions::new()
.write(true)
Expand Down Expand Up @@ -659,7 +659,7 @@ impl BlockStore {
///
/// # Errors
/// IO Error.
pub fn overwrite_block_hashes(&mut self, hashes: &[HashOf<SignedBlock>]) -> Result<()> {
pub fn overwrite_block_hashes(&mut self, hashes: &[HashOf<BlockHeader>]) -> Result<()> {
let path = self.path_to_blockchain.join(HASHES_FILE_NAME);
let hashes_file = std::fs::OpenOptions::new()
.write(true)
Expand Down
24 changes: 12 additions & 12 deletions core/src/smartcontracts/isi/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@ pub mod query {

impl ValidQuery for FindRolesByAccountId {
#[metrics(+"find_roles_by_account_id")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<RoleIdPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = RoleId> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = RoleId>, Error> {
let account_id = &self.id;
state_ro.world().account(account_id)?;
Ok(state_ro
Expand All @@ -515,11 +515,11 @@ pub mod query {

impl ValidQuery for FindPermissionsByAccountId {
#[metrics(+"find_permissions_by_account_id")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<PermissionPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = Permission> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = Permission>, Error> {
let account_id = &self.id;
Ok(state_ro
.world()
Expand All @@ -531,11 +531,11 @@ pub mod query {

impl ValidQuery for FindAccounts {
#[metrics(+"find_accounts")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<AccountPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = Account> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = Account>, Error> {
Ok(state_ro
.world()
.accounts_iter()
Expand All @@ -560,11 +560,11 @@ pub mod query {

impl ValidQuery for FindAccountsWithAsset {
#[metrics(+"find_accounts_with_asset")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<AccountPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> std::result::Result<impl Iterator<Item = Account> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> std::result::Result<impl Iterator<Item = Account>, Error> {
let asset_definition_id = self.asset_definition.clone();
iroha_logger::trace!(%asset_definition_id);

Expand Down
12 changes: 6 additions & 6 deletions core/src/smartcontracts/isi/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ pub mod query {

impl ValidQuery for FindAssets {
#[metrics(+"find_assets")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<AssetPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = Asset> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = Asset>, Error> {
Ok(state_ro
.world()
.assets_iter()
Expand All @@ -453,11 +453,11 @@ pub mod query {
}
impl ValidQuery for FindAssetsDefinitions {
#[metrics(+"find_asset_definitions")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<AssetDefinitionPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = AssetDefinition> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = AssetDefinition>, Error> {
Ok(state_ro
.world()
.asset_definitions_iter()
Expand Down
12 changes: 6 additions & 6 deletions core/src/smartcontracts/isi/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use crate::{smartcontracts::ValidQuery, state::StateReadOnly};

impl ValidQuery for FindBlocks {
#[metrics(+"find_blocks")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<SignedBlockPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = Self::Item> + 'state, QueryExecutionFail> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = Self::Item>, QueryExecutionFail> {
Ok(state_ro
.all_blocks(nonzero!(1_usize))
.rev()
Expand All @@ -34,11 +34,11 @@ impl ValidQuery for FindBlocks {

impl ValidQuery for FindBlockHeaders {
#[metrics(+"find_block_headers")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<BlockHeaderPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = Self::Item> + 'state, QueryExecutionFail> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = Self::Item>, QueryExecutionFail> {
Ok(state_ro
.all_blocks(nonzero!(1_usize))
.rev()
Expand Down
6 changes: 3 additions & 3 deletions core/src/smartcontracts/isi/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ pub mod query {

impl ValidQuery for FindDomains {
#[metrics(+"find_domains")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<DomainPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> std::result::Result<impl Iterator<Item = Domain> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> std::result::Result<impl Iterator<Item = Domain>, Error> {
Ok(state_ro
.world()
.domains_iter()
Expand Down
6 changes: 3 additions & 3 deletions core/src/smartcontracts/isi/triggers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ pub mod query {

impl ValidQuery for FindActiveTriggerIds {
#[metrics(+"find_active_triggers")]
fn execute<'state>(
fn execute(
self,
filter: CompoundPredicate<TriggerIdPredicateBox>,
state_ro: &'state impl StateReadOnly,
) -> Result<impl Iterator<Item = TriggerId> + 'state, Error> {
state_ro: &impl StateReadOnly,
) -> Result<impl Iterator<Item = TriggerId>, Error> {
Ok(state_ro
.world()
.triggers()
Expand Down
Loading

0 comments on commit a27bc2e

Please sign in to comment.