Skip to content

Commit

Permalink
use context in IntoFfi
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>

add support for data-carrying enums

Signed-off-by: Marin Veršić <[email protected]>

refactor ffi library implementation

Signed-off-by: Marin Veršić <[email protected]>

improve wasm types FFI serialization

Signed-off-by: Marin Veršić <[email protected]>

introduce Opaque and out-pointers in IR

Signed-off-by: Marin Veršić <[email protected]>

introduce Transparent in IR, use it for fieldless enums

Signed-off-by: Marin Veršić <[email protected]>

remove Transmute in favor of Transparent

Signed-off-by: Marin Veršić <[email protected]>

Extend IR, make generic impls for slices and vectors

Signed-off-by: Marin Veršić <[email protected]>

add implementation for data-carrying enum

Signed-off-by: Marin Veršić <[email protected]>

separate FfiType and FfiConvert traits

Signed-off-by: Marin Veršić <[email protected]>

implement support for arrays

Signed-off-by: Marin Veršić <[email protected]>

prevent use-after-free in FFI method return

Signed-off-by: Marin Veršić <[email protected]>

fix review comments

Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Sep 28, 2022
1 parent d0ac14f commit 7b265c4
Show file tree
Hide file tree
Showing 62 changed files with 3,206 additions and 2,734 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.

2 changes: 1 addition & 1 deletion crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ std = ["ursa"]
# Force static linking
vendored = ["openssl-sys"]
# Replace structures and methods with FFI equivalents to facilitate dynamic linkage (mainly used in smartcontracts)
ffi_import = []
#ffi_import = []

# Expose FFI API for dynamic linking (Internal use only)
ffi_export = ["std"]
Expand Down
51 changes: 30 additions & 21 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[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};

use derive_more::{DebugCustom, Deref, DerefMut, Display};
use iroha_ffi::FfiType;
use iroha_schema::prelude::*;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
Expand All @@ -12,26 +13,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,
FfiType,
)]
// TODO: Make transparent
//#[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 +107,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
12 changes: 5 additions & 7 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ mod varint;
#[cfg(not(feature = "std"))]
use alloc::{alloc::alloc, borrow::ToOwned, boxed::Box, format, string::String, vec::Vec};
use core::{fmt, str::FromStr};
#[cfg(feature = "std")]
use std::alloc::alloc;

#[cfg(feature = "base64")]
pub use base64;
use derive_more::{DebugCustom, Display, From};
use getset::Getters;
pub use hash::*;
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_ffi::FfiType;
use iroha_primitives::conststr::ConstString;
use iroha_schema::IntoSchema;
pub use merkle::MerkleTree;
Expand Down Expand Up @@ -66,7 +64,7 @@ impl std::error::Error for NoSuchAlgorithm {}

