You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FRAME v2中如何不通过construct_runtime访问frame_system::Config存储

解决方案
return (<Number<T>>报错是因为FRAME v2重构后,frame_system的Number存储项被改为不直接对外暴露的私有存储项,原有的直接访问存储项的写法已不再适用。无需依赖`construct_runtime`导出的别名即可实现存储值访问,方案如下:

## 代码修改步骤
1. 给`disperse_reward`函数增加泛型约束,绑定当前pallet的Config trait
2. 替换`<Number<T>>::get()`为`frame_system::Pallet::<T>::block_number()`

修改后的代码片段如下:
```rust
#[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>;

    }

    // 添加泛型参数T和约束
    fn disperse_reward<T: Config>(authorities: &[H256]) {
      ...

      let hash = BlakeTwo256::hash_of(&(
        &utxo,
        // 调用frame_system公开的区块号获取方法
        frame_system::Pallet::<T>::block_number().saturated_into::<u64>()
        ));

      ...
    }
}
```

## 原理解释
- `frame_system::Pallet::<T>::block_number()`是FRAME官方对外公开的标准区块号获取API,只要pallet的Config trait继承了`frame_system::Config`就可以直接调用,不需要提前执行`construct_runtime`。
- 测试代码中用到的`System::block_number()`是`construct_runtime`生成的运行时级别别名,其底层实现和上述pallet内部调用的方法完全一致,二者适用场景不同:前者用于运行时集成、测试代码等上层场景,后者用于pallet内部逻辑定义阶段。

内容的提问来源于stack exchange,提问作者Jitterbug)
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 12:06:12