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

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

Merged
merged 9 commits into from
Mar 24, 2023

Conversation

sea212
Copy link
Contributor

@sea212 sea212 commented Sep 16, 2022

Fixes #12276

This PR adds two events to the balances pallet, namely:

/// Some free balance was locked out from specific operations.
Locked { who: T::AccountId, amount: T::Balance, reason: Reasons },
/// Some balance that was locked out from specific operations was freed.
Unlocked { who: T::AccountId, amount: T::Balance, reason: Reasons },

Since locks in the same category of Reasons overlay each other, the design decision was made to include only the changes of total lock value in the amount fields of the respective events. Examples:

Locks = [10/MISC]          --> [15/MISC]              // emits Locked(..., amount=5, Reasons::Misc)
Locks = [15/MISC]          --> [15/MISC, 20/MISC]     // emits Locked(..., amount=5, Reasons::Misc)
Locks = [15/MISC, 20/MISC] --> [17/MISC, 20/MISC]     // emits nothing
Locks = [17/MISC, 20/MISC] --> [20/MISC]              // emits nothing
Locks = [20/MISC]          --> [10/MISC]              // emits Unlocked(..., amount=10, Reasons::Misc)
Locks = [10/MISC]          --> [10/MISC, 15/FEE]      // emits Locked(..., amount=15, Reasons::Fee)
Locks = [10/MISC, 15/FEE]  --> [20/MISC, 20/FEE]      // emits Locked(..., amount=10, Reasons::Misc) and
                                                      // emits Locked(..., amount=5, Reasons::Fee)
// etc.

There are also alternative approaches that could be applied:

  1. Add total_locked field to the respective events.
  2. Emit lock event that only contains total_locked instead of the amount that was added to or removed from the total lock value.

Some questions in regards to the actual changes in this PR:

  1. Currently Reasons::All is not used in the event. There is a corner case when a lock is set with Reasons::All that implies the same relative change of lock amounts for both Reasons::Misc and Reasons::Fee (all currently available reasons). In that case the event could emit Reasons::All. I decided against adding this because in terms of implementation effort this does not scale well at all. Instead, two distinct events are now emitted.
  2. The PR contains this logic: if a > b then a - b and if a < b then b - a. Is it okay to use non-saturating math here or is using saturating math only enforced meanwhile?

@cla-bot-2021
Copy link

cla-bot-2021 bot commented Sep 16, 2022

User @sea212, please sign the CLA here.

@sea212 sea212 marked this pull request as draft September 16, 2022 18:39
@sea212 sea212 marked this pull request as ready for review September 16, 2022 19:00
@kianenigma kianenigma added A0-please_review Pull request needs code review. B7-runtimenoteworthy C1-low PR touches the given topic and has a low impact on builders. D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Sep 21, 2022
@sea212 sea212 requested a review from ggwpez September 21, 2022 16:49
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

👍 (code-wise; don't know enough to reason about business logic)

Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

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

Putting it up for audit 👍

@ggwpez
Copy link
Member

ggwpez commented Jan 2, 2023

bot rebase

@paritytech-processbot
Copy link

Rebased


let deposit_lock_event = |prev: T::Balance, after: T::Balance, reason: Reasons| {
if prev < after {
let amount = after.saturating_sub(prev);
Copy link
Member

Choose a reason for hiding this comment

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

Is it okay to use non-saturating math here or is using saturating math only enforced meanwhile?

Would be okay here. But since you already have it - its doing no harm.

@ggwpez
Copy link
Member

ggwpez commented Jan 2, 2023

Emit lock event that only contains total_locked instead of the amount that was added to or removed from the total lock value.

I think both is fine. In both cases you either dont have the diff or not the total but putting both in the event could be done 🤷‍♂️

@louismerlin louismerlin added D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Jan 17, 2023
@the-right-joyce the-right-joyce added B1-note_worthy Changes should be noted in the release notes T1-runtime This PR/Issue is related to the topic “runtime”. and removed B7-runtimenoteworthy labels Feb 13, 2023
@bkchr
Copy link
Member

bkchr commented Mar 6, 2023

bot rebase

@paritytech-processbot
Copy link

Error: Command 'Command { std: "git" "push" "--porcelain" "sea212" "balances-add-lock-events", kill_on_drop: false }' failed with status Some(1); output: error: failed to push some refs to 'https://x-access-token:${SECRET}@github.com/sea212/substrate.git'

@bkchr
Copy link
Member

bkchr commented Mar 6, 2023

@sea212 please merge master.

@sea212 sea212 requested review from ggwpez, kianenigma and melekes and removed request for melekes, kianenigma and ggwpez March 24, 2023 16:07
@sea212
Copy link
Contributor Author

sea212 commented Mar 24, 2023

Does anyone know what is going on here in regards to the CI? If I run the command locally all tests pass. The changes in this PR are even not related to the errors that occur.

@bkchr @kianenigma @ggwpez @melekes

@paritytech-processbot
Copy link

Error: Statuses failed for d01e57b

@bkchr
Copy link
Member

bkchr commented Mar 24, 2023

bot rebase

@paritytech-processbot
Copy link

Rebased

@bkchr
Copy link
Member

bkchr commented Mar 24, 2023

bot merge

@paritytech-processbot
Copy link

Waiting for commit status.

@paritytech-processbot
Copy link

Merge cancelled due to error. Error: Statuses failed for 2e8c08d

@bkchr bkchr merged commit ea72382 into paritytech:master Mar 24, 2023
@paritytech-cicd-pr
Copy link

The CI pipeline was cancelled due to failure one of the required jobs.
Job name: test-linux-stable
Logs: https://gitlab.parity.io/parity/mirrors/substrate/-/jobs/2589497

@ggwpez
Copy link
Member

ggwpez commented Mar 25, 2023

Master is red now, but there were internal CI problems. Will fix.

@sea212 sea212 deleted the balances-add-lock-events branch March 27, 2023 19:06
nathanwhit pushed a commit to nathanwhit/substrate that referenced this pull request Jul 19, 2023
…ytech#12287)

* Emit Locked/Unlocked events

* Implement lock event tests

* Adhere to style guide

* Use saturating math

Co-authored-by: Kian Paimani <[email protected]>

* Fix typo

* Emit event on change locks and add tests

* Adjust event docstring

---------

Co-authored-by: Kian Paimani <[email protected]>
Co-authored-by: parity-processbot <>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B1-note_worthy Changes should be noted in the release notes C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited T1-runtime This PR/Issue is related to the topic “runtime”.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[Feature Request] Add Lock and Unlock events to pallet balances
8 participants