Skip to content

Commit

Permalink
replace client flag with ffi_export/ffi_import
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
  • Loading branch information
mversic committed Aug 8, 2022
1 parent dfb2d38 commit 1e1f14e
Show file tree
Hide file tree
Showing 32 changed files with 846 additions and 730 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ default = ["std"]
std = ["ursa"]
# Force static linking
vendored = ["openssl-sys"]
## Replace structures and methods with FFI equivalents. Facilitates dynamic linkage (mainly used in smartcontracts)
#ffi = ["iroha_ffi/client"] # TODO: Doesn't produce valid code yet
# Replace structures and methods with FFI equivalents to facilitate dynamic linkage (mainly used in smartcontracts)
ffi_import = []

# Expose FFI API for dynamic linking (Internal use only)
ffi_api = ["std"]
ffi_export = ["std"]

[dependencies]
iroha_primitives = { path = "../primitives", version = "=2.0.0-pre-rc.7", default-features = false }
Expand Down
14 changes: 14 additions & 0 deletions crypto/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Build script
//!
//! Warn if `ffi_import` and `ffi_export` features are active at the same time

fn main() {
let ffi_import = std::env::var_os("CARGO_FEATURE_FFI_IMPORT").is_some();
let ffi_export = std::env::var_os("CARGO_FEATURE_FFI_EXPORT").is_some();

#[allow(clippy::print_stderr)]
if ffi_import && ffi_export {
eprintln!("cargo:warning=Features `ffi_export` and `ffi_import` are mutually exclusive");
eprintln!("cargo:warning=When both active, `ffi_import` feature takes precedence");
}
}
95 changes: 64 additions & 31 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ mod signature;
mod varint;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, format, string::String, vec::Vec};
use alloc::{alloc::alloc, 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};
use getset::Getters;
pub use hash::*;
#[cfg(any(feature = "ffi_api", feature = "ffi"))]
use iroha_ffi::ffi_export;
use iroha_ffi::{ffi, IntoFfi, TryFromReprC};
use iroha_ffi::{IntoFfi, TryFromReprC};
use iroha_primitives::conststr::ConstString;
use iroha_schema::IntoSchema;
pub use merkle::MerkleTree;
Expand Down Expand Up @@ -64,22 +64,24 @@ pub struct NoSuchAlgorithm;
#[cfg(feature = "std")]
impl std::error::Error for NoSuchAlgorithm {}

