2

I've been working with parity's contracts node (latest version) and the substrate template node (tag polkadot-v0.9.18), both present the same issue when compiling.

I have a very simple pallet that stores certain items. The main structure is the following:

#[pallet::storage]
#[pallet::getter(fn items)]
/// 'Mapping' Item ID -> Item Data
pub(crate) type Items<T: Config> = StorageMap<_, Twox64Concat, T::Hash, Item<T>>;

I was trying to add a simple RPC method following this guides https://core.tetcoin.org/recipes/custom-rpc.html#rpc-to-call-a-runtime-api and https://core.tetcoin.org/recipes/runtime-api.html

I also checked some projects that already have custom RPC calls implementations, like de subsocial node and I have pretty much the same structure and dependencies.

My rpc method does nothing but return a number 2 just to make sure it works, but it doesn't. This is what the pallets directory looks like: pallets directory

When I try to compile, the following error shows

error: the wasm32-unknown-unknown target is not supported by default, you may need to 
enable the "js" feature. For more information see: 
https://docs.rs/getrandom/#webassembly-support

I don't even use that module, but I've read that it is used somewhere as an indirect dependency. I'm compiling my project with the following command

cargo build --release

Checking the documentation regarding the 'getrandom' crate issue, I added the following dependency in the Cargo.toml (I tried adding it in every Cargo.toml within the project, individually, by pairs, ...)

getrandom = { version = "0.2", features = ["js"] }

Then another error shows up:

error: failed to run custom build command for secp256k1-sys v0.4.1

Which again, doesn't make any sense to me. The project itself has nothing but the node template base and a new pallet that implements a create and transfer function. Without the RPC implementation, it works perfectly using the Polkadot App, but as soon as I include the custom rpc, it just doesn't compile.

This is my rust configuration (rustup show)

 installed toolchains
 --------------------

 stable-x86_64-apple-darwin (default)
 nightly-2021-11-04-x86_64-apple-darwin
 nightly-x86_64-apple-darwin

 active toolchain
 ----------------

 stable-x86_64-apple-darwin (default)
 rustc 1.59.0 (9d1b2106e 2022-02-23)

I haven't found anyone who is dealing with this kind of issue, and I don't know where the problem might be.

This is the first issue logs:

  error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
     --> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:229:9
      |
  229 | /         compile_error!("the wasm32-unknown-unknown target is not supported by \
  230 | |                         default, you may need to enable the \"js\" feature. \
  231 | |                         For more information see: \
  232 | |                         https://docs.rs/getrandom/#webassembly-support");
      | |________________________________________________________________________^

  error[E0433]: failed to resolve: use of undeclared crate or module `imp`
     --> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:256:5
      |
  256 |     imp::getrandom_inner(dest)
      |     ^^^ use of undeclared crate or module `imp`

  For more information about this error, try `rustc --explain E0433`.
  error: could not compile `getrandom` due to 2 previous errors
  warning: build failed, waiting for other jobs to finish...
  error: build failed

Current status (to reproduce error): https://github.com/andresvsm/substrate-pallet-rpc/tree/items-branch

andresvsm
  • 165
  • 1
  • 7
  • I have the same issue compiling a Ink contract when importing an external crate – mastro Jun 04 '22 at 23:29
  • For some reason a dude deleted my answer to this post. The issue I had was related to some missing code, so if you are not using that library and get the same error, you might be missing some lines in your code, or some marks like a semi-colon, a comma, etc. @mastro – andresvsm Jun 06 '22 at 05:34

2 Answers2

3

Sometimes, you can get this error from a deep dependency of another dependency, e.g. when you really build for a wasm32-unknown-unknown target, and getrandom is linked but even not used. It can be fixed (worked around) with the following trick:

In Cargo.toml, add this line:

[dependencies]
getrandom = {version = "0.2", default-features = false, features = ["custom"]}

It tells the compiler to use a dummy implementation inside of getrandom.

Alexander Fadeev
  • 2,110
  • 14
  • 16
0

Fixed for me when I added "default features = 'false'" into my Cargo.toml under the dependency in question.

jjreedv
  • 71
  • 2
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 15 '22 at 11:31
  • I was about to try that when I came across this post https://substrate.stackexchange.com/questions/1786/implementation-of-runtime-api-rpc, I noticed I was also missing the same line in my node/src/rpc.rs file. It solved everything and I didn't need to import the module. – andresvsm Apr 17 '22 at 11:20