cosmwasm: ``Wasm contract requires unsupported import`` and how to work around it

I am writing a smart contract that needs to use some cryptographic crates out of my control. Unfortunately, those crates import and use the rand crate. Although the code compiles, and the .wasm file is generated, whenever I try to test it using cosmwasm-vm I get the error

StaticValidationErr { 
msg: "Wasm contract requires unsupported import: \"__wbindgen_placeholder__.__wbindgen_throw\". 
   Required imports: {\"__wbindgen_externref_xform__.__wbindgen_externref_table_grow\", \"__wbindgen_externref_xform__.__wbindgen_externref_table_set_null\", \"__wbindgen_placeholder__.__wbindgen_describe\", ... 13 more}. 
   Available imports: [\"env.db_read\", \"env.db_write\", \"env.db_remove\", \"env.addr_validate\", \"env.addr_canonicalize\", \"env.addr_humanize\", \"env.secp256k1_verify\", \"env.secp256k1_recover_pubkey\", \"env.ed25519_verify\", \"env.ed25519_batch_verify\", \"env.debug\", \"env.query_chain\"]." }

I have tried to work around this issue by stripping away all use of rand functions and structs, but even leaving in something as simple as the rand::Rng trait, triggers the above error

The rand crate is deeply embedded into those crypto crates, and it would be a titanic effort for me to make that dependency optional, hide all the related code behind an optional feature and hope for the changes to be accepted upstream. On the other hand though, I am pretty sure that the functions I need from those crates do not make use of any rand-related functions.

My question is: is there a way for me to declare inside my contract code, those unsupported import functions so that when the wasm is loaded, cosmwasm-vm does not complain and it can carry on?

Using the wasm2wat tool I can see that, for instance, the 1st function __wbindgen_placeholder__.__wbindgen_throw appears as

  (import "__wbindgen_placeholder__" "__wbindgen_throw" (func $_ZN12wasm_bindgen16__wbindgen_throw17h8d8353266d312eeaE (type 5)))

where type 5 is

  (type (;5;) (func (param i32 i32)))

so it there a way for me to write something like

#[no_mangle]
pub mod __wbindgen_placeholder__ {
    pub extern "C" fn __wbindgen_throw(a: i32, b: i32) {
        panic!("__wbindgen_throw")
    }
}

and have cosmwasm-vm pick it up and use it to fullfill the missing import?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 24 (9 by maintainers)

Commits related to this issue

Most upvoted comments

By the way, you can now use cosmwasm-check to do such compatibility checks locally without having to upload the Wasm to a chain

It seems to be possible to make cargo tree target aware:

$ cargo tree -i cosmwasm-crypto
cosmwasm-crypto v1.0.0-beta (/myprojects/cosmwasm/packages/crypto)
├── cosmwasm-std v1.0.0-beta (/myprojects/cosmwasm/packages/std)
│   ├── cosmwasm-storage v1.0.0-beta (/myprojects/cosmwasm/packages/storage)
│   │   [dev-dependencies]
│   │   └── hackatom v0.0.0 (/myprojects/cosmwasm/contracts/hackatom)
│   ├── cosmwasm-vm v1.0.0-beta (/myprojects/cosmwasm/packages/vm)
│   │   [dev-dependencies]
│   │   └── hackatom v0.0.0 (/myprojects/cosmwasm/contracts/hackatom)
│   └── hackatom v0.0.0 (/myprojects/cosmwasm/contracts/hackatom)
└── cosmwasm-vm v1.0.0-beta (/myprojects/cosmwasm/packages/vm) (*)



$ cargo tree -i cosmwasm-crypto --target wasm32-unknown-unknown
cosmwasm-crypto v1.0.0-beta (/myprojects/cosmwasm/packages/crypto)
└── cosmwasm-vm v1.0.0-beta (/myprojects/cosmwasm/packages/vm)
    [dev-dependencies]
    └── hackatom v0.0.0 (/myprojects/cosmwasm/contracts/hackatom)

Hi @webmaster128, thanks a lot for your help. The crypto crates I am referring to are those from arkworks-rs. Any of those crates, from crypto-primitives, to ark-ff or ark-ec, they all depend on ark-std which depends on those rand traits.

I am going to try and see if I can make it compile with the alternative resolver.

@duguorong009

I am doing some further investigation in https://github.com/rust-lang/cargo/issues/9990 I might consider reopening the original ticket

Hi @ratankaliani , I never investigated that

While digging a bit further into this dependency nightmare, I found out this and this. How do you guys make getrandom compile and work in cosmwasm-crypto without facing the same issue I am fighting with?

cosmwasm-crypto is never compiled into the contract. It is only used for unit testing and as part of the VM. See https://github.com/CosmWasm/cosmwasm/blob/v1.0.0-beta/packages/std/Cargo.toml#L42-L43

Yeah, then it can really be a resolver issue because some dev-dependencies enables the getrandom feature of rand. ark-std itself does not need an entropy source: https://github.com/arkworks-rs/std/blob/v0.3.0/Cargo.toml#L16