Substrate Rust - 未解决的导入 node_template_runtime

Substrate Rust - unresolved imports node_template_runtime

我对用于区块链开发的 Substrate 和 Rust 还很陌生。我对学习区块链开发非常感兴趣。我正在按照这个 Substrate 教程:https://substrate.dev/docs/en/tutorials/build-a-dapp/pallet 创建 DApp。但是,在 运行 cargo build --release 时,我不断收到有关依赖项的错误,即使我遵循了教程中的每个步骤。这些是确切的错误消息:

error[E0432]: unresolved imports `node_template_runtime::AccountId`, `node_template_runtime::AuraConfig`, `node_template_runtime::BalancesConfig`, `node_template_runtime::GenesisConfig`, `node_template_runtime::GrandpaConfig`, `node_template_runtime::SudoConfig`, `node_template_runtime::SystemConfig`, `node_template_runtime::WASM_BINARY`, `node_template_runtime::Signature`
 --> node/src/chain_spec.rs:3:2
  |
3 |     AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
  |     ^^^^^^^^^  ^^^^^^^^^^  ^^^^^^^^^^^^^^  ^^^^^^^^^^^^^  ^^^^^^^^^^^^^ no `GrandpaConfig` in the root
  |     |          |           |               |
  |     |          |           |               no `GenesisConfig` in the root
  |     |          |           no `BalancesConfig` in the root
  |     |          no `AuraConfig` in the root
  |     no `AccountId` in the root
4 |     SudoConfig, SystemConfig, WASM_BINARY, Signature
  |     ^^^^^^^^^^  ^^^^^^^^^^^^  ^^^^^^^^^^^  ^^^^^^^^^ no `Signature` in the root
  |     |           |             |
  |     |           |             no `WASM_BINARY` in the root
  |     |           no `SystemConfig` in the root
  |     no `SudoConfig` in the root

error[E0432]: unresolved imports `node_template_runtime::opaque`, `node_template_runtime::RuntimeApi`
 --> node/src/service.rs:6:35
  |
6 | use node_template_runtime::{self, opaque::Block, RuntimeApi};
  |                                   ^^^^^^         ^^^^^^^^^^ no `RuntimeApi` in the root
  |                                   |
  |                                   could not find `opaque` in `node_template_runtime`

error[E0432]: unresolved imports `node_template_runtime::opaque`, `node_template_runtime::AccountId`, `node_template_runtime::Balance`, `node_template_runtime::Index`
  --> node/src/rpc.rs:10:29
   |
10 | use node_template_runtime::{opaque::Block, AccountId, Balance, Index};
   |                             ^^^^^^         ^^^^^^^^^  ^^^^^^^  ^^^^^ no `Index` in the root
   |                             |              |          |
   |                             |              |          no `Balance` in the root
   |                             |              no `AccountId` in the root
   |                             could not find `opaque` in `node_template_runtime`

error[E0433]: failed to resolve: could not find `api` in `node_template_runtime`
  --> node/src/service.rs:20:25
   |
20 |     node_template_runtime::api::dispatch,
   |                            ^^^ could not find `api` in `node_template_runtime`

error[E0425]: cannot find function `native_version` in crate `node_template_runtime`
  --> node/src/service.rs:21:25
   |
21 |     node_template_runtime::native_version,
   |                            ^^^^^^^^^^^^^^ not found in node_template_runtime

error[E0282]: type annotations needed
  --> node/src/chain_spec.rs:27:33
   |
27 | pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where
   |                                 ^^^^^^^ cannot infer type for type parameter TPublic

error[E0283]: type annotations needed
  --> node/src/rpc.rs:33:5
   |
33 |     C: HeaderBackend<Block> + HeaderMetadata<Block, Error=BlockChainError> + 'static,
   |        ^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter C
   | 
  ::: /home/ubuntu/.cargo/git/checkouts/substrate-7e08433d4c370a21/70ef0af/primitives/blockchain/src/backend.rs:33:41
   |
