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(x/staking): use sdk validator updates #19788

Merged
merged 7 commits into from
Mar 28, 2024

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Mar 19, 2024

Description

Follow-up of #19754

  • Removes ABCIValidatorUpdateZero and ABCIValidatorUpdate
  • Alias ValidatorUpdates in core/appmodule
  • Remove module.ValidatorUpdates from module/types
  • Remove ValidatorUpdates from staking keeper as it wasn't used.

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
  • 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.

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

Summary by CodeRabbit

  • Refactor
    • Improved data structures and function arguments in the staking module to enhance performance and reduce dependencies.
    • Streamlined interfaces for better efficiency.
  • New Features
    • Introduced ModuleValidatorUpdate to optimize validator updates processing, replacing ABCIValidatorUpdate.
  • Tests
    • Added new tests for token sharing functionality within the staking module.
  • Documentation
    • Updated CHANGELOG.md with detailed descriptions of changes in the staking module.

@julienrbrt julienrbrt requested a review from a team as a code owner March 19, 2024 20:16
Copy link
Contributor

coderabbitai bot commented Mar 19, 2024

Walkthrough

Walkthrough

The update focuses on transitioning the staking module in the Cosmos SDK to use appmodule.ValidatorUpdate instead of abci.ValidatorUpdate. This change spans various files, involving adjustments in return types, imports, and test cases. The goal is to enhance modularity, streamline code organization, and leverage new data structures for handling validator updates efficiently.

Changes

Files Change Summary
core/appmodule/module.go Introduced ValidatorUpdate type
tests/.../staking/keeper/genesis_test.go
x/staking/keeper/validator_test.go
Replaced abci.ValidatorUpdate with appmodule.ValidatorUpdate and adjusted declarations
x/staking/CHANGELOG.md Documented significant updates including data structure changes, function argument updates, and modularity enhancements
x/staking/keeper/abci.go
x/staking/keeper/genesis.go
x/staking/module.go
Updated function return types to appmodule.ValidatorUpdate and added new imports
x/staking/keeper/keeper.go
x/staking/keeper/val_state_change.go
Introduced moduleValidatorUpdatesCodec type and replaced references with appmodule.ValidatorUpdate
x/staking/types/validator.go
x/staking/types/validator_test.go
Replaced ABCIValidatorUpdate functions with appmodule.ValidatorUpdate and updated tests

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@@ -41,6 +42,49 @@ func HistoricalInfoCodec(cdc codec.BinaryCodec) collcodec.ValueCodec[types.Histo
})
}

