Search code examples
rustblockchainsubstrate

Substrate Rust - unresolved imports node_template_runtime


I'm pretty new with Substrate and Rust for blockchain development. I'm very interested in learning blockchain development. I was following this Substrate tutorial: https://substrate.dev/docs/en/tutorials/build-a-dapp/pallet for creating a DApp. However, I keep getting errors about a dependency when running cargo build --release even though I was following every single steps in the tutorial. These are the exact error messages:

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`

I have tried renaming the references of node_template_runtime to substrate_node_template_runtime, but still no luck. It was mentioned here in this link https://github.com/paritytech/substrate/issues/2241 that I may have to rename the node_template_runtime with my project name, which was substrate_node_template (because of following the tutorial).

I tried updating my parity-db by running cargo update -p parity-db but no luck with that as well, although I had a lot of updates after running that.

Any tips what might be causing this? I currently have no new leads on what to try next as I think I have followed the tutorial step by step, and I'm new to Rust and Substrate. Thank you in advanced!

Below are my code updates from following the tutorial: 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',
]

Solution

  • I finally was able to figure out was wrong. The file I edited was runtime/src/lib.rs instead of pallets/template/src/lib.rs . I didn't realize there were multiple of them. The tutorial involves deleting the contents of the whole lib.rs and replacing with a new one