Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
rename & helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Aug 21, 2023
1 parent 0fa3502 commit 3fa4c10
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
4 changes: 2 additions & 2 deletions frame/multisig/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ benchmarks! {
assert!(!Multisigs::<T>::contains_key(multi_account_id, call_hash));
}

clear_multi {
clear_expired_multi {
// Signatories, need at least 2 people
let s in 2 .. T::MaxSignatories::get();
// Transaction Length, not a component
Expand All @@ -229,7 +229,7 @@ benchmarks! {
// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
// For calling clear_multi all the signatories must be specified.
// For calling clear_expired_multi all the signatories must be specified.
signatories.push(caller.clone());
}: _(RawOrigin::Signed(caller), s as u16, signatories, timepoint, call_hash)
verify {
Expand Down
48 changes: 34 additions & 14 deletions frame/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ pub mod pallet {
ensure!(m.when == timepoint, Error::<T>::WrongTimepoint);
ensure!(m.depositor == who, Error::<T>::NotOwner);

let err_amount = T::Currency::unreserve(&m.depositor, m.deposit);
debug_assert!(err_amount.is_zero());
<Multisigs<T>>::remove(&id, &call_hash);
let maybe_expiry = <MultisigExpiries<T>>::get(&id, call_hash);

Self::remove_multisig(&id, call_hash, &m.depositor, m.deposit, maybe_expiry);

Self::deposit_event(Event::MultisigCancelled { timepoint, multisig: id, call_hash });
Ok(())
Expand Down Expand Up @@ -596,7 +596,7 @@ pub mod pallet {
/// - Storage: removes one item.
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::cancel_as_multi(signatories.len() as u32))]
pub fn clear_multi(
pub fn clear_expired_multi(
origin: OriginFor<T>,
threshold: u16,
signatories: Vec<T::AccountId>,
Expand All @@ -618,10 +618,7 @@ pub mod pallet {
);

// Clean up all the state that is not needed anymore since the multisig expired.
<Multisigs<T>>::remove(&id, call_hash);
<MultisigExpiries<T>>::remove(&id, call_hash);

T::Currency::unreserve(&m.depositor, m.deposit);
Self::remove_multisig(&id, call_hash, &m.depositor, m.deposit, Some(expiry));

Self::deposit_event(Event::MultisigCancelled { timepoint, multisig: id, call_hash });

Expand Down Expand Up @@ -675,12 +672,15 @@ impl<T: Config> Pallet<T> {
ensure!(m.when == timepoint, Error::<T>::WrongTimepoint);

// Ensure that the mutlisig did not expire.
if let Some(expiry) = <MultisigExpiries<T>>::get(&id, call_hash) {
ensure!(
expiry >= <frame_system::Pallet<T>>::block_number(),
Error::<T>::MultisigExpired
);
}
match <MultisigExpiries<T>>::get(&id, call_hash) {
Some(expiry) if expiry < <frame_system::Pallet<T>>::block_number() => {
// If the multisig is expired we will take the chance to remove it now so that
// the multisig creator does not have to make a separate call to clean it up.
Self::remove_multisig(&id, call_hash, &m.depositor, m.deposit, maybe_expiry);
return Err(Error::<T>::MultisigExpired.into())
},
_ => {},
};

// Ensure that either we have not yet signed or that it is at threshold.
let mut approvals = m.approvals.len() as u16;
Expand Down Expand Up @@ -799,6 +799,26 @@ impl<T: Config> Pallet<T> {
}
}

/// Removes a multisig operation from the state.
///
/// If the multisig had an expiry it will also get removed from the
/// `MultisigExpiries` storage map.
fn remove_multisig(
id: &T::AccountId,
call_hash: [u8; 32],
depositor: &T::AccountId,
deposit: BalanceOf<T>,
expiry: Option<BlockNumberFor<T>>,
) {
let err_amount = T::Currency::unreserve(&depositor, deposit);
debug_assert!(err_amount.is_zero());
<Multisigs<T>>::remove(id, call_hash);

if expiry.is_some() {
<MultisigExpiries<T>>::remove(id, call_hash);
}
}

/// The current `Timepoint`.
pub fn timepoint() -> Timepoint<BlockNumberFor<T>> {
Timepoint {
Expand Down
18 changes: 15 additions & 3 deletions frame/multisig/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,13 @@ fn mutlisig_with_expiry_works() {

// Cannot clear the multisig operation since it has not expired yet.
assert_noop!(
Multisig::clear_multi(RuntimeOrigin::signed(42), 2, vec![1, 2, 3], timepoint, hash),
Multisig::clear_expired_multi(
RuntimeOrigin::signed(42),
2,
vec![1, 2, 3],
timepoint,
hash
),
Error::<Test>::MultisigNotExpired
);

Expand Down Expand Up @@ -355,7 +361,7 @@ fn mutlisig_wont_get_executed_when_expired() {
Error::<Test>::MultisigExpired
);

assert_ok!(Multisig::clear_multi(
assert_ok!(Multisig::clear_expired_multi(
RuntimeOrigin::signed(42),
2,
vec![1, 2, 3],
Expand All @@ -374,7 +380,13 @@ fn mutlisig_wont_get_executed_when_expired() {

// Cannot clear the multisig operation since it does not longer exist.
assert_noop!(
Multisig::clear_multi(RuntimeOrigin::signed(42), 2, vec![1, 2, 3], timepoint, hash),
Multisig::clear_expired_multi(
RuntimeOrigin::signed(42),
2,
vec![1, 2, 3],
timepoint,
hash
),
Error::<Test>::NotFound
);
})
Expand Down

0 comments on commit 3fa4c10

Please sign in to comment.