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

test: add tests for cons pubkey rotation #18965

Merged
merged 15 commits into from
Jan 9, 2024
Merged

Conversation

atheeshp
Copy link
Contributor

@atheeshp atheeshp commented Jan 6, 2024

Description

ref: #18141


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

// exceedsMaxRotations returns true if the key rotations exceed the limit, currently we are limiting one rotation for unbonding period.
func (k Keeper) exceedsMaxRotations(ctx context.Context, valAddr sdk.ValAddress) error {
// ExceedsMaxRotations returns true if the key rotations exceed the limit, currently we are limiting one rotation for unbonding period.
func (k Keeper) ExceedsMaxRotations(ctx context.Context, valAddr sdk.ValAddress) error {
Copy link
Contributor Author

@atheeshp atheeshp Jan 8, 2024

Choose a reason for hiding this comment

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

exporting this to use in sims, hope this doesn't effect much since it is just reading the state.

@atheeshp atheeshp marked this pull request as ready for review January 8, 2024 10:23
@atheeshp atheeshp requested a review from a team as a code owner January 8, 2024 10:23
Copy link
Contributor

coderabbitai bot commented Jan 8, 2024

Walkthrough

The overarching change involves updating the staking and slashing modules to handle validator consensus public key rotation. Method names and function calls have been updated for consistency, and new error handling has been added to accommodate changes in validators' consensus addresses. The test suite has been expanded with new imports, additional test functions for key rotation and infraction handling, and adjustments to initialization code. A new operation for rotating consensus public keys has been introduced to the simulation framework.

Changes

Files Change Summary
x/staking/keeper/cons_pubkey.go, x/staking/keeper/msg_server.go Renamed exceedsMaxRotations to ExceedsMaxRotations to standardize method naming convention.
x/staking/keeper/validator.go, x/staking/keeper/validator_test.go Updated GetValidatorByConsAddr for new consensus address handling and added new test TestValidatorConsPubKeyUpdate for key rotation.
tests/integration/evidence/keeper/infraction_test.go, tests/integration/staking/keeper/common_test.go, tests/integration/staking/keeper/msg_server_test.go, tests/integration/staking/simulation/operations_test.go Added new imports and test functions for handling double sign and consensus public key rotation, updated initFixture function, and added simulation test TestSimulateRotateConsPubKey.
x/slashing/keeper/signing_info_test.go Included a new test TestPerformConsensusPubKeyUpdate to verify the AfterConsensusPubKeyUpdate hook behavior.
x/staking/simulation/operations.go Added a new operation MsgRotateConsPubKey for simulating consensus public key rotation in validators.

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:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your comments unless it is explicitly tagged.

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

Comment on lines +40 to +62
if err != nil {
// if the validator not found try to find it in the map of `OldToNewConsKeyMap` because validator may've rotated it's key.
if !errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, err
}

newConsAddr, err := k.OldToNewConsKeyMap.Get(ctx, consAddr)
newConsAddr, err := k.OldToNewConsKeyMap.Get(ctx, consAddr.Bytes())
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, types.ErrNoValidatorFound
}
return types.Validator{}, err
}

operatorAddr, err := k.ValidatorByConsensusAddress.Get(ctx, newConsAddr)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, types.ErrNoValidatorFound
}
return types.Validator{}, err
}

opAddr = newConsAddr
opAddr = operatorAddr
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic for handling key rotation in GetValidatorByConsAddr seems to be correct. However, there is a potential issue with the variable naming and error handling:

  • The variable opAddr is used to store the result from ValidatorByConsensusAddress.Get, which is expected to be an operator address, but the name opAddr could be more descriptive.
  • The error handling could be more concise by returning early if the new consensus address is not found, avoiding deep nesting.

Consider the following improvements for clarity and maintainability:

func (k Keeper) GetValidatorByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (validator types.Validator, err error) {
-   opAddr, err := k.ValidatorByConsensusAddress.Get(ctx, consAddr)
+   operatorAddr, err := k.ValidatorByConsensusAddress.Get(ctx, consAddr)
    if err != nil {
        if !errors.Is(err, collections.ErrNotFound) {
            return types.Validator{}, err
        }

-       newConsAddr, err := k.OldToNewConsKeyMap.Get(ctx, consAddr.Bytes())
+       newConsAddr, findErr := k.OldToNewConsKeyMap.Get(ctx, consAddr.Bytes())
        if findErr != nil {
            if errors.Is(findErr, collections.ErrNotFound) {
-               return types.Validator{}, types.ErrNoValidatorFound
+               return types.Validator{}, findErr
            }
            return types.Validator{}, findErr
        }

-       operatorAddr, err := k.ValidatorByConsensusAddress.Get(ctx, newConsAddr)
+       operatorAddr, getErr := k.ValidatorByConsensusAddress.Get(ctx, newConsAddr)
        if getErr != nil {
            if errors.Is(getErr, collections.ErrNotFound) {
                return types.Validator{}, types.ErrNoValidatorFound
            }
            return types.Validator{}, getErr
        }

-       opAddr = operatorAddr
    }

-   if opAddr == nil {
+   if operatorAddr == nil {
        return types.Validator{}, types.ErrNoValidatorFound
    }

-   return k.GetValidator(ctx, opAddr)
+   return k.GetValidator(ctx, operatorAddr)
}

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
if err != nil {
// if the validator not found try to find it in the map of `OldToNewConsKeyMap` because validator may've rotated it's key.
if !errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, err
}
newConsAddr, err := k.OldToNewConsKeyMap.Get(ctx, consAddr)
newConsAddr, err := k.OldToNewConsKeyMap.Get(ctx, consAddr.Bytes())
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, types.ErrNoValidatorFound
}
return types.Validator{}, err
}
operatorAddr, err := k.ValidatorByConsensusAddress.Get(ctx, newConsAddr)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, types.ErrNoValidatorFound
}
return types.Validator{}, err
}
opAddr = newConsAddr
opAddr = operatorAddr
if err != nil {
// if the validator not found try to find it in the map of `OldToNewConsKeyMap` because validator may've rotated it's key.
if !errors.Is(err, collections.ErrNotFound) {
return types.Validator{}, err
}
newConsAddr, findErr := k.OldToNewConsKeyMap.Get(ctx, consAddr.Bytes())
if findErr != nil {
if errors.Is(findErr, collections.ErrNotFound) {
return types.Validator{}, findErr
}
return types.Validator{}, findErr
}
operatorAddr, getErr := k.ValidatorByConsensusAddress.Get(ctx, newConsAddr)
if getErr != nil {
if errors.Is(getErr, collections.ErrNotFound) {
return types.Validator{}, types.ErrNoValidatorFound
}
return types.Validator{}, getErr
}
}
if operatorAddr == nil {
return types.Validator{}, types.ErrNoValidatorFound
}
return k.GetValidator(ctx, operatorAddr)

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.

ACK

Copy link
Member

@facundomedica facundomedica left a comment

Choose a reason for hiding this comment

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

lgtm

