forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguards.rs
More file actions
54 lines (45 loc) · 1.43 KB
/
Copy pathguards.rs
File metadata and controls
54 lines (45 loc) · 1.43 KB
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
//! Defensive guards used by state-changing entrypoints.
use soroban_sdk::{contracterror, panic_with_error, symbol_short, Env, Symbol};
const KEY_GUARD: Symbol = symbol_short!("RE_GUARD");
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum GuardError {
ReentrancyDetected = 100,
}
/// Sets the reentrancy lock. Panics if the current invocation already holds it.
pub fn enter_reentrancy_guard(env: &Env) {
if env.storage().temporary().has(&KEY_GUARD) {
panic_with_error!(env, GuardError::ReentrancyDetected);
}
env.storage().temporary().set(&KEY_GUARD, &true);
}
/// Clears the reentrancy lock.
pub fn exit_reentrancy_guard(env: &Env) {
env.storage().temporary().remove(&KEY_GUARD);
}
/// RAII guard for reentrancy protection.
pub struct ReentrancyGuard<'a> {
env: &'a Env,
}
impl<'a> ReentrancyGuard<'a> {
pub fn enter(env: &'a Env) -> Self {
enter_reentrancy_guard(env);
Self { env }
}
}
impl Drop for ReentrancyGuard<'_> {
fn drop(&mut self) {
exit_reentrancy_guard(self.env);
}
}
/// Wrap a state-changing function or block with the reentrancy guard.
#[macro_export]
macro_rules! non_reentrant {
($env:expr) => {
let _reentrancy_guard = $crate::guards::ReentrancyGuard::enter($env);
};
($env:expr, $body:block) => {{
let _reentrancy_guard = $crate::guards::ReentrancyGuard::enter($env);
$body
}};
}