33 | pub trait HeaderBackend<Block: BlockT>: Send + Sync {
   |                                         ---- required by this bound in HeaderBackend
   |
   = note: cannot satisfy C: std::marker::Send

error: aborting due to 7 previous errors

Some errors have detailed explanations: E0282, E0283, E0425, E0432, E0433.
For more information about an error, try rustc --explain E0282.
error: could not compile `node-template`

我已经尝试将 node_template_runtime 的引用重命名为 substrate_node_template_runtime,但仍然没有成功。在此 link https://github.com/paritytech/substrate/issues/2241 中提到,我可能必须将 node_template_runtime 重命名为我的项目名称,即 substrate_node_template(因为遵循教程)。

我尝试通过 运行 cargo update -p parity-db 更新我的 parity-db 但也没有成功,尽管在 运行 之后我有很多更新。

任何可能导致此问题的提示?我目前没有关于下一步尝试什么的新线索,因为我认为我已经一步步地遵循了教程,而且我是 Rust 和 Substrate 的新手。先谢谢你了!

以下是我根据教程更新的代码: lib.rs

#![cfg_attr(not(feature = "std"), no_std)]

// Re-export pallet items so that they can be accessed from the crate namespace.
pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
    use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
    use frame_system::pallet_prelude::*;
    use sp_std::vec::Vec; // Step 3.1 will include this in `Cargo.toml`

    /// Configure the pallet by specifying the parameters and types on which it depends.
    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// Because this pallet emits events, it depends on the runtime's definition of an event.
        type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
    }

    // Pallets use events to inform users when important changes are made.
    // Event documentation should end with an array that provides descriptive names for parameters.
    // https://substrate.dev/docs/en/knowledgebase/runtime/events
    #[pallet::event]
    #[pallet::metadata(T::AccountId = "AccountId")]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// Event emitted when a proof has been claimed. [who, claim]
        ClaimCreated(T::AccountId, Vec<u8>),
        /// Event emitted when a claim is revoked by the owner. [who, claim]
        ClaimRevoked(T::AccountId, Vec<u8>),
    }
    
    #[pallet::error]
    pub enum Error<T> {
        /// The proof has already been claimed.
        ProofAlreadyClaimed,
        /// The proof does not exist, so it cannot be revoked.
        NoSuchProof,
        /// The proof is claimed by another account, so caller can't revoke it.
        NotProofOwner,
    }
    
    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(_);
    
    #[pallet::storage] 
    pub(super) type Proofs<T: Config> = StorageMap<_, Blake2_128Concat, Vec<u8>, (T::AccountId, T::BlockNumber), ValueQuery>;   
    
    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
    
    // Dispatchable functions allows users to interact with the pallet and invoke state changes.
    // These functions materialize as "extrinsics", which are often compared to transactions.
    // Dispatchable functions must be annotated with a weight and must return a DispatchResult.
    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::weight(1_000)]
        pub(super) 
        fn create_claim(
            origin: OriginFor<T>,
            proof: Vec<u8>,
        ) -> DispatchResultWithPostInfo {

            // Check that the extrinsic was signed and get the signer.
            // This function will return an error if the extrinsic is not signed.
            // https://substrate.dev/docs/en/knowledgebase/runtime/origin
            let sender = ensure_signed(origin)?;
        
            // Verify that the specified proof has not already been claimed.         
            ensure!(!Proofs::<T>::contains_key(&proof), Error::<T>::ProofAlreadyClaimed);

            // Get the block number from the FRAME System module.
            let current_block = <frame_system::Module<T>>::block_number();

            // Store the proof with the sender and block number.
            Proofs::<T>::insert(&proof, (&sender, current_block));

            // Emit an event that the claim was created.
            Self::deposit_event(Event::ClaimCreated(sender, proof));

            Ok(().into())
        }

        #[pallet::weight(10_000)]
        fn revoke_claim(
            origin: OriginFor<T>,
            proof: Vec<u8>,
        ) -> DispatchResultWithPostInfo {
            // Check that the extrinsic was signed and get the signer.
            // This function will return an error if the extrinsic is not signed.
            // https://substrate.dev/docs/en/knowledgebase/runtime/origin
            let sender = ensure_signed(origin)?;

            // Verify that the specified proof has been claimed.
            ensure!(Proofs::<T>::contains_key(&proof), Error::<T>::NoSuchProof);

            // Get owner of the claim.
            let (owner, _) = Proofs::<T>::get(&proof);

            // Verify that sender of the current call is the claim owner.
            ensure!(sender == owner, Error::<T>::NotProofOwner);

            // Remove claim from storage.
            Proofs::<T>::remove(&proof);

            // Emit an event that the claim was erased.
            Self::deposit_event(Event::ClaimRevoked(sender, proof));

            Ok(().into())
        }
    }
}

cargo.tml

[package]
authors = ['Substrate DevHub <https://github.com/substrate-developer-hub>']
edition = '2018'
homepage = 'https://substrate.dev'
license = 'Unlicense'
name = 'node-template-runtime'
repository = 'https://github.com/substrate-developer-hub/substrate-node-template/'
version = '3.0.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']

[build-dependencies]
substrate-wasm-builder={version = '4.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}

[dependencies]
# external dependencies
codec = {default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.0.0'}
hex-literal= {optional = true, version = '0.3.1'}

# Substrate dependencies
frame-benchmarking = {default-features = false, optional = true, version = '3.1.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
frame-executive = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
frame-support = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
frame-system = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
frame-system-benchmarking = {default-features = false, optional = true, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
frame-system-rpc-runtime-api = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-aura = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-balances = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-nicks = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05' }
pallet-grandpa = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-randomness-collective-flip = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-sudo = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-timestamp = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-transaction-payment = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
pallet-transaction-payment-rpc-runtime-api = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-api = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-block-builder = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-consensus-aura = {default-features = false, version = '0.9.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-core = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-inherents = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-offchain = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-runtime = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-session = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
# sp-std = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-std = { default-features = false, version = '3.0.0' }
sp-transaction-pool = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}
sp-version = {default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-05'}

# local dependencies
pallet-template = {default-features = false, version = '3.0.0', path = '../pallets/template'}

[features]
default = ['std']
runtime-benchmarks = [
    'frame-benchmarking',
    'frame-support/runtime-benchmarks',
    'frame-system-benchmarking',
    'frame-system/runtime-benchmarks',
    'hex-literal',
    'pallet-balances/runtime-benchmarks',
    'pallet-template/runtime-benchmarks',
    'pallet-timestamp/runtime-benchmarks',
    'sp-runtime/runtime-benchmarks',
]
std = [
    'codec/std',
    'frame-executive/std',
    'frame-support/std',
    'frame-system-rpc-runtime-api/std',
    'frame-system/std',
    'pallet-aura/std',
    'pallet-balances/std',
    'pallet-nicks/std',
    'pallet-grandpa/std',
    'pallet-randomness-collective-flip/std',
    'pallet-sudo/std',
    'pallet-template/std',
    'pallet-timestamp/std',
    'pallet-transaction-payment-rpc-runtime-api/std',
    'pallet-transaction-payment/std',
    'sp-api/std',
    'sp-block-builder/std',
    'sp-consensus-aura/std',
    'sp-core/std',
    'sp-inherents/std',
    'sp-offchain/std',
    'sp-runtime/std',
    'sp-session/std',
    'sp-std/std',
    'sp-transaction-pool/std',
    'sp-version/std',
]

我终于知道错了。我编辑的文件是 runtime/src/lib.rs 而不是 pallets/template/src/lib.rs 。我没有意识到他们有多个。该教程涉及删除整个 lib.rs 的内容并替换为新内容