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

feat(x/gov): limit deposited coins to accepted proposal denom #18189

Merged
merged 8 commits into from
Oct 20, 2023

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Oct 20, 2023

Description

A-10


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

  • New Feature: Enhanced the governance module by adding a validation step to ensure deposited coins match the proposal's deposit parameters, improving the proposal submission process.
  • Refactor: Optimized the Slash function to return early if there are zero tokens to burn, enhancing system efficiency.
  • Refactor: Improved the q gov proposer command to directly query a proposal, increasing command reliability.
  • Test: Expanded test coverage to handle invalid deposited coins, ensuring robust error handling.
  • Documentation: Updated the governance module's documentation to reflect the new requirements for proposal submissions and deposits.
  • Bug Fix: Fixed an issue where the validator and delegator addresses parsing was inefficiently placed within the redelegation loop.
  • Chore: Introduced a new error variable ErrInvalidDepositDenom for better error tracking.

@julienrbrt julienrbrt requested a review from a team as a code owner October 20, 2023 11:11
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 20, 2023

Walkthrough

This update brings significant improvements to the governance module, enhancing the handling of proposals and deposits. It introduces validation for deposit denominations, optimizes the Slash function in the staking/keeper module, and refines the q gov proposer command. The changes also include additional test cases and error handling improvements.

Changes

File Summary
CHANGELOG.md Details improvements to the governance module, staking/keeper module, and command-line interface.
x/gov/keeper/deposit.go Introduces validation for deposit denominations, optimizes deposit handling, and refines function parameters.
x/gov/keeper/export_test.go Adds a new parameter to the ValidateInitialDeposit function for improved validation.
x/gov/keeper/msg_server.go Enhances the SubmitProposal function with parameter retrieval and validation.
x/gov/keeper/msg_server_test.go Adds test cases for invalid deposited coins and checks for the correct error messages.
x/gov/types/errors.go Adds a new error variable ErrInvalidDepositDenom for better error handling.
tests/integration/bank/app_test.go Updates the denomination of coins in the TestMsgSetSendEnabled function to align with the updated test setup.
x/gov/README.md Outlines the handling of MsgSubmitProposal, MsgDeposit, and MsgVote transactions and the emission of events by the governance module.

"In the land of code, where logic is king, 🐇
A rabbit hopped forth, changes to bring. 📝
With each line refined, and tests all green, ✅
A better codebase, the world has seen. 🌍
So let's hop together, to the future we steer, 🚀
With each update, the vision is clear!" 🌟


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.
  • Additionally, enter @coderabbitai pause to halt the bot's ongoing reviews as any issue or review comment. To
    resume reviews, enter @coderabbitai resume as any issue or review comment.

@julienrbrt julienrbrt added the backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release label Oct 20, 2023
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: 5

Commits Files that changed from the base of the PR and between 7421783 and edffa7c.
Files selected for processing (6)
  • CHANGELOG.md (1 hunks)
  • x/gov/keeper/deposit.go (4 hunks)
  • x/gov/keeper/export_test.go (1 hunks)
  • x/gov/keeper/msg_server.go (1 hunks)
  • x/gov/keeper/msg_server_test.go (2 hunks)
  • x/gov/types/errors.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • x/gov/types/errors.go
Additional comments (Suppressed): 8
x/gov/keeper/export_test.go (1)
  • 13-13: The function signature of validateInitialDeposit seems to have changed. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.
x/gov/keeper/msg_server.go (2)
  • 83-85: The validateInitialDeposit function now takes an additional params argument. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 87-89: The validateDepositDenom function is a new addition and it's good to see that error handling is properly done here. However, it would be helpful to add a comment explaining what this function does and why it's necessary.

x/gov/keeper/deposit.go (2)
  • 96-102: The retrieval of governance parameters has been removed from this section of the code. Ensure that the parameters are being retrieved before this check and are up-to-date. If the parameters are not retrieved or are outdated, the check for transitioning the proposal into the voting period might not work as expected.

  • 243-249: The function validateInitialDeposit now takes params as an argument instead of retrieving it within the function. This is a good practice as it reduces the number of database or network calls, improving performance. However, ensure that the params passed to this function are up-to-date and valid.

x/gov/keeper/msg_server_test.go (3)
  • 210-224: The test case "invalid deposited coin" checks if the system correctly handles a deposit with an invalid coin denomination. The expected error message indicates that the deposited coin's denomination is not accepted by the governance module. This is a good test case to ensure that the system correctly validates the deposited coins.

  • 225-239: The test case "invalid deposited coin (multiple)" checks if the system correctly handles a deposit with multiple coins, one of which has an invalid denomination. The expected error message indicates that one of the deposited coins' denomination is not accepted by the governance module. This is a good test case to ensure that the system correctly validates each coin in a deposit with multiple coins.

  • 825-842: The test cases "invalid deposited coin" and "invalid deposited coin (multiple)" in the TestMsgDeposit function are similar to the test cases in the TestMsgSubmitProposal function. They check if the system correctly handles a deposit with an invalid coin denomination and a deposit with multiple coins, one of which has an invalid denomination. The expected error messages are similar to the ones in the TestMsgSubmitProposal function. These are good test cases to ensure that the system correctly validates the deposited coins in the MsgDeposit message handler.

CHANGELOG.md Outdated Show resolved Hide resolved
x/gov/keeper/export_test.go Show resolved Hide resolved
x/gov/keeper/msg_server.go Outdated Show resolved Hide resolved
x/gov/keeper/deposit.go Show resolved Hide resolved
Comment on lines 271 to 290
func (keeper Keeper) validateDepositDenom(ctx context.Context, params v1.Params, depositAmount sdk.Coins) error {
denoms := []string{}
acceptedDenoms := make(map[string]bool, len(params.MinDeposit))
for _, coin := range params.MinDeposit {
acceptedDenoms[coin.Denom] = true
denoms = append(denoms, coin.Denom)
}

if len(depositAmount) > len(params.MinDeposit) {
return errors.Wrapf(types.ErrInvalidDepositDenom, "deposited %s, but gov accepts only the following denom(s): %v", depositAmount, denoms)
}

for _, coin := range depositAmount {
if _, ok := acceptedDenoms[coin.Denom]; !ok {
return errors.Wrapf(types.ErrInvalidDepositDenom, "deposited %s, but gov accepts only the following denom(s): %v", coin, denoms)
}
}

return nil
}
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 20, 2023

Choose a reason for hiding this comment

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

The new function validateDepositDenom is introduced to validate the deposit denomination against the accepted denominations specified in the governance parameters. This is a good addition as it ensures that only valid denominations are accepted for deposit. However, the error message in line 280 and 285 could be more informative by specifying which denomination(s) are invalid.

- return errors.Wrapf(types.ErrInvalidDepositDenom, "deposited %s, but gov accepts only the following denom(s): %v", depositAmount, denoms)
+ return errors.Wrapf(types.ErrInvalidDepositDenom, "deposited %s, but gov accepts only the following denom(s): %v. Invalid denom(s): %v", depositAmount, denoms, invalidDenoms)

[USER_EXPERIENCE]

Copy link
Member Author

Choose a reason for hiding this comment

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

won't do, as it requires looping through the whole depositAmount, which could be very big.

Copy link
Contributor

Choose a reason for hiding this comment

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

@julienrbrt I understand your concern about the potential performance impact of looping through a large depositAmount. However, it's important to note that the loop is already present in the current implementation to validate each coin in the depositAmount against the acceptedDenoms.

The suggestion was to enhance the error message by including the invalid denominations, which would require storing these invalid denominations during the existing loop. The additional cost would be the storage of these invalid denominations, which is relatively small compared to the cost of the loop itself.

That being said, if the size of depositAmount is expected to be significantly large, and performance is a critical concern, it might be better to stick with the current implementation. The trade-off here is between a more informative error message and the potential performance impact.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh wow, you can respond. Amazing.
Yes, performance is a concern.

