如何从 frame_system::Config 访问存储?

问题描述

我正在尝试将 Substrate 的 utxo-workshop 的 utxo.rs 转换为 FRAME v2。

这是在 <Number<T>> 处出错的片段(因为 Number 是私有的?)。

#[frame_support::pallet]
pub mod pallet {
    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(PhantomData<T>);

    #[pallet::config]
    pub trait Config: frame_system::Config {
        /// The overarching event type.
        type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

    }

    fn disperse_reward(authorities: &[H256]) {
      ...

      let hash = BlakeTwo256::hash_of(&(
        &utxo,<Number<T>>::get().unwrap().saturated_into::<u64>()
        ));

      ...
    }
}

按照该行调用 block_number(),我相信这等效于 the block_number getter of FRAME v2。构建运行时时可以访问它:

frame_support::construct_runtime!(
    pub enum Test 
    {
        System: frame_system::{Pallet,Call,Config,Storage,Event<T>},...
    }
);

fn testing() {
   let block_number = System::block_number();
}

但是,有没有办法访问 frame_system::Config 上的 Storage 值,没有 construct_runtime!

解决方法

一旦运行时构建完成,您就可以访问运行时托盘的存储,过程由宏 construct_runtime!{} 完成。正如您在此处阅读的那样https://substrate.dev/rustdocs/v3.0.0-monthly-2021-05/frame_support/macro.construct_runtime.html 未出现在该宏中的托盘不会成为您的运行时的一部分,因此将无法访问其存储。