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

[x/act V2]: Add MsgVote handling #794

Merged
merged 11 commits into from
Sep 10, 2024
Merged

Conversation

artur-abliazimov
Copy link
Contributor

@artur-abliazimov artur-abliazimov commented Sep 5, 2024

https://github.com/orgs/warden-protocol/projects/12/views/1?pane=issue&itemId=78574759

Summary by CodeRabbit

  • New Features

    • Introduced enhanced voting logic for actions, allowing for better handling of approved and rejected votes.
    • Added functionality to create and manage action votes, including checks for action status and duplicate votes.
    • Implemented error handling for existing votes to improve robustness.
  • Bug Fixes

    • Resolved issues related to action timeout handling in the voting process.

@artur-abliazimov artur-abliazimov added protocol x-act Related to `x/act` module labels Sep 5, 2024
@artur-abliazimov artur-abliazimov self-assigned this Sep 5, 2024
Copy link
Contributor

coderabbitai bot commented Sep 5, 2024

Walkthrough

Walkthrough

The changes introduce new types and methods to enhance the voting mechanism for actions within the warden module. Key modifications include the addition of environments for approved and rejected votes, a complete implementation of the VoteForAction function, and improvements to the Action type for managing votes. Error handling is also enhanced with new errors for duplicate votes and participation, facilitating better state management and encapsulation of voting logic.

Changes

Files Change Summary
warden/x/act/keeper/actions.go Introduced ActionApprovedVotesEnv and ActionRejectedVotesEnv for handling vote environments; modified TryExecuteVotedAction to utilize these environments for action execution based on voting outcomes.
warden/x/act/keeper/msg_server_vote_for_action.go Implemented the VoteForAction function, adding logic for retrieving actions, checking timeouts, adding votes, and executing actions based on voting conditions.
warden/x/act/types/v1beta1/action.go Added NewVote function for creating ActionVote instances and AddOrUpdateVote method to the Action struct to manage votes, ensuring valid state checks and event emissions.
warden/x/act/types/v1beta1/errors.go Introduced ErrActionVoteAlreadyExists and ErrAlreadyParticipatedInVoting for handling cases of duplicate votes and prior participation, enhancing error management.
proto/warden/act/v1beta1/action_vote.proto Changed field name from vote to vote_type in ActionVote message for improved clarity.
proto/warden/act/v1beta1/events.proto Renamed field vote to vote_type in EventActionVoted message to enhance clarity.
proto/warden/act/v1beta1/tx.proto Updated field name from vote to vote_type in MsgVoteForAction message to reflect clearer terminology.

Possibly related issues

Tip

New features

Walkthrough comment now includes:

  • Possibly related PRs: A list of potentially related PRs to help you recall past context.
  • Suggested labels: CodeRabbit can now suggest labels by learning from your past PRs. You can also provide custom labeling instructions in the UI or configuration file.

Notes:

  • Please share any feedback in the discussion post on our Discord.
  • Possibly related PRs, automatic label suggestions based on past PRs, learnings, and possibly related issues require data opt-in (enabled by default).

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

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

vercel bot commented Sep 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
warden-help-center ⬜️ Ignored (Inspect) Visit Preview Sep 9, 2024 4:39pm

Copy link

github-actions bot commented Sep 5, 2024

Hey @artur-abliazimov and thank you for opening this pull request! 👋🏼

It looks like you forgot to add a changelog entry for your changes. Make sure to add a changelog entry in the 'CHANGELOG.md' file.

