我如何定义一个可以在 decl_module 中定义的函数之间共享的变量?

How can I define a variable which can be shared between functions defined in decl_module?

我想实现以下流程:

During the processing of a block:
before executing any extrinsics (on_initialize): 
  initialize MyVariable

When MyFunction is called:
  update MyVariable

end of executing all extrinsics (on_finalize):
  update a variable defined in decl_storage with MyVariable

所以我在decl_module中定义了三个函数:on_initializeMyFunction on_finalize。 但是我如何定义MyVariable,它会在on_initialize中初始化,然后在MyFunction 中更新,最后在on_finalize中它将用于更新定义在decl_storage?

您将需要为该变量创建一个 decl_storage 存储项,或者只需在每个步骤中更新现有存储,因为无论如何您都会将值推送到 on_finalize 中。

可以看到像Timestamp这样的pallets中临时存储物品的使用:

https://github.com/paritytech/substrate/blob/master/frame/timestamp/src/lib.rs#L191

其中 DidUpdate 在块的较早位置设置,并在 on_finalize 期间删除。