Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pallet-balances] burn_allow_death extrinsic #3964

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
d1fadb2
Init commit
Dinonard Apr 3, 2024
e9c5ee5
Newline
Dinonard Apr 3, 2024
cc8641d
Modify interface
Dinonard Apr 3, 2024
fbfcb62
Polish
Dinonard Apr 3, 2024
f1db75e
Sort out missing weight functions
Dinonard Apr 3, 2024
0094a2c
CI fixes
Dinonard Apr 3, 2024
b8f4f48
More fixes
Dinonard Apr 3, 2024
d9493fe
Fix prdoc
Dinonard Apr 3, 2024
2c7c067
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard Apr 8, 2024
58268e4
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard Apr 12, 2024
56d3379
Update cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weight…
Dinonard Apr 30, 2024
45d191f
Update substrate/frame/balances/src/benchmarking.rs
Dinonard Apr 30, 2024
d8ade46
Review comments
Dinonard Apr 30, 2024
41d70ef
Merge remote-tracking branch 'origin/dinonard/pallet-balances-burn-ex…
Dinonard Apr 30, 2024
293ee0f
Formatting
Dinonard Apr 30, 2024
79db74f
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard Apr 30, 2024
1b48ad5
Comment
Dinonard Apr 30, 2024
9b6c3ce
Merge remote-tracking branch 'origin/dinonard/pallet-balances-burn-ex…
Dinonard Apr 30, 2024
6979ffa
Add Preservation to asset-conversion-ops
Dinonard Apr 30, 2024
21077a8
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
May 3, 2024
1617050
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 3, 2024
5657da1
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard May 3, 2024
3d48ae6
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard May 6, 2024
ac976b8
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
1a07ca2
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
May 6, 2024
976c936
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
ba662db
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
11a0475
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
eb29289
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
16f342a
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard May 6, 2024
a42db38
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
eb2f431
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
53e5112
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
fa0a300
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
051674d
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
27618af
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
f10a8eb
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
49b2ba7
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
d6d9936
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
851e629
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
190ca18
".git/.scripts/commands/bench-all/bench-all.sh" --pallet=pallet_balances
May 6, 2024
be3e92f
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard May 7, 2024
24c6422
Merge branch 'master' into dinonard/pallet-balances-burn-extrinsics
Dinonard May 8, 2024
04f75af
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
May 8, 2024
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
19 changes: 19 additions & 0 deletions substrate/frame/balances/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,25 @@ mod benchmarks {
assert_eq!(Balances::<T, I>::total_issuance(), ti + delta);
}

// Benchmark `burn_allow_death` extrinsic with the worst possible condition - burn kills the account.
#[benchmark]
fn burn_allow_death() {
let existential_deposit = T::ExistentialDeposit::get();
let caller = whitelisted_caller();

// Give some multiple of the existential deposit
let balance = existential_deposit.saturating_mul(ED_MULTIPLIER.into());
let _ = <Balances<T, I> as Currency<_>>::make_free_balance_be(&caller, balance);

// Burn enough to kill the account.
let burn_amount = balance - existential_deposit + 1u32.into();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), burn_amount);

assert_eq!(Balances::<T, I>::free_balance(&caller), Zero::zero());
}

impl_benchmark_test_suite! {
Balances,
crate::tests::ExtBuilder::default().build(),
Expand Down
15 changes: 15 additions & 0 deletions substrate/frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,21 @@ pub mod pallet {

Ok(())
}

/// Burn the specified liquid free balance from the origin account.
///
/// If the origin's account ends up below the existential deposit as a result
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
/// of the burn, the account will be reaped.
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::burn_allow_death())]
pub fn burn_allow_death(
origin: OriginFor<T>,
#[pallet::compact] value: T::Balance,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An additional call is overkill, but a keep_alive: bool similar to thetransfer_all would be fine i think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind doing that, but will need to change this function signature:
https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/support/src/traits/tokens/fungible/regular.rs#L260

It might end being a slightly lager change, hope that's ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated per request.

Will need help/suggestion with the remaining TODOs in the PR summary:

  • running benchmarks & updating weights (I cannot trigger the job)
  • check whether prdoc is ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you said in another PR that current review process isn't sustainable (I assume due to human resources), but is there someone else we can ask to take a look at the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggwpez sorry for the tag but could you please give a timeline when this can be checked out? Can we get someone else to review it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it is best to ask in chat for review. We have the open #fellowship-open-channel:parity.io channel for that. I invited you.
Otherwise it can get lost or not have any priority...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

) -> DispatchResult {
let source = ensure_signed(origin)?;
<Self as fungible::Mutate<_>>::burn_from(&source, value, Precision::Exact, Polite)?;
Ok(())
}
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
Expand Down
36 changes: 36 additions & 0 deletions substrate/frame/balances/src/tests/dispatchable_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,39 @@ fn force_adjust_total_issuance_rejects_more_than_inactive() {
assert_eq!(Balances::active_issuance(), 10);
});
}

#[test]
fn burn_allow_death_works() {
ExtBuilder::default().build().execute_with(|| {
// Prepare account with initial balance
let (account, init_balance) = (1, 37);
assert_ok!(Balances::force_set_balance(RuntimeOrigin::root(), account, init_balance));
let init_issuance = Balances::total_issuance();

// Cannot burn more than what's available
assert_noop!(
Balances::burn_allow_death(Some(account).into(), init_balance + 1),
TokenError::FundsUnavailable,
);

// Burn some funds, without reaping the account
let burn_amount_1 = 1;
assert_ok!(Balances::burn_allow_death(Some(account).into(), burn_amount_1));
System::assert_last_event(RuntimeEvent::Balances(Event::Burned {
who: account.clone(),
amount: burn_amount_1,
}));
assert_eq!(Balances::total_issuance(), init_issuance - burn_amount_1);
assert_eq!(Balances::total_balance(&account), init_balance - burn_amount_1);

// Burn some more funds, this time reaping the account
let burn_amount_2 = init_balance - burn_amount_1 - <Test as Config>::ExistentialDeposit::get() + 1;
assert_ok!(Balances::burn_allow_death(Some(account).into(), burn_amount_2));
System::assert_last_event(RuntimeEvent::Balances(Event::Burned {
who: account.clone(),
amount: burn_amount_2,
}));
assert_eq!(Balances::total_issuance(), init_issuance - burn_amount_1 - burn_amount_2);
assert!(Balances::total_balance(&account).is_zero());
});
}
19 changes: 19 additions & 0 deletions substrate/frame/balances/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading