如何使用 decl_storage 中的构建?

How to use build in decl_storage?

在承印物托盘中,build常用于decl_storage。

例如

pub ReferendumCount get(fn referendum_count) build(|_| 0 as ReferendumIndex): ReferendumIndex;

build 方法有什么作用,如何以及何时使用?

build($expr) 将构建逻辑添加到创世配置构建中。

您可以阅读宏文档了解更多相关信息,我会推荐与您的问题相关的这些要点

您可以在同一文档中找到示例

来自substrate.dev

Whereas the config extension to the decl_storage macro allows you to configure a module's genesis storage state within a chain specification, the build extension allows you to perform this same task within the module itself (this gives you access to the module's private functions). Like config, the build extension accepts a single parameter, but in this case the parameter is always required and must be a closure, which is essentially a function. The build closure will be invoked with a single parameter whose type will be the pallet's GenesisConfig type (this gives you easy access to all the attributes of the GenesisConfig type). You may use the build extension along with the config extension for a single storage item; in this case, the pallet's GenesisConfig type will have an attribute that corresponds to what was set using config whose value will be set in the chain specification, but it will be the value returned by the build closure that will be used to set the storage item's genesis value.