-1

当我按照步骤公开合同托盘 api 时显示错误。

https://substrate.dev/docs/en/tutorials/add-a-pallet-to-your-runtime/#install-the-node-template

我一直遵循这些步骤并修复了另一个与 ContractExecResult::Success 相关联的错误。现在我遇到了这两个错误。

我相信它与它的语法或教程尚未更新到的更新的库 api 有关。

$ cargo check -p node-template-runtime
   Compiling node-template-runtime v2.0.0-rc5 (C:\substrate-node-template2\runtime)
error: failed to run custom build command for `node-template-runtime v2.0.0-rc5 (C:\substrate-node-template2\runtime)`

Caused by:
  process didn't exit successfully: `C:\substrate-node-template2\target\debug\build\node-template-runtime-8af1699a702fd7e8\build-script-build` (exit code: 1)
--- stdout
Executing build command: "rustup" "run" "nightly" "cargo" "rustc" "--target=wasm32-unknown-unknown" "--manifest-path=C:\\substrate-node-template2\\target\\debug\\wbuild\\node-template-runtime\\Cargo.toml" "--color=always" "--release"

--- stderr
   Compiling wasm-build-runner-impl v1.0.0 (C:\substrate-node-template2\target\debug\wbuild-runner\node-template-runtime6610820868370414452)
    Finished dev [unoptimized + debuginfo] target(s) in 0.75s
     Running `C:\substrate-node-template2\target\debug\wbuild-runner\node-template-runtime6610820868370414452\target\x86_64-pc-windows-msvc\debug\wasm-build-runner-impl.exe`
   Compiling node-template-runtime v2.0.0-rc5 (C:\substrate-node-template2\runtime)
error[E0308]: mismatched types
   --> C:\substrate-node-template2\runtime\src\lib.rs:477:17
    |
476 |             match exec_result {
    |                   ----------- this expression has type `(core::result::Result<pallet_contracts::ExecReturnValue, sp_runtime::DispatchError>, u64)`
477 |                 Ok(v) => ContractExecResult::Success{
    |                 ^^^^^ expected tuple, found enum `core::result::Result`
    |
    = note: expected tuple `(core::result::Result<pallet_contracts::ExecReturnValue, sp_runtime::DispatchError>, u64)`
                found enum `core::result::Result<_, _>`

error[E0308]: mismatched types
   --> C:\substrate-node-template2\runtime\src\lib.rs:482:17
    |
476 |             match exec_result {
    |                   ----------- this expression has type `(core::result::Result<pallet_contracts::ExecReturnValue, sp_runtime::DispatchError>, u64)`
...
482 |                 Err(_) => ContractExecResult::Error,
    |                 ^^^^^^ expected tuple, found enum `core::result::Result`
    |
    = note: expected tuple `(core::result::Result<pallet_contracts::ExecReturnValue, sp_runtime::DispatchError>, u64)`
                found enum `core::result::Result<_, _>`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: could not compile `node-template-runtime`.

To learn more, run the command again with --verbose.
error: process didn't exit successfully: `C:\substrate-node-template2\target\debug\wbuild-runner\node-template-runtime6610820868370414452\target\x86_64-pc-windows-msvc\debug\wasm-build-runner-impl.exe` (exit code: 1)

这是本教程让我添加的 runtime\src\lib.rs 中导致问题的代码,以及我修改后的代码,其中添加了“flags”和“gas_consumed”并删除了“status”。

 /*** Add This Block ***/
    impl contracts_rpc_runtime_api::ContractsApi<Block, AccountId, Balance, BlockNumber>
        for Runtime
    {
        fn call(
            origin: AccountId,
            dest: AccountId,
            value: Balance,
            gas_limit: u64,
            input_data: Vec<u8>,
        ) -> ContractExecResult {
            let exec_result =
                Contracts::bare_call(origin, dest.into(), value, gas_limit, input_data);
            match exec_result {
                Ok(v) => ContractExecResult::Success{
                flags: v.status,
                data: v.data,
                gas_consumed: v.gas_consumed,
                },
                Err(_) => ContractExecResult::Error,
            }
        }

        fn get_storage(
            address: AccountId,
            key: [u8; 32],
        ) -> contracts_primitives::GetStorageResult {
            Contracts::get_storage(address, key)
        }

        fn rent_projection(
            address: AccountId,
        ) -> contracts_primitives::RentProjectionResult<BlockNumber> {
            Contracts::rent_projection(address)
        }
    }
   /*** End Added Block ***/
4

2 回答 2

0

从 v2.0.0-rc5 开始,Contracts 托盘中的 bare_call 函数返回 ExecResult 和 Gas,可以在第 633 行看到,而在之前版本的 Contracts 托盘上只返回 ExecResult。

为了继续本教程,我对教程中提供的代码片段进行了一些调整。这是我的代码的版本。

fn call(
        origin: AccountId,
        dest: AccountId,
        value: Balance,
        gas_limit: u64,
        input_data: Vec<u8>,
    ) -> ContractExecResult {
        let exec_result =
            Contracts::bare_call(origin, dest.into(), value, gas_limit, input_data);
        match exec_result {
            (Ok(v), gas) => ContractExecResult::Success {
                flags: v.flags.bits(),
                data: v.data,
                gas_consumed: gas,
            },
            (Err(_), _) => ContractExecResult::Error,
        }
    }

注意:我对 Rust 还很陌生,所以我的代码可能不是最佳解决方案。

于 2020-09-22T16:24:01.887 回答
0

根据输出,您似乎使用的是节点模板的 2.0.0-rc5 版本。目前,合同托盘教程是最新的 v2.0.0-rc4。您可以从该分支克隆节点模板并将其用作起点吗?git clone -b v2.0.0-rc4 --depth 1 https://github.com/substrate-developer-hub/substrate-node-template

于 2020-08-16T18:21:24.913 回答