Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Common lib (#350)
Browse files Browse the repository at this point in the history
* working util contract which can be shared between contracts

* rename shared contract to "util"

* add util to captcha contract

* move err macros

* refactor util contract to be named "util"

* rlib captcha contract to mirror other contracts (don't believe it's needed, but hey)

* pin to sr25519_verify ink pr

* move lazy macro

* rename util to common

* try error in common

* silence warning in proxy unused import for ink StorageLayout

* mod_name() macro, move err macro / func

* working

* add module name into named functions macro

* fmt

* rename contract from Util to Common

* rename ctor and func in common to avoid clippy warnings

* move unit test account functions to common

* move util testing functions to common

* fix get_contract_account() method in testing

* fmt

* move get_contract() to common, add reset_caller() and reset_callee()

* add common_dev to delineate common testing code from common contract code. Ink cannot handle the two together in one crate

* skip common_dev contract when building (does not compile due to being dev/testing module)

* fix skipping common_dev contract build in cli
  • Loading branch information
goastler authored May 31, 2023
1 parent bbb9d15 commit 48321f6
Show file tree
Hide file tree
Showing 13 changed files with 518 additions and 478 deletions.
286 changes: 55 additions & 231 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions contracts/captcha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ path = "../../crates/named_functions_macro"
version = "0.3.0"
path = "../../crates/disable_macro"

[dependencies.common]
version = "0.3.0"
path = "../../contracts/common"
default-features = false
features = ["ink-as-dependency"]

[dev-dependencies.common_dev]
version = "0.3.0"
path = "../../contracts/common_dev"
default-features = false
features = ["ink-as-dependency"]

[lib]
name = "captcha"
path = "src/lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
"rlib"
]

# Needed until https://github.com/paritytech/ink/issues/364 is resolved.
Expand Down
29 changes: 3 additions & 26 deletions contracts/captcha/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,18 @@

pub use self::captcha::{Captcha, CaptchaRef};

/// Print and return an error in ink
macro_rules! err {
($err:expr) => {{
Err(get_self!().print_err($err, function_name!()))
}};
}

// ($err:expr) => (
// |$err| crate::print_error($err, function_name!(), get_self!().env().block_number(), get_self!().env().caller())
// );

macro_rules! err_fn {
($err:expr) => {
|| get_self!().print_err($err, function_name!())
};
}

macro_rules! lazy {
($lazy:expr, $func:ident, $value:expr) => {
let mut contents = $lazy.get_or_default();
contents.$func($value);
$lazy.set(&contents);
};
}

#[allow(unused_macros)]
#[named_functions_macro::named_functions] // allows the use of the function_name!() macro
#[inject_self_macro::inject_self] // allows the use of the get_self!() macro
#[ink::contract]
pub mod captcha {

use common::err;
use common::err_fn;
use common::lazy;
use ink::env::debug_println as debug;
use ink::env::hash::{Blake2x128, Blake2x256, CryptoHash, HashOutput};
use ink::prelude::collections::btree_set::BTreeSet;

use ink::prelude::vec;
use ink::prelude::vec::Vec;
use ink::storage::Lazy;
Expand Down
32 changes: 32 additions & 0 deletions contracts/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "common"
version = "0.3.0"
authors = ["Chris Taylor [email protected]", "George Oastler [email protected]", "Vincenzo Ferrara", "Siniša Čanak"]
edition = "2021"

[dependencies]
ink = { git = "https://github.com/prosopo/ink", rev="9ca19c462d5f5d1572ddce595d2698207241fd4a", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[lib]
name = "common"
path = "src/lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
"rlib",
]

# Needed until https://github.com/paritytech/ink/issues/364 is resolved.
[profile.release]
overflow-checks = false

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
1 change: 1 addition & 0 deletions contracts/common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a common library for contracts. This is the non-testing contract version which should be imported using dependency, not dev-dependency. All non-testing contract related common contract code should go in this library. Unfortunately, ink does not expose tests from dev-dependencies _and_ build a contract properly, so we are forced to put them in a separate crates: one for testing (common-dev) and one for building contracts (common).
54 changes: 54 additions & 0 deletions contracts/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![cfg_attr(not(feature = "std"), no_std)]

/// Print and return an error in ink
#[macro_export]
macro_rules! err {
($e:expr) => {{
let self_ = get_self!();
ink::env::debug_println!(
"'{:?}' error in {:?}() at block {:?} with caller {:?}",
$e,
function_name!(),
self_.env().block_number(),
self_.env().caller(),
);
Err($e)
}};
}

#[macro_export]
macro_rules! err_fn {
($err:expr) => {
|| get_self!().print_err($err, function_name!())
};
}

#[macro_export]
macro_rules! lazy {
($lazy:expr, $func:ident, $value:expr) => {
let mut contents = $lazy.get_or_default();
contents.$func($value);
$lazy.set(&contents);
};
}

/// An ink contract must be defined in order to import functions into another contract
#[ink::contract]
pub mod common {

/// No fields are stored in the util contract as it's just filler
#[ink(storage)]
pub struct Common {}

/// Implementation of the contract
impl Common {
#[ink(constructor)]
pub fn noop_ctor() -> Self {
Self {}
}

/// No-op function to fill the mandatory ink message requirement
#[ink(message)]
pub fn noop_func(&self) {}
}
}
32 changes: 32 additions & 0 deletions contracts/common_dev/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "common_dev"
version = "0.3.0"
authors = ["Chris Taylor [email protected]", "George Oastler [email protected]", "Vincenzo Ferrara", "Siniša Čanak"]
edition = "2021"

[dependencies]
ink = { git = "https://github.com/prosopo/ink", rev="9ca19c462d5f5d1572ddce595d2698207241fd4a", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[lib]
name = "common_dev"
path = "src/lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
"rlib",
]

# Needed until https://github.com/paritytech/ink/issues/364 is resolved.
[profile.release]
overflow-checks = false

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
1 change: 1 addition & 0 deletions contracts/common_dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a common library for contracts. This is the dev version which should be imported using dev-dependency. All test related common contract code should go in this library. Unfortunately, ink does not expose tests from dev-dependencies _and_ build a contract properly, so we are forced to put them in a separate crates: one for testing (common-dev) and one for building contracts (common).
Loading

0 comments on commit 48321f6

Please sign in to comment.