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

[CORE-652] add params validation to bridge message validation and add non-empty authority unit test #570

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

tqin7
Copy link
Contributor

@tqin7 tqin7 commented Oct 11, 2023

Changelist

  1. add Params validation to bridge governance messages
  2. add unit test on non-empty authority validation in bridge governance messages

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this change affects functionality (features, bug fixes, breaking changes, etc.), update protocol/CHANGELOG.md and/or indexer/CHANGELOG.md appropriately.
  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

@linear
Copy link

linear bot commented Oct 11, 2023

CORE-652 Audit x/bridge/

  • Primary tian
  • Secondary brendan

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 11, 2023

Walkthrough

The changes primarily focus on enhancing the validation logic in various MsgUpdate functions across different modules in the protocol/x/bridge/types package. The updates also include comprehensive test cases to ensure the validity of parameters and error handling.

Changes

File(s) Summary
.../msg_update_event_params.go, .../msg_update_safety_params.go, .../msg_update_propose_params.go The import order has been rearranged. The ValidateBasic function now calls the Validate function of msg.Params for better validation.
.../msg_update_event_params_test.go, .../msg_update_safety_params_test.go The expectedErr field in the test cases has been changed from an error type to a string type. New test cases have been added to cover various scenarios and the expected error values have been updated accordingly.
.../msg_update_propose_params_test.go New test cases have been added to validate the behavior of the MsgUpdateProposeParams struct. The test cases cover scenarios such as negative propose delay duration, negative skip if blocked delayed by duration, out-of-bound skip rate ppm, and empty/invalid authority. The expected error messages are also updated accordingly.

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.

@tqin7 tqin7 changed the title add params validation to bridge message validation and add non-empty authority unit test [CORE-652] add params validation to bridge message validation and add non-empty authority unit test Oct 11, 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: 2

Commits Files that changed from the base of the PR and between 3b11170 and ba8f58d.
Files selected for processing (6)
  • protocol/x/bridge/types/msg_update_event_params.go (2 hunks)
  • protocol/x/bridge/types/msg_update_event_params_test.go (2 hunks)
  • protocol/x/bridge/types/msg_update_propose_params.go (2 hunks)
  • protocol/x/bridge/types/msg_update_propose_params_test.go (2 hunks)
  • protocol/x/bridge/types/msg_update_safety_params.go (2 hunks)
  • protocol/x/bridge/types/msg_update_safety_params_test.go (2 hunks)
Additional comments (Suppressed): 12
protocol/x/bridge/types/msg_update_event_params.go (2)
  • 1-8: The import order has been changed. While this doesn't affect the functionality, it's good to follow a consistent import order for better readability and maintainability.

  • 23-27: The ValidateBasic function now calls msg.Params.Validate(). Ensure that the Validate function of msg.Params is implemented and handles all necessary validation.

protocol/x/bridge/types/msg_update_propose_params.go (2)
  • 1-8: The import order has been changed. Ensure that this change is consistent with the project's import order conventions.

  • 23-27: The ValidateBasic function now calls msg.Params.Validate(). Ensure that the Validate function of msg.Params performs all necessary validation checks and handles errors appropriately.

-	return nil
+	return msg.Params.Validate()
protocol/x/bridge/types/msg_update_safety_params.go (1)
  • 23-27: The ValidateBasic function now includes a call to msg.Params.Validate(). Ensure that the Validate function of msg.Params is implemented correctly and handles all necessary validation.
- return nil
+ return msg.Params.Validate()
protocol/x/bridge/types/msg_update_safety_params_test.go (3)
  • 30-56: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [19-56]

The change from comparing error objects to comparing error strings is a good move for test readability and maintainability. However, it's important to ensure that the error messages are unique and descriptive enough to avoid false positives in your tests. Also, the use of ErrorContains instead of ErrorIs is a good practice as it allows for more flexible error message matching.

  • 33-44: The test cases for "Failure: empty authority" and "Failure: invalid authority" are good additions. They help ensure that the ValidateBasic function correctly handles invalid authority values.

  • 47-56: The test loop structure and the use of table-driven tests are good practices in Go. They make the tests more readable and maintainable. However, ensure that all possible edge cases are covered in your tests.

