Skip to content

Commit

Permalink
feat: add API in_replicated_execution() (#489)
Browse files Browse the repository at this point in the history
* update ic0.txt

* auto-gen files

* add the safe wrapper

* e2e

* Changelog
  • Loading branch information
lwshang authored May 6, 2024
1 parent 8620c0e commit 58faf92
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lto = true
opt-level = 'z'

[workspace.dependencies]
ic0 = { path = "src/ic0", version = "0.21.1" }
ic0 = { path = "src/ic0", version = "0.23.0" }
ic-cdk = { path = "src/ic-cdk", version = "0.13.2"}
ic-cdk-timers = { path = "src/ic-cdk-timers", version = "0.7.0" }

Expand Down
10 changes: 10 additions & 0 deletions e2e-tests/canisters/api_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ fn cycles_burn(amount: u128) -> u128 {
ic_cdk::api::cycles_burn(amount)
}

#[update]
fn update_is_replicated() -> bool {
ic_cdk::api::in_replicated_execution()
}

#[query]
fn query_is_not_replicated() -> bool {
ic_cdk::api::in_replicated_execution()
}

fn main() {}
8 changes: 8 additions & 0 deletions e2e-tests/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ fn test_api_call() {
)
.unwrap();
assert_eq!(result, WasmResult::Reject("manual reject".to_string()));

let (result,): (bool,) = call_candid(&env, canister_id, "update_is_replicated", ())
.expect("Failed to call update_is_replicated");
assert!(result);

let (result,): (bool,) = query_candid(&env, canister_id, "query_is_not_replicated", ())
.expect("Failed to call query_is_not_replicated");
assert!(!result);
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions ic0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ ic0.msg_cycles_refunded128 : (dst : i32) -> (); // R
ic0.msg_cycles_accept : (max_amount : i64) -> (amount : i64); // U Rt Ry
ic0.msg_cycles_accept128 : (max_amount_high : i64, max_amount_low: i64, dst : i32)
-> (); // U Rt Ry
ic0.cycles_burn128 : (amount_high : i64, amount_low : i64, dst : i32) -> ();// I G U Ry Rt C T

ic0.cycles_burn128 : (amount_high : i64, amount_low : i64, dst : i32) -> (); // I G U Ry Rt C T

ic0.canister_self_size : () -> i32; // *
ic0.canister_self_copy : (dst : i32, offset : i32, size : i32) -> (); // *
Expand Down Expand Up @@ -57,13 +58,14 @@ ic0.stable64_read : (dst : i64, offset : i64, size : i64) -> (); // *

ic0.certified_data_set : (src: i32, size: i32) -> (); // I G U Ry Rt T
ic0.data_certificate_present : () -> i32; // *
ic0.data_certificate_size : () -> i32; // *
ic0.data_certificate_copy : (dst: i32, offset: i32, size: i32) -> (); // *
ic0.data_certificate_size : () -> i32; // Q CQ
ic0.data_certificate_copy : (dst: i32, offset: i32, size: i32) -> (); // Q CQ

ic0.time : () -> (timestamp : i64); // *
ic0.global_timer_set : (timestamp : i64) -> i64; // I G U Ry Rt C T
ic0.performance_counter : (counter_type : i32) -> (counter : i64); // * s
ic0.is_controller: (src: i32, size: i32) -> ( result: i32); // * s
ic0.in_replicated_execution: () -> (result: i32); // * s

ic0.debug_print : (src : i32, size : i32) -> (); // * s
ic0.trap : (src : i32, size : i32) -> (); // * s
ic0.trap : (src : i32, size : i32) -> (); // * s
5 changes: 5 additions & 0 deletions src/candid-extractor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

### Added

- Upgrade `ic0` to 0.23.0 which includes the new system API `in_replicated_execution`.

## [0.1.3] - 2024-04-22

### Added
Expand All @@ -17,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Includes new system API `cycles_burn128`. (#434)

## [0.1.1] - 2023-09-19

### Added
Expand Down
1 change: 1 addition & 0 deletions src/candid-extractor/ic_mock.wat
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
(func (export "global_timer_set") (param i64) (result i64) i64.const 0)
(func (export "performance_counter") (param i32) (result i64) i64.const 0)
(func (export "is_controller") (param i32 i32) (result i32) i32.const 0)
(func (export "in_replicated_execution") (result i32) i32.const 0)
(func (export "debug_print") (param i32 i32) )
(func (export "trap") (param i32 i32) )
)
9 changes: 7 additions & 2 deletions src/ic-cdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `wasm_memory_limit` to the management canister API types: (#483)
- Add `wasm_memory_limit` to the management canister API types: (#483)
* `CanisterSettings`
* `DefiniteCanisterSettings`.
- Provide safe wrapper of `in_replicated_execution` in ic-cdk. (#489)

### Changed

- Upgrade `ic0` to v0.23.0. (#489)

## [0.13.2] - 2024-04-08

### Added

- Management canister methods for interacting with the chunk store. (#461)
- Provide safe wrapper of global_timer_set in ic-cdk. (#475)
- Provide safe wrapper of `global_timer_set` in ic-cdk. (#475)

## [0.13.1] - 2024-03-01

Expand Down
12 changes: 12 additions & 0 deletions src/ic-cdk/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,15 @@ pub fn set_global_timer(timestamp: u64) -> u64 {
// SAFETY: ic0.global_timer_set is always safe to call.
unsafe { ic0::global_timer_set(timestamp as i64) as u64 }
}

/// Checks if in replicated execution.
///
/// The canister can check whether it is currently running in replicated or non replicated execution.
pub fn in_replicated_execution() -> bool {
// SAFETY: ic0.in_replicated_execution is always safe to call.
match unsafe { ic0::in_replicated_execution() } {
0 => false,
1 => true,
_ => unreachable!(),
}
}
2 changes: 1 addition & 1 deletion src/ic0/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ic0"
version = "0.21.1"
version = "0.23.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions src/ic0/src/ic0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extern "C" {
pub fn global_timer_set(timestamp: i64) -> i64;
pub fn performance_counter(counter_type: i32) -> i64;
pub fn is_controller(src: i32, size: i32) -> i32;
pub fn in_replicated_execution() -> i32;
pub fn debug_print(src: i32, size: i32);
pub fn trap(src: i32, size: i32);
}
Expand Down Expand Up @@ -222,6 +223,9 @@ mod non_wasm {
pub unsafe fn is_controller(src: i32, size: i32) -> i32 {
panic!("is_controller should only be called inside canisters.");
}
pub unsafe fn in_replicated_execution() -> i32 {
panic!("in_replicated_execution should only be called inside canisters.");
}
pub unsafe fn debug_print(src: i32, size: i32) {
panic!("debug_print should only be called inside canisters.");
}
Expand Down

0 comments on commit 58faf92

Please sign in to comment.