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

Use the nested if's linter to reduce complexity globally #1629

Closed
wants to merge 4 commits into from

Conversation

pysel
Copy link
Contributor

@pysel pysel commented May 22, 2024

1. Summary

Fixes #1609

Enables gocyclo linter

Copy link

vercel bot commented May 22, 2024

@pysel is attempting to deploy a commit to the quicksilver Team on Vercel.

A member of the Team first needs to authorize it.

@pysel pysel changed the title Add Use the nested if's linter to reduce complexity globally #1609 Use the nested if's linter to reduce complexity globally May 22, 2024
Copy link
Contributor

coderabbitai bot commented May 22, 2024

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce the gocyclo linter to the .golangci.yml configuration and add //nolint:gocyclo comments to several functions in the codebase to bypass cyclomatic complexity checks. Additionally, two new functions, HandleAcknowledgement and SetValidatorForZone, were added to the Keeper struct in the x/interchainstaking/keeper package.

Changes

Files/Paths Change Summaries
.golangci.yml Added gocyclo to the list of linters.
cmd/quicksilverd/genaccounts.go Added //nolint:gocyclo directive to AddGenesisAccountCmd function.
x/interchainstaking/keeper/callbacks_test.go Added //nolint:gocyclo directive to TestHandleValsetCallback function.
x/interchainstaking/keeper/ibc_packet_handlers.go Added new function HandleAcknowledgement with //nolint:gocyclo directive.
x/interchainstaking/keeper/keeper.go Added new function SetValidatorForZone to the Keeper struct.
x/interchainstaking/keeper/proposal_handler.go Added //nolint:gocyclo directive to HandleUpdateZoneProposal function.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant Keeper
    participant Zone
    User->>Keeper: Call HandleAcknowledgement
    Keeper->>Zone: SetValidatorForZone
    Zone->>Keeper: Validator Set
    Keeper->>User: Acknowledgement Handled
Loading

Assessment against linked issues

Objective (Issue #1609) Addressed Explanation
Use the nested if's linter to reduce complexity globally. The changes only added //nolint:gocyclo directives without reducing complexity.

In code we trust, through lint we strive,
Complexity tamed, new functions arrive.
With cyclo ignored, our tasks align,
Quicksilver's future, in code, will shine.
🐇✨


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 testing code 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 testing code 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 testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @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.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 750de82 and d699a7b.
Files selected for processing (1)
  • .golangci.yml (1 hunks)
Files skipped from review due to trivial changes (1)
  • .golangci.yml

Copy link
Contributor

@tuantran1702 tuantran1702 left a comment

Choose a reason for hiding this comment

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

Thanks @pysel for contributing! Could you fix the lint errors after adding the gocyclo lint? It seems there's quite lot of work to be done, would be very nice if we can actually reduce the complexity globally.

Copy link

codecov bot commented May 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.60%. Comparing base (12c4c4c) to head (947bad6).
Report is 9 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1629   +/-   ##
=======================================
  Coverage   63.60%   63.60%           
=======================================
  Files         195      195           
  Lines       13773    13773           
=======================================
  Hits         8760     8760           
  Misses       4156     4156           
  Partials      857      857           
Flag Coverage Δ
unittests 63.60% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
cmd/quicksilverd/genaccounts.go 14.28% <ø> (ø)
x/interchainstaking/keeper/ibc_packet_handlers.go 64.33% <ø> (ø)
x/interchainstaking/keeper/keeper.go 73.77% <ø> (ø)
x/interchainstaking/keeper/proposal_handler.go 62.62% <ø> (ø)

@pysel
Copy link
Contributor Author

pysel commented May 27, 2024

hey @tropicaldog . I added nolints to problematic places. I do not have much context on problematic functions, so I would probably not try to refactor them, since I might introduce unexpected bugs by doing that

CI should pass now though

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.

Actionable comments posted: 3

Outside diff range and nitpick comments (2)
x/interchainstaking/keeper/ibc_packet_handlers.go (2)

Line range hint 110-110: Refactor error handling for better clarity.

Consider refactoring the error handling in the case for /cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward to separate the logic of error checking and response handling for better clarity and maintainability.


Line range hint 130-130: Standardize error messages for consistency.

The error messages in the case for /cosmos.staking.v1beta1.MsgRedeemTokensForShares vary significantly. Standardizing these messages would improve the readability and maintainability of the code.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between d699a7b and 41a815e.
Files selected for processing (5)
  • cmd/quicksilverd/genaccounts.go (1 hunks)
  • x/interchainstaking/keeper/callbacks_test.go (1 hunks)
  • x/interchainstaking/keeper/ibc_packet_handlers.go (1 hunks)
  • x/interchainstaking/keeper/keeper.go (1 hunks)
  • x/interchainstaking/keeper/proposal_handler.go (1 hunks)
Files skipped from review due to trivial changes (3)
  • cmd/quicksilverd/genaccounts.go
  • x/interchainstaking/keeper/callbacks_test.go
  • x/interchainstaking/keeper/proposal_handler.go
Additional comments not posted (1)
x/interchainstaking/keeper/ibc_packet_handlers.go (1)

Line range hint 150-150: Verify conditions before processing tokenization.

Ensure that all preconditions are verified before processing the tokenization of shares in the case for /cosmos.staking.v1beta1.MsgTokenizeShares. This may include checking the validity of the input data and the state of related entities.

@@ -324,6 +324,7 @@ func (k *Keeper) SetValidatorsForZone(ctx sdk.Context, data []byte, icqQuery icq
return nil
}

// nolint:gocyclo
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 //nolint:gocyclo is justified given the complexity of the function. However, consider refactoring to improve maintainability.


Tip

Codebase Verification

The SetValidatorForZone function in x/interchainstaking/keeper/keeper.go includes error handling and checks for nil data. Specifically, it logs an error and returns nil if the data parameter is nil, and it handles various error conditions throughout its logic.

However, the test cases in x/interchainstaking/keeper/keeper_test.go and x/interchainstaking/client/cli/cli_test.go do not appear to cover all error paths explicitly. The tests reference SetValidatorForZone, but they do not seem to verify the function's behavior in the presence of nil data or other specific error conditions.

  • Implementation of SetValidatorForZone:

    • Handles nil data by logging an error and returning nil.
    • Handles errors from UnmarshalValidator, ValAddressFromBech32, and other operations by logging and returning the errors.
  • Test Coverage:

    • Tests in keeper_test.go and cli_test.go reference SetValidatorForZone but do not explicitly test all error paths.

To ensure comprehensive test coverage, additional unit tests should be added to explicitly verify the function's behavior when data is nil and when other errors occur.

  • Files to update:
    • x/interchainstaking/keeper/keeper_test.go
    • x/interchainstaking/client/cli/cli_test.go
Analysis chain

Verify the correct handling of nil data and error propagation.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all error paths in `SetValidatorForZone` are covered by unit tests.

# Test: Search for unit tests covering `SetValidatorForZone`. Expect: Tests covering all error paths.
rg --type go $'func Test.*SetValidatorForZone.*'

Length of output: 49



Script:

#!/bin/bash
# Locate the implementation of the `SetValidatorForZone` function in the codebase.
ast-grep --lang go --pattern 'func SetValidatorForZone($_, $_) $_' 

# Search for any related tests that might indirectly cover the `SetValidatorForZone` function.
rg --type go 'SetValidatorForZone'

Length of output: 847



Script:

#!/bin/bash
# Extract the implementation of the `SetValidatorForZone` function.
ast-grep --lang go --pattern 'func (k *Keeper) SetValidatorForZone(ctx sdk.Context, zone *types.Zone, data []byte) error {
  $$$
}' x/interchainstaking/keeper/keeper.go

# Extract the test cases from `x/interchainstaking/keeper/keeper_test.go` and `x/interchainstaking/client/cli/cli_test.go` that reference `SetValidatorForZone`.
rg --type go --context 10 'SetValidatorForZone' x/interchainstaking/keeper/keeper_test.go x/interchainstaking/client/cli/cli_test.go

Length of output: 16592

@@ -62,6 +62,7 @@ func DeserializeCosmosTxTyped(cdc codec.BinaryCodec, data []byte) ([]TypedMsg, e
return msgs, nil
}

//nolint:gocyclo
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper documentation for HandleAcknowledgement.

The function HandleAcknowledgement is complex and handles many different message types. It would benefit from detailed documentation explaining each case and the overall flow.

@tuantran1702
Copy link
Contributor

Yeah I guess this PR should be merged after the lint issue is fixed, wdyt @faddat @joe-bowman?

@tuantran1702 tuantran1702 marked this pull request as draft May 27, 2024 16:20
@joe-bowman
Copy link
Contributor

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you!

@joe-bowman joe-bowman added Stale and removed Stale labels Jun 11, 2024
@joe-bowman
Copy link
Contributor

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed if no further activity occurs. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use the nested if's linter to reduce complexity globally
4 participants