ffi::ffi_item! {
/// Algorithm for hashing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, IntoFfi, TryFromReprC)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, FfiType)]
#[repr(u8)]
pub enum Algorithm {
/// Ed25519
Expand Down Expand Up @@ -168,7 +166,7 @@ impl KeyGenConfiguration {

ffi::ffi_item! {
/// Pair of Public and Private keys.
#[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize, IntoFfi, TryFromReprC)]
#[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize, FfiType)]
#[getset(get = "pub")]
pub struct KeyPair {
/// Public Key.
Expand Down Expand Up @@ -364,7 +362,7 @@ impl std::error::Error for KeyParseError {}

ffi::ffi_item! {
/// Public Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, IntoFfi, TryFromReprC, IntoSchema)]
#[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, FfiType, IntoSchema)]
#[debug(
fmt = "{{ digest: {digest_function}, payload: {} }}",
"hex::encode_upper(payload.as_slice())"
Expand Down Expand Up @@ -501,7 +499,7 @@ impl Decode for PublicKey {

ffi::ffi_item! {
/// Private Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, Display, Serialize, IntoFfi, TryFromReprC)]
#[derive(DebugCustom, Clone, PartialEq, Eq, Display, Serialize, FfiType)]
#[debug(fmt = "{{ digest: {digest_function}, payload: {:X?}}}", payload)]
#[display(fmt = "{}", "hex::encode(payload)")]
#[allow(clippy::multiple_inherent_impl)]
Expand Down
2 changes: 1 addition & 1 deletion data_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ default = ["std"]
# Please refer to https://docs.rust-embedded.org/book/intro/no-std.html
std = ["iroha_macro/std", "iroha_version/std", "iroha_version/warp", "iroha_crypto/std", "iroha_primitives/std", "thiserror", "strum/std", "dashmap", "tokio"]
# Replace structures and methods with FFI equivalents to facilitate dynamic linkage (mainly used in smartcontracts)
ffi_import = ["iroha_crypto/ffi_import"]
#ffi_import = ["iroha_crypto/ffi_import"]

# Expose FFI API for dynamic linking (Internal use only)
ffi_export = ["std", "iroha_crypto/ffi_export"]
Expand Down
1 change: 1 addition & 0 deletions data_model/derive/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ pub fn impl_filter(event: &EventEnum) -> TokenStream {
Encode,
Deserialize,
Serialize,
iroha_ffi::FfiType,
IntoSchema,
Hash,
)]
Expand Down
26 changes: 9 additions & 17 deletions data_model/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#[cfg(not(feature = "std"))]
use alloc::{
alloc::alloc,
boxed::Box,
collections::{btree_map, btree_set},
format,
Expand All @@ -12,15 +11,12 @@ use alloc::{
};
use core::str::FromStr;
#[cfg(feature = "std")]
use std::{
alloc::alloc,
collections::{btree_map, btree_set},
};
use std::collections::{btree_map, btree_set};

use derive_more::Display;
use getset::{Getters, MutGetters, Setters};
use iroha_data_model_derive::IdOrdEqHash;
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_ffi::FfiType;
use iroha_schema::IntoSchema;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
Expand All @@ -35,7 +31,7 @@ use crate::{
},
domain::prelude::*,
expression::{ContainsAny, ContextValue, EvaluatesTo},
ffi::ffi_item,
ffi::declare_item,
metadata::Metadata,
role::{prelude::RoleId, RoleIds},
HasMetadata, Identifiable, Name, ParseError, PublicKey, Registered,
Expand Down Expand Up @@ -97,8 +93,7 @@ impl From<GenesisAccount> for Account {
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
FfiType,
IntoSchema,
)]
pub struct SignatureCheckCondition(pub EvaluatesTo<bool>);
Expand Down Expand Up @@ -135,7 +130,7 @@ impl Default for SignatureCheckCondition {
}
}

ffi_item! {
declare_item! {
/// Builder which should be submitted in a transaction to create a new [`Account`]
#[derive(
Debug,
Expand All @@ -146,8 +141,7 @@ ffi_item! {
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
FfiType,
IntoSchema,
)]
#[id(type = "<Account as Identifiable>::Id")]
Expand Down Expand Up @@ -216,7 +210,7 @@ impl NewAccount {
}
}

ffi_item! {
declare_item! {
/// Account entity is an authority which is used to execute `Iroha Special Instructions`.
#[derive(
Debug,
Expand All @@ -230,8 +224,7 @@ ffi_item! {
Encode,
Deserialize,
Serialize,
IntoFfi,
TryFromReprC,
FfiType,
IntoSchema,
)]
#[allow(clippy::multiple_inherent_impl)]
Expand Down Expand Up @@ -400,8 +393,7 @@ impl FromIterator<Account> for crate::Value {
Encode,
DeserializeFromStr,
SerializeDisplay,
IntoFfi,
TryFromReprC,
FfiType,
IntoSchema,
)]
#[display(fmt = "{name}@{domain_id}")]
Expand Down
Loading

0 comments on commit 7b265c4

Please sign in to comment.