-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
executable file
·83 lines (69 loc) · 1.99 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![cfg_attr(not(feature = "std"), no_std)]
#[openbrush::contract]
mod reentrancy_test {
use openbrush::contracts::reentrancy_guard::{self, *};
use openbrush::traits::Storage;
use ink::primitives::Key;
#[ink(event)]
pub struct StorageDecodedAsGuard {
status: u8,
}
#[ink(event)]
pub struct StorageDecodedCorrectly {
value: u32,
}
#[ink(storage)]
#[derive(Storage)]
pub struct ReentrancyTest {
#[storage_field]
guard: reentrancy_guard::Data,
value: u32,
}
impl ReentrancyTest {
#[ink(constructor)]
pub fn new() -> Self {
Self {
guard: Default::default(),
value: 123456789,
}
}
#[ink(message)]
pub fn get(&self) -> u32 {
self.value
}
#[ink(message)]
pub fn increment_recursive_reentrant(&mut self, num: u32) {
self.check_storage();
if num == 0 {
self.value += 1;
return;
}
}
#[ink(message)]
#[openbrush::modifiers(non_reentrant)]
pub fn increment_recursive(&mut self, num: u32) -> Result<(), ReentrancyGuardError> {
self.check_storage();
if num == 0 {
self.value += 1;
return Ok(());
}
Ok(())
}
fn check_storage(&self) {
match ink::env::get_contract_storage::<Key, ReentrancyTest>(&0) {
Ok(Some(s)) => {
self.env()
.emit_event(StorageDecodedCorrectly { value: s.value });
}
_ => {}
};
match ink::env::get_contract_storage::<Key, reentrancy_guard::Data>(&0) {
Ok(Some(g)) => {
self.env()
.emit_event(StorageDecodedAsGuard { status: g.status });
}
_ => {}
};
}
}
}