I am trying to generate random numbers with a canister implemented with rust but when I try to import (use rand::Rng) I get an error (subject). How can I import/use a non IC rust library within a rust canister?
I partially solved it by adding the corresponding dependency in the cargo file, but now I get an error when compiling [getrandom v0.2.6] saying that this is not supported (Error: the wasm32-unknown-unknown target is not supported by default). So it looks like I cannot use this library. So, this raises two questions:
Is it correct that I cannot use the rand library?
If (1) is correct, how can I know what libraries can/not I use?
getrandom = { version = "0.2.3", features = ["js"]}
rand = "0.7.3"
Try these two out, they work as of yesterday. Some later versions error out from what I tried, but I haven’t spent the time to track down exactly what’s causing this.
It compiles now but when I call the canister query with the random generator it outputs a n execution error (getrandom: this target is not supported).I am using dfx version 0.9.3. Should I use some other version?
ildefons@ildefons-VirtualBox:~/rstest/rust_hello$ dfx canister call rust_hello greet world
[Canister rrkah-fqaaa-aaaaa-aaaaq-cai] Panicked at ‘could not initialize thread_rng: getrandom: this target is not supported’, /home/ildefons/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.7.3/src/rngs/thread.rs:65:17
Error: The Replica returned an error: code 5, message: “IC0503: Canister rrkah-fqaaa-aaaaa-aaaaq-cai trapped explicitly: Panicked at ‘could not initialize thread_rng: getrandom: this target is not supported’, /home/ildefons/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.7.3/src/rngs/thread.rs:65:17”
@paulyoung I tried your method but I get a compilation error. It says (below) that could not find CanisterId in `ic_cdk’. I declared as a query (below), is this ok?
ildefons@ildefons-VirtualBox:~/rstest/rust_hello$ dfx deploy
Deploying all canisters.
All canisters have already been created.
Building canisters…
Executing: cargo build --target wasm32-unknown-unknown --release -p rust_hello
Compiling rust_hello v0.1.0 (/home/ildefons/rstest/rust_hello/src/rust_hello)
error[E0433]: failed to resolve: could not find CanisterId in ic_cdk
→ src/rust_hello/src/lib.rs:55:39
|
55 | let management_canister = ic_cdk::CanisterId::from(Vec::new());
| ^^^^^^^^^^ could not find CanisterId in ic_cdk
I changed to ‘update’ but I get the same error (below)
Compiling rust_hello v0.1.0 (/home/ildefons/rstest/rust_hello/src/rust_hello)
error[E0433]: failed to resolve: could not find CanisterId in ic_cdk
→ src/rust_hello/src/lib.rs:55:39
|
55 | let management_canister = ic_cdk::CanisterId::from(Vec::new());
| ^^^^^^^^^^ could not find CanisterId in ic_cdk
It now compiles and deploys correctly, but I get a “FIX ME!” execution error (below). Could it be related to the candid interface? Maybe should I call the update method in a different way?
--------Execution error:
ildefons@ildefons-VirtualBox:~/rstest/rust_hello$ dfx canister call rust_hello subnet_raw_rand
FIX ME! variant { 17_724 : table1; 3_456_837 : text } <: opt variant { ok : vec nat8; error : text } via special opt rule.
This means the sender and receiver type has diverged, and can cause data loss.
(null)
Hey there. I am doing all of the above, but I am still getting an error:
Error: The Replica returned an error: code 5, message: “IC0504: Canister xxxx violated contract: “ic0_call_new” cannot be executed in non replicated query mode”.
Any clue?
Code looks like this:
...
let management_canister = ic_cdk::export::Principal::management_canister();
let random_bytes: Vec<u8> = match ic_cdk::call(management_canister, "raw_rand", ()).await {
Ok((res,)) => res,
Err((_, err)) => ic_cdk::trap(&format!("Failed to get random seed: {}", err)),
};
...