protocol/x/bridge/types/msg_update_propose_params_test.go (1)
  • 20-95: The test for the ValidateBasic function now covers more edge cases, including negative durations and out-of-bound skip rates. This is a good improvement as it ensures that the function behaves as expected under a wider range of inputs. The change from comparing error objects to comparing error messages (using the Error() method) is also a good practice as it makes the tests less brittle.
protocol/x/bridge/types/msg_update_event_params_test.go (3)
  • 24-28: The expectedErr field in the test cases has been changed from an error type to a string type. This change allows for more flexible error matching in the tests, as it enables the use of partial error messages for validation. However, it's important to ensure that the error messages used for validation are unique and specific enough to prevent false positives.

  • 39-72: New test cases have been added to cover invalid values for parameters such as Denom, EthAddress, and Authority. These tests are important for ensuring that the ValidateBasic function correctly handles invalid input and returns the expected errors. The error messages used for validation appear to be unique and specific, which should help prevent false positives.

  • 78-82: The error handling in the tests has been updated to use the require.ErrorContains function when an error is expected. This function checks whether the actual error message contains the expected error message, which allows for more flexible error matching. However, it's important to ensure that the expected error messages are unique and specific enough to prevent false positives.

Comment on lines 1 to 8
package types

import (
errorsmod "cosmossdk.io/errors"
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

The import order has been changed. While this doesn't affect the functionality, it's good to follow a consistent import order for better readability and maintainability.

Comment on lines 20 to 95
func TestMsgUpdateProposeParams_ValidateBasic(t *testing.T) {
tests := map[string]struct {
msg types.MsgUpdateProposeParams
expectedErr error
expectedErr string
}{
"Success": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: 10_000,
ProposeDelayDuration: time.Second,
SkipRatePpm: 800_000,
SkipIfBlockDelayedByDuration: 5_000,
SkipIfBlockDelayedByDuration: time.Minute,
},
},
},
"Failure: Invalid authority": {
"Failure: negative propose delay duration": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: -time.Second,
SkipRatePpm: 800_000,
SkipIfBlockDelayedByDuration: time.Minute,
},
},
expectedErr: types.ErrNegativeDuration.Error(),
},
"Failure: negative skip if blocked delayed by duration": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: time.Second,
SkipRatePpm: 800_000,
SkipIfBlockDelayedByDuration: -time.Minute,
},
},
expectedErr: types.ErrNegativeDuration.Error(),
},
"Failure: out-of-bound skip rate ppm": {
msg: types.MsgUpdateProposeParams{
Authority: validAuthority,
Params: types.ProposeParams{
MaxBridgesPerBlock: 5,
ProposeDelayDuration: time.Second,
SkipRatePpm: 1_000_001,
SkipIfBlockDelayedByDuration: time.Minute,
},
},
expectedErr: types.ErrRateOutOfBounds.Error(),
},
"Failure: empty authority": {
msg: types.MsgUpdateProposeParams{
Authority: "",
},
expectedErr: types.ErrInvalidAuthority,
expectedErr: types.ErrInvalidAuthority.Error(),
},
"Failure: invalid authority": {
msg: types.MsgUpdateProposeParams{
Authority: "dydx1abc",
},
expectedErr: types.ErrInvalidAuthority.Error(),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.expectedErr == nil {
if tc.expectedErr == "" {
require.NoError(t, err)
} else {
require.ErrorIs(t, err, tc.expectedErr)
require.ErrorContains(t, err, tc.expectedErr)
}
})
}
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 cases have been expanded to cover more edge cases, including negative durations and out-of-bound skip rates. The expectedErr field has been changed from an error type to a string type, which is a good practice for comparing error messages. However, the test for an invalid authority (lines 78-83) seems to be using a hardcoded string "dydx1abc". It would be better to use a dynamically generated invalid authority for more robust testing.

- Authority: "dydx1abc",
+ Authority: generateInvalidAuthority(),

@tqin7 tqin7 added the chore label Oct 11, 2023
@tqin7 tqin7 merged commit 6375de1 into main Oct 11, 2023
16 checks passed
@tqin7 tqin7 deleted the tq/core-652-types branch October 11, 2023 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants