-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
52 lines (42 loc) · 1.22 KB
/
main.rs
File metadata and controls
52 lines (42 loc) · 1.22 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
use ic_cdk::export::candid::candid_method;
use ic_cdk::export::Principal;
use ic_cdk_macros::{query, update};
use std::cell::RefCell;
thread_local!(
static COUNTER: RefCell<u64> = RefCell::new(0);
);
#[query]
#[candid_method(query)]
fn query() -> u64 {
COUNTER.with(|c| c.borrow().clone())
}
#[update]
#[candid_method(update)]
fn update() {
COUNTER.with(|c| {
*c.borrow_mut() += 1;
});
ic_cdk::block_on(async {
// usually, when the canister is trapped, it will auto rollback the state
// ic_cdk::trap("panic!");
// some calls
let (rand,): (Vec<u8>,) = ic_cdk::call(Principal::management_canister(), "raw_rand", ())
.await
.unwrap();
ic_cdk::println!("{:?}", rand);
// if the panic occurred after the async/await calls, the atomicity doesn't guarantee here
// we have to rollback manually
// ic_cdk::trap("panic!");
// rollback
COUNTER.with(|c| {
*c.borrow_mut() -= 1;
});
});
}
#[cfg(any(target_arch = "wasm32", test))]
fn main() {}
#[cfg(not(any(target_arch = "wasm32", test)))]
fn main() {
ic_cdk::export::candid::export_service!();
std::print!("{}", __export_service());
}