contracts: Don't rely on reserved balances keeping an account alive (#13369)

* Move storage deposits to their own account

* Take ed for contract's account from origin

* Apply suggestions from code review

Co-authored-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Update stale docs

* Use 16 bytes prefix for address derivation

* Update frame/contracts/src/address.rs

Co-authored-by: Cyrill Leutwiler <bigcyrill@hotmail.com>

* Fix merge

* ".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts

* Update frame/contracts/primitives/src/lib.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

---------

Co-authored-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Co-authored-by: command-bot <>
This commit is contained in:
Alexander Theißen
2023-02-16 16:58:28 +01:00
committed by GitHub
parent 292e5ee4e7
commit 330484bb19
11 changed files with 2109 additions and 2184 deletions
+128 -108
View File
@@ -22,13 +22,15 @@ pub mod meter;
use crate::{
exec::{AccountIdOf, StorageKey},
weights::WeightInfo,
BalanceOf, CodeHash, Config, ContractInfoOf, DeletionQueue, Error, TrieId, SENTINEL,
AddressGenerator, BalanceOf, CodeHash, Config, ContractInfoOf, DeletionQueue, Error, Pallet,
TrieId, SENTINEL,
};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
dispatch::{DispatchError, DispatchResult},
storage::child::{self, ChildInfo},
weights::Weight,
RuntimeDebugNoBound,
};
use scale_info::TypeInfo;
use sp_io::KillStorageResult;
@@ -36,7 +38,7 @@ use sp_runtime::{
traits::{Hash, Saturating, Zero},
RuntimeDebug,
};
use sp_std::{marker::PhantomData, prelude::*};
use sp_std::{ops::Deref, prelude::*};
/// Information for managing an account and its sub trie abstraction.
/// This is the required info to cache for an account.
@@ -45,28 +47,68 @@ use sp_std::{marker::PhantomData, prelude::*};
pub struct ContractInfo<T: Config> {
/// Unique ID for the subtree encoded as a bytes vector.
pub trie_id: TrieId,
/// The account that holds this contracts storage deposit.
///
/// This is held in a separate account to prevent the contract from spending it.
deposit_account: DepositAccount<T>,
/// The code associated with a given account.
pub code_hash: CodeHash<T>,
/// How many bytes of storage are accumulated in this contract's child trie.
pub storage_bytes: u32,
storage_bytes: u32,
/// How many items of storage are accumulated in this contract's child trie.
pub storage_items: u32,
storage_items: u32,
/// This records to how much deposit the accumulated `storage_bytes` amount to.
pub storage_byte_deposit: BalanceOf<T>,
/// This records to how much deposit the accumulated `storage_items` amount to.
pub storage_item_deposit: BalanceOf<T>,
storage_item_deposit: BalanceOf<T>,
/// This records how much deposit is put down in order to pay for the contract itself.
///
/// We need to store this information separately so it is not used when calculating any refunds
/// since the base deposit can only ever be refunded on contract termination.
pub storage_base_deposit: BalanceOf<T>,
storage_base_deposit: BalanceOf<T>,
}
impl<T: Config> ContractInfo<T> {
/// Constructs a new contract info **without** writing it to storage.
///
/// This returns an `Err` if an contract with the supplied `account` already exists
/// in storage.
pub fn new(
account: &AccountIdOf<T>,
nonce: u64,
code_hash: CodeHash<T>,
) -> Result<Self, DispatchError> {
if <ContractInfoOf<T>>::contains_key(account) {
return Err(Error::<T>::DuplicateContract.into())
}
let trie_id = {
let buf = (account, nonce).using_encoded(T::Hashing::hash);
buf.as_ref()
.to_vec()
.try_into()
.expect("Runtime uses a reasonable hash size. Hence sizeof(T::Hash) <= 128; qed")
};
let deposit_account = DepositAccount(T::AddressGenerator::deposit_address(account));
let contract = Self {
trie_id,
deposit_account,
code_hash,
storage_bytes: 0,
storage_items: 0,
storage_byte_deposit: Zero::zero(),
storage_item_deposit: Zero::zero(),
storage_base_deposit: Zero::zero(),
};
Ok(contract)
}
/// Associated child trie unique id is built from the hash part of the trie id.
#[cfg(test)]
pub fn child_trie_info(&self) -> ChildInfo {
child_trie_info(&self.trie_id[..])
ChildInfo::new_default(self.trie_id.as_ref())
}
/// The deposit paying for the accumulated storage generated within the contract's child trie.
@@ -76,79 +118,30 @@ impl<T: Config> ContractInfo<T> {
/// Same as [`Self::extra_deposit`] but including the base deposit.
pub fn total_deposit(&self) -> BalanceOf<T> {
self.extra_deposit().saturating_add(self.storage_base_deposit)
}
}
/// Associated child trie unique id is built from the hash part of the trie id.
fn child_trie_info(trie_id: &[u8]) -> ChildInfo {
ChildInfo::new_default(trie_id)
}
#[derive(Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct DeletedContract {
pub(crate) trie_id: TrieId,
}
/// Information about what happended to the pre-existing value when calling [`Storage::write`].
#[cfg_attr(test, derive(Debug, PartialEq))]
pub enum WriteOutcome {
/// No value existed at the specified key.
New,
/// A value of the returned length was overwritten.
Overwritten(u32),
/// The returned value was taken out of storage before being overwritten.
///
/// This is only returned when specifically requested because it causes additional work
/// depending on the size of the pre-existing value. When not requested [`Self::Overwritten`]
/// is returned instead.
Taken(Vec<u8>),
}
impl WriteOutcome {
/// Extracts the size of the overwritten value or `0` if there
/// was no value in storage.
pub fn old_len(&self) -> u32 {
match self {
Self::New => 0,
Self::Overwritten(len) => *len,
Self::Taken(value) => value.len() as u32,
}
self.extra_deposit()
.saturating_add(self.storage_base_deposit)
.saturating_sub(Pallet::<T>::min_balance())
}
/// Extracts the size of the overwritten value or `SENTINEL` if there
/// was no value in storage.
///
/// # Note
///
/// We cannot use `0` as sentinel value because there could be a zero sized
/// storage entry which is different from a non existing one.
pub fn old_len_with_sentinel(&self) -> u32 {
match self {
Self::New => SENTINEL,
Self::Overwritten(len) => *len,
Self::Taken(value) => value.len() as u32,
}
/// Return the account that storage deposits should be deposited into.
pub fn deposit_account(&self) -> &DepositAccount<T> {
&self.deposit_account
}
}
pub struct Storage<T>(PhantomData<T>);
impl<T: Config> Storage<T> {
/// Reads a storage kv pair of a contract.
///
/// The read is performed from the `trie_id` only. The `address` is not necessary. If the
/// contract doesn't store under the given `key` `None` is returned.
pub fn read<K: StorageKey<T>>(trie_id: &TrieId, key: &K) -> Option<Vec<u8>> {
child::get_raw(&child_trie_info(trie_id), key.hash().as_slice())
pub fn read<K: StorageKey<T>>(&self, key: &K) -> Option<Vec<u8>> {
child::get_raw(&self.child_trie_info(), key.hash().as_slice())
}
/// Returns `Some(len)` (in bytes) if a storage item exists at `key`.
///
/// Returns `None` if the `key` wasn't previously set by `set_storage` or
/// was deleted.
pub fn size<K: StorageKey<T>>(trie_id: &TrieId, key: &K) -> Option<u32> {
child::len(&child_trie_info(trie_id), key.hash().as_slice())
pub fn size<K: StorageKey<T>>(&self, key: &K) -> Option<u32> {
child::len(&self.child_trie_info(), key.hash().as_slice())
}
/// Update a storage entry into a contract's kv storage.
@@ -159,13 +152,13 @@ impl<T: Config> Storage<T> {
/// This function also records how much storage was created or removed if a `storage_meter`
/// is supplied. It should only be absent for testing or benchmarking code.
pub fn write<K: StorageKey<T>>(
trie_id: &TrieId,
&self,
key: &K,
new_value: Option<Vec<u8>>,
storage_meter: Option<&mut meter::NestedMeter<T>>,
take: bool,
) -> Result<WriteOutcome, DispatchError> {
let child_trie_info = &child_trie_info(trie_id);
let child_trie_info = &self.child_trie_info();
let hashed_key = key.hash();
let (old_len, old_value) = if take {
let val = child::get_raw(child_trie_info, &hashed_key);
@@ -208,37 +201,11 @@ impl<T: Config> Storage<T> {
})
}
/// Creates a new contract descriptor in the storage with the given code hash at the given
/// address.
///
/// Returns `Err` if there is already a contract at the given address.
pub fn new_contract(
account: &AccountIdOf<T>,
trie_id: TrieId,
code_hash: CodeHash<T>,
) -> Result<ContractInfo<T>, DispatchError> {
if <ContractInfoOf<T>>::contains_key(account) {
return Err(Error::<T>::DuplicateContract.into())
}
let contract = ContractInfo::<T> {
code_hash,
trie_id,
storage_bytes: 0,
storage_items: 0,
storage_byte_deposit: Zero::zero(),
storage_item_deposit: Zero::zero(),
storage_base_deposit: Zero::zero(),
};
Ok(contract)
}
/// Push a contract's trie to the deletion queue for lazy removal.
///
/// You must make sure that the contract is also removed when queuing the trie for deletion.
pub fn queue_trie_for_deletion(contract: &ContractInfo<T>) -> DispatchResult {
<DeletionQueue<T>>::try_append(DeletedContract { trie_id: contract.trie_id.clone() })
pub fn queue_trie_for_deletion(&self) -> DispatchResult {
<DeletionQueue<T>>::try_append(DeletedContract { trie_id: self.trie_id.clone() })
.map_err(|_| <Error<T>>::DeletionQueueFull.into())
}
@@ -289,7 +256,10 @@ impl<T: Config> Storage<T> {
// Cannot panic due to loop condition
let trie = &mut queue[0];
#[allow(deprecated)]
let outcome = child::kill_storage(&child_trie_info(&trie.trie_id), Some(remaining_key_budget));
let outcome = child::kill_storage(
&ChildInfo::new_default(&trie.trie_id),
Some(remaining_key_budget),
);
let keys_removed = match outcome {
// This happens when our budget wasn't large enough to remove all keys.
KillStorageResult::SomeRemaining(c) => c,
@@ -307,17 +277,8 @@ impl<T: Config> Storage<T> {
weight_limit.saturating_sub(weight_per_key.saturating_mul(u64::from(remaining_key_budget)))
}
/// Generates a unique trie id by returning `hash(account_id ++ nonce)`.
pub fn generate_trie_id(account_id: &AccountIdOf<T>, nonce: u64) -> TrieId {
let buf = (account_id, nonce).using_encoded(T::Hashing::hash);
buf.as_ref()
.to_vec()
.try_into()
.expect("Runtime uses a reasonable hash size. Hence sizeof(T::Hash) <= 128; qed")
}
/// Returns the code hash of the contract specified by `account` ID.
pub fn code_hash(account: &AccountIdOf<T>) -> Option<CodeHash<T>> {
pub fn load_code_hash(account: &AccountIdOf<T>) -> Option<CodeHash<T>> {
<ContractInfoOf<T>>::get(account).map(|i| i.code_hash)
}
@@ -332,3 +293,62 @@ impl<T: Config> Storage<T> {
<DeletionQueue<T>>::put(bounded);
}
}
#[derive(Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct DeletedContract {
pub(crate) trie_id: TrieId,
}
/// Information about what happended to the pre-existing value when calling [`ContractInfo::write`].
#[cfg_attr(test, derive(Debug, PartialEq))]
pub enum WriteOutcome {
/// No value existed at the specified key.
New,
/// A value of the returned length was overwritten.
Overwritten(u32),
/// The returned value was taken out of storage before being overwritten.
///
/// This is only returned when specifically requested because it causes additional work
/// depending on the size of the pre-existing value. When not requested [`Self::Overwritten`]
/// is returned instead.
Taken(Vec<u8>),
}
impl WriteOutcome {
/// Extracts the size of the overwritten value or `0` if there
/// was no value in storage.
pub fn old_len(&self) -> u32 {
match self {
Self::New => 0,
Self::Overwritten(len) => *len,
Self::Taken(value) => value.len() as u32,
}
}
/// Extracts the size of the overwritten value or `SENTINEL` if there
/// was no value in storage.
///
/// # Note
///
/// We cannot use `0` as sentinel value because there could be a zero sized
/// storage entry which is different from a non existing one.
pub fn old_len_with_sentinel(&self) -> u32 {
match self {
Self::New => SENTINEL,
Self::Overwritten(len) => *len,
Self::Taken(value) => value.len() as u32,
}
}
}
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebugNoBound, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub struct DepositAccount<T: Config>(AccountIdOf<T>);
impl<T: Config> Deref for DepositAccount<T> {
type Target = AccountIdOf<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}