Copy link
Contributor

Choose a reason for hiding this comment

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

@julienrbrt I understand. Performance is indeed a critical aspect to consider, especially in systems handling large amounts of data. In this case, maintaining the current implementation without the additional storage and processing for invalid denominations is a reasonable decision. It's always important to balance the trade-offs between user experience and system performance. Thank you for your clarification.

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

Commits Files that changed from the base of the PR and between edffa7c and 39a4d9c.
Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Additional comments (Suppressed): 1
CHANGELOG.md (1)
  • 58-58: The change log entry correctly summarizes the changes made in the PR. It mentions the new feature of limiting the accepted deposit coins for a proposal to the minimum proposal deposit denoms.

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: 1

Commits Files that changed from the base of the PR and between 39a4d9c and 601a1f2.
Files selected for processing (2)
  • tests/integration/bank/app_test.go (1 hunks)
  • x/gov/keeper/msg_server.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/integration/bank/app_test.go
Additional comments (Suppressed): 2
x/gov/keeper/msg_server.go (2)
  • 83-85: The validation of the initial deposit is a good practice. It ensures that the deposit meets the necessary requirements before proceeding. This can prevent potential issues down the line.

  • 87-89: The addition of a validation step for the deposit denomination is a good practice. It ensures that the denomination of the deposit matches the accepted denominations specified in the governance parameters. This can prevent potential issues and confusion.

x/gov/keeper/msg_server.go Show resolved Hide resolved
x/gov/keeper/deposit.go Outdated Show resolved Hide resolved
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

Commits Files that changed from the base of the PR and between 601a1f2 and 11f1eaf.
Files selected for processing (2)
  • x/gov/keeper/deposit.go (4 hunks)
  • x/gov/keeper/msg_server_test.go (2 hunks)
Additional comments (Suppressed): 7
x/gov/keeper/msg_server_test.go (3)
  • 210-224: The test case "invalid deposited coin" is checking if the function correctly handles a scenario where an invalid coin is deposited. The expected error message is correctly defined. This test case is important to ensure that the function correctly validates the deposited coins against the accepted denominations.

  • 225-239: The test case "invalid deposited coin (multiple)" is checking if the function correctly handles a scenario where multiple coins are deposited, and one of them is invalid. The expected error message is correctly defined. This test case is important to ensure that the function correctly validates each deposited coin against the accepted denominations.

  • 825-842: The test cases "invalid deposited coin" and "invalid deposited coin (multiple)" are similar to the ones in the previous hunk. They are checking if the function correctly handles scenarios where an invalid coin or multiple coins with an invalid one are deposited. The expected error messages are correctly defined. These test cases are important to ensure that the function correctly validates the deposited coins against the accepted denominations.

x/gov/keeper/deposit.go (4)
  • 70-85: The new hunk introduces a check to validate if the coins to be deposited match the proposal's deposit parameters. This is a good addition as it ensures that only valid denominations are accepted for deposit. However, the retrieval of parameters from the keeper.Params could potentially be a performance bottleneck if it involves a database or network call. Consider passing the parameters as an argument to the function if they are already available in the calling context.
    [PERFORMANCE]

  • 96-102: The new hunk checks if the deposit has provided sufficient total funds to transition the proposal into the voting period. This is a good practice as it ensures that the proposal can only proceed to the voting period if it has met the minimum deposit requirement. However, the retrieval of parameters from the keeper.Params has been removed in this hunk. Ensure that the params variable is still correctly initialized before this point.

  • 243-249: The new hunk modifies the validateInitialDeposit function to take params as an argument instead of retrieving it within the function. This is a good practice as it reduces the number of potentially expensive operations. However, ensure that all calls to this function throughout the codebase have been updated to match the new signature.

  • 271-286: The new hunk introduces a function validateDepositDenom to validate the deposit denomination against the accepted denominations specified in the governance parameters. This is a good addition as it ensures that only valid denominations are accepted for deposit. However, the creation of the acceptedDenoms map and the denoms slice could potentially be a performance bottleneck if the number of denominations is large. Consider optimizing this by creating these data structures once and reusing them.