@julienrbrt julienrbrt changed the title chore: add tests for cons pubkey rotation test: add tests for cons pubkey rotation Jan 8, 2024
Comment on lines +116 to +160
func (s *KeeperTestSuite) TestPerformConsensusPubKeyUpdate() {
ctx, slashingKeeper := s.ctx, s.slashingKeeper

require := s.Require()

pks := simtestutil.CreateTestPubKeys(500)

oldConsAddr := sdk.ConsAddress(pks[0].Address())
newConsAddr := sdk.ConsAddress(pks[1].Address())

newInfo := slashingtypes.NewValidatorSigningInfo(
oldConsAddr.String(),
int64(4),
int64(3),
time.Unix(2, 0).UTC(),
false,
int64(10),
)

err := slashingKeeper.ValidatorSigningInfo.Set(ctx, oldConsAddr, newInfo)
require.NoError(err)

s.stakingKeeper.EXPECT().ValidatorIdentifier(gomock.Any(), oldConsAddr).Return(oldConsAddr, nil)
err = slashingKeeper.SetMissedBlockBitmapValue(ctx, oldConsAddr, 10, true)
require.NoError(err)

err = slashingKeeper.Hooks().AfterConsensusPubKeyUpdate(ctx, pks[0], pks[1], sdk.Coin{})
require.NoError(err)

// check pubkey relation is set properly
savedPubKey, err := slashingKeeper.GetPubkey(ctx, newConsAddr.Bytes())
require.NoError(err)
require.Equal(savedPubKey, pks[1])

// check validator SigningInfo is set properly to new consensus pubkey
signingInfo, err := slashingKeeper.ValidatorSigningInfo.Get(ctx, newConsAddr)
require.NoError(err)
require.Equal(signingInfo, newInfo)

// missed blocks maps to old cons key only since there is a identifier added to get the missed blocks using the new cons key.
missedBlocks, err := slashingKeeper.GetValidatorMissedBlocks(ctx, oldConsAddr)
require.NoError(err)

require.Len(missedBlocks, 1)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The test TestPerformConsensusPubKeyUpdate appears to be comprehensive, testing the update of a consensus public key and its effects. It would be beneficial to include negative test cases to ensure robustness, such as what happens if the update fails or if an invalid public key is provided.

Comment on lines +371 to +395
func (s *SimTestSuite) TestSimulateRotateConsPubKey() {
require := s.Require()
blockTime := time.Now().UTC()
ctx := s.ctx.WithHeaderInfo(header.Info{Time: blockTime})

_ = s.getTestingValidator2(ctx)

// begin a new block
_, err := s.app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: s.app.LastBlockHeight() + 1, Hash: s.app.LastCommitID().Hash, Time: blockTime})
require.NoError(err)

// execute operation
op := simulation.SimulateMsgRotateConsPubKey(s.txConfig, s.accountKeeper, s.bankKeeper, s.stakingKeeper)
operationMsg, futureOperations, err := op(s.r, s.app.BaseApp, ctx, s.accounts, "")
require.NoError(err)

var msg types.MsgRotateConsPubKey
err = proto.Unmarshal(operationMsg.Msg, &msg)
require.NoError(err)

require.True(operationMsg.OK)
require.Equal(sdk.MsgTypeURL(&types.MsgRotateConsPubKey{}), sdk.MsgTypeURL(&msg))
require.Equal("cosmosvaloper1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7epjs3u", msg.ValidatorAddress)
require.Len(futureOperations, 0)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The test TestSimulateRotateConsPubKey correctly simulates the rotation of a consensus public key. It's important to ensure that the test covers various scenarios, including failure cases and edge conditions, to fully validate the rotation logic.

Comment on lines +423 to +428
func (s *SimTestSuite) getTestingValidator2(ctx sdk.Context) types.Validator {
val := s.getTestingValidator0(ctx)
val.Status = types.Bonded
s.Require().NoError(s.stakingKeeper.SetValidator(ctx, val))
s.Require().NoError(s.stakingKeeper.SetValidatorByConsAddr(ctx, val))
return val
Copy link
Contributor

Choose a reason for hiding this comment

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

The helper function getTestingValidator2 is used to set up a bonded validator for testing. It's crucial to ensure that this setup aligns with the expected preconditions of the tests that use it. If there are specific conditions under which a validator is expected to rotate their consensus public key, those should be reflected in the test setup.

@atheeshp atheeshp added this pull request to the merge queue Jan 9, 2024
Merged via the queue into main with commit b16aa35 Jan 9, 2024
60 of 61 checks passed
@atheeshp atheeshp deleted the ap/cons-key-rotation-tests branch January 9, 2024 04:57
@atheeshp atheeshp mentioned this pull request Jan 9, 2024
7 tasks
@coderabbitai coderabbitai bot mentioned this pull request Jan 17, 2024
12 tasks
relyt29 pushed a commit to relyt29/cosmos-sdk that referenced this pull request Jan 22, 2024
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.

5 participants