为自定义托盘 Substrate 添加自定义 RPC 时出错

发布于 2025-01-18 21:10:34 字数 3937 浏览 4 评论 0 原文

我一直在使用奇偶校验的 Contracts nodes node (最新版本)和<“ nofollow noreferrer”> sidistrate template node node (tag polkadot-v0。 9.18),两者都在编译时呈现相同的问题。

我有一个非常简单的托盘,可以存储某些物品。主要结构是以下内容:

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

我试图在此指南之后添加一个简单的RPC方法 https://core.tetcoin.org/recipes/custom-rpc.html#rpc-to-cal-call-a-runtime-api-api-api https://core.tetcoin.org/recipes/runtime-api.html

已经具有自定义RPC调用实现的项目,例如de sposocial node 我和我几乎相同结构和依赖性。

我的RPC方法除了返回数字2只是为了确保它有效,但事实并非如此。这就是托盘目录的样子: pallets目录

当我尝试编译时,以下错误显示

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

我什至不使用该模块,但是我已经读到它被用作间接依赖性。 我正在使用以下命令编译我的项目,

cargo build --release

以检查有关“ GetRandom”板条箱问题的文档,我在货物中添加了以下依赖项(我尝试在项目中的每个货物中添加它...)

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

然后出现另一个错误:

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

这再次对我来说没有任何意义。 该项目本身只有节点模板基库和实现创建和传输功能的新托盘。没有RPC实现,它可以使用Polkadot应用程序完美地工作,但是一旦我包括自定义RPC,它就不会编译。

这是我的Rust配置(Rusup 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)

我还没有找到任何正在处理此类问题的人,我不知道问题可能在哪里。

这是第一个问题日志:

  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

当前状态(重现错误):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2025-01-25 21:10:34

有时,您可能会从另一个依赖项的深度依赖项中得到此错误,例如,当您真正为 wasm32-unknown-unknown 目标构建时,并且 getrandom 已链接,但甚至 < em>未使用。它可以通过以下技巧修复(解决):

Cargo.toml 中,添加以下行:

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

它告诉编译器在 getrandom 内部使用虚拟实现。

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.

拥有 2025-01-25 21:10:34

当我将“默认功能='false'”添加到有关依赖项下的cargo.toml中时,请修复。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文