type moduleValidatorUpdatesCodec struct {
Copy link
Member Author

Choose a reason for hiding this comment

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

@testinginprod, can we generalize that in collection? Like a JSONValueCodec that always does the following?

Copy link
Member Author

Choose a reason for hiding this comment

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

It isn't needed here, but I'll create one now in collections

@julienrbrt julienrbrt marked this pull request as draft March 19, 2024 20:19
@@ -84,7 +128,7 @@ type Keeper struct {
// LastTotalPower value: LastTotalPower
LastTotalPower collections.Item[math.Int]
// ValidatorUpdates value: ValidatorUpdates
ValidatorUpdates collections.Item[types.ValidatorUpdates]
ValidatorUpdates collections.Item[types.ModuleValidatorUpdates]
Copy link
Member

Choose a reason for hiding this comment

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

does anyone use this value? seems we only set it

Copy link
Member Author

@julienrbrt julienrbrt Mar 25, 2024

Choose a reason for hiding this comment

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

That's a good point 👍🏾 Let me ask, we don't use it ourselves indeed.

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.yml

Commits Files that changed from the base of the PR and between bfa734c and 100f280.
Files selected for processing (11)
  • core/appmodule/module.go (1 hunks)
  • tests/integration/staking/keeper/genesis_test.go (2 hunks)
  • x/staking/CHANGELOG.md (1 hunks)
  • x/staking/keeper/abci.go (2 hunks)
  • x/staking/keeper/genesis.go (2 hunks)
  • x/staking/keeper/keeper.go (4 hunks)
  • x/staking/keeper/val_state_change.go (7 hunks)
  • x/staking/keeper/validator_test.go (2 hunks)
  • x/staking/module.go (2 hunks)
  • x/staking/types/validator.go (3 hunks)
  • x/staking/types/validator_test.go (1 hunks)
Additional comments: 19
x/staking/keeper/abci.go (2)
  • 7-7: The addition of the "cosmossdk.io/core/appmodule" import is necessary for the updated functionality in the EndBlocker function. This change aligns with the PR's objective to standardize validator updates handling.
  • 21-21: The update of the EndBlocker function's return type to []appmodule.ValidatorUpdate is in line with the PR's goal of improving modularity and standardization. Ensure that the BlockValidatorUpdates function, which is called within EndBlocker, also returns this updated type.
core/appmodule/module.go (1)
  • 32-34: The addition of the ValidatorUpdate type as an alias to appmodule.ValidatorUpdate is a clear and effective way to standardize the handling of validator updates across different modules. This change supports the PR's objectives well.
x/staking/module.go (2)
  • 148-148: The update of the InitGenesis function's return type to []appmodule.ValidatorUpdate aligns with the PR's objectives of improving modularity and standardization. Ensure that the implementation of InitGenesis is compatible with this updated return type.
  • 176-176: The update of the EndBlock function's return type to []appmodule.ValidatorUpdate is consistent with the PR's goal of standardizing validator updates handling. Ensure that the EndBlocker function, which is called within EndBlock, also returns this updated type.
tests/integration/staking/keeper/genesis_test.go (1)
  • 10-10: The addition of the "cosmossdk.io/core/appmodule" import in the test file is necessary to support the updated appmodule.ValidatorUpdate type. This change is consistent with the PR's objectives.
x/staking/CHANGELOG.md (2)
  • 46-93: The changelog entries under "API Breaking Changes" detail the use of collections for various data structures and the removal of specific functions and keys. These changes are significant for developers and should be clearly communicated. Ensure that the descriptions are accurate and provide enough context for developers to understand the impact of these changes.
  • 99-102: The "State Breaking changes" section mentions significant changes that could affect the state and operation of the Cosmos SDK. It's crucial to ensure that these entries are clear and provide sufficient detail for users to understand the implications of the changes. Consider adding more context or examples if necessary to clarify the impact of these changes.
x/staking/keeper/genesis.go (2)
  • 20-20: The update to the InitGenesis function to use []appmodule.ValidatorUpdate as the return type aligns with the PR's objective to standardize how validator updates are handled. This change is crucial for ensuring consistency across different modules. Ensure that all references to the old return type have been updated accordingly.
  • 178-178: The introduction of moduleValidatorUpdates as a slice of appmodule.ValidatorUpdate is a direct consequence of the updated return type of the InitGenesis function. This change is necessary for accumulating validator updates to be returned by the function. Verify that the logic for populating this slice correctly reflects the intended validator updates, especially in the context of exported and non-exported data.
x/staking/types/validator_test.go (1)
  • 60-65: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

Please ensure that all removed tests have their functionality covered by new or existing tests, especially since ABCIValidatorUpdate and ABCIValidatorUpdateZero functions have been removed. This is crucial for maintaining the integrity of the test suite.

x/staking/types/validator.go (2)
  • 13-13: The addition of "cosmossdk.io/core/appmodule" is necessary for the new ModuleValidatorUpdate and ModuleValidatorUpdateZero functions. This aligns with the PR's goal of integrating more closely with the Cosmos SDK's core functionalities.
  • 259-288: The new ModuleValidatorUpdate and ModuleValidatorUpdateZero functions are well-implemented replacements for the removed ABCIValidatorUpdate and ABCIValidatorUpdateZero. However, it's crucial to ensure that these changes are reflected wherever validator updates are handled across the SDK to maintain consistency and avoid potential integration issues.
x/staking/keeper/keeper.go (2)
  • 45-82: The introduction of moduleValidatorUpdatesCodec with JSON encoding and decoding methods for ModuleValidatorUpdates is a significant change. Ensure that JSON is the preferred format for these operations, considering performance and compatibility with the rest of the Cosmos SDK. JSON parsing can be more CPU-intensive compared to binary formats like Protobuf, which might already be used elsewhere in the SDK.
  • 131-131: The update to ValidatorUpdates to use types.ModuleValidatorUpdates aligns with the PR's objective to standardize validator updates handling. However, ensure that all references and usages of ValidatorUpdates throughout the staking module and any other dependent modules have been updated accordingly to prevent type mismatches.
Verification successful

The references to types.ValidatorUpdates in both x/staking/keeper/keeper.go and simapp/sim_test.go are related to keys or identifiers, not direct type usages that would require updating to types.ModuleValidatorUpdates. These findings indicate that the initial concern about ensuring all references and usages of ValidatorUpdates have been updated accordingly to prevent type mismatches does not apply to these cases. Therefore, the review comment is verified as the changes align with the PR's objective without any issues found in the context provided.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for old `types.ValidatorUpdates` usage that might have been missed.
rg --type go "types.ValidatorUpdates"

Length of output: 276



Script:

#!/bin/bash
# Examine the context of `types.ValidatorUpdates` usage in `x/staking/keeper/keeper.go`
rg --type go -C 5 "types.ValidatorUpdates" x/staking/keeper/keeper.go

# Examine the context of `types.ValidatorUpdates` usage in `simapp/sim_test.go`
rg --type go -C 5 "types.ValidatorUpdates" simapp/sim_test.go

Length of output: 1607

x/staking/keeper/val_state_change.go (2)
  • 25-25: The function BlockValidatorUpdates now returns []appmodule.ValidatorUpdate instead of the previous type. This change is consistent with the PR's objective to standardize validator updates handling. Ensure that all callers of this function are updated to handle the new return type correctly.
  • 215-215: The use of appmodule.ValidatorUpdate in various parts of the code, including key rotations and setting validator updates, is noted. Ensure that the transition from the old type to the new appmodule.ValidatorUpdate is seamless and does not affect the logic for updating validator sets, especially during consensus key rotations.

Also applies to: 250-250, 292-292, 331-331

Verification successful

Given the initial search did not yield results, it's necessary to broaden our approach to better understand how appmodule.ValidatorUpdate is handled, especially in contexts that might not explicitly mention "consensus key rotation". Let's perform a more general search for ValidatorUpdate references in the Go files to uncover its usage patterns.


The broadened search results confirm the extensive use of appmodule.ValidatorUpdate throughout the staking module, including in the context of consensus key rotations. The specific implementation and usage patterns, such as in x/staking/types/validator.go and x/staking/keeper/val_state_change.go, demonstrate a well-integrated approach to handling validator updates. This includes the generation of validator updates during consensus public key rotations, aligning with the review comment's emphasis on ensuring seamless transitions and updates. Therefore, the handling of appmodule.ValidatorUpdate during consensus key rotations and other validator update scenarios appears to be correctly implemented and integrated into the codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify the handling of `appmodule.ValidatorUpdate` during consensus key rotations.
rg --type go "ValidatorUpdate" | grep "consensus key rotation"

Length of output: 62



Script:

#!/bin/bash
# Broaden the search for `ValidatorUpdate` to understand its usage in the codebase.
rg --type go "ValidatorUpdate"

Length of output: 30648

x/staking/keeper/validator_test.go (2)
  • 9-9: The addition of the import cosmossdk.io/core/appmodule is necessary for the updated functionality in this test suite. This change aligns with the PR's objective to standardize validator updates handling.
  • 20-20: The change in the return type of applyValidatorSetUpdates from module.ValidatorUpdate to appmodule.ValidatorUpdate is crucial for aligning with the new standard for handling validator updates. Please ensure that all usages of this function within the test suite have been updated to handle the new return type correctly.
Verification successful

The review comment is accurate regarding the change in the return type of applyValidatorSetUpdates from module.ValidatorUpdate to appmodule.ValidatorUpdate within the x/staking/keeper/validator_test.go file and its direct usages. It's important to ensure that all usages of this function within the test suite have been updated to handle the new return type correctly. Additionally, it's noted that there is another function with the same name but a different return type (module.ValidatorUpdate) used in the integration tests, which is a separate context and does not contradict the review comment but is worth acknowledging for a comprehensive understanding of the codebase changes.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that `applyValidatorSetUpdates` is correctly used with its new return type throughout the test suite.
rg "applyValidatorSetUpdates" --type go

Length of output: 5482

Comment on lines 60 to 65
require.Equal(t, d, d3)
}

func TestABCIValidatorUpdate(t *testing.T) {
validator := newValidator(t, valAddr1, pk1)
abciVal := validator.ABCIValidatorUpdate(sdk.DefaultPowerReduction)
pk, err := validator.TmConsPublicKey()
require.NoError(t, err)
require.Equal(t, pk, abciVal.PubKey)
require.Equal(t, validator.BondedTokens().Int64(), abciVal.Power)
}

func TestABCIValidatorUpdateZero(t *testing.T) {
validator := newValidator(t, valAddr1, pk1)
abciVal := validator.ABCIValidatorUpdateZero()
pk, err := validator.TmConsPublicKey()
require.NoError(t, err)
require.Equal(t, pk, abciVal.PubKey)
require.Equal(t, int64(0), abciVal.Power)
}

func TestShareTokens(t *testing.T) {
validator := mkValidator(100, math.LegacyNewDec(100))
assert.True(math.LegacyDecEq(t, math.LegacyNewDec(50), validator.TokensFromShares(math.LegacyNewDec(50))))
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

Given the significant changes in the handling of validator updates, it's important to add tests that specifically verify the new ModuleValidatorUpdate and ModuleValidatorUpdateZero functionalities to ensure they behave as expected within the staking module.

Would you like assistance in drafting these tests?

// ModuleValidatorUpdate returns a appmodule.ValidatorUpdate from a staking validator type
// with the full validator power.
// It replaces the previous ABCIValidatorUpdate function.
func (v Validator) ModuleValidatorUpdate(r math.Int) appmodule.ValidatorUpdate {
Copy link
Contributor

Choose a reason for hiding this comment

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

The use of panic in ModuleValidatorUpdate and ModuleValidatorUpdateZero for handling errors from ConsPubKey() is a concern. Consider returning an error from these functions instead of panicking to allow for more graceful error handling in the calling code.

- func (v Validator) ModuleValidatorUpdate(r math.Int) appmodule.ValidatorUpdate {
+ func (v Validator) ModuleValidatorUpdate(r math.Int) (appmodule.ValidatorUpdate, error) {
- func (v Validator) ModuleValidatorUpdateZero() appmodule.ValidatorUpdate {
+ func (v Validator) ModuleValidatorUpdateZero() (appmodule.ValidatorUpdate, error) {
- panic(err)
+ return appmodule.ValidatorUpdate{}, err

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func (v Validator) ModuleValidatorUpdate(r math.Int) appmodule.ValidatorUpdate {
func (v Validator) ModuleValidatorUpdate(r math.Int) (appmodule.ValidatorUpdate, error) {

@@ -128,8 +128,7 @@ func TestInitGenesis(t *testing.T) {
assert.Assert(t, found)
assert.Equal(t, types.Bonded, resVal.Status)

abcivals := make([]abci.ValidatorUpdate, len(vals))
validatorUpdates := make([]module.ValidatorUpdate, len(abcivals))
validatorUpdates := make([]appmodule.ValidatorUpdate, len(vals))
Copy link
Contributor

Choose a reason for hiding this comment

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

The update of references from abci.ValidatorUpdate to appmodule.ValidatorUpdate in the test file aligns with the PR's goal of standardizing the handling of validator updates. Consider adding more comprehensive tests to ensure that all scenarios involving validator updates are thoroughly covered.

Would you like me to help with adding more comprehensive tests to cover these changes?

@@ -138,7 +136,7 @@ func (k Keeper) BlockValidatorUpdates(ctx context.Context) ([]module.ValidatorUp
// CONTRACT: Only validators with non-zero power or zero-power that were bonded
// at the previous block height or were removed from the validator set entirely
// are returned to CometBFT.
func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) ([]module.ValidatorUpdate, error) {
func (k Keeper) ApplyAndReturnValidatorSetUpdates(ctx context.Context) ([]appmodule.ValidatorUpdate, error) {
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 ApplyAndReturnValidatorSetUpdates also reflects the change to []appmodule.ValidatorUpdate. Given the critical role of this function in the staking logic, thoroughly test these changes to ensure that the validator updates are correctly applied and returned, especially in edge cases such as validator power changes and key rotations.

Consider adding more unit tests covering various scenarios of validator set updates to ensure the robustness of these changes.

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

Configuration used: .coderabbit.yml

Commits Files that changed from the base of the PR and between 100f280 and e8d51b6.
Files selected for processing (1)
  • x/staking/CHANGELOG.md (2 hunks)

Comment on lines 93 to 103
* remove `Keeper`: `SetLastTotalPower`, `GetLastTotalPower`
* [#17335](https://github.com/cosmos/cosmos-sdk/pull/17335) Remove usage of `"cosmossdk.io/x/staking/types".Infraction_*` in favour of `"cosmossdk.io/api/cosmos/staking/v1beta1".Infraction_` in order to remove dependency between modules on staking
* [#17655](https://github.com/cosmos/cosmos-sdk/pull/17655) `QueryHistoricalInfo` was adjusted to return `HistoricalRecord` and marked `Hist` as deprecated.
* [#19414](https://github.com/cosmos/cosmos-sdk/pull/19414) Staking module takes an environment variable in `NewStakingKeeper` instead of individual services.
* [#19754](https://github.com/cosmos/cosmos-sdk/pull/19754) update to use `[]module.ValidatorUpdate` as return for `ApplyAndReturnValidatorSetUpdates`.

### State Breaking changes

* [#18841](https://github.com/cosmos/cosmos-sdk/pull/18841) In a undelegation or redelegation if the shares being left delegated correspond to less than 1 token (in base denom) the entire delegation gets removed.
* [#18142](https://github.com/cosmos/cosmos-sdk/pull/18142) Introduce `key_rotation_fee` param to calculate fees while rotating the keys
* [#17655](https://github.com/cosmos/cosmos-sdk/pull/17655) `HistoricalInfo` was replaced with `HistoricalRecord`, it removes the validator set and comet header and only keep what is needed for IBC.
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic. No newline at end of file
* [#19740](https://github.com/cosmos/cosmos-sdk/pull/19740) Verify `InitGenesis` and `ExportGenesis` module code and keeper code do not panic.
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [46-103]

The changelog provides a comprehensive overview of the significant changes made to the staking module. However, there are a few areas that could benefit from additional clarity or correction:

  1. Ensure consistency in the presentation of changes, particularly in the "API Breaking Changes" section. Each entry should clearly describe the change and its impact on the module or SDK users.

  2. For removed functions and types, consider providing brief guidance on the migration path or alternatives that developers should use. This can help ease the transition for those affected by the breaking changes.

  3. In the "State Breaking Changes" section, the description of changes could be enhanced by explaining the rationale behind the changes and their expected impact on the staking module's behavior or state.

  4. Verify the accuracy of all linked PRs and ensure they correctly correspond to the described changes. This helps readers to explore the changes in more detail if needed.

  5. Check for typographical errors and grammatical issues throughout the document to maintain professionalism and readability.

Improving these areas will enhance the changelog's utility as a resource for developers and users of the Cosmos SDK.

@julienrbrt julienrbrt marked this pull request as ready for review March 25, 2024 16:28
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.yml

Commits Files that changed from the base of the PR and between e8d51b6 and b91e82f.
Files selected for processing (13)
  • api/cosmos/staking/v1beta1/staking.pulsar.go (2 hunks)
  • x/staking/CHANGELOG.md (2 hunks)
  • x/staking/README.md (2 hunks)
  • x/staking/keeper/keeper.go (2 hunks)
  • x/staking/keeper/migrations.go (2 hunks)
  • x/staking/keeper/val_state_change.go (8 hunks)
  • x/staking/migrations/v5/store.go (2 hunks)
  • x/staking/migrations/v6/keys.go (1 hunks)
  • x/staking/migrations/v6/store.go (1 hunks)
  • x/staking/module.go (3 hunks)
  • x/staking/proto/cosmos/staking/v1beta1/staking.proto (1 hunks)
  • x/staking/types/keys.go (2 hunks)
  • x/staking/types/validator.go (3 hunks)
Files skipped from review as they are similar to previous changes (3)
  • x/staking/keeper/val_state_change.go
  • x/staking/module.go
  • x/staking/types/validator.go
Additional comments: 23
x/staking/migrations/v6/keys.go (1)
  • 5-5: The introduction of ValidatorUpdatesKey with a prefix value of 97 is noted. It's good practice to add a comment explaining the purpose of this key or the type of data it will be associated with in the store for future reference and clarity.
x/staking/migrations/v6/store.go (1)
  • 11-15: The MigrateStore function correctly performs the intended migration step by deleting the ValidatorUpdatesKey. The implementation is straightforward and follows best practices for store migrations.
x/staking/keeper/migrations.go (1)
  • 45-49: The addition of the Migrate5to6 function is well-implemented, following the established patterns for migration functions within the Cosmos SDK. It correctly utilizes the v6.MigrateStore for the migration logic.
x/staking/migrations/v5/store.go (2)
  • 16-16: Simplifying the migrateDelegationsByValidatorIndex function by removing unused parameters improves clarity and maintainability. Good refactor.
  • 34-34: The update to the MigrateStore function to call migrateDelegationsByValidatorIndex with its new signature is correctly implemented. The rest of the migration logic remains clear and focused.
x/staking/CHANGELOG.md (1)
  • 88-102: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [46-102]

The updates to the changelog provide a comprehensive overview of the significant changes made to the staking module, including new features, improvements, bug fixes, and breaking changes. It's important to maintain clarity and accuracy in these entries to ensure they serve as a useful resource for developers and users. The categorization and referencing of pull requests are well done.

x/staking/types/keys.go (3)
  • 60-61: The introduction of HistoricalInfoKey and the modification of ParamsKey are consistent with the Cosmos SDK's practices for defining store keys. It's good practice to add comments explaining the purpose of these keys for future reference.
  • 73-76: The reserved key prefix with a value of 97 is noted. While its purpose is not immediately clear, reserving keys for future use is a common practice. Adding a comment to explain its intended use or reservation would improve clarity.
  • 77-77: The introduction of the UnbondingType enum provides a clear way to distinguish between different types of unbonding operations. This is a good practice for enhancing code readability and maintainability.
x/staking/keeper/keeper.go (9)
  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-1]

Please ensure the package documentation comment is present at the top of the file to provide a high-level overview of its purpose and functionalities. This is a common practice in Go for better code readability and maintainability.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [17-17]

The use of gogotypes "github.com/cosmos/gogoproto/types" is noted. Ensure that the specific types from this package are necessary and correctly used throughout the file, especially considering the move towards more standardized Protobuf types in newer Cosmos SDK versions.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [29-29]

The introduction of appmodule and addresscodec from cosmossdk.io/core aligns with the PR's objective to integrate more closely with Cosmos SDK's core functionalities. Ensure that these imports replace previous mechanisms effectively and that there are no remnants of the old implementation that could cause confusion or errors.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [33-33]

The removal of ValidatorUpdates from the Keeper struct is a significant change. Ensure that all functionalities relying on ValidatorUpdates have been appropriately updated or refactored to work without it. This change should also be reflected in any relevant documentation or comments within the code.

Verification successful

The search results indicate that ValidatorUpdates is still referenced and utilized within the x/staking module, particularly in protobuf definitions, keeper functionalities, and migrations. This suggests that while ValidatorUpdates may have been removed from the Keeper struct, its concept and utility persist in various forms across the module. The presence of operational code and migrations handling ValidatorUpdates implies that the necessary updates or refactors following its removal from the Keeper struct have been considered and implemented. Therefore, the review comment's concern appears to have been addressed through the module's current handling of ValidatorUpdates.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any code segments that might still be referencing `ValidatorUpdates` within the staking module.
rg --type go 'ValidatorUpdates' x/staking/

Length of output: 3537

* 83-88: > 📝 **NOTE** > This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The HistoricalInfoCodec function demonstrates good use of the collcodec.ValueCodec interface for custom serialization logic. Ensure that the deprecation of HistoricalInfo and the transition to HistoricalRecord are well-documented and communicated to module users, as this could impact downstream applications.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The comment //nolint: staticcheck // HistoricalInfo is deprecated directly addresses the use of deprecated types. While this is a valid approach to suppress linting errors, it's crucial to plan for the removal of deprecated types and ensure that all references to HistoricalInfo are eventually replaced with HistoricalRecord or other updated types.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The transition from HistoricalInfo to HistoricalRecord within the HistoricalInfoCodec function aligns with the PR's objectives of improving modularity and organization. Ensure that this change is consistently applied across the module and that any external modules or applications depending on the staking module are updated accordingly.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The explicit error handling in HistoricalInfoCodec is commendable. It ensures that any issues during the unmarshalling process are caught and handled appropriately. This practice should be consistently applied throughout the codebase to maintain robustness and reliability.

  • 83-88: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The use of types.HistoricalRecord as the return type in HistoricalInfoCodec is a good practice, as it encapsulates the relevant data in a more structured and type-safe manner. This approach enhances code readability and maintainability.

x/staking/proto/cosmos/staking/v1beta1/staking.proto (1)
  • 409-410: Marking ValidatorUpdates as deprecated is a significant change. It's important to communicate this change effectively to developers and users of the Cosmos SDK to ensure a smooth transition. Consider adding more detailed deprecation notices in the documentation or release notes.
x/staking/README.md (2)
  • 23-28: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [1-7]

The update to the table of contents aligns with the removal of the ValidatorUpdates section as mentioned. The changes are clear and maintain the document's structure.

  • 23-28: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [9-79]

The updates to the LastTotalPower description and the detailed explanation of UnbondingID are well-articulated, enhancing the clarity of the documentation. The technical terms used are accurate, and there are no spelling or grammatical errors in the context of this technical documentation.

api/cosmos/staking/v1beta1/staking.pulsar.go (2)
  • 15898-15899: The ValidatorUpdates type is marked as deprecated, which is good for indicating that it should no longer be used. However, it would be beneficial to provide guidance on what should be used instead or any other recommended actions for developers. This could be in the form of a comment suggesting an alternative type or a reference to documentation for further reading.
  • 16414-16478: It appears this hunk includes changes to protobuf serialization data, likely as a result of modifications to protobuf definitions. While reviewing the binary data directly isn't practical, it's important to ensure that these changes are consistent with the intended modifications to the protobuf definitions. If the protobuf definitions were updated as part of this PR, ensure that all necessary regeneration steps have been followed and that the changes accurately reflect the updated definitions.

Comment on lines 83 to 88
HistoricalInfo collections.Map[uint64, types.HistoricalRecord]
// LastTotalPower value: LastTotalPower
LastTotalPower collections.Item[math.Int]
// ValidatorUpdates value: ValidatorUpdates
ValidatorUpdates collections.Item[types.ValidatorUpdates]
// DelegationsByValidator key: valAddr+delAddr | value: none used (index key for delegations by validator index)
DelegationsByValidator collections.Map[collections.Pair[sdk.ValAddress, sdk.AccAddress], []byte]
UnbondingID collections.Sequence
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The implementation of HistoricalInfoCodec for backward compatibility is noted. However, ensure that there's a clear migration path for users of the staking module to move away from deprecated types like HistoricalInfo. This might involve providing scripts, documentation, or other forms of support to facilitate the transition.

Would you like assistance in creating a migration guide or script for users to transition away from HistoricalInfo?


📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [35-35]

The use of custom serialization logic in HistoricalInfoCodec is a critical change. It's important to thoroughly test this logic to prevent any data corruption or loss during the serialization and deserialization processes. Consider adding unit tests specifically targeting edge cases and error handling scenarios.

Do you need help with writing these unit tests?

Comment on lines +409 to +410
option deprecated = true;

Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The script successfully found a reference to ValidatorUpdates within a .proto file in the repository. This indicates that there are still references to ValidatorUpdates in the codebase, which aligns with the need to ensure all references to this deprecated message are updated as mentioned in the review comment. Therefore, it's important to address these references and provide appropriate migration documentation for users of the staking module.

Analysis chain

The deprecation of the ValidatorUpdates message aligns with the PR's objectives of streamlining the handling of validator updates. Ensure that all references to ValidatorUpdates in the codebase are updated to use the new mechanism, and consider providing migration documentation for external users of the staking module.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any remaining references to `ValidatorUpdates` in the staking module and related modules.
rg --type proto 'ValidatorUpdates' proto/cosmos/staking/

Length of output: 86



Script:

#!/bin/bash
# Corrected search for any remaining references to `ValidatorUpdates` in `.proto` files within the staking module and related modules.
rg 'ValidatorUpdates' proto/cosmos/staking/*.proto

Length of output: 121



Script:

#!/bin/bash
# Use fd to find .proto files within the staking module and rg to search for ValidatorUpdates within those files.
fd '.proto$' proto/cosmos/staking/ --exec rg 'ValidatorUpdates'

Length of output: 172



Script:

#!/bin/bash
# Search the entire repository for .proto files related to the staking module and check for references to ValidatorUpdates.
fd '.proto$' --exec rg 'ValidatorUpdates'

Length of output: 138

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.yml

Commits Files that changed from the base of the PR and between b91e82f and 6fc6c7e.
Files selected for processing (1)
  • x/staking/module.go (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • x/staking/module.go

Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

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

Lgtm

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

lgtm

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.yml

Commits Files that changed from the base of the PR and between 6fc6c7e and 689a154.
Files selected for processing (1)
  • simapp/sim_test.go (1 hunks)
Path instructions used (1)
simapp/sim_test.go (2)

**/*.go
Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


**/*_test.go
Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request

Additional comments (1)
simapp/sim_test.go (1)
  • 197-197: The removal of stakingtypes.ValidatorUpdatesKey from the skipPrefixes map for stakingtypes.StoreKey aligns with the PR's objective of refactoring and improving the staking module. This change suggests that ValidatorUpdatesKey is no longer relevant or necessary in the context of the TestAppImportExport function, which likely reflects broader changes in how validator updates are handled within the module.

Ensure that this removal does not inadvertently affect the logic of the TestAppImportExport test, particularly in the comparison of stores section. It's important to verify that the test still accurately reflects the intended state and behavior of the application after the refactoring.

@julienrbrt julienrbrt added this pull request to the merge queue Mar 28, 2024
Merged via the queue into main with commit d54e940 Mar 28, 2024
62 of 63 checks passed
@julienrbrt julienrbrt deleted the julien/val-update-stacking branch March 28, 2024 13:52
@coderabbitai coderabbitai bot mentioned this pull request May 3, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants