我看过有关创建单个Substrate Runtime模块here的板条箱以重用该功能的Substrate教程,但我想知道是否有一种自定义模块可以从中访问存储或功能的方法。另一个自定义模块?
遵循以下原则:
/// In ModuleA
pub type IndexType = u64;
decl_storage! {
trait Store for Module<T: Trait> as ModuleA {
pub MyIndexCount get(my_index_count): Option<IndexType>;
}
}
然后在ModuleB中-要使用/包括ModuleA的功能,我该怎么办?
/// In ModuleB
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;
pub fn edit_index(origin) -> Result {
let sender = ensure_signed(origin)?;
// --->>>> I want to read some storage from ModuleA whilst inside ModuleB
let c: IndexType = ReadStorageFromModuleA >>> my_index_count().ok_or("Storage Read Error: cannot get index")?;
// change storage in ModuleA from ModuleB
WriteToStorageInModuleA <MyIndexCount<T>>::put(&c + 1);
Ok(())
}
}
}
答案 0 :(得分:4)
如果要构建直接依赖于另一个模块(module1)的模块(module2),则必须在module2的特征定义中继承module1的特征:
pub trait Trait: module1::Trait {
...
}
要从module2中的module1访问公共存储项目,您需要执行以下操作:
StorageValue
,StorageMap
等... <module1::Something<T>>::get()
<module1::Something<T>>::put()
要访问模块2中模块1的其他 public 功能,您需要使用Module
类型:
<module1::Module<T>>::public_function();
这是两个模块以这种方式交互的简单示例:
module1.rs
请注意,此模块中的所有内容均标记为公开(
pub
)
use support::{decl_module, decl_storage, StorageValue};
pub trait Trait: system::Trait {}
decl_storage! {
trait Store for Module<T: Trait> as TemplateModule {
pub Something: u32;
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
}
}
impl<T: Trait> Module<T> {
pub fn get_value() -> u32 {
<Something<T>>::get()
}
}
module2.rs
use support::{decl_module, decl_event, StorageValue, dispatch::Result};
use system::ensure_signed;
use crate::module1;
pub trait Trait: module1::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_module! {
/// The module declaration.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;
pub fn get_value_directly(origin) -> Result {
let who = ensure_signed(origin)?;
let value = <module1::Something<T>>::get();
Self::deposit_event(RawEvent::ValueIs(value, who));
Ok(())
}
pub fn set_value_directly(origin, value: u32) -> Result {
let _ = ensure_signed(origin)?;
<module1::Something<T>>::put(value);
Ok(())
}
pub fn get_value_public_function(origin) -> Result {
let who = ensure_signed(origin)?;
let value = <module1::Module<T>>::get_value();
Self::deposit_event(RawEvent::ValueIs(value, who));
Ok(())
}
}
}
decl_event!(
pub enum Event<T> where <T as system::Trait>::AccountId {
ValueIs(u32, AccountId),
}
);