Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Prev Previous commit
Next Next commit
review updates
  • Loading branch information
juangirini committed Aug 15, 2023
commit e051ce7a8aa152b06dfc2fa732b287e33b71df11
2 changes: 1 addition & 1 deletion frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ where
}
let frame = self.top_frame_mut();
let info = frame.terminate();
frame.nested_storage.terminate(&info, beneficiary);
frame.nested_storage.terminate(&info, beneficiary.clone());

info.queue_trie_for_deletion();
ContractInfoOf::<T>::remove(&frame.account_id);
Expand Down
31 changes: 21 additions & 10 deletions frame/contracts/src/storage/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use sp_runtime::{
traits::{Saturating, Zero},
FixedPointNumber, FixedU128,
};
use sp_std::{marker::PhantomData, vec, vec::Vec};
use sp_std::{marker::PhantomData, ops::Deref, vec, vec::Vec};

/// Deposit that uses the native fungible's balance type.
pub type DepositOf<T> = Deposit<BalanceOf<T>>;
Expand Down Expand Up @@ -225,15 +225,24 @@ impl Diff {
}

/// The beneficiary of a contract termination.
type BeneficiaryOf<T> = AccountIdOf<T>;
#[derive(RuntimeDebugNoBound, Clone, PartialEq, Eq)]
pub struct Beneficiary<T: Config>(AccountIdOf<T>);

impl<T: Config> Deref for Beneficiary<T> {
type Target = AccountIdOf<T>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// The status of a contract.
///
/// In case of termination the beneficiary is indicated.
#[derive(RuntimeDebugNoBound, Clone, PartialEq, Eq)]
pub enum ContractStatus<T: Config> {
Alive,
Terminated(BeneficiaryOf<T>),
Terminated(Beneficiary<T>),
}

/// Records information to charge or refund a plain account.
Expand Down Expand Up @@ -262,7 +271,7 @@ enum Contribution<T: Config> {
Checked(DepositOf<T>),
/// The contract was terminated. In this process the [`Diff`] was converted into a [`Deposit`]
/// in order to calculate the refund.
Terminated(DepositOf<T>, BeneficiaryOf<T>),
Terminated(DepositOf<T>, Beneficiary<T>),
}

impl<T: Config> Contribution<T> {
Expand Down Expand Up @@ -486,10 +495,12 @@ where
/// This will manipulate the meter so that all storage deposit accumulated in
/// `contract_info` will be refunded to the `origin` of the meter. And the free
/// (`reducible_balance`) will be sent to the `beneficiary`.
pub fn terminate(&mut self, info: &ContractInfo<T>, beneficiary: &T::AccountId) {
pub fn terminate(&mut self, info: &ContractInfo<T>, beneficiary: T::AccountId) {
debug_assert!(self.is_alive());
self.own_contribution =
Contribution::Terminated(Deposit::Refund(info.total_deposit()), beneficiary.clone());
self.own_contribution = Contribution::Terminated(
Deposit::Refund(info.total_deposit()),
Beneficiary(beneficiary),
);
}

/// [`Self::charge`] does not enforce the storage limit since we want to do this check as late
Expand Down Expand Up @@ -621,7 +632,7 @@ impl<T: Config> Ext<T> for ReservingExt {
// Whatever is left in the contract is sent to the termination beneficiary.
T::Currency::transfer(
&contract,
beneficiary,
&*beneficiary,
T::Currency::reducible_balance(&contract, Preservation::Expendable, Polite),
Preservation::Expendable,
)?;
Expand Down Expand Up @@ -883,7 +894,7 @@ mod tests {
origin: ALICE,
contract: CHARLIE,
amount: Deposit::Refund(119),
status: ContractStatus::Terminated(CHARLIE),
status: ContractStatus::Terminated(Beneficiary(CHARLIE)),
},
Charge {
origin: ALICE,
Expand Down Expand Up @@ -925,7 +936,7 @@ mod tests {
let mut nested1 = nested0.nested(BalanceOf::<Test>::zero());
nested1.charge(&Diff { items_removed: 5, ..Default::default() });
nested1.charge(&Diff { bytes_added: 20, ..Default::default() });
nested1.terminate(&nested1_info, &CHARLIE);
nested1.terminate(&nested1_info, CHARLIE);
nested0.enforce_limit(Some(&mut nested1_info)).unwrap();
nested0.absorb(nested1, &CHARLIE, None);

Expand Down