@@ -30,6 +30,73 @@ func (approvers ApproversEnv) Get(name string) (object.Object, bool) {

var _ shield.Environment = ApproversEnv{}
Copy link
Contributor

Choose a reason for hiding this comment

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

@Pitasi why do we need this here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess it's a type check.

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c992b59 and b29f31a.

Files selected for processing (4)
  • warden/x/act/keeper/actions.go (1 hunks)
  • warden/x/act/keeper/msg_server_vote_for_action.go (1 hunks)
  • warden/x/act/types/v1beta1/action.go (2 hunks)
  • warden/x/act/types/v1beta1/errors.go (1 hunks)
Additional context used
Path-based instructions (4)
warden/x/act/keeper/msg_server_vote_for_action.go (1)

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

warden/x/act/types/v1beta1/errors.go (1)

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

warden/x/act/types/v1beta1/action.go (1)

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

warden/x/act/keeper/actions.go (1)

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

Additional comments not posted (10)
warden/x/act/keeper/msg_server_vote_for_action.go (8)

11-11: Context unwrapping is correctly implemented.

The use of sdk.UnwrapSDKContext(goCtx) is appropriate for extracting the SDK context from the Go context, which is a standard practice in Cosmos SDK modules.


12-16: Proper error handling for action retrieval.

The function correctly handles the scenario where an action cannot be retrieved, which is crucial for robust error management. Returning the error directly is a good practice as it allows the caller to handle it appropriately.


18-24: Timeout condition and status update are well-handled.

The check for act.TimeoutHeight and the subsequent status update to ACTION_STATUS_TIMEOUT are well implemented. The error handling within this block ensures that any failure in updating the action's status or saving it back to the keeper results in an early return with an error, which is a good practice.


26-29: Clear and concise response construction for timeout scenario.

The response construction using types.MsgVoteForActionResponse with the action's status is clear and follows good coding practices by directly mapping the status to its string representation.


31-33: Error handling for adding a vote.

The function AddVote is called with proper error handling. If adding a vote fails, the function returns immediately with the error, which is a good practice to prevent further execution with an invalid state.


35-37: Error handling for updating the action keeper.

After adding a vote, the action is updated in the keeper. The error handling here is consistent with the rest of the function, ensuring that any issues during the save operation prevent further execution.


39-41: Error handling for executing the voted action.

The function TryExecuteVotedAction is called, and any errors are handled appropriately by returning them. This ensures that execution errors do not silently fail and are reported up the call stack.


43-43: Final response construction is correct and concise.

The final response is constructed correctly, providing the current status of the action. This is a clean and effective way to communicate the result of the voting process back to the caller.

warden/x/act/types/v1beta1/errors.go (1)

26-26: Well-defined new error variable.

The addition of ErrActionVoteAlreadyExists is well-integrated within the existing error definitions. The use of a unique error code and a clear, descriptive message aligns with best practices for error handling in the module.

warden/x/act/types/v1beta1/action.go (1)

18-24: Review of the NewVote function

The NewVote function is well-implemented and follows the Go best practices. It initializes an ActionVote struct with the provided parameters. The function is concise and effectively encapsulates the creation of a vote object.

  • Correctness: The function correctly assigns the participant, vote, and timestamp to the respective fields in the ActionVote struct.
  • Maintainability: The function is simple and straightforward, making it easy to maintain.
  • Performance: There are no apparent performance issues as the function simply constructs an object.

Overall, this function is approved as it meets the requirements and follows good coding practices.

warden/x/act/types/v1beta1/action.go Outdated Show resolved Hide resolved
warden/x/act/keeper/actions.go Outdated Show resolved Hide resolved
warden/x/act/keeper/actions.go Outdated Show resolved Hide resolved
warden/x/act/keeper/actions.go Outdated Show resolved Hide resolved
backsapc
backsapc previously approved these changes Sep 5, 2024
warden/x/act/keeper/actions.go Outdated Show resolved Hide resolved
@github-actions github-actions bot removed the x-warden label Sep 5, 2024
backsapc
backsapc previously approved these changes Sep 5, 2024
Copy link
Contributor

@mn13 mn13 left a comment

Choose a reason for hiding this comment

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

I believe that AddApprover calling in AddAction (v1beta1/action.go 243) should be replaced with AddVote (where we add initial approver for action)

@artur-abliazimov
Copy link
Contributor Author

I believe that AddApprover calling in AddAction (v1beta1/action.go 243) should be replaced with AddVote (where we add initial approver for action)

I do not think that the initial approver must be set during action creation. AddVote is added separately msg_server_vote_for_action.go => VoteForAction.

@Pitasi what do you think?

@Pitasi
Copy link
Contributor

Pitasi commented Sep 9, 2024

I believe that AddApprover calling in AddAction (v1beta1/action.go 243) should be replaced with AddVote (where we add initial approver for action)

I do not think that the initial approver must be set during action creation. AddVote is added separately msg_server_vote_for_action.go => VoteForAction.

@Pitasi what do you think?

i think that 99% of the times if I'm creating an action I also want to approve it, so it makes sense to save some time and gas and do it

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b46916f and a4e861f.

Files selected for processing (3)
  • warden/x/act/keeper/actions.go (3 hunks)
  • warden/x/act/keeper/msg_server_vote_for_action.go (1 hunks)
  • warden/x/act/types/v1beta1/action.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • warden/x/act/keeper/actions.go
Additional context used
Path-based instructions (2)
warden/x/act/keeper/msg_server_vote_for_action.go (1)

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

warden/x/act/types/v1beta1/action.go (1)

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

Additional comments not posted (1)
warden/x/act/types/v1beta1/action.go (1)

18-24: Constructor function NewVote is correctly implemented.

The NewVote function is straightforward and correctly sets up ActionVote instances. It follows best practices for constructor functions in Go.

warden/x/act/keeper/msg_server_vote_for_action.go Outdated Show resolved Hide resolved
warden/x/act/types/v1beta1/action.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.

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a4e861f and 716a3c0.

Files selected for processing (1)
  • warden/x/act/types/v1beta1/action.go (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • warden/x/act/types/v1beta1/action.go

Copy link
Contributor

@Pitasi Pitasi left a comment

Choose a reason for hiding this comment

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

one last thing then looks good 👌

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 716a3c0 and bccb869.

Files selected for processing (1)
  • warden/x/act/keeper/msg_server_vote_for_action.go (1 hunks)
Additional context used
Path-based instructions (1)
warden/x/act/keeper/msg_server_vote_for_action.go (1)

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

GitHub Check: lint
warden/x/act/keeper/msg_server_vote_for_action.go

[failure] 48-48:
undefined: fmt (typecheck)


[failure] 48-48:
undefined: fmt) (typecheck)


[failure] 48-48:
undefined: fmt) (typecheck)


