This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fungible conformance tests: Unbalanced and Balanced #14655
Open
liamaharon
wants to merge
12
commits into
master
Choose a base branch
from
liam-remaining-fungible-conformance-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 tasks
liamaharon
added
A3-in_progress
Pull request is in progress. No review needed at this stage.
C3-medium
PR touches the given topic and has a medium impact on builders.
D3-trivial 🧸
PR contains trivial changes in a runtime directory that do not require an audit
B1-note_worthy
Changes should be noted in the release notes
T1-runtime
This PR/Issue is related to the topic “runtime”.
D5-nicetohaveaudit ⚠️
PR contains trivial changes to logic that should be properly reviewed.
and removed
D3-trivial 🧸
PR contains trivial changes in a runtime directory that do not require an audit
labels
Jul 27, 2023
…maining-fungible-conformance-tests
liamaharon
added
A0-please_review
Pull request needs code review.
and removed
A3-in_progress
Pull request is in progress. No review needed at this stage.
labels
Jul 31, 2023
Merged
9 tasks
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
C3-medium
PR touches the given topic and has a medium impact on builders.
D5-nicetohaveaudit ⚠️
PR contains trivial changes to logic that should be properly reviewed.
T1-runtime
This PR/Issue is related to the topic “runtime”.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Partial paritytech/polkadot-sdk#225
Unbalanced
Balanced
fungible
default implementations and theBalances
palletUnbalanced::decrease_balance
can reap account whenPreservation == Preserve
Balanced::pair
can return pairs of imbalances which do not cancel each other outBalances
palletactive_issuance
'underflow'regular.rs
go into a test file namedregular.rs
, tests for traits infreezes.rs
go into a test file namedfreezes.rs
, etc.Fixes
Unbalanced::decrease_balance
can reap account when called withPreservation::Preserve
There is a potential issue in the default implementation of
Unbalanced::decrease_balance
. The implementation can delete an account even when it is called withpreservation: Preservation::Preserve
. This seems to contradict the documentation ofPreservation::Preserve
:I updated
Unbalanced::decrease_balance
to returnErr(TokenError::BelowMinimum)
when a withdrawal would cause the account to be reaped andpreservation: Preservation::Preserve
.Test for this behavior:
substrate/frame/support/src/traits/tokens/fungible/conformance_tests/regular.rs
Lines 935 to 961 in ae4a2c4
Balanced::pair
returning non-canceling pairsBalanced::pair
is supposed to create a pair of imbalances that cancel each other out. However this is not the case when the method is called with an amount greater than the total supply.In the existing default implementation,
Balanced::pair
creates a pair by first rescinding the balance, creatingDebt
, and then issuing the balance, creatingCredit
.When creating
Debt
, if theamount
to create exceeds thetotal_supply
,total_supply
units ofDebt
are created instead ofamount
units ofDebt
. This can lead to non-canceling amount ofCredit
andDebt
being created.To address this, I reversed the creation order of
Credit
andDebt
. NowCredit
is produced first, ensuring thetotal_supply
of the system will be >= the amount ofDebt
that will need to be created.Note: This change alters the
Balanced::pair
API, making it a breaking change. However the method is not used in Substrate (or likely any/many other places), and trivial to update, so I don't think the breakage is a concern.Test for this behavior:
substrate/frame/support/src/traits/tokens/fungible/conformance_tests/regular.rs
Lines 1348 to 1370 in 46db2ed
Balances
palletactive_issuance
'underflow'This PR resolves an issue in the
Balances
pallet that can lead to odd behavior ofactive_issuance
.Currently, the Balances pallet doesn't check if
InactiveIssuance
remains less than or equal toTotalIssuance
when supply is deactivated. This allowsInactiveIssuance
to be greater thanTotalIssuance
, which can result in unexpected behavior from the perspective of thefungible
API.active_issuance
is calculated withTotalIssuance.saturating_sub(InactiveIssuance)
.If an amount is deactivated that causes
InactiveIssuance
to become greaterTotalIssuance
,active_issuance
will return 0. However once in that state, reactivating an amount will not increaseactive_issuance
by the reactivated amount as expected.Consider this test where the last assertion would fail due to this issue:
substrate/frame/support/src/traits/tokens/fungible/conformance_tests/regular.rs
Lines 1061 to 1095 in 177d55e
To address this, I've tweaked the
deactivate
function to ensureInactiveIssuance
never surpassesTotalIssuance
.