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

Implement x/govplus MsgSlashValidator #1016

Merged
merged 3 commits into from
Jan 30, 2024
Merged

Implement x/govplus MsgSlashValidator #1016

merged 3 commits into from
Jan 30, 2024

Conversation

roy-dydx
Copy link
Contributor

Changelist

  • Implement MsgSlashValidator for community enabled slashing

Test Plan

  • Wrote tests for correct validation of some fields.
  • Tested on a local network

Author/Reviewer Checklist

  • 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 the PR has breaking postgres changes to the indexer add the indexer-postgres-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.

Copy link
Contributor

coderabbitai bot commented Jan 26, 2024

Walkthrough

The overall change introduces and enhances governance functionalities within the dydxprotocol, specifically focusing on the ability to slash misbehaving validators through a new governance module, govplus. This update restructures imports, expands the dydxprotocol namespace with additional entities, and integrates govplus into various parts of the protocol. It includes new RPC methods, message types for slashing validators, and updates to keeper and testing structures to support the new governance actions.

Changes

File Path Change Summary
indexer/.../dydxprotocol/bundle.ts
indexer/.../dydxprotocol/govplus/tx.rpc.msg.ts
indexer/.../dydxprotocol/govplus/tx.ts
proto/dydxprotocol/govplus/tx.proto
Reordered/added imports; introduced govplus functionality for slashing validators; updated message types and RPC methods.
protocol/app/app.go
protocol/app/basic_manager/basic_manager.go
protocol/app/msgs/... (all_msgs.go, internal_msgs.go, internal_msgs_test.go)
protocol/app/simulation_test.go
protocol/lib/ante/internal_msg.go
protocol/testutil/app/app.go
Modified to include govplusmoduletypes.StoreKey; added govplusmodule to imported modules list; added new messages related to govplus; reordered imports; added govplusmodule package; added handling for govplus.GenesisState; new import for govplus; updated keeper and introduced SlashValidator method; defined new errors; introduced StakingKeeper interface; updated StoreKey; added validation for MsgSlashValidator.

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

@@ -1,851 +0,0 @@
package keeper_test
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Delete. Accidentally copied from x/ratelimit earlier.

@@ -6,5 +6,5 @@ const (
ModuleName = "govplus"

// StoreKey defines the primary module store key
StoreKey = ModuleName
StoreKey = "dydxgovplus"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Store keys can't be prefixes of another apparently, so I can't use govplus with gov.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks likes break the convention that StoreKey = ModuleName. Should we just update module name to dydxgovplus (and update directory names accordingly)?

Copy link
Contributor

Choose a reason for hiding this comment

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

May be worth adding a comment here to reflect that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feel like it's not a particularly worthwhile convention to keep. For example not all cosmos modules even follow the convention: https://github.com/dydxprotocol/cosmos-sdk/blob/dydx-fork-v0.50.3/x/auth/types/keys.go#L12

I'd rather have a nicer module name than try to keep everything the same.

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 UI

Commits Files that changed from the base of the PR and between a13958a and 456706f.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (19)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts (8 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.rpc.msg.ts (1 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/rpc.tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/app/app.go (2 hunks)
  • protocol/app/basic_manager/basic_manager.go (2 hunks)
  • protocol/app/msgs/all_msgs.go (1 hunks)
  • protocol/app/msgs/internal_msgs.go (2 hunks)
  • protocol/app/msgs/internal_msgs_test.go (1 hunks)
  • protocol/app/simulation_test.go (4 hunks)
  • protocol/lib/ante/internal_msg.go (2 hunks)
  • protocol/testutil/app/app.go (3 hunks)
  • protocol/x/govplus/keeper/keeper.go (1 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/keeper/msg_server_test.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/expected_keepers.go (1 hunks)
  • protocol/x/govplus/types/keys.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • protocol/app/simulation_test.go
Additional comments: 24
protocol/x/govplus/types/keys.go (1)
  • 9-9: The change from ModuleName to "dydxgovplus" for StoreKey is clear and improves specificity for the govplus module's store key. Ensure any data previously stored under the old key is migrated or accessed correctly with the new key.
protocol/x/govplus/types/errors.go (1)
  • 8-12: Introduced a new error ErrValidatorAddress for handling bech32 address conversion issues. This is a good practice for specific error handling and improves the module's robustness.
protocol/x/govplus/types/expected_keepers.go (1)
  • 10-12: Introduced StakingKeeper interface with a Slash method, enabling the govplus module to interact with staking logic for slashing validators. This is a necessary addition for the module's functionality.
indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.rpc.msg.ts (1)
  • 6-25: Implemented the slashValidator method in the Msg service interface and MsgClientImpl class for the govplus module, enabling slashing of validators through governance. The implementation follows standard practices for RPC services in TypeScript.
protocol/x/govplus/keeper/keeper.go (2)
  • 16-18: Added stakingKeeper field to the Keeper struct to enable interaction with staking logic, particularly for slashing validators. This addition is correctly implemented and necessary for the module's functionality.
  • 25-33: Updated NewKeeper function to accept stakingKeeper as a parameter and correctly initialize the Keeper struct with it. This change is essential for integrating staking logic into the govplus module.
indexer/packages/v4-protos/src/codegen/dydxprotocol/rpc.tx.ts (1)
  • 13-13: Updated the createRPCMsgClient function to include the govplus module, enabling RPC interactions with the module's services. This change is correctly implemented and aligns with the module's introduction.
protocol/x/govplus/keeper/msg_server.go (1)
  • 25-59: Implemented the SlashValidator method in the msgServer type, handling the logic for slashing validators. The method includes proper error handling and calls the stakingKeeper.Slash method. Note the use of panic on slashing error, which could halt the application but might be intended to ensure immediate attention to slashing errors.
protocol/x/govplus/keeper/msg_server_test.go (1)
  • 29-68: Added TestSlashValidator_InvalidAuthority test function to cover various input cases for the SlashValidator method. The test cases are well-structured and enhance the module's test coverage, particularly for error handling scenarios.
proto/dydxprotocol/govplus/tx.proto (1)
  • 12-55: Introduced the SlashValidator RPC method and message types MsgSlashValidator and MsgSlashValidatorResponse in the tx.proto file. The definitions are well-structured, with clear documentation and appropriate use of custom types for specific fields. This addition is crucial for the slashing functionality in the govplus module.
protocol/lib/ante/internal_msg.go (1)
  • 92-93: Added govplus.MsgSlashValidator to the IsInternalMsg function, correctly categorizing it as an internal message. This change aligns with the governance functionality of the govplus module.
protocol/app/basic_manager/basic_manager.go (1)
  • 31-31: Added govplusmodule to the ModuleBasics manager, integrating the govplus module into the application's basic module manager. This change is necessary for the module's lifecycle management within the application.
protocol/app/msgs/internal_msgs_test.go (1)
  • 100-102: Updated test cases in internal_msgs_test.go to include MsgSlashValidator and MsgSlashValidatorResponse, ensuring they are correctly categorized as internal messages. This addition maintains comprehensive test coverage for internal message categorization.
indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (2)
  • 5-35: Updated the MsgSlashValidator interface and added MsgSlashValidatorSDKType to align with protobuf naming conventions. Also, implemented encoding and decoding functions, enabling TypeScript-based interactions with the govplus module's slashing functionality.
  • 73-73: No changes were made to the MsgSlashValidatorResponse interface, but it's correctly defined to match the protobuf definition.
indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts (2)
  • 129-137: Reorganized imports and updated the dydxprotocol namespace to include additional entities such as govplus, indexer, prices, and others. This change improves the structure and expands the protocol's functionality within the TypeScript codebase.
  • 309-311: Expanded the ClientFactory entity within the dydxprotocol namespace to include a new import, extending its capabilities. This change is consistent with the protocol's functionality expansion.
protocol/app/msgs/internal_msgs.go (1)
  • 24-24: The import of the govplus module is correctly added to support the new message types for slashing validators.
protocol/app/msgs/all_msgs.go (1)
  • 193-195: The addition of MsgSlashValidator and MsgSlashValidatorResponse to AllTypeMessages correctly registers these new message types for the govplus module. This is necessary for the app's InterfaceRegistry to recognize and handle these messages appropriately. The syntax and structure of the additions are correct.
protocol/testutil/app/app.go (3)
  • 58-58: The import of the govplus package aligns with the PR's objective to integrate the govplus module into the application's governance mechanisms.
  • 202-203: The addition of govplus.GenesisState to the GenesisStates interface is necessary for supporting the new govplus module's genesis state in the application's test utilities. This change ensures that the govplus module's genesis state can be manipulated and tested like other modules' genesis states.
  • 255-256: The handling of govplus.GenesisState in the UpdateGenesisDocWithAppStateForModule function is crucial for enabling tests to modify the govplus module's genesis state within the application's genesis document. This addition is consistent with the PR's goal of integrating the govplus module and allows for comprehensive testing of the module's functionality.
protocol/app/app.go (2)
  • 394-394: The addition of govplusmoduletypes.StoreKey to the KV store keys is correctly implemented, ensuring the govplus module has its dedicated storage space.
  • 1011-1011: The initialization of GovPlusKeeper with app.StakingKeeper and govplusmoduletypes.StoreKey is correctly done, aligning with the PR's objectives to integrate the govplus module into the application. However, ensure that the GovPlusKeeper is correctly utilized in other parts of the application where necessary.

protocol/app/msgs/internal_msgs.go 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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a13958a and 8f2de8b.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (20)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts (8 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.rpc.msg.ts (1 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/rpc.tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/app/app.go (2 hunks)
  • protocol/app/basic_manager/basic_manager.go (2 hunks)
  • protocol/app/msgs/all_msgs.go (1 hunks)
  • protocol/app/msgs/internal_msgs.go (2 hunks)
  • protocol/app/msgs/internal_msgs_test.go (1 hunks)
  • protocol/app/simulation_test.go (4 hunks)
  • protocol/lib/ante/internal_msg.go (2 hunks)
  • protocol/testutil/app/app.go (3 hunks)
  • protocol/x/govplus/keeper/keeper.go (1 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/keeper/msg_server_test.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/expected_keepers.go (1 hunks)
  • protocol/x/govplus/types/keys.go (1 hunks)
  • protocol/x/govplus/types/keys_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (18)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/bundle.ts
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.rpc.msg.ts
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/rpc.tx.ts
  • proto/dydxprotocol/govplus/tx.proto
  • protocol/app/app.go
  • protocol/app/basic_manager/basic_manager.go
  • protocol/app/msgs/all_msgs.go
  • protocol/app/msgs/internal_msgs.go
  • protocol/app/msgs/internal_msgs_test.go
  • protocol/app/simulation_test.go
  • protocol/lib/ante/internal_msg.go
  • protocol/testutil/app/app.go
  • protocol/x/govplus/keeper/keeper.go
  • protocol/x/govplus/keeper/msg_server_test.go
  • protocol/x/govplus/types/errors.go
  • protocol/x/govplus/types/expected_keepers.go
  • protocol/x/govplus/types/keys.go
Additional comments: 2
protocol/x/govplus/types/keys_test.go (1)
  • 12-12: The change from "govplus" to "dydxgovplus" for types.StoreKey in the test assertion correctly aligns with the PR's objective to update the StoreKey value. This modification ensures that the test reflects the new StoreKey value, maintaining consistency across the codebase.
protocol/x/govplus/keeper/msg_server.go (1)
  • 4-9: The addition of new imports for context handling, error management, SDK types, governance types, and logging is appropriate for the functionality introduced by the SlashValidator method. These imports are necessary for the method's operation, including context management, error handling, and logging.

protocol/x/govplus/keeper/msg_server.go Outdated Show resolved Hide resolved

// Multiplier for how much of the validator's stake should be slashed.
// slash_factor * power = amount slashed
string slash_factor = 5 [
Copy link
Contributor

Choose a reason for hiding this comment

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

For my understanding why is the extra options needed, besides [(cosmos_proto.scalar) = "cosmos.Dec"] ? The gov v1 fields only have this one option, while v1beta fields have more options (example)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cosmos_protos are just annotations. gogoproto.customtype adds some codegen to help using that type. gogoproto.nullable affects the codegen (pointer or value)

ctx,
consAddr,
int64(msg.InfractionHeight),
msg.PowerAtInfractionHeight.BigInt().Int64(),
Copy link
Contributor

@teddyding teddyding Jan 26, 2024

Choose a reason for hiding this comment

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

It seems pretty bareboned that proposer would have to input the voting power themselves. Can we at least calculating the voting power from the delegation coin amount?

Also, from Slash() implementation, it calculates the amount to check with no additional validation on this number. If proposer mistakenly put a larger than actual amount, would this lead to any delegator being slashed more than its delegation amount? In general, it seems like we need some E2Es test to cover possible edge cases.

Copy link
Contributor

@teddyding teddyding Jan 26, 2024

Choose a reason for hiding this comment

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

The Slash() function in x/staking was not meant to be exposed for usage by human, and it has a couple of input contracts listed. Can we explicitly check these contracts (if not checked within the function)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You cannot because you can only get the delegation coin amount at the time of execution. If someone sees a proposal to slash them and knows they can avoid it by manipulating their delegation amount, it becomes worthless

Copy link
Contributor

@teddyding teddyding Jan 26, 2024

Choose a reason for hiding this comment

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

Can we at least calculating the voting power from the delegation coin amount?

I meant having user input the total delegated coin amount, as opposed to voting power. The conversion is trivial and can just be done on protocol if needed.

I'm not sure if voting power is ever exposed in any API. Can you check response fields of relevant x/staking queries and determine whether it returns staking power in tokens or voting power?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just in genesis state.. I can change this to tokens instead if you still prefer it that way. Let me know

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we explicitly check these contracts (if not checked within the function)?
All parts of the contract are checked in the Slash function and will error out. I've added to ValidateBasic as well.

(Except "Infraction was committed equal to or less than an unbonding period in the past, so all unbonding delegations and redelegations from that height are stored". It is up to the community to slash before the tokens are gone.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you identify a query supported by x/staking that directly returns either a validator's voting power or their total delegation amount, and update the proto field accordingly? This way a user can easily construct the proposal json.


ctx := sdk.UnwrapSDKContext(goCtx)

_, err = k.stakingKeeper.Slash(
Copy link
Contributor

Choose a reason for hiding this comment

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

curious - why do we call stakingKeeper.Slash here and not slashingKeeper.Slash? slashing calls staking.Slash but also emits some events

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No particular reason. We didn't have any use for that event.

@@ -6,5 +6,5 @@ const (
ModuleName = "govplus"

// StoreKey defines the primary module store key
StoreKey = ModuleName
StoreKey = "dydxgovplus"
Copy link
Contributor

Choose a reason for hiding this comment

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

May be worth adding a comment here to reflect that

_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight),
Copy link
Contributor

Choose a reason for hiding this comment

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

should the proto itself just use int64/sint64?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We use uint32 elsewhere for height.

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 UI

Commits Files that changed from the base of the PR and between 8f2de8b and 9082c1d.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (6)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/keys.go (1 hunks)
  • protocol/x/govplus/types/tx.go (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts
  • proto/dydxprotocol/govplus/tx.proto
  • protocol/x/govplus/types/errors.go
  • protocol/x/govplus/types/keys.go
Additional comments: 2
protocol/x/govplus/types/tx.go (1)
  • 12-36: The ValidateBasic method for MsgSlashValidator correctly validates the Authority and ValidatorAddress fields, checks for a positive PowerAtInfractionHeight, and ensures the SlashFactor is within a valid range. However, it's important to ensure that all error messages provide clear, actionable feedback and that all conditions are logically sound and secure.
protocol/x/govplus/keeper/msg_server.go (1)
  • 25-58: The SlashValidator method implementation in msg_server.go correctly handles the slashing of a validator, including authority validation, address parsing, and calling the stakingKeeper.Slash method. However, it's crucial to ensure that error handling is robust, logging is appropriate, and all inputs are validated correctly before proceeding with the slashing operation.

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 UI

Commits Files that changed from the base of the PR and between 8f2de8b and a59681e.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (8)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/keeper/msg_server_test.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/keys.go (1 hunks)
  • protocol/x/govplus/types/tx.go (1 hunks)
  • protocol/x/govplus/types/tx_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (6)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts
  • proto/dydxprotocol/govplus/tx.proto
  • protocol/x/govplus/keeper/msg_server_test.go
  • protocol/x/govplus/types/errors.go
  • protocol/x/govplus/types/keys.go
  • protocol/x/govplus/types/tx.go
Additional comments: 2
protocol/x/govplus/keeper/msg_server.go (1)
  • 4-9: The imports include context handling, error management, SDK types, governance types, and logging. Ensure these are all necessary for the functionality being implemented.
protocol/x/govplus/types/tx_test.go (1)
  • 17-64: The TestValidateBasic function covers various error scenarios for MsgSlashValidator, including invalid authority, bad address, incorrect power at infraction height, and invalid slash factor. Ensure all other relevant fields and scenarios are covered by tests to guarantee comprehensive validation.

Comment on lines 25 to 58
func (k msgServer) SlashValidator(
goCtx context.Context,
msg *types.MsgSlashValidator,
) (*types.MsgSlashValidatorResponse, error) {
if !k.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
govtypes.ErrInvalidSigner,
"invalid authority %s",
msg.Authority,
)
}

consAddr, err := sdk.ConsAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, types.ErrValidatorAddress
}

ctx := sdk.UnwrapSDKContext(goCtx)
_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight), // Casting from uint32
msg.PowerAtInfractionHeight.BigInt().Int64(),
msg.SlashFactor,
)
if err != nil {
log.ErrorLogWithError(
ctx,
"error occured when slashing validator",
err,
)
return nil, err
}
return &types.MsgSlashValidatorResponse{}, nil
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 27, 2024

Choose a reason for hiding this comment

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

The SlashValidator method correctly implements the slashing logic, including authority checks, address parsing, and invoking the Slash method on stakingKeeper. However, consider adding more detailed logging for debugging purposes, especially for the slashing operation's success case.

+ log.InfoLog(ctx, "Validator slashed successfully", "validatorAddress", msg.ValidatorAddress)

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 (k msgServer) SlashValidator(
goCtx context.Context,
msg *types.MsgSlashValidator,
) (*types.MsgSlashValidatorResponse, error) {
if !k.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
govtypes.ErrInvalidSigner,
"invalid authority %s",
msg.Authority,
)
}
consAddr, err := sdk.ConsAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, types.ErrValidatorAddress
}
ctx := sdk.UnwrapSDKContext(goCtx)
_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight), // Casting from uint32
msg.PowerAtInfractionHeight.BigInt().Int64(),
msg.SlashFactor,
)
if err != nil {
log.ErrorLogWithError(
ctx,
"error occured when slashing validator",
err,
)
return nil, err
}
return &types.MsgSlashValidatorResponse{}, nil
func (k msgServer) SlashValidator(
goCtx context.Context,
msg *types.MsgSlashValidator,
) (*types.MsgSlashValidatorResponse, error) {
if !k.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
govtypes.ErrInvalidSigner,
"invalid authority %s",
msg.Authority,
)
}
consAddr, err := sdk.ConsAddressFromBech32(msg.ValidatorAddress)
if err != nil {
return nil, types.ErrValidatorAddress
}
ctx := sdk.UnwrapSDKContext(goCtx)
_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight), // Casting from uint32
msg.PowerAtInfractionHeight.BigInt().Int64(),
msg.SlashFactor,
)
if err != nil {
log.ErrorLogWithError(
ctx,
"error occured when slashing validator",
err,
)
return nil, err
}
log.InfoLog(ctx, "Validator slashed successfully", "validatorAddress", msg.ValidatorAddress)
return &types.MsgSlashValidatorResponse{}, nil

Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds helpful. Can we add some info logs here for debugging purpose?

Copy link
Contributor

Choose a reason for hiding this comment

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

@teddyding Absolutely, adding info logs for debugging purposes is a great idea. Here's how you can include an info log right after the slashing operation succeeds, but before the method returns a response:

log.InfoLog(ctx, "Validator slashed successfully", "validatorAddress", msg.ValidatorAddress)

You can place this line of code right after the Slash method call within the SlashValidator function, ensuring that it logs a success message whenever a validator is successfully slashed. This will greatly aid in debugging and monitoring the slashing process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used to have info logs here. But it was just redundant cuz the underlying method has a bunch of logging already.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

protocol/x/govplus/keeper/msg_server.go Outdated Show resolved Hide resolved
protocol/x/govplus/keeper/msg_server.go Show resolved Hide resolved
@roy-dydx roy-dydx force-pushed the roy/slash branch 2 times, most recently from d6a064b to 7f63e5b Compare January 27, 2024 00:41
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 UI

Commits Files that changed from the base of the PR and between 8f2de8b and c723cdc.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (10)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/app/simulation_test.go (2 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/keeper/msg_server_test.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/expected_keepers.go (1 hunks)
  • protocol/x/govplus/types/keys.go (1 hunks)
  • protocol/x/govplus/types/tx.go (1 hunks)
  • protocol/x/govplus/types/tx_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (9)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts
  • proto/dydxprotocol/govplus/tx.proto
  • protocol/app/simulation_test.go
  • protocol/x/govplus/keeper/msg_server_test.go
  • protocol/x/govplus/types/errors.go
  • protocol/x/govplus/types/expected_keepers.go
  • protocol/x/govplus/types/keys.go
  • protocol/x/govplus/types/tx.go
  • protocol/x/govplus/types/tx_test.go
Additional comments: 2
protocol/x/govplus/keeper/msg_server.go (2)
  • 4-9: The imports section has been updated to include necessary packages for the new functionality. Ensure that all new imports are used within the file to avoid unnecessary dependencies.
  • 25-58: The SlashValidator method implementation correctly checks for authority, validates the validator address, and performs the slashing operation. However, there are a few areas to consider:
  1. Authority Validation (Lines 29-35): The method correctly checks if the caller has the authority to perform the slashing. This is crucial for ensuring that only authorized entities can execute this operation.

  2. Validator Address Parsing (Lines 37-40): The method correctly parses the validator address from Bech32 format. Proper error handling is implemented if parsing fails.

  3. Casting Infraction Height (Line 46): The casting of msg.InfractionHeight from uint32 to int64 is necessary due to SDK constraints. It's good practice to add a comment explaining why this casting is necessary for future maintainers.

  4. Error Logging (Lines 50-57): The method logs an error if the slashing operation fails, which is good for debugging purposes. However, consider enhancing the error message to include more context about the failure, such as the validator address.

  5. Response (Line 58): The method returns an empty response upon successful execution. Ensure that this aligns with the expected behavior and that no additional information is needed in the response.

Overall, the method is well-implemented with proper error handling and validation. Consider adding more context to the error logging for better traceability.

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 UI

Commits Files that changed from the base of the PR and between c723cdc and e6b0cf8.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (6)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/tx.go (1 hunks)
  • protocol/x/govplus/types/tx_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (5)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts
  • proto/dydxprotocol/govplus/tx.proto
  • protocol/x/govplus/types/errors.go
  • protocol/x/govplus/types/tx.go
  • protocol/x/govplus/types/tx_test.go
Additional comments: 1
protocol/x/govplus/keeper/msg_server.go (1)
  • 4-11: The imports have been organized and expanded to include necessary packages for context handling, error management, SDK types, governance types, and logging. This change supports the new functionality introduced in the SlashValidator method.

protocol/x/govplus/keeper/msg_server.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.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between c723cdc and 688049a.
Files ignored due to path filters (1)
  • protocol/x/govplus/types/tx.pb.go is excluded by: !**/*.pb.go
Files selected for processing (6)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts (1 hunks)
  • proto/dydxprotocol/govplus/tx.proto (1 hunks)
  • protocol/x/govplus/keeper/msg_server.go (2 hunks)
  • protocol/x/govplus/types/errors.go (1 hunks)
  • protocol/x/govplus/types/tx.go (1 hunks)
  • protocol/x/govplus/types/tx_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (5)
  • indexer/packages/v4-protos/src/codegen/dydxprotocol/govplus/tx.ts
  • proto/dydxprotocol/govplus/tx.proto
  • protocol/x/govplus/types/errors.go
  • protocol/x/govplus/types/tx.go
  • protocol/x/govplus/types/tx_test.go
Additional comments: 5
protocol/x/govplus/keeper/msg_server.go (5)
  • 4-10: The import section has been updated to include necessary packages for context handling, error management, SDK types, governance types, and logging. Ensure all new imports are utilized within the file to avoid unnecessary dependencies.
  • 1-13: > 📝 NOTE

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

The removal of NewMsgServerImpl is not shown in the provided code but mentioned in the summary. If this function is indeed removed, verify that it is not used elsewhere in the codebase to prevent breaking changes.

Verification successful

The search results indicate that NewMsgServerImpl is widely used across the codebase, with each module having its own implementation. This suggests that the function's removal from the govplus module is an isolated change. It's important to ensure that the govplus module has an alternative or updated mechanism in place for registering its message server. Given the pattern of usage across modules, the impact of the removal seems to be limited to the govplus module itself, assuming each module implements its own version of this function.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for references to NewMsgServerImpl across the codebase.
rg "NewMsgServerImpl"

Length of output: 8695

* 26-36: The `SlashValidator` method correctly checks if the caller has the necessary authority. This is a crucial step for security, ensuring that only authorized entities can initiate slashing. * 38-41: The conversion of `msg.ValidatorAddress` to a consensus address is properly handled, including error checking. This ensures that the address format is valid before proceeding with the slashing logic. * 52-60: The error handling within the slashing operation is robust, logging the error before returning it. This aids in debugging and monitoring. The method concludes by returning a successful response, which is the expected behavior.

Comment on lines +43 to +51
ctx := sdk.UnwrapSDKContext(goCtx)
_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight), // Casting from uint32
sdk.TokensToConsensusPower(
sdkmath.NewIntFromBigInt(msg.TokensAtInfractionHeight.BigInt()), sdk.DefaultPowerReduction),
msg.SlashFactor,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

The slashing operation correctly unwraps the SDK context and performs the slashing with the appropriate parameters. The casting of msg.InfractionHeight from uint32 to int64 is necessary due to SDK constraints. However, adding a comment explaining this casting would improve code readability and maintainability.

+ // Casting from uint32 to int64 is necessary due to SDK method signature constraints

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
ctx := sdk.UnwrapSDKContext(goCtx)
_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight), // Casting from uint32
sdk.TokensToConsensusPower(
sdkmath.NewIntFromBigInt(msg.TokensAtInfractionHeight.BigInt()), sdk.DefaultPowerReduction),
msg.SlashFactor,
)
ctx := sdk.UnwrapSDKContext(goCtx)
_, err = k.stakingKeeper.Slash(
ctx,
consAddr,
int64(msg.InfractionHeight), // Casting from uint32 to int64 is necessary due to SDK method signature constraints
sdk.TokensToConsensusPower(
sdkmath.NewIntFromBigInt(msg.TokensAtInfractionHeight.BigInt()), sdk.DefaultPowerReduction),
msg.SlashFactor,
)

@roy-dydx roy-dydx merged commit f3202cc into main Jan 30, 2024
33 checks passed
@roy-dydx roy-dydx deleted the roy/slash branch January 30, 2024 03:43
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.

4 participants