substrate - decl_storage 中 pub 的用途是什么?
问题描述
在基板中实现运行时模块时,给定以下存储
decl_storage! {
trait Store for Module<T: Trait> as CatAuction {
Kitties get(kitties): map T::Hash => Kitty<T::Hash, T::Balance>;
KittyOwner get(owner_of): map T::Hash => Option<T::AccountId>;
OwnedKitties get(kitties_owned): map T::AccountId => T::Hash;
pub AllKittiesCount get(all_kitties_cnt): u64;
Nonce: u64;
// if you want to initialize value in storage, use genesis block
}
}
pub
前面的目的是AllKittiesCount
什么?因为不管有pub
没有,polkadot UI 还是可以查询到的,就好像它是一个公共变量一样。
解决方案
在这里稍微扩展一下,就像任何 Rust 类型一样,您需要明确不同类型的可见性。该decl_storage
宏struct
会为您的每个存储项目生成一个。例如:
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
Something get(something): u32;
}
}
将导致(为清楚起见删除了一些内容):
struct Something<T: Trait>(...);
impl <T: Trait> ... for Something<T> {
fn get<S: ... >(storage: &S) -> Self::Query {
storage.get(...).unwrap_or_else(|| Default::default())
}
fn take<S: ...>(storage: &S) -> Self::Query {
storage.take(...).unwrap_or_else(|| Default::default())
}
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: ...>(f: F, storage: &S) -> R {
let mut val = <Self as ...>::get(storage);
let ret = f(&mut val);
<Self as ...>::put(&val, storage);
ret
}
}
如果您制作存储项目pub
,您只需pub
在struct Something
. 这意味着,您现在可以从其他模块调用结构公开的所有这些函数,例如get
, 。否则,您将需要创建自己的公共函数来公开 API 以修改存储。take
mutate
推荐阅读
- git - `git show` 自己做了什么?
- django - 在 Django 项目中将 Stripe 用户结帐电子邮件设为 {{ request.user.email }}
- docker - How does Docker handle different kernel versions?
- javascript - 为什么我的 Observable 从未解决?
- jquery - 对文件名的 jquery 更改在 firefox 和 chrome 上的行为不同
- loops - Userform - Catia VBA - 循环遍历文本框
- php - PHP Mobile Camera Upload Not Working
- django - Django Restful API 设计验证逻辑
- javascript - 找不到在 docker 环境中运行的节点 js 应用程序的模块错误
- mysql - 如果忘记分号,则更新 Postgres psql 提示