Rust Wasm __getrandom_custom

I am trying to get proptest, a property-testing library in Rust, to compile and run from within a canister. It has a dependency on getrandom, and I assume that because getrandom is being compiled for a Wasm environment, it expects a __getrandom_custom import from the host environment. I get the following error when trying to deploy a canister with that uses proptest:

Installing canisters...
Upgrading code for canister graphql, with canister_id rrkah-fqaaa-aaaaa-aaaaq-cai
The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Wasm module of canister rrkah-fqaaa-aaaaa-aaaaq-cai is not valid: Wasm module has an invalid import section. Module imports function '__getrandom_custom' from 'env' that is not exported by the runtime.

It would be very nice for the Rust CDK to provide this functionality, it seems that dealing with Rust randomness, packages like rand and getrandom, isn’t as smooth as it could be. I would expect randomness to simply work across the major Rust packages, since the IC has an excellent source of randomness with the random beacon.

Until all of that is sorted out, is there a way for me to provide the __getrandom_custom function to my canister?

1 Like

I’ve at least gotten this to compile.

Cargo.toml:

[dependencies]
proptest = { version = "1.0.0", default-features = false, features = ["std"] }
getrandom = { version = "0.2.2", features = ["custom"] }

I needed to enable the custom feature for getrandom, and in my canister I needed to implement the function:

use getrandom::register_custom_getrandom;

fn custom_getrandom(buf: &mut [u8]) -> Result<(), getrandom::Error> {
    // TODO get some randomness
    return Ok(());
}

register_custom_getrandom!(custom_getrandom);

It compiles, but now when I try to actually use proptest, I get a panic.

Here’s the function I am trying to run:

#[query]
fn test_canister(schema: String) -> String {
    let mut runner = TestRunner::new(Config {
        cases: 10,
        max_shrink_iters: 0,
        .. Config::default()
    });

    return "{}".to_string();
}

I’m just creating a basic proptest runner, and I get: `

I’m just creating a basic proptest runner, and I get:

[Canister rrkah-fqaaa-aaaaa-aaaaq-cai] Panicked at 'not supported on this platform', library/std/src/sys/wasm/../unsupported/os.rs:76:5

If anyone has experience with proptest, randomness, etc with the wasm32-unknown-unknown, I would love some help tracking down the source of this panic.

Im seeking a similar answer, basically these random libraries use wasm-pack and this is not supported on the IC as it requires JS imports the IC does not provide.

1 Like