Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error adding Custom RPCs for custom pallet Substrate

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

like image 725
andresvsm Avatar asked Mar 04 '26 13:03

andresvsm


1 Answers

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.

like image 166
Alexander Fadeev Avatar answered Mar 07 '26 01:03

Alexander Fadeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!