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

refactor: remove usage of global basedenom #18268

Merged
merged 8 commits into from
Oct 30, 2023

Conversation

tac0turtle
Copy link
Member

@tac0turtle tac0turtle commented Oct 26, 2023

Description

this was brought to my attention by celestia team, i moved to deprecate the global as its not used any longer


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
  • added ! to 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
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • run make lint and make test
  • 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.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Summary by CodeRabbit

Refactor:

  • Deprecated several functions and variables in types/denom.go to encourage the use of BaseDenom from the staking module.
  • Simplified the logic for determining the baseDenom value in x/distribution/keeper/delegation.go for improved code readability.

New Features:

  • Added a new method BondDenom() to the StakingKeeper interface in x/distribution/types/expected_keepers.go.
  • Introduced new functions in types/coin.go for coin normalization and conversion.

Tests:

  • Updated test cases in x/distribution/keeper/delegation_test.go to include expectations for the new BondDenom function.

Chores:

  • Adjusted import statements and function signatures across multiple files to resolve import and type conflicts.

Documentation:

  • Updated CHANGELOG.md to reflect changes in the "types" module and other modifications.

@tac0turtle tac0turtle marked this pull request as ready for review October 26, 2023 08:48
@tac0turtle tac0turtle requested a review from a team as a code owner October 26, 2023 08:48
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 26, 2023

Walkthrough

The codebase underwent a significant refactor, primarily focusing on deprecating certain functions and variables in favor of using the BaseDenom from the staking module. Additionally, import statements and function signatures were updated across multiple files to resolve conflicts and improve code readability. New functions were introduced to normalize and truncate decimal coins.

Changes

File(s) Summary
types/denom.go, x/distribution/keeper/delegation.go, CHANGELOG.md Deprecated existing functions and variables in favor of using BaseDenom from the staking module. Simplified the logic for determining the baseDenom value.
x/distribution/testutil/expected_keepers_mocks.go, x/evidence/testutil/expected_keepers_mocks.go, x/gov/testutil/expected_keepers_mocks.go, x/slashing/testutil/expected_keepers_mocks.go, x/staking/testutil/expected_keepers_mocks.go Updated import statements and function signatures to resolve conflicts and improve code readability.
x/distribution/types/expected_keepers.go Modified the StakingKeeper interface to include a new method BondDenom().
x/distribution/keeper/delegation_test.go Added an expectation for the BondDenom function in the stakingKeeper mock in the Test100PercentCommissionReward test function.
types/coin.go Introduced new functions to normalize and truncate decimal coins.

🐇🍂 "As leaves fall and seasons change, so does our code rearrange. 🍁🍃
Deprecated old, in with the new, making our codebase feel brand new. 🎃👻
With each commit, we refine, our codebase like a fine wine. 🍷🌙
So here's to changes, big and small, CodeRabbit celebrates them all!" 🎉🥕


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 793ca9a and 66b74db.
Files selected for processing (8)
  • types/denom.go (4} hunks)
  • x/distribution/keeper/delegation.go (1} hunks)
  • x/distribution/testutil/expected_keepers_mocks.go (26} hunks)
  • x/distribution/types/expected_keepers.go (1} hunks)
  • x/evidence/testutil/expected_keepers_mocks.go (14} hunks)
  • x/gov/testutil/expected_keepers_mocks.go (62} hunks)
  • x/slashing/testutil/expected_keepers_mocks.go (27} hunks)
  • x/staking/testutil/expected_keepers_mocks.go (37} hunks)
Files skipped from review due to trivial changes (5)
  • x/distribution/keeper/delegation.go
  • x/distribution/testutil/expected_keepers_mocks.go
  • x/distribution/types/expected_keepers.go
  • x/gov/testutil/expected_keepers_mocks.go
  • x/slashing/testutil/expected_keepers_mocks.go
Additional comments: 38
types/denom.go (4)
  • 14-19: The baseDenom variable and RegisterDenom function have been deprecated and the recommendation is to use BaseDenom from the staking module instead. Ensure that all calls to these throughout the codebase have been updated to match the new usage. Also, consider removing these deprecated items in a future version to avoid confusion and maintain clean code.

  • 36-42: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [39-43]

The GetDenomUnit function has been deprecated and the recommendation is to use BaseDenom from the staking module instead. Ensure that all calls to this function throughout the codebase have been updated to match the new usage. Also, consider removing this deprecated function in a future version to avoid confusion and maintain clean code.

  • 52-58: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [55-59]

The SetBaseDenom function has been deprecated and the recommendation is to use BaseDenom from the staking module instead. Ensure that all calls to this function throughout the codebase have been updated to match the new usage. Also, consider removing this deprecated function in a future version to avoid confusion and maintain clean code.

  • 63-69: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [66-70]

The GetBaseDenom function has been deprecated and the recommendation is to use BaseDenom from the staking module instead. Ensure that all calls to this function throughout the codebase have been updated to match the new usage. Also, consider removing this deprecated function in a future version to avoid confusion and maintain clean code.

x/staking/testutil/expected_keepers_mocks.go (20)
  • 14-16: The import paths for types and types0 have been swapped. Ensure that this change is intentional and that it does not break the existing functionality. Also, verify that the new import paths are correct and the packages exist at those locations.

  • 58-61: The function GetAccount now uses types0.AccAddress and types0.AccountI instead of types.AccAddress and types.AccountI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 72-75: The function GetModuleAccount now uses types0.ModuleAccountI instead of types.ModuleAccountI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 86-89: The function GetModuleAddress now uses types0.AccAddress instead of types.AccAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 100-100: The function IterateAccounts now uses types0.AccountI instead of types.AccountI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 112-112: The function SetModuleAccount now uses types0.ModuleAccountI instead of types.ModuleAccountI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 147-147: The function BurnCoins now uses types0.Coins instead of types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 161-161: The function DelegateCoinsFromAccountToModule now uses types0.AccAddress and types0.Coins instead of types.AccAddress and types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 175-178: The function GetAllBalances now uses types0.AccAddress and types0.Coins instead of types.AccAddress and types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 189-192: The function GetBalance now uses types0.AccAddress and types0.Coin instead of types.AccAddress and types.Coin. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 203-206: The function GetSupply now uses types0.Coin instead of types.Coin. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 217-220: The function LockedCoins now uses types0.AccAddress and types0.Coins instead of types.AccAddress and types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 231-231: The function SendCoinsFromModuleToModule now uses types0.Coins instead of types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 245-248: The function SpendableCoins now uses types0.AccAddress and types0.Coins instead of types.AccAddress and types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 259-259: The function UndelegateCoinsFromModuleToAccount now uses types0.AccAddress and types0.Coins instead of types.AccAddress and types.Coins. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 296-299: The function Delegation now uses types0.AccAddress and types0.ValAddress instead of types.AccAddress and types.ValAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 311-311: The function GetPubKeyByConsAddr now uses types0.ConsAddress instead of types.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 326-326: The function IterateBondedValidatorsByPower now uses types.ValidatorI instead of types0.ValidatorI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 340-340: The function IterateValidators now uses types.ValidatorI instead of types0.ValidatorI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 354-354: The function Jail now uses types0.ConsAddress instead of types.ConsAddress. Ensure that

x/evidence/testutil/expected_keepers_mocks.go (14)
  • 15-17: The import paths have been updated to reflect the new package paths. Ensure that these changes are consistent across the entire codebase and that the new paths are correct.

  • 59-62: The function signature for ValidatorByConsAddr has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 97-100: The function signature for GetPubkey has been updated. The type of arg1 has been changed from types.Address to types0.Address. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 112-113: The function signature for HasValidatorSigningInfo has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 126-127: The function signature for IsTombstoned has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 140-141: The function signature for Jail has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 154-155: The function signature for JailUntil has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 168-169: The function signature for Slash has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 197-198: The function signature for SlashWithInfractionReason has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 211-212: The function signature for Tombstone has been updated. The type of arg1 has been changed from types0.ConsAddress to types1.ConsAddress. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 248-249: The function signature for SetAccount has been updated. The type of acc has been changed from types0.AccountI to types1.AccountI. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 283-286: The function signature for GetAllBalances has been updated. The types of ctx and addr have been changed from types0.Context and types0.AccAddress to types1.Context and types1.AccAddress respectively. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 297-298: The function signature for MintCoins has been updated. The types of ctx and amt have been changed from types0.Context and types0.Coins to types1.Context and types1.Coins respectively. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 311-312: The function signature for SendCoinsFromModuleToAccount has been updated. The types of ctx, recipientAddr and amt have been changed from types0.Context, types0.AccAddress and types0.Coins to types1.Context, types1.AccAddress and types1.Coins respectively. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

@tac0turtle
Copy link
Member Author

should we just remove instead of deprecating as its something that should not be used what so ever

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 66b74db and 5f1db5a.
Files selected for processing (4)
  • x/distribution/keeper/delegation.go (1} hunks)
  • x/distribution/keeper/delegation_test.go (1} hunks)
  • x/distribution/testutil/expected_keepers_mocks.go (26} hunks)
  • x/distribution/types/expected_keepers.go (1} hunks)
