问题描述
我在 decl_storage 中有以下变量:
Candidate get(fn candidate_name): map hasher(blake2_128_concat) T::AccountId => Vec<u8>;
这是我的 decl_module 函数:
#[weight = 10_000 + T::DbWeight::get().reads_writes(2,2)]
pub fn add_peers_to_deparment(origin,departmentid: u128) -> dispatch::dispatchResult {
let who = ensure_signed(origin)?;
let mut approved_peer_dep = PeerDepartments::<T>::get(&who);
match approved_peer_dep.binary_search(&departmentid) {
Ok(_) => Err(Error::<T>::DepartmentExists.into()),Err(index) => {
approved_peer_dep.insert(index,departmentid.clone());
PeerDepartments::<T>::insert(&who,approved_peer_dep);
Self::deposit_event(RawEvent::PeerDepartment(departmentid,who));
Ok(())
}
}
}
这里我写了两个语句,首先将 departmentid
插入到 approved_peer_dep
,然后再次将 allowed_peer_dep 插入到区块链存储 PeerDepartments
。
approved_peer_dep.insert(index,departmentid.clone());
PeerDepartments::<T>::insert(&who,approved_peer_dep);
疑问是如果approved_peer_dep
包含一个非常大的Vec
,区块链(PeerDepartments
)会插入包含所有部门使用更多资源的完整Vec,还是会使用添加一个部门?如果它将使用更多资源,那么编写代码的更好方法是什么。
如何拥有一个数据结构,以便我可以通过一个密钥访问 Vec 并更新它?
解决方法
首先,这里有一些资源可以帮助您决定前进的道路:
- 对于存储地图:transparent keys in substrate(博客)
- 对于 storage best practices(文档)
- 非常好的deep dive on storage(视频)