在实现我的链逻辑时,我想知道是否要使用事件,因为它们可能会为事件日志花费额外的存储空间。这里涉及的实际存储成本是多少?日志在某个时候会被自动清除吗?
发布于 2019-07-27 19:24:34
运行时事件由系统模块处理。在您自己的模块中,通常实现默认的deposit_event函数:
来自内部代码文档:
deposit_event:用于保存事件的辅助函数。默认行为是从deposit_event调用系统模块。但是,您可以为运行时中的事件编写自己的实现。若要使用默认行为,请将fn deposit_event<T>() = default;添加到Module中。
如果您查看系统模块代码,您将发现最终调用了一个助手函数,该函数存储事件:
/// Deposits an event into this block's event record adding this event
/// to the corresponding topic indexes.
///
/// This will update storage entries that correspond to the specified topics.
/// It is expected that light-clients could subscribe to this topics.
pub fn deposit_event_indexed(topics: &[T::Hash], event: T::Event) { ... }此函数修改系统模块的decl_storage中可以找到的三个存储项:
/// Events deposited for the current block.
Events get(events): Vec<EventRecord<T::Event, T::Hash>>;
/// The number of events in the `Events<T>` list.
EventCount get(event_count): EventIndex;
/// Mapping between a topic (represented by T::Hash) and a vector of indexes
/// of events in the `<Events<T>>` list.
EventTopics get(event_topics): double_map hasher(blake2_256) (), blake2_256(T::Hash)
=> Vec<(T::BlockNumber, EventIndex)>;事件故事的最后一部分可以在系统模块中的initialize函数中找到,其中所有三个项都被“清理”了:
pub fn initialize( ... ) {
...
<Events<T>>::kill();
EventCount::kill();
<EventTopics<T>>::remove_prefix(&());
}这个initialize函数在每个块开始时在执行模块中调用,然后调用任何模块的on_initialize:
fn initialize_block_impl(
block_number: &System::BlockNumber,
parent_hash: &System::Hash,
extrinsics_root: &System::Hash,
digest: &Digest<System::Hash>,
) {
<system::Module<System>>::initialize(block_number, parent_hash, extrinsics_root, digest);
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
}总之,在运行时中添加单个事件的成本是:
deposit_event_indexed函数。发布于 2019-07-26 20:48:18
事件的创建与任何其他存储项相同,并根据所发出的内部数据的类型使用相同的存储量。
https://stackoverflow.com/questions/57219830
复制相似问题