Skip to content

Commit 2f8b47b

Browse files
authored
feat: cycles_burn (#434)
* ic0.txt * safe api * doc * update ic-test-state-machine * test * changelog
1 parent 9d1b929 commit 2f8b47b

File tree

13 files changed

+88
-10
lines changed

13 files changed

+88
-10
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ lto = true
1919
opt-level = 'z'
2020

2121
[workspace.dependencies]
22-
ic0 = { path = "src/ic0", version = "0.21.0" }
23-
ic-cdk = { path = "src/ic-cdk", version = "0.11.1" }
22+
ic0 = { path = "src/ic0", version = "0.21.1" }
23+
ic-cdk = { path = "src/ic-cdk", version = "0.11.2" }
2424
ic-cdk-timers = { path = "src/ic-cdk-timers", version = "0.5.0" }
2525

2626
candid = "0.9.6"

e2e-tests/canisters/api_call.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ic_cdk::{api::call::ManualReply, query};
1+
use ic_cdk::{api::call::ManualReply, query, update};
22

33
#[query]
44
fn instruction_counter() -> u64 {
@@ -10,4 +10,9 @@ fn manual_reject() -> ManualReply<u64> {
1010
ManualReply::reject("manual reject")
1111
}
1212

13+
#[update]
14+
fn cycles_burn(amount: u128) -> u128 {
15+
ic_cdk::api::cycles_burn(amount)
16+
}
17+
1318
fn main() {}

e2e-tests/tests/e2e.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,36 @@ fn test_canister_info() {
439439
}
440440
);
441441
}
442+
443+
#[test]
444+
fn test_cycles_burn() {
445+
let env = env();
446+
let wasm = cargo_build_canister("api-call");
447+
let canister_id = env.create_canister(None);
448+
env.add_cycles(canister_id, 1500);
449+
450+
env.install_canister(canister_id, wasm, vec![], None);
451+
eprintln!("Canister installed.");
452+
let balance1 = env.cycle_balance(canister_id);
453+
eprintln!("Balance 1: {balance1}");
454+
455+
let attempted = 1000u128;
456+
457+
// Scenario 1: burn less than balance
458+
let (burned,): (u128,) = call_candid(&env, canister_id, "cycles_burn", (attempted,))
459+
.expect("Error calling cycles_burn");
460+
eprintln!("Attempted to burn {attempted}, actually burned {burned}");
461+
assert_eq!(burned, attempted);
462+
let balance2 = env.cycle_balance(canister_id);
463+
eprintln!("Balance 2: {balance2}");
464+
465+
// Scenario 2: burn more than balance
466+
let (burned,): (u128,) = call_candid(&env, canister_id, "cycles_burn", (attempted,))
467+
.expect("Error calling cycles_burn");
468+
eprintln!("Attempted to burn {attempted}, actually burned {burned}");
469+
assert!(burned < attempted);
470+
assert_eq!(burned, balance2);
471+
let balance3 = env.cycle_balance(canister_id);
472+
eprintln!("Balance 3: {balance3}");
473+
assert_eq!(balance3, 0);
474+
}

ic0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ic0.msg_cycles_refunded128 : (dst : i32) -> (); // R
1717
ic0.msg_cycles_accept : (max_amount : i64) -> (amount : i64); // U Rt Ry
1818
ic0.msg_cycles_accept128 : (max_amount_high : i64, max_amount_low: i64, dst : i32)
1919
-> (); // U Rt Ry
20+
ic0.cycles_burn128 : (amount_high : i64, amount_low : i64, dst : i32) -> ();// I G U Ry Rt C T
2021

2122
ic0.canister_self_size : () -> i32; // *
2223
ic0.canister_self_copy : (dst : i32, offset : i32, size : i32) -> (); // *

scripts/download_state_machine_binary.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cd "$SCRIPTS_DIR/.."
88

99
uname_sys=$(uname -s | tr '[:upper:]' '[:lower:]')
1010
echo "uname_sys: $uname_sys"
11-
commit_sha="b314222935b7d06c70036b0b54aa80a33252d79c"
11+
commit_sha="15e69667ae983fa92c33794a3954d9ca87518af6"
1212

1313
curl -sLO "https://download.dfinity.systems/ic/$commit_sha/binaries/x86_64-$uname_sys/ic-test-state-machine.gz"
1414
gzip -d ic-test-state-machine.gz

src/candid-extractor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "candid-extractor"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors.workspace = true
55
edition.workspace = true
66
license.workspace = true

src/candid-extractor/ic_mock.wat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
(func (export "msg_cycles_refunded128") (param i32) )
1818
(func (export "msg_cycles_accept") (param i64) (result i64) i64.const 0)
1919
(func (export "msg_cycles_accept128") (param i64 i64 i32) )
20+
(func (export "cycles_burn128") (param i64 i64 i32) )
2021
(func (export "canister_self_size") (result i32) i32.const 0)
2122
(func (export "canister_self_copy") (param i32 i32 i32) )
2223
(func (export "canister_cycle_balance") (result i64) i64.const 0)

src/ic-cdk/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [unreleased]
88

9+
## [0.11.2] - 2023-10-11
10+
11+
### Added
12+
13+
- `cycles_burn` corresponding to system API `ic0.cycles_burn128`. (#434)
14+
15+
### Changed
16+
17+
- Upgrade `ic0` to `0.21.1`. (#434)
18+
19+
## [0.11.1] - 2023-10-11
20+
21+
### Changed
22+
23+
- Upgrade `ic0` to `0.21.0`. (#433)
24+
925
## [0.11.0] - 2023-09-18
1026

1127
### Changed

src/ic-cdk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ic-cdk"
3-
version = "0.11.1"
3+
version = "0.11.2"
44
authors.workspace = true
55
edition.workspace = true
66
license.workspace = true

0 commit comments

Comments
 (0)