/// Algorithm for hashing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, IntoFfi, TryFromReprC)]
#[repr(u8)]
pub enum Algorithm {
/// Ed25519
#[display(fmt = "{}", "ED_25519")]
Ed25519,
/// Secp256k1
#[display(fmt = "{}", "SECP_256_K1")]
Secp256k1,
/// BlsNormal
#[display(fmt = "{}", "BLS_NORMAL")]
BlsNormal,
/// BlsSmall
#[display(fmt = "{}", "BLS_SMALL")]
BlsSmall,
ffi::ffi_item! {
/// Algorithm for hashing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, IntoFfi, TryFromReprC)]
#[repr(u8)]
pub enum Algorithm {
/// Ed25519
#[display(fmt = "{}", "ED_25519")]
Ed25519,
/// Secp256k1
#[display(fmt = "{}", "SECP_256_K1")]
Secp256k1,
/// BlsNormal
#[display(fmt = "{}", "BLS_NORMAL")]
BlsNormal,
/// BlsSmall
#[display(fmt = "{}", "BLS_SMALL")]
BlsSmall,
}
}

impl Default for Algorithm {
Expand Down Expand Up @@ -164,7 +166,7 @@ impl KeyGenConfiguration {
}
}

ffi! {
ffi::ffi_item! {
/// Pair of Public and Private keys.
#[derive(Debug, Clone, PartialEq, Eq, Getters, Serialize, IntoFfi, TryFromReprC)]
#[getset(get = "pub")]
Expand Down Expand Up @@ -364,7 +366,7 @@ impl From<multihash::ConvertError> for KeyParseError {
#[cfg(feature = "std")]
impl std::error::Error for KeyParseError {}

ffi! {
ffi::ffi_item! {
/// Public Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, IntoFfi, TryFromReprC, IntoSchema)]
#[debug(
Expand All @@ -379,7 +381,11 @@ ffi! {
}
}

#[cfg_attr(any(feature = "ffi_api", feature = "ffi"), ffi_export)]
#[cfg_attr(
all(feature = "ffi_export", not(feature = "ffi_import")),
iroha_ffi::ffi_export
)]
#[cfg_attr(feature = "ffi_import", iroha_ffi::ffi_import)]
impl PublicKey {
/// Key payload
pub fn payload(&self) -> &[u8] {
Expand Down Expand Up @@ -496,7 +502,7 @@ impl Decode for PublicKey {
}
}

ffi! {
ffi::ffi_item! {
/// Private Key used in signatures.
#[derive(DebugCustom, Clone, PartialEq, Eq, Display, Serialize, IntoFfi, TryFromReprC)]
#[debug(fmt = "{{ digest: {digest_function}, payload: {:X?}}}", payload)]
Expand All @@ -511,7 +517,11 @@ ffi! {
}
}

#[cfg_attr(any(feature = "ffi_api", feature = "ffi"), ffi_export)]
#[cfg_attr(
all(feature = "ffi_export", not(feature = "ffi_import")),
iroha_ffi::ffi_export
)]
#[cfg_attr(feature = "ffi_import", iroha_ffi::ffi_import)]
impl PrivateKey {
/// Key payload
pub fn payload(&self) -> &[u8] {
Expand Down Expand Up @@ -577,16 +587,39 @@ impl<'de> Deserialize<'de> for PrivateKey {
}
}

#[cfg(any(feature = "ffi_api", feature = "ffi"))]
mod ffi {
pub mod ffi {
//! Definitions and implementations of FFI related functionalities

use super::*;

macro_rules! ffi_item {
($it: item) => {
#[cfg(not(feature = "ffi_import"))]
$it

#[cfg(feature = "ffi_import")]
iroha_ffi::ffi! { $it }
};
}

#[cfg(any(feature = "ffi_export", feature = "ffi_import"))]
macro_rules! ffi_fn {
($macro_name: ident) => {
iroha_ffi::$macro_name! { Clone: KeyPair, PublicKey, PrivateKey }
iroha_ffi::$macro_name! { Eq: KeyPair, PublicKey, PrivateKey }
iroha_ffi::$macro_name! { Ord: PublicKey }
iroha_ffi::$macro_name! { Drop: KeyPair, PublicKey, PrivateKey }
};
}

iroha_ffi::handles! {KeyPair, PublicKey, PrivateKey}

iroha_ffi::ffi_fn! { Clone: KeyPair, PublicKey, PrivateKey}
iroha_ffi::ffi_fn! { Eq: KeyPair, PublicKey, PrivateKey}
iroha_ffi::ffi_fn! { Ord: PublicKey }
iroha_ffi::ffi_fn! { Drop: KeyPair, PublicKey, PrivateKey}
#[cfg(feature = "ffi_import")]
ffi_fn! {decl_ffi_fn}
#[cfg(all(feature = "ffi_export", not(feature = "ffi_import")))]
ffi_fn! {def_ffi_fn}

pub(crate) use ffi_item;
}

/// The prelude re-exports most commonly used traits, structs and macros from this crate.
Expand Down
6 changes: 3 additions & 3 deletions data_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ default = ["std"]
# Disabled for WASM interoperability, to reduce the binary size.
# 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. Facilitates dynamic linkage (mainly used in smartcontracts)
#ffi = ["iroha_ffi/client"] # TODO: Doesn't produce valid code yet.
# Replace structures and methods with FFI equivalents to facilitate dynamic linkage (mainly used in smartcontracts)
ffi_import = ["iroha_crypto/ffi_import"]

# Expose FFI API for dynamic linking (Internal use only)
ffi_api = ["std"]
ffi_export = ["std", "iroha_crypto/ffi_export"]
# Expose API for mutating structures (Internal use only)
mutable_api = []

Expand Down
14 changes: 14 additions & 0 deletions data_model/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Build script
//!
//! Warn if `ffi_import` and `ffi_export` features are active at the same time

fn main() {
let ffi_import = std::env::var_os("CARGO_FEATURE_FFI_IMPORT").is_some();
let ffi_export = std::env::var_os("CARGO_FEATURE_FFI_EXPORT").is_some();

#[allow(clippy::print_stderr)]
if ffi_import && ffi_export {
println!("cargo:warning=Features `ffi_export` and `ffi_import` are mutually exclusive");
println!("cargo:warning=When both active, `ffi_import` feature takes precedence");
}
}
Loading

0 comments on commit 1e1f14e

Please sign in to comment.