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

Add Deposit and Withdraw Events to Balances Pallet #9425

Merged
29 commits merged into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
63704c7
add Deposit and Withdraw events to balances
apopiak Jul 23, 2021
5ebddce
line length
apopiak Jul 23, 2021
bea9cb5
move events to the end to avoid changing indices
apopiak Jul 23, 2021
579a48c
Merge branch 'master' of github.com:paritytech/substrate into apopiak…
apopiak Jul 23, 2021
91862d2
bump spec_version
apopiak Jul 26, 2021
831b035
cargo fmt
apopiak Jul 26, 2021
8f323af
adjust block import bench to new event count
apopiak Jul 27, 2021
59c54b5
fix node executor tests
apopiak Jul 27, 2021
5bdc024
adjust import bench comment
apopiak Jul 27, 2021
96eb842
fix typo and formatting
apopiak Jul 27, 2021
bb78c63
adjust event number
apopiak Jul 27, 2021
3cddb5f
fix copy pasta
apopiak Jul 27, 2021
12b2687
Merge branch 'master' of github.com:paritytech/substrate into apopiak…
apopiak Jul 27, 2021
f20e5a4
fix contracts pallets tests
apopiak Jul 28, 2021
7febf6e
cargo fmt
apopiak Jul 28, 2021
99550c7
WIP fix events in tests
apopiak Aug 13, 2021
b56c29f
Merge branch 'master' into apopiak/balances-events
shawntabrizi Sep 3, 2021
6d9c73c
Merge branch 'apopiak/balances-events' of github.com:paritytech/subst…
apopiak Oct 7, 2021
6bdc33a
Merge branch 'master' of github.com:paritytech/substrate into apopiak…
apopiak Oct 7, 2021
6d62ae4
fix offences tests
apopiak Oct 7, 2021
e646900
fix tests
apopiak Oct 7, 2021
8e0cce5
Merge branch 'master' of github.com:paritytech/substrate into apopiak…
apopiak Oct 7, 2021
a897bf1
cargo +nightly fmt
apopiak Oct 7, 2021
dd75a03
fix contracts pallets tests
apopiak Oct 7, 2021
fef4b75
cargo +nightly fmt
apopiak Oct 8, 2021
928b2f5
fix offences tests
apopiak Oct 8, 2021
ce1aa09
formatting and compile fixes
apopiak Oct 8, 2021
f09fd5e
Merge branch 'master' of github.com:paritytech/substrate into apopiak…
apopiak Oct 8, 2021
eeee06c
Merge branch 'master' of github.com:paritytech/substrate into apopiak…
apopiak Oct 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions bin/node/bench/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,20 @@ impl core::Benchmark for ImportBenchmark {
.inspect_state(|| {
match self.block_type {
BlockType::RandomTransfersKeepAlive => {
// should be 5 per signed extrinsic + 1 per unsigned
// should be 7 per signed extrinsic + 1 per unsigned
// we have 1 unsigned and the rest are signed in the block
// those 5 events per signed are:
// - new account (RawEvent::NewAccount) as we always transfer fund to
// non-existant account
// - endowed (RawEvent::Endowed) for this new account
// - successful transfer (RawEvent::Transfer) for this transfer operation
// - deposit event for charging transaction fee
// those 7 events per signed are:
// - withdraw (Balances::Withdraw) for charging the transaction fee
// - new account (System::NewAccount) as we always transfer fund to
// non-existent account
// - endowed (Balances::Endowed) for this new account
// - successful transfer (Event::Transfer) for this transfer operation
// - 2x deposit (Balances::Deposit and Treasury::Deposit) for depositing
// the transaction fee into the treasury
// - extrinsic success
assert_eq!(
node_runtime::System::events().len(),
(self.block.extrinsics.len() - 1) * 5 + 1,
(self.block.extrinsics.len() - 1) * 7 + 1,
);
},
BlockType::Noop => {
Expand Down
39 changes: 39 additions & 0 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ fn full_native_block_import_works() {
})),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Balances(pallet_balances::Event::Withdraw(alice().into(), fees)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Balances(pallet_balances::Event::Transfer(
Expand All @@ -394,6 +399,14 @@ fn full_native_block_import_works() {
)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Balances(pallet_balances::Event::Deposit(
pallet_treasury::Pallet::<Runtime>::account_id(),
fees * 8 / 10,
)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Treasury(pallet_treasury::Event::Deposit(fees * 8 / 10)),
Expand Down Expand Up @@ -439,6 +452,11 @@ fn full_native_block_import_works() {
})),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Balances(pallet_balances::Event::Withdraw(bob().into(), fees)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Balances(pallet_balances::Event::Transfer(
Expand All @@ -448,6 +466,14 @@ fn full_native_block_import_works() {
)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Balances(pallet_balances::Event::Deposit(
pallet_treasury::Pallet::<Runtime>::account_id(),
fees * 8 / 10,
)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::Treasury(pallet_treasury::Event::Deposit(fees * 8 / 10)),
Expand All @@ -461,6 +487,11 @@ fn full_native_block_import_works() {
})),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::Balances(pallet_balances::Event::Withdraw(alice().into(), fees)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::Balances(pallet_balances::Event::Transfer(
Expand All @@ -470,6 +501,14 @@ fn full_native_block_import_works() {
)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::Balances(pallet_balances::Event::Deposit(
pallet_treasury::Pallet::<Runtime>::account_id(),
fees * 8 / 10,
)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(2),
event: Event::Treasury(pallet_treasury::Event::Deposit(fees * 8 / 10)),
Expand Down
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 267,
impl_version: 1,
spec_version: 268,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
};
Expand Down
38 changes: 33 additions & 5 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,6 @@ pub mod pallet {
Transfer(T::AccountId, T::AccountId, T::Balance),
/// A balance was set by root. \[who, free, reserved\]
BalanceSet(T::AccountId, T::Balance, T::Balance),
/// Some amount was deposited (e.g. for transaction fees). \[who, deposit\]
Deposit(T::AccountId, T::Balance),
/// Some balance was reserved (moved from free to reserved). \[who, value\]
Reserved(T::AccountId, T::Balance),
/// Some balance was unreserved (moved from reserved to free). \[who, value\]
Expand All @@ -473,6 +471,14 @@ pub mod pallet {
/// Final argument indicates the destination balance type.
/// \[from, to, balance, destination_status\]
ReserveRepatriated(T::AccountId, T::AccountId, T::Balance, Status),
/// Some amount was deposited into the account (e.g. for transaction fees). \[who,
/// deposit\]
Deposit(T::AccountId, T::Balance),
/// Some amount was withdrawn from the account (e.g. for transaction fees). \[who, value\]
Withdraw(T::AccountId, T::Balance),
/// Some amount was removed from the account (e.g. for misbehavior). \[who,
/// amount_slashed\]
Slashed(T::AccountId, T::Balance),
}

/// Old name generated by `decl_event`.
Expand Down Expand Up @@ -1103,6 +1109,7 @@ impl<T: Config<I>, I: 'static> fungible::Mutate<T::AccountId> for Pallet<T, I> {
Ok(())
})?;
TotalIssuance::<T, I>::mutate(|t| *t += amount);
Self::deposit_event(Event::Deposit(who.clone(), amount));
Ok(())
}

Expand All @@ -1123,6 +1130,7 @@ impl<T: Config<I>, I: 'static> fungible::Mutate<T::AccountId> for Pallet<T, I> {
},
)?;
TotalIssuance::<T, I>::mutate(|t| *t -= actual);
Self::deposit_event(Event::Withdraw(who.clone(), amount));
Ok(actual)
}
}
Expand All @@ -1141,7 +1149,10 @@ impl<T: Config<I>, I: 'static> fungible::Transfer<T::AccountId> for Pallet<T, I>

impl<T: Config<I>, I: 'static> fungible::Unbalanced<T::AccountId> for Pallet<T, I> {
fn set_balance(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
Self::mutate_account(who, |account| account.free = amount)?;
Self::mutate_account(who, |account| {
account.free = amount;
Self::deposit_event(Event::BalanceSet(who.clone(), account.free, account.reserved));
})?;
Ok(())
}

Expand Down Expand Up @@ -1583,7 +1594,13 @@ where
}
},
) {
Ok(r) => return r,
Ok((imbalance, not_slashed)) => {
Self::deposit_event(Event::Slashed(
who.clone(),
value.saturating_sub(not_slashed),
));
return (imbalance, not_slashed)
},
Err(_) => (),
}
}
Expand All @@ -1608,6 +1625,7 @@ where
|account, is_new| -> Result<Self::PositiveImbalance, DispatchError> {
ensure!(!is_new, Error::<T, I>::DeadAccount);
account.free = account.free.checked_add(&value).ok_or(ArithmeticError::Overflow)?;
Self::deposit_event(Event::Deposit(who.clone(), value));
Ok(PositiveImbalance::new(value))
},
)
Expand Down Expand Up @@ -1640,6 +1658,7 @@ where
None => return Ok(Self::PositiveImbalance::zero()),
};

Self::deposit_event(Event::Deposit(who.clone(), value));
Ok(PositiveImbalance::new(value))
},
)
Expand Down Expand Up @@ -1677,6 +1696,7 @@ where

account.free = new_free_account;

Self::deposit_event(Event::Withdraw(who.clone(), value));
Ok(NegativeImbalance::new(value))
},
)
Expand Down Expand Up @@ -1709,6 +1729,7 @@ where
SignedImbalance::Negative(NegativeImbalance::new(account.free - value))
};
account.free = value;
Self::deposit_event(Event::BalanceSet(who.clone(), account.free, account.reserved));
Ok(imbalance)
},
)
Expand Down Expand Up @@ -1824,7 +1845,13 @@ where
// underflow should never happen, but it if does, there's nothing to be done here.
(NegativeImbalance::new(actual), value - actual)
}) {
Ok(r) => return r,
Ok((imbalance, not_slashed)) => {
Self::deposit_event(Event::Slashed(
who.clone(),
value.saturating_sub(not_slashed),
));
return (imbalance, not_slashed)
},
Err(_) => (),
}
}
Expand Down Expand Up @@ -1965,6 +1992,7 @@ where
// `actual <= to_change` and `to_change <= amount`; qed;
reserves[index].amount -= actual;

Self::deposit_event(Event::Slashed(who.clone(), actual));
(imb, value - actual)
},
Err(_) => (NegativeImbalance::zero(), value),
Expand Down
20 changes: 19 additions & 1 deletion frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ macro_rules! decl_tests {
<$ext_builder>::default().monied(true).build().execute_with(|| {
assert_eq!(Balances::total_balance(&1), 10);
assert_ok!(Balances::deposit_into_existing(&1, 10).map(drop));
System::assert_last_event(Event::Balances(crate::Event::Deposit(1, 10)));
assert_eq!(Balances::total_balance(&1), 20);
assert_eq!(<TotalIssuance<$test>>::get(), 120);
});
Expand Down Expand Up @@ -341,6 +342,7 @@ macro_rules! decl_tests {
fn balance_works() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 42);
System::assert_has_event(Event::Balances(crate::Event::Deposit(1, 42)));
assert_eq!(Balances::free_balance(1), 42);
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::total_balance(&1), 42);
Expand Down Expand Up @@ -435,6 +437,19 @@ macro_rules! decl_tests {
});
}

#[test]
fn withdrawing_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&2, 111);
let _ = Balances::withdraw(
&2, 11, WithdrawReasons::TRANSFER, ExistenceRequirement::KeepAlive
);
System::assert_last_event(Event::Balances(crate::Event::Withdraw(2, 11)));
assert_eq!(Balances::free_balance(2), 100);
assert_eq!(<TotalIssuance<$test>>::get(), 100);
});
}

#[test]
fn slashing_incomplete_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
Expand Down Expand Up @@ -749,6 +764,7 @@ macro_rules! decl_tests {
[
Event::System(system::Event::KilledAccount(1)),
Event::Balances(crate::Event::DustLost(1, 99)),
Event::Balances(crate::Event::Slashed(1, 1)),
]
);
});
Expand Down Expand Up @@ -777,7 +793,8 @@ macro_rules! decl_tests {
assert_eq!(
events(),
[
Event::System(system::Event::KilledAccount(1))
Event::System(system::Event::KilledAccount(1)),
Event::Balances(crate::Event::Slashed(1, 100)),
]
);
});
Expand All @@ -797,6 +814,7 @@ macro_rules! decl_tests {
assert_eq!(Balances::slash(&1, 900), (NegativeImbalance::new(900), 0));
// Account is still alive
assert!(System::account_exists(&1));
System::assert_last_event(Event::Balances(crate::Event::Slashed(1, 900)));

// SCENARIO: Slash will kill account because not enough balance left.
assert_ok!(Balances::set_balance(Origin::root(), 1, 1_000, 0));
Expand Down
3 changes: 2 additions & 1 deletion frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() {
assert_eq!(res, (NegativeImbalance::new(98), 0));

// no events
assert_eq!(events(), []);
assert_eq!(events(), [Event::Balances(crate::Event::Slashed(1, 98))]);

let res = Balances::slash(&1, 1);
assert_eq!(res, (NegativeImbalance::new(1), 0));
Expand All @@ -183,6 +183,7 @@ fn emit_events_with_no_existential_deposit_suicide_with_dust() {
[
Event::System(system::Event::KilledAccount(1)),
Event::Balances(crate::Event::DustLost(1, 1)),
Event::Balances(crate::Event::Slashed(1, 1)),
]
);
});
Expand Down
15 changes: 7 additions & 8 deletions frame/balances/src/tests_reentrancy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ fn transfer_dust_removal_tst1_should_work() {
assert_eq!(Balances::free_balance(&1), 1050);

// Verify the events
// Number of events expected is 8
assert_eq!(System::events().len(), 11);
assert_eq!(System::events().len(), 12);

System::assert_has_event(Event::Balances(crate::Event::Transfer(2, 3, 450)));
System::assert_has_event(Event::Balances(crate::Event::DustLost(2, 50)));
System::assert_has_event(Event::Balances(crate::Event::Deposit(1, 50)));
});
}

Expand All @@ -195,11 +195,11 @@ fn transfer_dust_removal_tst2_should_work() {
assert_eq!(Balances::free_balance(&1), 1500);

// Verify the events
// Number of events expected is 8
assert_eq!(System::events().len(), 9);
assert_eq!(System::events().len(), 10);

System::assert_has_event(Event::Balances(crate::Event::Transfer(2, 1, 450)));
System::assert_has_event(Event::Balances(crate::Event::DustLost(2, 50)));
System::assert_has_event(Event::Balances(crate::Event::Deposit(1, 50)));
});
}

Expand Down Expand Up @@ -232,16 +232,15 @@ fn repatriating_reserved_balance_dust_removal_should_work() {
assert_eq!(Balances::free_balance(1), 1500);

// Verify the events
// Number of events expected is 10
assert_eq!(System::events().len(), 10);
assert_eq!(System::events().len(), 11);

System::assert_has_event(Event::Balances(crate::Event::ReserveRepatriated(
2,
1,
450,
Status::Free,
)));

System::assert_last_event(Event::Balances(crate::Event::DustLost(2, 50)));
System::assert_has_event(Event::Balances(crate::Event::DustLost(2, 50)));
System::assert_last_event(Event::Balances(crate::Event::Deposit(1, 50)));
});
}
5 changes: 5 additions & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@ fn instantiate_and_call_and_deposit_event() {
assert_eq!(
System::events(),
vec![
EventRecord {
phase: Phase::Initialization,
event: Event::Balances(pallet_balances::Event::Deposit(ALICE, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::Initialization,
event: Event::System(frame_system::Event::NewAccount(ALICE.clone())),
Expand Down
Loading