Companion to child trie api change #4857 (#950)

This commit is contained in:
cheme
2020-04-21 10:39:35 +02:00
committed by GitHub
parent fb2e0c0ca9
commit 1a4db93c50
8 changed files with 178 additions and 164 deletions
+6 -23
View File
@@ -80,7 +80,6 @@ use sp_runtime::{ModuleId,
use crate::slots;
use codec::{Encode, Decode};
use sp_std::vec::Vec;
use sp_core::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX;
use primitives::parachain::{Id as ParaId, HeadData};
const MODULE_ID: ModuleId = ModuleId(*b"py/cfund");
@@ -529,46 +528,30 @@ impl<T: Trait> Module<T> {
MODULE_ID.into_sub_account(index)
}
pub fn id_from_index(index: FundIndex) -> Vec<u8> {
pub fn id_from_index(index: FundIndex) -> child::ChildInfo {
let mut buf = Vec::new();
buf.extend_from_slice(b"crowdfund");
buf.extend_from_slice(&index.to_le_bytes()[..]);
CHILD_STORAGE_KEY_PREFIX.into_iter()
.chain(b"default:")
.chain(T::Hashing::hash(&buf[..]).as_ref().into_iter())
.cloned()
.collect()
}
/// Child trie unique id for a crowdfund is built from the hash part of the fund id.
pub fn trie_unique_id(fund_id: &[u8]) -> child::ChildInfo {
let start = CHILD_STORAGE_KEY_PREFIX.len() + b"default:".len();
child::ChildInfo::new_default(&fund_id[start..])
child::ChildInfo::new_default(T::Hashing::hash(&buf[..]).as_ref())
}
pub fn contribution_put(index: FundIndex, who: &T::AccountId, balance: &BalanceOf<T>) {
let id = Self::id_from_index(index);
who.using_encoded(|b| child::put(id.as_ref(), Self::trie_unique_id(id.as_ref()), b, balance));
who.using_encoded(|b| child::put(&Self::id_from_index(index), b, balance));
}
pub fn contribution_get(index: FundIndex, who: &T::AccountId) -> BalanceOf<T> {
let id = Self::id_from_index(index);
who.using_encoded(|b| child::get_or_default::<BalanceOf<T>>(
id.as_ref(),
Self::trie_unique_id(id.as_ref()),
&Self::id_from_index(index),
b,
))
}
pub fn contribution_kill(index: FundIndex, who: &T::AccountId) {
let id = Self::id_from_index(index);
who.using_encoded(|b| child::kill(id.as_ref(), Self::trie_unique_id(id.as_ref()), b));
who.using_encoded(|b| child::kill(&Self::id_from_index(index), b));
}
pub fn crowdfund_kill(index: FundIndex) {
let id = Self::id_from_index(index);
child::kill_storage(id.as_ref(), Self::trie_unique_id(id.as_ref()));
child::kill_storage(&Self::id_from_index(index));
}
}