Jim*_*Chu 2 parity-io substrate
在基板中实现运行时模块时,给定以下存储
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
}
}
Run Code Online (Sandbox Code Playgroud)
pub前面的目的是什么AllKittiesCount?因为不管有pub没有,polkadot UI 还是可以查询到的,就好像它是一个公共变量一样。
在这里稍微展开一下,就像任何 Rust 类型一样,您需要明确不同类型的可见性。该decl_storage宏struct为您的每个存储项目生成一个。例如:
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
Something get(something): u32;
}
}
Run Code Online (Sandbox Code Playgroud)
会导致(为了清楚起见,删除了一些东西):
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
}
}
Run Code Online (Sandbox Code Playgroud)
如果您制作存储项目,pub您只需pub在struct Something. 这意味着,你现在可以把所有这些功能通过类似结构暴露get,take,mutate来自其他模块。否则,您需要创建自己的公共函数来公开 API 以修改存储。
| 归档时间: |
|
| 查看次数: |
240 次 |
| 最近记录: |