gpt4 book ai didi

substrate - 基板中的事件存储成本是多少?

转载 作者:行者123 更新时间:2023-12-03 19:44:27 26 4
gpt4 key购买 nike

在实现我的链逻辑时,我想知道是否要使用事件,因为它们可能会花费节点额外的存储来存储事件日志。这里涉及的实际存储成本是多少?日志会在某个时候自动清除吗?

最佳答案

运行时事件由 System module 处理。 .在您自己的模块中,您通常实现默认的 deposit_event功能:

从代码内文档:

deposit_event: Helper function for depositing an event. The default behavior is to call deposit_event from the System module. However, you can write your own implementation for events in your runtime. To use the default behavior, add fn deposit_event<T>() = default; to your Module.



如果您查看 System module code ,你会发现最终调用了一个辅助函数来存储事件:

/// 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 中找到。 System 模块中的函数,其中所有这三个项目都被“清理”:

pub fn initialize( ... ) {
...
<Events<T>>::kill();
EventCount::kill();
<EventTopics<T>>::remove_prefix(&());
}

这个 initialize函数在 Executive module 中调用在每个 block 的开头, 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);
}

总之,在运行时添加单个 Event 的成本是:
  • 运行 deposit_event_indexed功能。
  • 将新项目添加到运行时存储中的两个向量。
  • ...它们在下一个区 block 开始时被清理,因此存储成本不会增加。
  • 关于substrate - 基板中的事件存储成本是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57219830/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com