use crate::{
exec::{AccountIdOf, StorageKey},
AliveContractInfo, BalanceOf, CodeHash, ContractInfo, ContractInfoOf, Trait, TrieId,
};
use sp_std::prelude::*;
use sp_io::hashing::blake2_256;
use sp_runtime::traits::Bounded;
use frame_support::{storage::child, StorageMap};
#[cfg_attr(test, derive(PartialEq, Eq, Debug))]
pub struct ContractAbsentError;
pub fn read_contract_storage(trie_id: &TrieId, key: &StorageKey) -> Option<Vec<u8>> {
child::get_raw(&crate::child_trie_info(&trie_id), &blake2_256(key))
}
pub fn write_contract_storage<T: Trait>(
account: &AccountIdOf<T>,
trie_id: &TrieId,
key: &StorageKey,
opt_new_value: Option<Vec<u8>>,
) -> Result<(), ContractAbsentError> {
let mut new_info = match <ContractInfoOf<T>>::get(account) {
Some(ContractInfo::Alive(alive)) => alive,
None | Some(ContractInfo::Tombstone(_)) => return Err(ContractAbsentError),
};
let hashed_key = blake2_256(key);
let child_trie_info = &crate::child_trie_info(&trie_id);
let opt_prev_value = child::get_raw(&child_trie_info, &hashed_key);
match (&opt_prev_value, &opt_new_value) {
(Some(prev_value), None) => {
new_info.total_pair_count -= 1;
if prev_value.is_empty() {
new_info.empty_pair_count -= 1;
}
},
(None, Some(new_value)) => {
new_info.total_pair_count += 1;
if new_value.is_empty() {
new_info.empty_pair_count += 1;
}
},
(Some(prev_value), Some(new_value)) => {
if prev_value.is_empty() {
new_info.empty_pair_count -= 1;
}
if new_value.is_empty() {
new_info.empty_pair_count += 1;
}
}
(None, None) => {}
}
let prev_value_len = opt_prev_value
.as_ref()
.map(|old_value| old_value.len() as u32)
.unwrap_or(0);
let new_value_len = opt_new_value
.as_ref()
.map(|new_value| new_value.len() as u32)
.unwrap_or(0);
new_info.storage_size = new_info
.storage_size
.saturating_add(new_value_len)
.saturating_sub(prev_value_len);
new_info.last_write = Some(<frame_system::Module<T>>::block_number());
<ContractInfoOf<T>>::insert(&account, ContractInfo::Alive(new_info));
match opt_new_value {
Some(new_value) => child::put_raw(&child_trie_info, &hashed_key, &new_value[..]),
None => child::kill(&child_trie_info, &hashed_key),
}
Ok(())
}
pub fn rent_allowance<T: Trait>(
account: &AccountIdOf<T>,
) -> Result<BalanceOf<T>, ContractAbsentError> {
<ContractInfoOf<T>>::get(account)
.and_then(|i| i.as_alive().map(|i| i.rent_allowance))
.ok_or(ContractAbsentError)
}
pub fn set_rent_allowance<T: Trait>(
account: &AccountIdOf<T>,
rent_allowance: BalanceOf<T>,
) -> Result<(), ContractAbsentError> {
<ContractInfoOf<T>>::mutate(account, |maybe_contract_info| match maybe_contract_info {
Some(ContractInfo::Alive(ref mut alive_info)) => {
alive_info.rent_allowance = rent_allowance;
Ok(())
}
_ => Err(ContractAbsentError),
})
}
#[cfg(test)]
pub fn code_hash<T: Trait>(account: &AccountIdOf<T>) -> Result<CodeHash<T>, ContractAbsentError> {
<ContractInfoOf<T>>::get(account)
.and_then(|i| i.as_alive().map(|i| i.code_hash))
.ok_or(ContractAbsentError)
}
pub fn place_contract<T: Trait>(
account: &AccountIdOf<T>,
trie_id: TrieId,
ch: CodeHash<T>,
) -> Result<(), &'static str> {
<ContractInfoOf<T>>::mutate(account, |maybe_contract_info| {
if maybe_contract_info.is_some() {
return Err("Alive contract or tombstone already exists");
}
*maybe_contract_info = Some(
AliveContractInfo::<T> {
code_hash: ch,
storage_size: 0,
trie_id,
deduct_block: <frame_system::Module<T>>::block_number(),
rent_allowance: <BalanceOf<T>>::max_value(),
empty_pair_count: 0,
total_pair_count: 0,
last_write: None,
}
.into(),
);
Ok(())
})
}
pub fn destroy_contract<T: Trait>(address: &AccountIdOf<T>, trie_id: &TrieId) {
<ContractInfoOf<T>>::remove(address);
child::kill_storage(&crate::child_trie_info(&trie_id));
}