Files skipped from review due to trivial changes (3)
  • x/distribution/keeper/delegation_test.go
  • x/distribution/testutil/expected_keepers_mocks.go
  • x/distribution/types/expected_keepers.go
Additional comments: 1
x/distribution/keeper/delegation.go (1)
  • 304-306: The new code retrieves the baseDenom value directly from the stakingKeeper instead of using the global sdk.GetBaseDenom() function. This is a good practice as it reduces global state and makes the code more modular and easier to test. However, the old code had a fallback to sdk.DefaultBondDenom if baseDenom was an empty string. If there's a possibility that k.stakingKeeper.BondDenom(ctx) could return an empty string, you might want to add a similar fallback mechanism to ensure the function behaves as expected in all scenarios.
+		baseDenom, err := k.stakingKeeper.BondDenom(ctx)
+		if err != nil {
+			return nil, err
+		}
+		if baseDenom == "" {
+			baseDenom = sdk.DefaultBondDenom
+		}
Committable suggestion (Beta)
		baseDenom, err := k.stakingKeeper.BondDenom(ctx)
		if err != nil {
			return nil, err
		}
		if baseDenom == "" {
			baseDenom = sdk.DefaultBondDenom
		}

@julienrbrt
Copy link
Member

julienrbrt commented Oct 26, 2023

Can we get a changelog?
If we don't backport we can already deprecate it in v0.50.

@tac0turtle
Copy link
Member Author

Can we get a changelog? If we don't backport we can already deprecate it in v0.50.

ill make a pr to 0.50 on deprecation

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5f1db5a and 835146b.
Files selected for processing (2)
  • CHANGELOG.md (1} hunks)
  • types/coin.go (2} hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
Additional comments: 3
types/coin.go (3)
  • 903-974: The new functions normalizeDecCoin, NormalizeCoins, convertDecCoin, and getDenomUnit are added for coin normalization and conversion. These functions are used to convert coins to their smallest registered unit. If the conversion fails, the original coin is returned. The denomUnits map is used to store the unit multipliers for each denomination. The getDenomUnit function retrieves the unit multiplier for a given denomination. If the denomination is not registered, it returns zero and false. These changes improve the flexibility of coin handling by allowing coins to be converted to different denominations.

  • 932-955: The function convertDecCoin attempts to convert a decimal coin to a given denomination. If the given denomination is invalid or if neither denomination is registered, an error is returned. This function improves the flexibility of coin handling by allowing coins to be converted to different denominations.

  • 958-974: The denomUnits map and the getDenomUnit function are used to store and retrieve the unit multipliers for each denomination. If the denomination is not registered, getDenomUnit returns zero and false. This allows for flexible handling of different coin denominations.

types/coin.go Outdated
Comment on lines 907 to 915
// NormalizeDecCoin try to convert a decimal coin to the smallest unit registered,
// returns original one if failed.
func normalizeDecCoin(coin DecCoin) DecCoin {
newCoin, err := convertDecCoin(coin, coin.Denom)
if err != nil {
return coin
}
return newCoin
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The function normalizeDecCoin tries to convert a decimal coin to the smallest unit registered and returns the original coin if the conversion fails. However, it's not clear what the failure conditions are. It would be helpful to document under what conditions the conversion might fail.

types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated

srcUnit, ok := getDenomUnit(coin.Denom)
if !ok {
return DecCoin{}, fmt.Errorf("source denom not registered: %s", coin.Denom)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit, please quote the value with %q

types/coin.go Outdated

// denomUnits contains a mapping of denomination mapped to their respective unit
// multipliers (e.g. 1atom = 10^-6uatom).
var denomUnits = map[string]math.LegacyDec{}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where is denomUnits being set? I only see it being used to lookup

Copy link
Member Author

Choose a reason for hiding this comment

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

thank you, it was related to the global, it is not needed anymore

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 835146b and eb43914.
Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • x/distribution/keeper/delegation_test.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • x/distribution/keeper/delegation_test.go

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between eb43914 and c4940a0.
Files selected for processing (1)
  • types/coin.go (2 hunks)

types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated
Comment on lines 908 to 916
// normalizeDecCoin try to convert a decimal coin to the smallest unit registered,
// returns original one if failed.
func normalizeDecCoin(coin DecCoin) DecCoin {
newCoin, err := convertDecCoin(coin)
if err != nil {
return coin
}
return newCoin
}
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 30, 2023

Choose a reason for hiding this comment

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

The function normalizeDecCoin is marked as being used for testing purposes, but it's being used in production code. If it's meant to be used in production code, the comment should be updated to reflect this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tac0turtle kindly check out this comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like humans are talking. I'll be quiet for now. If you need me again, just tag @coderabbitai in a new comment.

types/coin.go Outdated
Comment on lines 936 to 942
func convertDecCoin(coin DecCoin) (DecCoin, error) {
if err := ValidateDenom(coin.Denom); err != nil {
return DecCoin{}, err
}

return NewDecCoinFromDec(coin.Denom, coin.Amount), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The function convertDecCoin doesn't seem to be doing any conversion. It's just creating a new DecCoin with the same denomination and amount as the input coin. If there's no conversion happening, consider renaming the function to better reflect its purpose.

Copy link
Collaborator

@odeke-em odeke-em left a comment

Choose a reason for hiding this comment

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

Thanks @tac0turtle, LGTM!

types/coin.go Outdated Show resolved Hide resolved
types/coin.go Outdated
Comment on lines 908 to 916
// normalizeDecCoin try to convert a decimal coin to the smallest unit registered,
// returns original one if failed.
func normalizeDecCoin(coin DecCoin) DecCoin {
newCoin, err := convertDecCoin(coin)
if err != nil {
return coin
}
return newCoin
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

@tac0turtle kindly check out this comment.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c4940a0 and dd5957e.
Files selected for processing (1)
  • types/coin.go (2 hunks)

Comment on lines +884 to +885
coin, _ = NewDecCoinFromDec(decCoin.Denom, decCoin.Amount).TruncateDecimal()

Copy link
Contributor

Choose a reason for hiding this comment

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

The error returned by NewDecCoinFromDec().TruncateDecimal() is ignored. It's a good practice to handle all errors to prevent unexpected behavior. Consider adding error handling.

- coin, _ = NewDecCoinFromDec(decCoin.Denom, decCoin.Amount).TruncateDecimal()
+ coin, err = NewDecCoinFromDec(decCoin.Denom, decCoin.Amount).TruncateDecimal()
+ if err != nil {
+     // handle error
+ }

Commitable suggestion (Beta)
Suggested change
coin, _ = NewDecCoinFromDec(decCoin.Denom, decCoin.Amount).TruncateDecimal()
coin, err := NewDecCoinFromDec(decCoin.Denom, decCoin.Amount).TruncateDecimal()
if err != nil {
// handle error
}

Comment on lines +907 to +920
// NormalizeCoins normalize and truncate a list of decimal coins
func NormalizeCoins(coins []DecCoin) Coins {
if coins == nil {
return nil
}
result := make([]Coin, 0, len(coins))

for _, coin := range coins {
newCoin, _ := NewDecCoinFromDec(coin.Denom, coin.Amount).TruncateDecimal()
result = append(result, newCoin)
}

return result
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The function NormalizeCoins is well implemented. It checks if the input is nil and returns nil in that case. It also preallocates the slice result with the length of coins which is a good practice for performance. However, similar to the previous comment, the error returned by NewDecCoinFromDec().TruncateDecimal() is ignored. Consider adding error handling.

- newCoin, _ := NewDecCoinFromDec(coin.Denom, coin.Amount).TruncateDecimal()
+ newCoin, err := NewDecCoinFromDec(coin.Denom, coin.Amount).TruncateDecimal()
+ if err != nil {
+     // handle error
+ }

Commitable suggestion (Beta)
Suggested change
// NormalizeCoins normalize and truncate a list of decimal coins
func NormalizeCoins(coins []DecCoin) Coins {
if coins == nil {
return nil
}
result := make([]Coin, 0, len(coins))
for _, coin := range coins {
newCoin, _ := NewDecCoinFromDec(coin.Denom, coin.Amount).TruncateDecimal()
result = append(result, newCoin)
}
return result
}
// NormalizeCoins normalize and truncate a list of decimal coins
func NormalizeCoins(coins []DecCoin) Coins {
if coins == nil {
return nil
}
result := make([]Coin, 0, len(coins))
for _, coin := range coins {
newCoin, err := NewDecCoinFromDec(coin.Denom, coin.Amount).TruncateDecimal()
if err != nil {
// handle error
}
result = append(result, newCoin)
}
return result
}

@sonarcloud
Copy link

sonarcloud bot commented Oct 30, 2023

[Cosmos SDK - Distribution] Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@tac0turtle tac0turtle added this pull request to the merge queue Oct 30, 2023
Merged via the queue into main with commit e6a4c78 Oct 30, 2023
62 checks passed
@tac0turtle tac0turtle deleted the marko/remove_bonddenom_global branch October 30, 2023 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/distribution distribution module related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants