Skip to content

Commit

Permalink
attach ffi_bindgen to data model structs, support Into<T> in ffi_bindgen
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 May 9, 2022
1 parent 1810caf commit 6de64fe
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 192 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.

1 change: 0 additions & 1 deletion client/src/http_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ trait SetSingleHeader {
}

impl SetSingleHeader for AttoHttpRequestBuilderWithBytes {
#[allow(clippy::only_used_in_recursion)] // False-positive
fn header(self, key: HeaderName, value: String) -> Self {
self.header(key, value)
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/smartcontracts/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,20 @@ impl<'wrld, W: WorldTrait> State<'wrld, W> {
}
}

struct IrohaModule<'a, W: WorldTrait> {
linker: Linker<State<'a, W>>,
struct IrohaModule<'wrld, W: WorldTrait> {
linker: Linker<State<'wrld, W>>,
module: Module,
}

impl<'a, W: WorldTrait> IrohaModule<'a, W> {
impl<'wrld, W: WorldTrait> IrohaModule<'wrld, W> {
fn new(engine: &Engine, module_path: &str) -> Result<Self, Error> {
let linker = Self::create_linker(engine).map_err(Error::Initialization)?;
let module = Module::from_file(engine, module_path).map_err(Error::Initialization)?;

Ok(Self { linker, module })
}

fn create_linker(engine: &Engine) -> Result<Linker<State<'a, W>>, anyhow::Error> {
fn create_linker(engine: &Engine) -> Result<Linker<State<'wrld, W>>, anyhow::Error> {
let mut linker = Linker::new(engine);

linker
Expand Down Expand Up @@ -224,7 +224,7 @@ impl<'a, W: WorldTrait> IrohaModule<'a, W> {
.ok_or_else(|| Trap::new(format!("{}: not a memory", WASM_MEMORY_NAME)))
}

fn instantiate(&self, store: &mut Store<State<'a, W>>) -> Result<Instance, Error> {
fn instantiate(&self, store: &mut Store<State<'wrld, W>>) -> Result<Instance, Error> {
self.linker
.instantiate(store, &self.module)
.map_err(Error::Instantiation)
Expand Down
20 changes: 13 additions & 7 deletions data_model/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl Default for SignatureCheckCondition {
}

/// Builder which can be submitted in a transaction to create a new [`Account`]
#[allow(clippy::multiple_inherent_impl)]
#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema)]
pub struct NewAccount {
id: Id,
Expand Down Expand Up @@ -140,13 +141,6 @@ impl NewAccount {
}
}

/// Add [`Metadata`] to the account replacing previously defined
#[must_use]
pub fn with_metadata(mut self, metadata: Metadata) -> Self {
self.metadata = metadata;
self
}

/// Construct [`Account`]
#[must_use]
#[cfg(feature = "mutable_api")]
Expand All @@ -163,6 +157,16 @@ impl NewAccount {
}
}

#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
impl NewAccount {
/// Add [`Metadata`] to the account replacing previously defined
#[must_use]
pub fn with_metadata(mut self, metadata: Metadata) -> Self {
self.metadata = metadata;
self
}
}

/// Account entity is an authority which is used to execute `Iroha Special Instructions`.
#[derive(
Debug,
Expand All @@ -180,6 +184,7 @@ impl NewAccount {
)]
#[getset(get = "pub")]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
pub struct Account {
/// An Identification of the [`Account`].
id: <Self as Identifiable>::Id,
Expand Down Expand Up @@ -222,6 +227,7 @@ impl Ord for Account {
}
}

#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
impl Account {
/// Construct builder for [`Account`] identifiable by [`Id`] containing the given signatories.
#[must_use]
Expand Down
50 changes: 33 additions & 17 deletions data_model/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub type AssetDefinitionsMap =
IntoSchema,
)]
#[getset(get = "pub")]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
pub struct AssetDefinitionEntry {
/// Asset definition.
#[cfg_attr(feature = "mutable_api", getset(get_mut = "pub"))]
Expand All @@ -68,6 +70,7 @@ impl Ord for AssetDefinitionEntry {
}
}

#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
impl AssetDefinitionEntry {
/// Constructor.
pub const fn new(
Expand All @@ -79,12 +82,14 @@ impl AssetDefinitionEntry {
registered_by,
}
}
}

#[cfg(feature = "mutable_api")]
impl AssetDefinitionEntry {
/// Turn off minting for this asset.
///
/// # Errors
/// If the asset was declared as `Mintable::Infinitely`
#[cfg(feature = "mutable_api")]
pub fn forbid_minting(&mut self) -> Result<(), super::MintabilityError> {
self.definition.forbid_minting()
}
Expand All @@ -106,6 +111,8 @@ impl AssetDefinitionEntry {
IntoSchema,
)]
#[getset(get = "pub")]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
pub struct AssetDefinition {
/// An Identification of the [`AssetDefinition`].
id: <Self as Identifiable>::Id,
Expand Down Expand Up @@ -165,6 +172,7 @@ pub enum Mintable {
Debug, Clone, PartialEq, Eq, Getters, Decode, Encode, Deserialize, Serialize, IntoSchema,
)]
#[getset(get = "pub")]
#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
pub struct Asset {
/// Component Identification.
id: <Self as Identifiable>::Id,
Expand Down Expand Up @@ -361,6 +369,7 @@ pub struct Id {
}

/// Builder which can be submitted in a transaction to create a new [`AssetDefinition`]
#[allow(clippy::multiple_inherent_impl)]
#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema)]
pub struct NewAssetDefinition {
id: <AssetDefinition as Identifiable>::Id,
Expand Down Expand Up @@ -394,6 +403,22 @@ impl NewAssetDefinition {
}
}

/// Construct [`AssetDefinition`]
#[inline]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> AssetDefinition {
AssetDefinition {
id: self.id,
value_type: self.value_type,
mintable: self.mintable,
metadata: self.metadata,
}
}
}

#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
impl NewAssetDefinition {
/// Set mintability to [`Mintable::Once`]
#[inline]
#[must_use]
Expand All @@ -409,21 +434,9 @@ impl NewAssetDefinition {
self.metadata = metadata;
self
}

/// Construct [`AssetDefinition`]
#[inline]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> AssetDefinition {
AssetDefinition {
id: self.id,
value_type: self.value_type,
mintable: self.mintable,
metadata: self.metadata,
}
}
}

#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
impl AssetDefinition {
/// Construct builder for [`AssetDefinition`] identifiable by [`Id`].
#[must_use]
Expand Down Expand Up @@ -452,13 +465,15 @@ impl AssetDefinition {
pub fn store(id: <Self as Identifiable>::Id) -> <Self as Identifiable>::RegisteredWith {
<Self as Identifiable>::RegisteredWith::new(id, AssetValueType::Store)
}
}

#[cfg(feature = "mutable_api")]
impl AssetDefinition {
/// Stop minting on the [`AssetDefinition`] globally.
///
/// # Errors
/// If the [`AssetDefinition`] is not `Mintable::Once`.
#[inline]
#[cfg(feature = "mutable_api")]
pub fn forbid_minting(&mut self) -> Result<(), super::MintabilityError> {
if self.mintable == Mintable::Once {
self.mintable = Mintable::Not;
Expand All @@ -469,11 +484,12 @@ impl AssetDefinition {
}
}

#[cfg_attr(feature = "ffi", iroha_ffi::ffi_bindgen)]
impl Asset {
/// Constructor
pub fn new<V: Into<AssetValue>>(
pub fn new(
id: <Asset as Identifiable>::Id,
value: V,
value: impl Into<AssetValue>,
) -> <Self as Identifiable>::RegisteredWith {
Self {
id,
Expand Down
34 changes: 21 additions & 13 deletions data_model/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use core::{cmp::Ordering, fmt, str::FromStr};

use getset::{Getters, MutGetters};
use iroha_crypto::PublicKey;
#[cfg(feature = "ffi")]
use iroha_ffi::ffi_bindgen;
use iroha_schema::IntoSchema;
use parity_scale_codec::{Decode, Encode, Input};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -68,6 +70,7 @@ impl From<GenesisDomain> for Domain {

/// Builder which can be submitted in a transaction to create a new [`Domain`]
#[derive(Debug, Clone, PartialEq, Eq, Decode, Encode, Deserialize, Serialize, IntoSchema)]
#[allow(clippy::multiple_inherent_impl)]
pub struct NewDomain {
/// The identification associated to the domain builder.
id: <Domain as Identifiable>::Id,
Expand Down Expand Up @@ -102,6 +105,22 @@ impl NewDomain {
}
}

/// Construct [`Domain`]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> Domain {
Domain {
id: self.id,
accounts: AccountsMap::default(),
asset_definitions: AssetDefinitionsMap::default(),
metadata: self.metadata,
logo: self.logo,
}
}
}

#[cfg_attr(feature = "ffi", ffi_bindgen)]
impl NewDomain {
/// Add [`logo`](IpfsPath) to the domain replacing previously defined value
#[must_use]
pub fn with_logo(mut self, logo: IpfsPath) -> Self {
Expand All @@ -115,19 +134,6 @@ impl NewDomain {
self.metadata = metadata;
self
}

/// Construct [`Domain`]
#[must_use]
#[cfg(feature = "mutable_api")]
pub fn build(self) -> Domain {
Domain {
id: self.id,
accounts: AccountsMap::default(),
asset_definitions: AssetDefinitionsMap::default(),
metadata: self.metadata,
logo: self.logo,
}
}
}

/// Named group of [`Account`] and [`Asset`](`crate::asset::Asset`) entities.
Expand All @@ -146,6 +152,7 @@ impl NewDomain {
)]
#[getset(get = "pub")]
#[allow(clippy::multiple_inherent_impl)]
#[cfg_attr(feature = "ffi", ffi_bindgen)]
pub struct Domain {
/// Identification of this [`Domain`].
id: <Self as Identifiable>::Id,
Expand Down Expand Up @@ -181,6 +188,7 @@ impl Ord for Domain {
}
}

#[cfg_attr(feature = "ffi", ffi_bindgen)]
impl Domain {
/// Construct builder for [`Domain`] identifiable by [`Id`].
pub fn new(id: <Self as Identifiable>::Id) -> <Self as Identifiable>::RegisteredWith {
Expand Down

0 comments on commit 6de64fe

Please sign in to comment.