Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add inter-canister call use case to canister logging examples #906

Merged
merged 13 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/motoko-canister-logs-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Provision Darwin
run: bash .github/workflows/provision-darwin.sh
run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-darwin.sh
- name: Motoko Canister Logs Darwin
run: |
dfx start --background
Expand All @@ -31,7 +31,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Provision Linux
run: bash .github/workflows/provision-linux.sh
run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-linux.sh
- name: Motoko Canister Logs Linux
run: |
dfx start --background
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rust-canister-logs-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Provision Darwin
run: bash .github/workflows/provision-darwin.sh
run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-darwin.sh
- name: Rust Canister Logs Darwin
run: |
dfx start --background
Expand All @@ -31,7 +31,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Provision Linux
run: bash .github/workflows/provision-linux.sh
run: DFX_VERSION="0.21.0-beta.0" bash .github/workflows/provision-linux.sh
- name: Rust Canister Logs Linux
run: |
dfx start --background
Expand Down
5 changes: 5 additions & 0 deletions motoko/canister_logs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ test: install
dfx canister logs CanisterLogs \
| grep 'timer trap' && echo 'PASS'

# Test raw_rand.
dfx canister call CanisterLogs raw_rand
dfx canister logs CanisterLogs \
| grep 'ic.raw_rand() call succeeded' && echo 'PASS'

.PHONY: clean
.SILENT: clean
clean:
Expand Down
8 changes: 8 additions & 0 deletions motoko/canister_logs/src/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ actor CanisterLogs {

let timerDelaySeconds = 5;
let second = 1_000_000_000;
let ic00_raw_rand = (actor "aaaaa-aa" : actor { raw_rand : () -> async Blob }).raw_rand;

private func execute_timer() : async () {
Debug.print("right before timer trap");
Expand Down Expand Up @@ -45,4 +46,11 @@ actor CanisterLogs {
let _blob = StableMemory.loadBlob(offset, value); // Expect reading outside of memory bounds to trap.
};

public func raw_rand() : async Blob {
Debug.print("pre ic.raw_rand() call");
let bytes = await ic00_raw_rand();
Debug.print("ic.raw_rand() call succeeded");
bytes;
};

};
5 changes: 5 additions & 0 deletions rust/canister_logs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ test: install
dfx canister logs canister_logs \
| grep 'timer trap' && echo 'PASS'

# Test raw_rand.
dfx canister call canister_logs raw_rand
dfx canister logs canister_logs \
| grep 'ic.raw_rand() call succeeded' && echo 'PASS'

.PHONY: clean
.SILENT: clean
clean:
Expand Down
1 change: 1 addition & 0 deletions rust/canister_logs/canister_logs.did
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ service : {
"panic" : (text) -> ();
"memory_oob" : () -> ();
"failed_unwrap" : () -> ();
"raw_rand" : () -> (blob);
};
19 changes: 18 additions & 1 deletion rust/canister_logs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use ic_cdk::{init, post_upgrade, query, update};
use ic_cdk::{
api::management_canister::main::raw_rand as ic00_raw_rand, init, post_upgrade, query, update,
};
use std::time::Duration;

const TIMER_INTERVAL_SEC: u64 = 5;
Expand Down Expand Up @@ -61,3 +63,18 @@ fn failed_unwrap() {
ic_cdk::print("right before failed unwrap");
String::from_utf8(vec![0xc0, 0xff, 0xee]).unwrap(); // Invalid utf8 should panic.
}

#[update]
async fn raw_rand() -> Vec<u8> {
ic_cdk::println!("pre ic.raw_rand() call");
match ic00_raw_rand().await {
Ok((bytes,)) => {
ic_cdk::println!("ic.raw_rand() call succeeded");
bytes
}
Err(err) => {
ic_cdk::println!("ic.raw_rand() call failed: {:?}", err);
vec![]
}
}
}
Loading