我需要确保特定模块的 on_initialize
处理程序在我的运行时中所有其他模块之前的同一处理程序之前运行。
a) 如何确保这一点?
b) 是否有一些编译或运行时检查我可以强制执行以绝对保证这将得到尊重?
最佳答案
每个 Substrate 运行时模块的 on_initialize
函数通过 Executive module 调用,它处理所有顶级的东西;本质上只是执行 block /外部函数。
每次执行 block (execute_block
) 时,首先调用 initialize_block
,最终调用 AllModules 的
类型:on_initalize
block
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
AllModules
类型是运行时中不同模块标识符的元组。它由 construct_runtime!
macro 生成并按照您在宏中定义它们的顺序列出模块。例如,对于给定的 construct_runtime!
定义:
construct_runtime!(
pub enum Runtime with Log(InternalLog: DigestItem<Hash, AuthorityId, AuthoritySignature>) where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{default, Log(ChangesTrieRoot)},
Timestamp: timestamp::{Module, Call, Storage, Config<T>, Inherent},
Consensus: consensus::{Module, Call, Storage, Config<T>, Log(AuthoritiesChange), Inherent},
Aura: aura::{Module},
Indices: indices,
Balances: balances,
Sudo: sudo,
// Used for the module template in `./template.rs`
TemplateModule: template::{Module, Call, Storage, Event<T>},
TemplateModule1: template1::{Module, Call, Storage, Event<T>},
TemplateModule2: template2::{Module, Call, Storage, Event<T>},
}
);
您将获得以下 AllModules
类型:
type AllModules = (Timestamp, Consensus, Aura, Indices, Balances, Sudo, TemplateModule, TemplateModule1, TemplateModule2);
因此,on_initialize
函数会按照您在运行时定义模块的顺序调用。您无需执行任何操作来“确保这一点得到遵守”,因为此处的代码流是连续的和确定性的。
关于rust - Substrate 运行时中不同模块调用 `on_initialize` 的顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56699795/