denoms = append(denoms, coin.Denom)
}

for _, coin := range depositAmount {
Copy link
Member

Choose a reason for hiding this comment

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

can we add a godoc and a mention in the readme about this feature.

If a user sends a valid token and invalid token the tx will be rejected, etc

Copy link
Member Author

Choose a reason for hiding this comment

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

Added 👍🏾

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

Commits Files that changed from the base of the PR and between 11f1eaf and fd6ef30.
Files selected for processing (3)
  • CHANGELOG.md (1 hunks)
  • x/gov/README.md (4 hunks)
  • x/gov/keeper/deposit.go (4 hunks)
Additional comments (Suppressed): 8
CHANGELOG.md (1)
  • 58-58: The changelog entry correctly summarizes the changes made in the PR, specifically the addition of a feature to limit the deposited coins for a proposal to the accepted denominations specified in the governance parameters.
x/gov/README.md (3)
  • 521-521: The new requirement for initialDeposit to conform to the accepted denom of the MinDeposit param is introduced. Ensure that this change is propagated throughout the codebase and any potential impacts are considered.

  • 535-542: The conditions for accepting a deposit have been updated to include a check for the deposited coins to conform to the accepted denom from the MinDeposit param. This is a significant change and should be thoroughly tested.

  • 554-556: The pseudocode for handling MsgDeposit transactions has been removed. Ensure that the logic is still correctly implemented in the actual code.

x/gov/keeper/deposit.go (4)
  • 73-85: The retrieval of parameters from keeper.Params could potentially be a performance bottleneck if it involves a database or network call. Consider passing the parameters as an argument to the function if they are already available in the calling context. This would reduce the number of potentially expensive operations and improve performance. However, if the parameters are not frequently changing, the current implementation is efficient.
    [PERFORMANCE]

  • 96-102: The new hunk introduces a check to validate if the deposit has provided sufficient total funds to transition the proposal into the voting period. This is a good addition as it ensures that only valid denominations are accepted for deposit. However, the retrieval of parameters from the keeper.Params could potentially be a performance bottleneck if it involves a database or network call. Consider passing the parameters as an argument to the function if they are already available in the calling context. This would reduce the number of potentially expensive operations and improve performance. However, if the parameters are not frequently changing, the current implementation is efficient.
    [PERFORMANCE]

  • 243-249: The new hunk introduces a check to validate if the initial deposit is greater than or equal to the minimum required at the time of proposal submission. This is a good addition as it ensures that only valid denominations are accepted for deposit. However, the retrieval of parameters from the keeper.Params could potentially be a performance bottleneck if it involves a database or network call. Consider passing the parameters as an argument to the function if they are already available in the calling context. This would reduce the number of potentially expensive operations and improve performance. However, if the parameters are not frequently changing, the current implementation is efficient.
    [PERFORMANCE]

  • 271-287: The new function validateDepositDenom is a good addition as it validates the denomination of the deposit against the accepted denominations specified in the governance parameters. This ensures that only valid denominations are accepted for deposit. The use of a map for acceptedDenoms is a good choice as it allows for efficient lookup of denominations. However, consider using a set data structure if available, as it would be more semantically appropriate for this use case.

@julienrbrt julienrbrt added this pull request to the merge queue Oct 20, 2023
Merged via the queue into main with commit 2fbc547 Oct 20, 2023
53 checks passed
@julienrbrt julienrbrt deleted the julien/gov-limit-deposit branch October 20, 2023 16:07
mergify bot pushed a commit that referenced this pull request Oct 20, 2023
(cherry picked from commit 2fbc547)

# Conflicts:
#	CHANGELOG.md
julienrbrt added a commit that referenced this pull request Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.50.x PR scheduled for inclusion in the v0.50's next stable release C:x/gov
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants