Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions examples/reentrancy_guard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "flipper"
version = "1.4.0"
authors = ["Supercolony <green.baneling@supercolony.net>"]
edition = "2021"

[dependencies]
ink_primitives = { branch = "master", git = "https://github.com/paritytech/ink", default-features = false }
ink_metadata = { branch = "master", git = "https://github.com/paritytech/ink", default-features = false, features = ["derive"], optional = true }
ink_env = { branch = "master", git = "https://github.com/paritytech/ink", default-features = false }
ink_storage = { branch = "master", git = "https://github.com/paritytech/ink", default-features = false }
ink_lang = { branch = "master", git = "https://github.com/paritytech/ink", default-features = false }
ink_prelude = { branch = "master", git = "https://github.com/paritytech/ink", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

# These dependencies
brush = { path = "../..", default-features = false, features = ["reentrancy_guard"] }

[lib]
name = "flipper"
path = "lib.rs"

crate-type = [
"rlib",
]

[features]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"scale/std",
"scale-info",
"scale-info/std",

# These dependencies
"brush/std",
]

ink-as-dependency = []

[profile.dev]
codegen-units = 16
overflow-checks = false

[profile.release]
overflow-checks = false
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ ink_prelude = { branch = "master", git = "https://github.com/paritytech/ink", de
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

# This dependencies
# We will do a cross contract call via `MyFlipper` wrapper imported here
brush = { path = "../../..", default-features = false, features = ["reentrancy_guard"] }
my_flipper_guard = { path = "../flipper", default-features = false, features = ["ink-as-dependency"] }
flipper = { path = "../..", default-features = false }
brush = { path = "../../../..", default-features = false }

[lib]
name = "flip_on_me"
Expand All @@ -41,8 +39,8 @@ std = [
"scale-info",
"scale-info/std",

# This dependencies
"my_flipper_guard/std",
"flipper/std",
"brush/std",
]
ink-as-dependency = []

Expand Down
20 changes: 20 additions & 0 deletions examples/reentrancy_guard/contracts/flip_on_me/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[brush::contract]
pub mod flip_on_me {
use flipper::traits::flip_on_me::*;
use ink_storage::traits::SpreadAllocate;

#[ink(storage)]
#[derive(Default, SpreadAllocate)]
pub struct FlipOnMeContract {}

impl FlipOnMeContract {
#[ink(constructor)]
pub fn new() -> Self {
ink_lang::codegen::initialize_contract(|_instance: &mut Self| {})
}
}

impl FlipOnMe for FlipOnMeContract {}
}
9 changes: 9 additions & 0 deletions examples/reentrancy_guard/contracts/flipper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }

# These dependencies
brush = { path = "../../..", default-features = false, features = ["reentrancy_guard"] }
flipper = { path = "../..", default-features = false }
brush = { path = "../../../..", default-features = false }

[lib]
name = "my_flipper_guard"
path = "lib.rs"

crate-type = [
"cdylib",
# This contract will be imported by FlipOnMe contract, so we need build this crate also like a `rlib`
"rlib",
]

[features]
Expand All @@ -42,6 +41,7 @@ std = [
"scale-info/std",

# These dependencies
"flipper/std",
"brush/std",
]

Expand Down
34 changes: 34 additions & 0 deletions examples/reentrancy_guard/contracts/flipper/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[brush::contract]
pub mod my_flipper_guard {
use flipper::traits::flipper::*;
use ink_storage::traits::SpreadAllocate;

#[ink(storage)]
#[derive(Default, SpreadAllocate, ReentrancyGuardStorage)]
pub struct MyFlipper {
#[ReentrancyGuardStorageField]
guard: ReentrancyGuardData,
value: bool,
}

impl FlipperStorage for MyFlipper {
fn value(&self) -> &bool {
&self.value
}

fn value_mut(&mut self) -> &mut bool {
&mut self.value
}
}

impl MyFlipper {
#[ink(constructor)]
pub fn new() -> Self {
ink_lang::codegen::initialize_contract(|_instance: &mut Self| {})
}
}

impl Flipper for MyFlipper {}
}
31 changes: 0 additions & 31 deletions examples/reentrancy_guard/flip_on_me/lib.rs

This file was deleted.

104 changes: 0 additions & 104 deletions examples/reentrancy_guard/flipper/lib.rs

This file was deleted.

3 changes: 3 additions & 0 deletions examples/reentrancy_guard/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod traits;
23 changes: 23 additions & 0 deletions examples/reentrancy_guard/traits/flip_on_me.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub use brush::contracts::reentrancy_guard::*;
use brush::traits::{
AccountId,
InkStorage,
};

#[brush::wrapper]
pub type FlipOnMeRef = dyn FlipOnMe;

#[brush::trait_definition]
pub trait FlipOnMe: InkStorage {
#[ink(message)]
fn flip_on_me(&mut self) -> Result<(), ReentrancyGuardError> {
let caller = Self::env().caller();
self.flip_on_target(caller)
}

#[ink(message)]
fn flip_on_target(&mut self, callee: AccountId) -> Result<(), ReentrancyGuardError> {
// This method does a cross-contract call to caller contract and calls the `flip` method.
crate::traits::flipper::FlipperRef::flip(&callee)
}
}
38 changes: 38 additions & 0 deletions examples/reentrancy_guard/traits/flipper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub use brush::contracts::reentrancy_guard::*;
use brush::{
modifiers,
traits::AccountId,
};

pub trait FlipperStorage {
fn value(&self) -> &bool;
fn value_mut(&mut self) -> &mut bool;
}

#[brush::wrapper]
pub type FlipperRef = dyn Flipper;

#[brush::trait_definition]
pub trait Flipper: FlipperStorage + ReentrancyGuardStorage {
#[ink(message)]
fn get_value(&self) -> bool {
self.value().clone()
}

#[ink(message)]
#[brush::modifiers(non_reentrant)]
fn flip(&mut self) -> Result<(), ReentrancyGuardError> {
*self.value_mut() = !self.value().clone();
Ok(())
}

#[ink(message)]
#[modifiers(non_reentrant)]
fn call_flip_on_me(&mut self, callee: AccountId) -> Result<(), ReentrancyGuardError> {
// This method will do a cross-contract call to callee account. It calls method `flip_on_me`.
// Callee contract during execution of `flip_on_me` will call `flip` of this contract.
// `call_flip_on_me` and `flip` are marked with `non_reentrant` modifier. It means,
// that call of `flip` after `call_flip_on_me` must fail.
crate::traits::flip_on_me::FlipOnMeRef::flip_on_me(&callee)
}
}
2 changes: 2 additions & 0 deletions examples/reentrancy_guard/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod flip_on_me;
pub mod flipper;
2 changes: 1 addition & 1 deletion redspot.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
contract: {
ink: {
toolchain: 'nightly',
sources: ['example_project_structure/contracts/**', 'examples/**', 'mock/**']
sources: ['example_project_structure/contracts/**', 'examples/**/', 'mock/**', `!examples/reentrancy_guard/Cargo.toml`]
}
},
networks: {
Expand Down
Loading