如何通过 FRAME v2 解决 "Offchain-worker" 编译错误

How can I solve "Offchain-worker" comile error by FRAME v2

我正在尝试使用 FRAME V2 调色板模板实施链外工作人员。 但是Runtime/lib.rs的CreateSignedTransaction实现给出编译错误,无法解决。 请帮助我。

环境

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

stable-x86_64-apple-darwin (default)
nightly-2020-04-06-x86_64-apple-darwin
nightly-x86_64-apple-darwin
1.51.0-x86_64-apple-darwin

installed targets for active toolchain
--------------------------------------

wasm32-unknown-unknown
x86_64-apple-darwin

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

stable-x86_64-apple-darwin (default)
rustc 1.55.0 (c8dfcfe04 2021-09-06)

问题

-- snip --
        let raw_payload = SignedPayload::new(call, extra)
            .map_err(|e| {
                log::warn!("Unable to create signed payload: {:?}", e);
            })
            .ok()?;
        // let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
        let signature = codec::Encode::using_encoded(&raw_payload, |payload| C::sign(payload, public));
        let address = Indices::unlookup(account);
        let (call, extra, _) = raw_payload.deconstruct();
        Some((call, (address, signature.into(), extra)))
--snip--
  error[E0308]: mismatched types
     --> /Users/shin.takahashi/develop/substrate/latest_node_template/substrate-node-template/runtime/src/lib.rs:329:16
      |
  329 |         Some((call, (address, signature.into(), extra)))
      |                      ^^^^^^^ expected `()`, found `u32`
      |
      = note: expected enum `MultiAddress<_, ()>`
                 found enum `MultiAddress<_, u32>`

  error[E0277]: the trait bound `MultiSignature: From<Option<MultiSignature>>` is not satisfied
     --> /Users/shin.takahashi/develop/substrate/latest_node_template/substrate-node-template/runtime/src/lib.rs:329:8
      |
  329 |         Some((call, (address, signature.into(), extra)))
      |         ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Option<MultiSignature>>` is not implemented for `MultiSignature`
      |         |
      |         required by a bound introduced by this call
      |
      = help: the following implementations were found:
                <MultiSignature as From<sp_core::ecdsa::Signature>>
                <MultiSignature as From<sp_core::ed25519::Signature>>
                <MultiSignature as From<sp_core::sr25519::Signature>>
      = note: required because of the requirements on the impl of `Into<MultiSignature>` for `Option<MultiSignature>`

  Some errors have detailed explanations: E0277, E0308.
  For more information about an error, try `rustc --explain E0277`.
  warning: `node-template-runtime` (lib) generated 1 warning
  error: could not compile `node-template-runtime` due to 2 previous errors; 1 warning emitted

根据 Clark 的建议使用源代码,此错误已解决。这是 Clark 源代码的摘录。

        let raw_payload = SignedPayload::new(call, extra)
            .map_err(|e| {
                log::warn!("Unable to create signed payload: {:?}", e);
            })
            .ok()?;
        let signature = raw_payload.using_encoded(|payload| C::sign(payload, public))?;
        let address = account;
        let (call, extra, _) = raw_payload.deconstruct();
        Some((call, (sp_runtime::MultiAddress::Id(address), signature.into(), extra)))