Skip to content
Snippets Groups Projects
Commit 22027135 authored by Harald Heckmann's avatar Harald Heckmann Committed by GitHub
Browse files

Emit event when changing total locked value in pallet-balances (#12287)


* Emit Locked/Unlocked events

* Implement lock event tests

* Adhere to style guide

* Use saturating math

Co-authored-by: default avatarKian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fix typo

* Emit event on change locks and add tests

* Adjust event docstring

---------

Co-authored-by: default avatarKian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: parity-processbot <>
parent cef4b363
No related merge requests found
......@@ -330,6 +330,10 @@ pub mod pallet {
Issued { amount: T::Balance },
/// Total issuance was decreased by `amount`, creating a debt to be balanced.
Rescinded { amount: T::Balance },
/// Some balance was locked.
Locked { who: T::AccountId, amount: T::Balance },
/// Some balance was unlocked.
Unlocked { who: T::AccountId, amount: T::Balance },
}
#[pallet::error]
......@@ -1016,9 +1020,12 @@ pub mod pallet {
);
}
let freezes = Freezes::<T, I>::get(who);
let mut prev_frozen = Zero::zero();
let mut after_frozen = Zero::zero();
// TODO: Revisit this assumption. We no manipulate consumer/provider refs.
// No way this can fail since we do not alter the existential balances.
let res = Self::mutate_account(who, |b| {
prev_frozen = b.frozen;
b.frozen = Zero::zero();
for l in locks.iter() {
b.frozen = b.frozen.max(l.amount);
......@@ -1026,6 +1033,7 @@ pub mod pallet {
for l in freezes.iter() {
b.frozen = b.frozen.max(l.amount);
}
after_frozen = b.frozen;
});
debug_assert!(res.is_ok());
if let Ok((_, maybe_dust)) = res {
......@@ -1053,6 +1061,14 @@ pub mod pallet {
);
}
}
if prev_frozen > after_frozen {
let amount = prev_frozen.saturating_sub(after_frozen);
Self::deposit_event(Event::Unlocked { who: who.clone(), amount });
} else if after_frozen > prev_frozen {
let amount = after_frozen.saturating_sub(prev_frozen);
Self::deposit_event(Event::Locked { who: who.clone(), amount });
}
}
/// Update the account entry for `who`, given the locks.
......
......@@ -754,6 +754,61 @@ fn emit_events_with_reserve_and_unreserve() {
});
}
#[test]
fn emit_events_with_changing_locks() {
ExtBuilder::default().build_and_execute_with(|| {
let _ = Balances::deposit_creating(&1, 100);
System::reset_events();
// Locks = [] --> [10]
Balances::set_lock(*b"LOCK_000", &1, 10, WithdrawReasons::TRANSFER);
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Locked { who: 1, amount: 10 })]);
// Locks = [10] --> [15]
Balances::set_lock(*b"LOCK_000", &1, 15, WithdrawReasons::TRANSFER);
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Locked { who: 1, amount: 5 })]);
// Locks = [15] --> [15, 20]
Balances::set_lock(*b"LOCK_001", &1, 20, WithdrawReasons::TRANSACTION_PAYMENT);
assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Locked { who: 1, amount: 5 })]);
// Locks = [15, 20] --> [17, 20]
Balances::set_lock(*b"LOCK_000", &1, 17, WithdrawReasons::TRANSACTION_PAYMENT);
for event in events() {
match event {
RuntimeEvent::Balances(crate::Event::Locked { .. }) => {
assert!(false, "unexpected lock event")
},
RuntimeEvent::Balances(crate::Event::Unlocked { .. }) => {
assert!(false, "unexpected unlock event")
},
_ => continue,
}
}
// Locks = [17, 20] --> [17, 15]
Balances::set_lock(*b"LOCK_001", &1, 15, WithdrawReasons::TRANSFER);
assert_eq!(
events(),
[RuntimeEvent::Balances(crate::Event::Unlocked { who: 1, amount: 3 })]
);
// Locks = [17, 15] --> [15]
Balances::remove_lock(*b"LOCK_000", &1);
assert_eq!(
events(),
[RuntimeEvent::Balances(crate::Event::Unlocked { who: 1, amount: 2 })]
);
// Locks = [15] --> []
Balances::remove_lock(*b"LOCK_001", &1);
assert_eq!(
events(),
[RuntimeEvent::Balances(crate::Event::Unlocked { who: 1, amount: 15 })]
);
});
}
#[test]
fn emit_events_with_existential_deposit() {
ExtBuilder::default().existential_deposit(100).build_and_execute_with(|| {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment