Skip to content

Commit

Permalink
add support for data-carrying enums
Browse files Browse the repository at this point in the history
  • Loading branch information
mversic committed Aug 23, 2022
1 parent 7bc4d84 commit ebb8dbb
Show file tree
Hide file tree
Showing 35 changed files with 1,368 additions and 1,599 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 32 additions & 21 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec, vec::Vec};
use alloc::{alloc::alloc, format, string::String, vec, vec::Vec};
use core::{hash, marker::PhantomData};
#[cfg(feature = "std")]
use std::alloc::alloc;

use derive_more::{DebugCustom, Deref, DerefMut, Display};
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_schema::prelude::*;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
Expand All @@ -12,26 +15,33 @@ use ursa::blake2::{
VarBlake2b,
};

/// Hash of Iroha entities. Currently supports only blake2b-32.
#[derive(
Clone,
Copy,
Display,
DebugCustom,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
)]
#[display(fmt = "{}", "hex::encode(_0)")]
#[debug(fmt = "{{ Hash({}) }}", "hex::encode(_0)")]
pub struct Hash([u8; Self::LENGTH]);
use crate::ffi;

ffi::ffi_item! {
/// Hash of Iroha entities. Currently supports only blake2b-32.
#[derive(
Clone,
Copy,
Display,
DebugCustom,
Hash,
Eq,
PartialEq,
Ord,
PartialOrd,
Decode,
Encode,
Deserialize,
Serialize,
IntoSchema,
IntoFfi,
TryFromReprC,
)]
#[repr(transparent)]
#[display(fmt = "{}", "hex::encode(_0)")]
#[debug(fmt = "{{ Hash({}) }}", "hex::encode(_0)")]
pub struct Hash([u8; Self::LENGTH]);
}

impl Hash {
/// Length of hash
Expand Down Expand Up @@ -99,6 +109,7 @@ impl<T> From<HashOf<T>> for Hash {
#[display(fmt = "{}", _0)]
#[debug(fmt = "{{ {} {_0} }}", "core::any::type_name::<Self>()")]
#[serde(transparent)]
#[repr(transparent)]
pub struct HashOf<T>(
#[deref]
#[deref_mut]
Expand Down
1 change: 0 additions & 1 deletion data_model/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ pub enum AssetValueType {
TryFromReprC,
IntoSchema,
)]
#[repr(u8)]
pub enum AssetValue {
/// Asset's Quantity.
#[display(fmt = "{_0}q")]
Expand Down
61 changes: 39 additions & 22 deletions data_model/src/block_value.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
//! This module contains [`BlockValue`] and [`BlockHeaderValue`] structures, their implementation and related traits and
//! instructions implementations.
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};
use alloc::{alloc::alloc, format, string::String, vec::Vec};
use core::cmp::Ordering;
#[cfg(feature = "std")]
use std::alloc::alloc;

use derive_more::Display;
use iroha_crypto::{Hash, HashOf, MerkleTree};
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_schema::IntoSchema;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

use crate::{
events::Event,
ffi::ffi_item,
transaction::{VersionedRejectedTransaction, VersionedTransaction, VersionedValidTransaction},
};

/// Block header
#[derive(
Debug, Clone, Display, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema,
)]
#[display(fmt = "Block №{height} (hash: {transactions_hash});")]
pub struct BlockHeaderValue {
/// Unix time (in milliseconds) of block forming by a peer.
pub timestamp: u128,
/// a number of blocks in the chain up to the block.
pub height: u64,
/// Hash of a previous block in the chain.
/// Is an array of zeros for the first block.
pub previous_block_hash: Hash,
/// Hash of merkle tree root of the tree of valid transactions' hashes.
pub transactions_hash: HashOf<MerkleTree<VersionedTransaction>>,
/// Hash of merkle tree root of the tree of rejected transactions' hashes.
pub rejected_transactions_hash: HashOf<MerkleTree<VersionedTransaction>>,
/// Hashes of the blocks that were rejected by consensus.
pub invalidated_blocks_hashes: Vec<Hash>,
/// Hash of the most recent block
pub current_block_hash: Hash,
ffi_item! {
/// Block header
#[derive(
Debug,
Clone,
Display,
PartialEq,
Eq,
Decode,
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
IntoSchema,
)]
#[display(fmt = "Block №{height} (hash: {transactions_hash});")]
pub struct BlockHeaderValue {
/// Unix time (in milliseconds) of block forming by a peer.
pub timestamp: u128,
/// a number of blocks in the chain up to the block.
pub height: u64,
/// Hash of a previous block in the chain.
/// Is an array of zeros for the first block.
pub previous_block_hash: Hash,
/// Hash of merkle tree root of the tree of valid transactions' hashes.
pub transactions_hash: HashOf<MerkleTree<VersionedTransaction>>,
/// Hash of merkle tree root of the tree of rejected transactions' hashes.
pub rejected_transactions_hash: HashOf<MerkleTree<VersionedTransaction>>,
/// Hashes of the blocks that were rejected by consensus.
pub invalidated_blocks_hashes: Vec<Hash>,
/// Hash of the most recent block
pub current_block_hash: Hash,
}
}

impl PartialOrd for BlockHeaderValue {
Expand Down
3 changes: 3 additions & 0 deletions data_model/src/events/data/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use core::{fmt::Debug, hash::Hash};

use iroha_ffi::TryFromReprC;

use super::*;

/// Filter for all events
Expand All @@ -19,6 +21,7 @@ pub type EventFilter = FilterOpt<EntityFilter>;
Serialize,
Deserialize,
IntoSchema,
TryFromReprC,
Hash,
)]
/// Optional filter. May pass all items or may filter them by `F`
Expand Down
9 changes: 9 additions & 0 deletions data_model/src/events/execute_trigger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
//! Trigger execution event and filter
#[cfg(not(feature = "std"))]
use alloc::alloc::alloc;
#[cfg(feature = "std")]
use std::alloc::alloc;

use iroha_ffi::{IntoFfi, TryFromReprC};

use super::*;
use crate::prelude::*;

Expand Down Expand Up @@ -36,6 +43,8 @@ impl Event {
Hash,
Serialize,
Deserialize,
IntoFfi,
TryFromReprC,
)]
pub struct EventFilter {
/// Id of trigger catch executions of
Expand Down
3 changes: 3 additions & 0 deletions data_model/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String, vec::Vec};

use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_macro::FromVariant;
use iroha_schema::prelude::*;
use iroha_version::prelude::*;
Expand Down Expand Up @@ -160,6 +161,8 @@ pub trait Filter {
Hash,
Serialize,
Deserialize,
IntoFfi,
TryFromReprC,
)]
pub enum FilterBox {
/// Listen to pipeline events with filter.
Expand Down
7 changes: 6 additions & 1 deletion data_model/src/events/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! Pipeline events.
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};
use alloc::{alloc::alloc, format, string::String, vec::Vec};
#[cfg(feature = "std")]
use std::alloc::alloc;

use iroha_crypto::Hash;
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_macro::FromVariant;
use iroha_schema::prelude::IntoSchema;
use parity_scale_codec::{Decode, Encode};
Expand All @@ -28,6 +31,8 @@ pub use crate::transaction::RejectionReason as PipelineRejectionReason;
Hash,
Serialize,
Deserialize,
IntoFfi,
TryFromReprC,
)]
pub struct EventFilter {
/// If `Some::<EntityKind>` filters by the [`EntityKind`]. If `None` accepts all the [`EntityKind`].
Expand Down
9 changes: 9 additions & 0 deletions data_model/src/events/time.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
//! Time event and filter
#![allow(clippy::std_instead_of_core, clippy::arithmetic)]

#[cfg(not(feature = "std"))]
use alloc::alloc::alloc;
use core::{ops::Range, time::Duration};
#[cfg(feature = "std")]
use std::alloc::alloc;

use iroha_ffi::{IntoFfi, TryFromReprC};

use super::*;

Expand Down Expand Up @@ -42,7 +48,10 @@ impl Event {
Hash,
Serialize,
Deserialize,
IntoFfi,
TryFromReprC,
)]
#[repr(transparent)]
pub struct EventFilter(pub ExecutionTime);

impl Filter for EventFilter {
Expand Down
Loading

0 comments on commit ebb8dbb

Please sign in to comment.