[failure] 48-48:
undefined: fmt) (typecheck)

GitHub Check: test
warden/x/act/keeper/msg_server_vote_for_action.go

[failure] 48-48:
undefined: fmt

Comment on lines +11 to +51
ctx := sdk.UnwrapSDKContext(goCtx)
act, err := k.ActionKeeper.Get(ctx, msg.ActionId)

if err != nil {
return nil, err
}

if act.TimeoutHeight > 0 && act.TimeoutHeight < uint64(ctx.BlockHeight()) {
if err := act.SetStatus(ctx, types.ActionStatus_ACTION_STATUS_TIMEOUT); err != nil {
return nil, err
}
if err := k.ActionKeeper.Set(ctx, act); err != nil {
return nil, err
}

return &types.MsgVoteForActionResponse{
Status: act.Status.String(),
}, nil
}

if err := act.AddOrUpdateVote(ctx, msg.Participant, msg.VoteType); err != nil {
return nil, err
}

if err := k.ActionKeeper.Set(ctx, act); err != nil {
return nil, err
}

if msg.VoteType == types.ActionVoteType_VOTE_TYPE_APPROVED {
if err := k.TryExecuteVotedAction(ctx, &act); err != nil {
return nil, err
}
} else if msg.VoteType == types.ActionVoteType_VOTE_TYPE_REJECTED {
if err := k.TryRejectVotedAction(ctx, &act); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("unhandled VoteType value: %v", msg.VoteType)
}

return &types.MsgVoteForActionResponse{Status: act.Status.String()}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Comprehensive Review of VoteForAction Function

The VoteForAction function has been significantly updated to handle the complete logic flow for voting on actions. Here are the key observations and suggestions:

  1. Error Handling and Logic Flow:

    • The function correctly handles errors at multiple points, ensuring that any failure in retrieving actions or updating their status results in an immediate return of the error (lines 14-15, 19-20, 22-23, 32, 36, 41, 45). This is a good practice as it prevents further execution when an error state is detected.
  2. Handling of Timeout:

    • The logic to handle action timeouts is well implemented (lines 18-29). If the action has timed out, it updates the action's status and returns a response indicating the timeout status. This is crucial for maintaining the integrity of the action's lifecycle.
  3. Vote Handling and Action Execution:

    • The function handles different types of votes and attempts to execute or reject the action based on the vote type (lines 39-47). This is a critical part of the function, ensuring that actions are processed according to the collective decision of the participants.
  4. Performance Consideration:

    • As previously noted by another reviewer, the function makes multiple calls to ActionKeeper.Set (lines 22, 35). While necessary, it's important to review whether these calls can be optimized or batched to reduce overhead, especially in a blockchain context where such operations can be costly.
  5. Error Handling Improvement:

    • The function lacks the import for fmt, which is used in line 48. This results in a compilation error when handling unhandled VoteType values. This needs to be addressed by adding the appropriate import statement.
  6. Code Style and Best Practices:

    • The code adheres well to the Uber Golang style guide, with clear error handling and separation of concerns. However, the missing import highlights a need for better static analysis tool integration or review processes to catch such issues before PR submission.

Suggestions:

  • Consider optimizing the multiple Set calls if possible to improve performance.
  • Add the missing fmt import to fix the compilation error.
Tools
GitHub Check: lint

[failure] 48-48:
undefined: fmt (typecheck)


[failure] 48-48:
undefined: fmt) (typecheck)


[failure] 48-48:
undefined: fmt) (typecheck)


[failure] 48-48:
undefined: fmt) (typecheck)

GitHub Check: test

[failure] 48-48:
undefined: fmt

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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bccb869 and 5fd3439.

Files selected for processing (1)
  • warden/x/act/keeper/msg_server_vote_for_action.go (1 hunks)
Additional context used
Path-based instructions (1)
warden/x/act/keeper/msg_server_vote_for_action.go (1)

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

Additional comments not posted (4)
warden/x/act/keeper/msg_server_vote_for_action.go (4)

12-17: Robust error handling for action retrieval.

The function correctly handles errors when retrieving the action, ensuring that any failure results in an immediate return of the error. This is a good practice as it prevents further execution when an error state is detected.


19-25: Proper handling of action timeouts.

The logic to handle action timeouts is well implemented. If the action has timed out, it updates the action's status and returns a response indicating the timeout status. This is crucial for maintaining the integrity of the action's lifecycle.


32-38: Vote handling and action execution logic.

The function handles different types of votes and attempts to execute or reject the action based on the vote type. This is a critical part of the function, ensuring that actions are processed according to the collective decision of the participants.


49-50: Consider handling unhandled VoteType values.

The function includes a default case for unhandled VoteType values, which is a good practice for robustness. However, consider documenting potential VoteType values that might lead to this error to aid in debugging and future maintenance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
protobuf protocol x-act Related to `x/act` module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants