From 388c0c4ac2ffc58d711e5dd63519a9c0e7e6a34d Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Fri, 31 Mar 2023 11:35:20 +0200 Subject: [PATCH 1/5] Add Freeze/Thaw events and tests --- frame/balances/src/lib.rs | 16 +++++++ frame/balances/src/tests/fungible_tests.rs | 55 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index d4be806982dfe..de33181a9baa0 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -334,6 +334,10 @@ pub mod pallet { Locked { who: T::AccountId, amount: T::Balance }, /// Some balance was unlocked. Unlocked { who: T::AccountId, amount: T::Balance }, + /// Some balance was frozen + Frozen { who: T::AccountId, amount: T::Balance }, + /// Some balance was thawn. + Thawed { who: T::AccountId, amount: T::Balance }, } #[pallet::error] @@ -1071,12 +1075,16 @@ pub mod pallet { } } + /// Update the account entry for `who`, given the locks. /// Update the account entry for `who`, given the locks. pub(crate) fn update_freezes( who: &T::AccountId, freezes: BoundedSlice, T::MaxFreezes>, ) -> DispatchResult { + let mut prev_frozen = Zero::zero(); + let mut after_frozen = Zero::zero(); let (_, maybe_dust) = Self::mutate_account(who, |b| { + prev_frozen = b.frozen; b.frozen = Zero::zero(); for l in Locks::::get(who).iter() { b.frozen = b.frozen.max(l.amount); @@ -1084,6 +1092,7 @@ pub mod pallet { for l in freezes.iter() { b.frozen = b.frozen.max(l.amount); } + after_frozen = b.frozen; })?; debug_assert!(maybe_dust.is_none(), "Not altering main balance; qed"); if freezes.is_empty() { @@ -1091,6 +1100,13 @@ pub mod pallet { } else { Freezes::::insert(who, freezes); } + if prev_frozen > after_frozen { + let amount = prev_frozen.saturating_sub(after_frozen); + Self::deposit_event(Event::Thawed { who: who.clone(), amount }); + } else if after_frozen > prev_frozen { + let amount = after_frozen.saturating_sub(prev_frozen); + Self::deposit_event(Event::Frozen { who: who.clone(), amount }); + } Ok(()) } diff --git a/frame/balances/src/tests/fungible_tests.rs b/frame/balances/src/tests/fungible_tests.rs index 128086885391f..e803e3bd48c48 100644 --- a/frame/balances/src/tests/fungible_tests.rs +++ b/frame/balances/src/tests/fungible_tests.rs @@ -397,3 +397,58 @@ fn unholding_frees_hold_slot() { assert_ok!(Balances::hold(&TestId::Baz, &1, 10)); }); } + +#[test] +fn emit_events_with_changing_freezes() { + ExtBuilder::default().build_and_execute_with(|| { + let _ = Balances::set_balance(&1, 100); + System::reset_events(); + + // Freeze = [] --> [10] + assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 10)); + assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Frozen { who: 1, amount: 10 })]); + + // Freeze = [10] --> [15] + assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 15)); + assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Frozen { who: 1, amount: 5 })]); + + // Freeze = [15] --> [15, 20] + assert_ok!(Balances::set_freeze(&TestId::Bar, &1, 20)); + assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Frozen { who: 1, amount: 5 })]); + + // Freeze = [15, 20] --> [17, 20] + assert_ok!(Balances::set_freeze(&TestId::Foo, &1, 17)); + for event in events() { + match event { + RuntimeEvent::Balances(crate::Event::Frozen { .. }) => { + assert!(false, "unexpected freeze event") + }, + RuntimeEvent::Balances(crate::Event::Thawed { .. }) => { + assert!(false, "unexpected thaw event") + }, + _ => continue, + } + } + + // Freeze = [17, 20] --> [17, 15] + assert_ok!(Balances::set_freeze(&TestId::Bar, &1, 15)); + assert_eq!( + events(), + [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 3 })] + ); + + // Freeze = [17, 15] --> [15] + assert_ok!(Balances::thaw(&TestId::Foo, &1)); + assert_eq!( + events(), + [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 2 })] + ); + + // Freeze = [15] --> [] + assert_ok!(Balances::thaw(&TestId::Bar, &1)); + assert_eq!( + events(), + [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 15 })] + ); + }); +} \ No newline at end of file From 8b31a4161abf7662e201dcb7d887ddabab8d6a62 Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Fri, 31 Mar 2023 11:48:01 +0200 Subject: [PATCH 2/5] Remove duplicate docstring --- frame/balances/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index de33181a9baa0..e1107217792f8 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -1075,7 +1075,6 @@ pub mod pallet { } } - /// Update the account entry for `who`, given the locks. /// Update the account entry for `who`, given the locks. pub(crate) fn update_freezes( who: &T::AccountId, From 0df63bff9b24e549dbce96a09a27baf1016c1dc2 Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Fri, 31 Mar 2023 11:48:57 +0200 Subject: [PATCH 3/5] Correct spelling error --- frame/balances/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index e1107217792f8..07a1116b516f5 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -336,7 +336,7 @@ pub mod pallet { Unlocked { who: T::AccountId, amount: T::Balance }, /// Some balance was frozen Frozen { who: T::AccountId, amount: T::Balance }, - /// Some balance was thawn. + /// Some balance was thawed. Thawed { who: T::AccountId, amount: T::Balance }, } From cba4a1e7c3d47dda7c3f3c0d887bdda9275bad32 Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Fri, 31 Mar 2023 12:03:10 +0200 Subject: [PATCH 4/5] Cargo fmt --- frame/balances/src/tests/fungible_tests.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/frame/balances/src/tests/fungible_tests.rs b/frame/balances/src/tests/fungible_tests.rs index e803e3bd48c48..185396019b13d 100644 --- a/frame/balances/src/tests/fungible_tests.rs +++ b/frame/balances/src/tests/fungible_tests.rs @@ -432,23 +432,14 @@ fn emit_events_with_changing_freezes() { // Freeze = [17, 20] --> [17, 15] assert_ok!(Balances::set_freeze(&TestId::Bar, &1, 15)); - assert_eq!( - events(), - [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 3 })] - ); + assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 3 })]); // Freeze = [17, 15] --> [15] assert_ok!(Balances::thaw(&TestId::Foo, &1)); - assert_eq!( - events(), - [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 2 })] - ); + assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 2 })]); // Freeze = [15] --> [] assert_ok!(Balances::thaw(&TestId::Bar, &1)); - assert_eq!( - events(), - [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 15 })] - ); + assert_eq!(events(), [RuntimeEvent::Balances(crate::Event::Thawed { who: 1, amount: 15 })]); }); -} \ No newline at end of file +} From 7d9cbdfc552ae7c40dd37648cfac80e454ca01e1 Mon Sep 17 00:00:00 2001 From: Harald Heckmann Date: Tue, 4 Apr 2023 12:18:25 +0200 Subject: [PATCH 5/5] Use proper punctuation in docstring Co-authored-by: Jegor Sidorenko <5252494+jsidorenko@users.noreply.github.com> --- frame/balances/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/balances/src/lib.rs b/frame/balances/src/lib.rs index 07a1116b516f5..bc4ab9ae01b8b 100644 --- a/frame/balances/src/lib.rs +++ b/frame/balances/src/lib.rs @@ -334,7 +334,7 @@ pub mod pallet { Locked { who: T::AccountId, amount: T::Balance }, /// Some balance was unlocked. Unlocked { who: T::AccountId, amount: T::Balance }, - /// Some balance was frozen + /// Some balance was frozen. Frozen { who: T::AccountId, amount: T::Balance }, /// Some balance was thawed. Thawed { who: T::AccountId, amount: T::Balance },