Skip to content

fix(x/bank): Better handling of negative spendable balances #24060

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

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

hoangdv2429
Copy link
Contributor

Description

Closes: #23828
Backport


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

@aljo242
Copy link
Contributor

aljo242 commented Mar 19, 2025

@hoangdv2429 needs a changelog

@aljo242
Copy link
Contributor

aljo242 commented Mar 19, 2025

@hoangdv2429 looks like lint failing

@@ -194,7 +194,23 @@ func (k BaseViewKeeper) LockedCoins(ctx context.Context, addr sdk.AccAddress) sd
// by address. If the account has no spendable coins, an empty Coins slice is
// returned.
func (k BaseViewKeeper) SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins {
spendable, _ := k.spendableCoins(ctx, addr)
total := k.GetAllBalances(ctx, addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a comment to the function that describes exactly how this works?

Copy link
Contributor

Choose a reason for hiding this comment

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

i.e.

What we do when we have:

  • no locked coins
  • more locked than in balances
  • some locked, some unlocked

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 updated with specific flow

Copy link
Contributor

@aljo242 aljo242 left a comment

Choose a reason for hiding this comment

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

Are all of the new code paths tested in this PR?

@aljo242 aljo242 linked an issue Mar 20, 2025 that may be closed by this pull request
@@ -1473,12 +1473,27 @@ func (suite *KeeperTestSuite) TestMsgMultiSendEvents() {
require.Equal(abci.Event(event3), events[29])
}

// TestSpendableCoins verifies the behavior of SpendableCoins under various scenarios:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aljo242 I also added document on what testcases we have. I hope it cover every paths you need.

@aljo242 aljo242 requested a review from technicallyty March 24, 2025 18:13
Copy link
Contributor

@technicallyty technicallyty left a comment

Choose a reason for hiding this comment

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

code looks good to me. does this have any implications for smart contracts, or other modules that may query from bank?

@hoangdv2429
Copy link
Contributor Author

code looks good to me. does this have any implications for smart contracts, or other modules that may query from bank?

Now code will consider locked coins when returning balances (minus it). Smart contracts that interact with the bank module to check account balances or spending limits will now receive more accurate data, reflecting the actual spendable amounts. This change can influence the logic within smart contracts that perform balance checks, potentially preventing overspending or ensuring compliance with spending rules. For module, I don't think it alter any behavior.

@technicallyty
Copy link
Contributor

code looks good to me. does this have any implications for smart contracts, or other modules that may query from bank?

Now code will consider locked coins when returning balances (minus it). Smart contracts that interact with the bank module to check account balances or spending limits will now receive more accurate data, reflecting the actual spendable amounts. This change can influence the logic within smart contracts that perform balance checks, potentially preventing overspending or ensuring compliance with spending rules. For module, I don't think it alter any behavior.

makes sense. i think we are probably safe to merge here. wdyt @aljo242 ?

@aljo242
Copy link
Contributor

aljo242 commented Mar 26, 2025

Yeah this makes sense to me - this is "query breaking" - we should make note that it is in the changelog

@technicallyty technicallyty changed the base branch from release/v0.53.x to main April 1, 2025 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix handling of negative spendable balances
3 participants