Skip to content

Commit

Permalink
refactor: derive block hash from header
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
  • Loading branch information
Erigara committed Jul 24, 2024
1 parent 79cfafc commit e305422
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
Binary file modified configs/swarm/executor.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions core/src/sumeragi/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ impl Sumeragi {
),
_ => {
if let Some(mut voted_block) = voting_block.take() {
let actual_hash = voted_block.block.as_ref().hash_of_payload();
let actual_hash = voted_block.block.as_ref().hash();

if hash != actual_hash {
error!(
Expand Down Expand Up @@ -697,7 +697,7 @@ impl Sumeragi {
Role::Leader | Role::ValidatingPeer | Role::ObservingPeer,
) => {
if let Some(mut voted_block) = voting_block.take() {
let actual_hash = voted_block.block.as_ref().hash_of_payload();
let actual_hash = voted_block.block.as_ref().hash();

if actual_hash == hash {
match voted_block
Expand Down
12 changes: 6 additions & 6 deletions core/src/sumeragi/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Contains message structures for p2p communication during consensus.
use iroha_crypto::HashOf;
use iroha_data_model::block::{BlockPayload, BlockSignature, SignedBlock};
use iroha_data_model::block::{BlockSignature, SignedBlock};
use iroha_macro::*;
use parity_scale_codec::{Decode, Encode};

Expand Down Expand Up @@ -56,15 +56,15 @@ impl From<&ValidBlock> for BlockCreated {
#[derive(Debug, Clone, Decode, Encode)]
pub struct BlockSigned {
/// Hash of the block being signed.
pub hash: HashOf<BlockPayload>,
pub hash: HashOf<SignedBlock>,
/// Signature of the block
pub signature: BlockSignature,
}

impl From<&ValidBlock> for BlockSigned {
fn from(block: &ValidBlock) -> Self {
Self {
hash: block.as_ref().hash_of_payload(),
hash: block.as_ref().hash(),
signature: block
.as_ref()
.signatures()
Expand All @@ -79,15 +79,15 @@ impl From<&ValidBlock> for BlockSigned {
#[derive(Debug, Clone, Encode)]
pub struct BlockCommitted {
/// Hash of the block being signed.
pub hash: HashOf<BlockPayload>,
pub hash: HashOf<SignedBlock>,
/// Set of signatures.
pub signatures: Vec<BlockSignature>,
}

impl From<&CommittedBlock> for BlockCommitted {
fn from(block: &CommittedBlock) -> Self {
Self {
hash: block.as_ref().hash_of_payload(),
hash: block.as_ref().hash(),
signatures: block.as_ref().signatures().cloned().collect(),
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ mod candidate {
#[derive(Decode)]
struct BlockCommittedCandidate {
/// Hash of the block being signed.
pub hash: HashOf<BlockPayload>,
pub hash: HashOf<SignedBlock>,
/// Set of signatures.
pub signatures: Vec<BlockSignature>,
}
Expand Down
20 changes: 6 additions & 14 deletions data_model/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ mod model {
#[derive(
Debug, Display, Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Serialize, IntoSchema,
)]
#[cfg_attr(not(feature = "std"), display(fmt = "Signed block"))]
#[cfg_attr(feature = "std", display(fmt = "{}", "self.hash()"))]
#[display(fmt = "{}", "self.hash()")]
#[ffi_type]
pub struct SignedBlockV1 {
/// Signatures of peers which approved this block.
Expand Down Expand Up @@ -175,9 +174,10 @@ impl BlockPayload {
}

impl SignedBlockV1 {
#[cfg(feature = "std")]
fn hash(&self) -> iroha_crypto::HashOf<SignedBlock> {
iroha_crypto::HashOf::from_untyped_unchecked(iroha_crypto::HashOf::new(self).into())
iroha_crypto::HashOf::from_untyped_unchecked(
iroha_crypto::HashOf::new(&self.payload.header).into(),
)
}
}

Expand Down Expand Up @@ -223,7 +223,8 @@ impl SignedBlock {
/// Calculate block hash
#[inline]
pub fn hash(&self) -> HashOf<Self> {
iroha_crypto::HashOf::new(self)
let SignedBlock::V1(block) = self;
block.hash()
}

/// Add signature to the block
Expand Down Expand Up @@ -261,15 +262,6 @@ impl SignedBlock {
std::mem::replace(&mut block.signatures, signatures)
}

/// Calculate block payload [`Hash`](`iroha_crypto::HashOf`).
#[inline]
#[cfg(feature = "std")]
#[cfg(feature = "transparent_api")]
pub fn hash_of_payload(&self) -> iroha_crypto::HashOf<BlockPayload> {
let SignedBlock::V1(block) = self;
iroha_crypto::HashOf::new(&block.payload)
}

/// Add additional signatures to this block
#[cfg(all(feature = "std", feature = "transparent_api"))]
pub fn sign(&mut self, private_key: &iroha_crypto::PrivateKey, signatory: usize) {
Expand Down

0 comments on commit e305422

Please sign in to comment.