Skip to content

Commit 65443f9

Browse files
authored
refactor: api module overhaul (#540)
1 parent 0ab2864 commit 65443f9

File tree

22 files changed

+1728
-589
lines changed

22 files changed

+1728
-589
lines changed

e2e-tests/src/bin/api.rs

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
use candid::Principal;
2+
use ic_cdk::api::*;
3+
4+
#[export_name = "canister_update call_msg_arg_data"]
5+
fn call_msg_arg_data() {
6+
assert_eq!(msg_arg_data(), vec![42]);
7+
msg_reply(vec![]);
8+
}
9+
10+
#[export_name = "canister_update call_msg_caller"]
11+
fn call_msg_caller() {
12+
assert_eq!(msg_caller(), Principal::anonymous());
13+
msg_reply(vec![]);
14+
}
15+
16+
#[export_name = "canister_update call_msg_reply"]
17+
fn call_msg_reply() {
18+
msg_reply(vec![42]);
19+
}
20+
21+
#[export_name = "canister_update call_msg_reject"]
22+
fn call_msg_reject() {
23+
msg_reject("e2e test reject");
24+
}
25+
26+
#[export_name = "canister_update call_msg_cycles_available"]
27+
fn call_msg_cycles_available() {
28+
assert_eq!(msg_cycles_available(), 0);
29+
msg_reply(vec![]);
30+
}
31+
32+
#[export_name = "canister_update call_msg_cycles_accept"]
33+
fn call_msg_cycles_accept() {
34+
// The available cycles are 0, so the actual cycles accepted are 0.
35+
assert_eq!(msg_cycles_accept(1000), 0);
36+
msg_reply(vec![]);
37+
}
38+
39+
#[export_name = "canister_update call_cycles_burn"]
40+
fn call_cycles_burn() {
41+
assert_eq!(cycles_burn(1000), 1000);
42+
msg_reply(vec![]);
43+
}
44+
45+
#[export_name = "canister_update call_canister_self"]
46+
fn call_canister_self() {
47+
let self_id = canister_self();
48+
// The sender sended canister ID
49+
let data = msg_arg_data();
50+
assert_eq!(self_id.as_slice(), data);
51+
msg_reply(vec![]);
52+
}
53+
54+
#[export_name = "canister_update call_canister_cycle_balance"]
55+
fn call_canister_cycle_balance() {
56+
assert!(canister_cycle_balance() > 0);
57+
msg_reply(vec![]);
58+
}
59+
60+
#[export_name = "canister_update call_canister_status"]
61+
fn call_canister_status() {
62+
assert_eq!(canister_status(), CanisterStatusCode::Running);
63+
assert_eq!(canister_status(), 1);
64+
msg_reply(vec![]);
65+
}
66+
67+
#[export_name = "canister_update call_canister_version"]
68+
fn call_canister_version() {
69+
assert!(canister_version() > 0);
70+
msg_reply(vec![]);
71+
}
72+
73+
#[export_name = "canister_inspect_message"]
74+
fn inspect_message() {
75+
assert!(msg_method_name().starts_with("call_"));
76+
accept_message();
77+
}
78+
79+
#[export_name = "canister_update call_stable"]
80+
fn call_stable() {
81+
assert_eq!(stable_size(), 0);
82+
assert_eq!(stable_grow(1), 0);
83+
let data = vec![42];
84+
stable_write(0, &data);
85+
let mut read_buf = vec![0];
86+
stable_read(0, &mut read_buf);
87+
assert_eq!(read_buf, data);
88+
msg_reply(vec![]);
89+
}
90+
91+
#[export_name = "canister_update call_certified_data_set"]
92+
fn call_certified_data_set() {
93+
certified_data_set(vec![42]);
94+
msg_reply(vec![]);
95+
}
96+
97+
#[export_name = "canister_query call_data_certificate"]
98+
fn call_data_certificate() {
99+
assert!(data_certificate().is_some());
100+
msg_reply(vec![]);
101+
}
102+
103+
#[export_name = "canister_update call_time"]
104+
fn call_time() {
105+
assert!(time() > 0);
106+
msg_reply(vec![]);
107+
}
108+
109+
#[export_name = "canister_update call_performance_counter"]
110+
fn call_performance_counter() {
111+
let t0 = PerformanceCounterType::InstructionCounter;
112+
assert_eq!(t0, 0);
113+
let ic0 = performance_counter(0);
114+
let ic1 = performance_counter(t0);
115+
let ic2 = instruction_counter();
116+
assert!(ic0 < ic1);
117+
assert!(ic1 < ic2);
118+
119+
let t1 = PerformanceCounterType::CallContextInstructionCounter;
120+
assert_eq!(t1, 1);
121+
let ccic0 = performance_counter(1);
122+
let ccic1 = performance_counter(t1);
123+
let ccic2 = call_context_instruction_counter();
124+
assert!(ccic0 < ccic1);
125+
assert!(ccic1 < ccic2);
126+
msg_reply(vec![]);
127+
}
128+
129+
#[export_name = "canister_update call_is_controller"]
130+
fn call_is_controller() {
131+
// The canister was created by the anonymous principal.
132+
assert!(is_controller(&Principal::anonymous()));
133+
msg_reply(vec![]);
134+
}
135+
136+
/// This entry point will be called by both update and query calls.
137+
/// The query call will return 0, and the update call will return 1.
138+
#[export_name = "canister_query call_in_replicated_execution"]
139+
fn call_in_replicated_execution() {
140+
let res = match in_replicated_execution() {
141+
true => 1,
142+
false => 0,
143+
};
144+
msg_reply(vec![res]);
145+
}
146+
147+
#[export_name = "canister_update call_debug_print"]
148+
fn call_debug_print() {
149+
debug_print("Hello, world!");
150+
msg_reply(vec![]);
151+
}
152+
153+
#[export_name = "canister_update call_trap"]
154+
fn call_trap() {
155+
trap("It's a trap!");
156+
}
157+
158+
fn main() {}

e2e-tests/src/bin/async.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async fn panic_after_async() {
3131
let value = *lock;
3232
// Do not drop the lock before the await point.
3333

34-
let _: (u64,) = ic_cdk::call(ic_cdk::api::id(), "inc", (value,))
34+
let _: (u64,) = ic_cdk::call(ic_cdk::api::canister_self(), "inc", (value,))
3535
.await
3636
.expect("failed to call self");
3737
ic_cdk::api::trap("Goodbye, cruel world.")
@@ -47,7 +47,7 @@ async fn panic_twice() {
4747
}
4848

4949
async fn async_then_panic() {
50-
let _: (u64,) = ic_cdk::call(ic_cdk::api::id(), "on_notify", ())
50+
let _: (u64,) = ic_cdk::call(ic_cdk::api::canister_self(), "on_notify", ())
5151
.await
5252
.expect("Failed to call self");
5353
panic!();
@@ -66,7 +66,7 @@ fn on_notify() {
6666
#[update]
6767
fn notify(whom: Principal, method: String) {
6868
ic_cdk::notify(whom, method.as_str(), ()).unwrap_or_else(|reject| {
69-
ic_cdk::api::trap(&format!(
69+
ic_cdk::api::trap(format!(
7070
"failed to notify (callee={}, method={}): {:?}",
7171
whom, method, reject
7272
))
@@ -89,8 +89,12 @@ async fn greet_self(greeter: Principal) -> String {
8989
#[update]
9090
async fn invalid_reply_payload_does_not_trap() -> String {
9191
// We're decoding an integer instead of a string, decoding must fail.
92-
let result: Result<(u64,), _> =
93-
ic_cdk::call(ic_cdk::api::id(), "greet", ("World".to_string(),)).await;
92+
let result: Result<(u64,), _> = ic_cdk::call(
93+
ic_cdk::api::canister_self(),
94+
"greet",
95+
("World".to_string(),),
96+
)
97+
.await;
9498

9599
match result {
96100
Ok((_n,)) => ic_cdk::api::trap("expected the decoding to fail"),

0 commit comments

Comments
 (0)