#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
/// The `AccountId` of the sudo key.
pub key: T::AccountId,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { key: Default::default() }
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
<Key<T>>::put(&self.key);
}
}
genesis_config
和 genesis_build
宏在这里发生了什么?一些阅读似乎建议实现特征 GenesisBuild<T,I = ()>但我对文档感到困惑:“T
和 I
是 pallet trait 和 pallet 实例的占位符。”
什么是托盘实例?我还假设 pallet trait 是指 sudo pallet 的 Config
trait。它与为创世 block 配置唯一的 StorageValue
有什么关系,以便运行节点的每个实例都可以验证同一个帐户是 Root 吗?谁能帮我分解一下。
最佳答案
What are pallet instances?
托盘可以实例化。这意味着一个 Pallet 可以在运行时多次使用。每个实例将使用相同的代码(通过配置特征对不同的配置取模),但每个实例的存储将不同。
参见:https://docs.substrate.io/how-to-guides/v3/basics/instantiable-pallets/
I also assume pallet trait means the Config trait for the sudo pallet. Is it some how related to configuring a unique StorageValue for the Genesis block so that each instance of a running node can verify that the same Account is Root? Can someone help break it down for me.
Yes Pallet trait 表示 Config
trait。
GenesisBuild
采用两个通用参数的要点是支持所有类型的 Pallet。每个 Pallet 都可以通过 trait 进行配置,因此 GenesisBuild
trait 需要采用 Config
泛型参数 T
。有些Pallets是可以实例化的,所以也需要带上I
。
这一切归结为一些 Rust 的“怪癖”,以支持使用默认实例和自定义实例的可实例化 Pallet,以及支持根本不使用任何实例化的 Pallet。
关于substrate - 宏 #[pallet::genesis_config] 和 #[pallet::genesis_build] 在 sudo pallet 上做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70324883/