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

vim*_*vim 4 parity-io substrate

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

Sha*_*izi 8

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

从代码文档中:

deposit_event:用于存放事件的辅助函数。默认行为是deposit_eventSystem 模块调用。但是,您可以在运行时为事件编写自己的实现。要使用默认行为,请添加fn deposit_event<T>() = default;到您的Module.

如果查看System 模块代码,您会发现最终调用了一个存储事件的辅助函数:

/// 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) { ... }
Run Code Online (Sandbox Code Playgroud)

此函数修改了三个存储项,您可以在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)>;
Run Code Online (Sandbox Code Playgroud)

事件故事的最后一部分可以initialize在 System 模块的函数中找到,其中所有三个项目都被“清理”了:

pub fn initialize( ... ) {
    ...
    <Events<T>>::kill();
    EventCount::kill();
    <EventTopics<T>>::remove_prefix(&());
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

总之,在运行时添加单个事件的成本是:

  • 运行deposit_event_indexed函数。
  • 向运行时存储中的两个向量添加一个新项目。
  • ...在下一个块的开头清理,因此存储成本不会增加。