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

fix: genesis data validation #259

Merged

Conversation

MaxMustermann2
Copy link
Contributor

@MaxMustermann2 MaxMustermann2 commented Dec 5, 2024

  1. In x/assets, non-Ethereum assets are now supported.
  2. The total_staking_amount for the exo token is not recorded, and hence, the validation fails. Instead, this should be statefully checked.
  3. Zero slashing is exported, so importing should be allowed too. The other alternative is to disallow 0 slashing export.

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced asset management with improved initialization of key components, including AssetsKeeper, DelegationKeeper, and StakingKeeper.
    • Introduced a new AccountKeeper interface for better account management.
    • Added new CLI command for querying associated stakers by operator.
    • Implemented a method to retrieve associated stakers for a specified operator.
    • Expanded delegation module functionality with new query messages and RPC method.
  • Bug Fixes

    • Improved validation logic for asset delegation during initialization to ensure accurate financial data.
    • Adjusted slashing state validations to allow zero values while maintaining checks for nil or negative amounts.
    • Added duplicate asset ID checks in asset validation.
  • Documentation

    • Updated comments and documentation to clarify changes and functionality across various components.

Copy link
Contributor

coderabbitai bot commented Dec 5, 2024

Walkthrough

The pull request introduces modifications to the initialization logic of the ExocoreApp, specifically updating the AssetsKeeper and DelegationKeeper to include an AccountKeeper parameter. Changes to the InitGenesis function in x/assets/keeper/genesis.go enhance error handling related to asset delegation. Additionally, the Keeper struct in x/assets/keeper/keeper.go is updated to incorporate an AccountKeeper, while a new AccountKeeper interface is introduced in x/assets/types/expected_keepers.go. Overall, these changes improve asset and delegation management within the application.

Changes

File Path Change Summary
app/app.go Updated AssetsKeeper and DelegationKeeper initialization to include app.AccountKeeper.
x/assets/keeper/genesis.go Enhanced InitGenesis function with new imports, added exoDelegation variable, and improved validation logic for asset delegation.
x/assets/keeper/keeper.go Added ak field of type assetstype.AccountKeeper to Keeper struct and updated NewKeeper constructor accordingly.
x/assets/types/expected_keepers.go Introduced new AccountKeeper interface with GetModuleAccount method.
x/assets/types/genesis.go Removed Ethereum address validation from ValidateTokens method and added exception handling for native assets in ValidateOperatorAssets.
x/operator/types/genesis.go Modified ValidateSlashStates method to allow zero values for slashing amounts while maintaining checks for nil and negative amounts.
proto/exocore/delegation/v1/query.proto Added new messages QueryAssociatedStakersByOperatorReq and QueryAssociatedStakersByOperatorResponse, along with the QueryAssociatedStakersByOperator RPC method.
x/delegation/client/cli/query.go Introduced a new CLI command QueryAssociatedStakersByOperator for querying associated stakers by operator.
x/delegation/keeper/delegation_state.go Added GetAssociatedStakers method to retrieve staker IDs associated with a specified operator.
x/delegation/keeper/grpc_query.go Introduced QueryAssociatedStakersByOperator method to handle queries for associated stakers by operator.
x/dogfood/types/params.go Modified ValidateAssetIDs function to include a check for duplicate asset IDs.

Possibly related PRs

Suggested labels

enhancement, C:Types

Suggested reviewers

  • cloud8little
  • trestinlsd
  • leonz789
  • bwhour
  • mikebraver
  • adu-web3

🐇 In the code we hop and play,
With assets bright, we pave the way.
Account keepers join the fun,
Together we’ll make our app run!
With validations strong and true,
Our changes bring a world anew! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between cdd78ed and 5c1ba95.

📒 Files selected for processing (1)
  • x/delegation/keeper/delegation_state.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • x/delegation/keeper/delegation_state.go

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6fe63ef and fa83a40.

📒 Files selected for processing (6)
  • app/app.go (1 hunks)
  • x/assets/keeper/genesis.go (3 hunks)
  • x/assets/keeper/keeper.go (3 hunks)
  • x/assets/types/expected_keepers.go (2 hunks)
  • x/assets/types/genesis.go (1 hunks)
  • x/operator/types/genesis.go (1 hunks)
🔇 Additional comments (12)
x/assets/keeper/genesis.go (2)

5-7: Imports added correctly

The added imports for utils and delegationtypes are necessary and appropriately included.


Line range hint 51-63: Correct accumulation of exoDelegation for Exocore assets

The logic to accumulate exoDelegation when assetInfo.AssetID == types.ExocoreAssetID is correctly implemented.

x/assets/types/expected_keepers.go (1)

19-22: Addition of AccountKeeper interface is appropriate

The AccountKeeper interface with the GetModuleAccount method is correctly defined to support interactions with module accounts.

x/assets/keeper/keeper.go (3)

18-18: Included ak field in Keeper struct

Adding the ak field of type assetstype.AccountKeeper enhances the keeper's ability to access module account information.


28-28: Updated NewKeeper constructor to accept AccountKeeper

Including ak assetstype.AccountKeeper as a parameter in the NewKeeper function is necessary for initializing the ak field.


41-41: Initialized ak field in Keeper

The ak field is properly initialized within the Keeper struct.

x/assets/types/genesis.go (2)

248-251: Adjusted validation to exclude native asset from total staking check

The validation now skips the total staking amount check for the native asset (ExocoreAssetID). This change acknowledges that native assets are handled by x/bank and should not be subjected to the same constraints.


Line range hint 412-417: Allowed zero values in slashing amounts from the assets pool

The validation logic now permits zero values for slashFromAssetsPool.Amount, ensuring consistency when exporting data that may include zero values.

x/operator/types/genesis.go (1)

412-417: Permitted zero values in slashing amount validation

The updated validation in ValidateSlashStates allows zero values for slashFromAssetsPool.Amount, which aligns with the requirement to handle zero slashing exports consistently.

app/app.go (3)

Line range hint 554-562: LGTM! Dependencies properly ordered.

The addition of AccountKeeper and BankKeeper to DelegationKeeper is consistent with the changes to AssetsKeeper. The dependency order is correct, with AssetsKeeper being initialized first.


Line range hint 1012-1024: LGTM! Module initialization order is correct.

The module initialization order properly handles dependencies:

  1. Assets module is initialized first
  2. Delegation module follows
  3. Other dependent modules are initialized afterward

This order ensures proper genesis data validation, which aligns with the PR objectives.


550-553: LGTM! Verify keeper interface usage.

The addition of AccountKeeper and BankKeeper as dependencies to AssetsKeeper is appropriate for handling non-Ethereum assets. This change aligns with the PR objectives.

Let's verify the keeper interface usage:

✅ Verification successful

Let me verify the keeper interface usage with one more script to check the actual implementation in the assets keeper:


Keeper interfaces are properly defined and used

The verification confirms that:

  • The AccountKeeper and BankKeeper interfaces are properly defined in x/assets/types/expected_keepers.go
  • The AssetsKeeper correctly includes both keepers as dependencies in its struct
  • The interfaces define appropriate methods needed for asset management:
    • AccountKeeper: Access to module accounts
    • BankKeeper: Balance queries and coin operations
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the AccountKeeper and BankKeeper interfaces are properly defined and used

# Check for AccountKeeper interface definition
ast-grep --pattern 'type AccountKeeper interface {
  $$$
}'

# Check for BankKeeper interface definition
ast-grep --pattern 'type BankKeeper interface {
  $$$
}'

# Check for usage of these interfaces in AssetsKeeper
ast-grep --pattern 'type Keeper struct {
  $$$
  ak AccountKeeper
  bk BankKeeper
  $$$
}'

Length of output: 8216


Script:

#!/bin/bash
# Check the AssetsKeeper struct and its initialization
rg -A 10 "type Keeper struct" x/assets/keeper/

Length of output: 572

x/assets/keeper/genesis.go Outdated Show resolved Hide resolved
@cloud8little
Copy link
Contributor

Testing.

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 UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3bfe8e5 and cdd78ed.

⛔ Files ignored due to path filters (2)
  • x/delegation/types/query.pb.go is excluded by !**/*.pb.go
  • x/delegation/types/query.pb.gw.go is excluded by !**/*.pb.gw.go
📒 Files selected for processing (5)
  • proto/exocore/delegation/v1/query.proto (2 hunks)
  • x/delegation/client/cli/query.go (2 hunks)
  • x/delegation/keeper/delegation_state.go (1 hunks)
  • x/delegation/keeper/grpc_query.go (1 hunks)
  • x/dogfood/types/params.go (1 hunks)
🔇 Additional comments (7)
x/delegation/keeper/grpc_query.go (1)

61-70: LGTM! Clean implementation following established patterns.

The implementation is concise, handles errors appropriately, and follows the same patterns as other query methods in the file.

proto/exocore/delegation/v1/query.proto (2)

117-127: LGTM! Well-structured message definitions.

The message types are well-defined with clear field names and appropriate documentation.


170-175: LGTM! RPC method follows best practices.

The RPC method is properly configured with:

  • Module query safety option
  • RESTful HTTP endpoint
  • Consistent path parameter mapping
x/delegation/client/cli/query.go (2)

35-35: LGTM! Command properly registered.

The new command is correctly added to the parent command's list.


249-279: LGTM! Well-structured command implementation.

The implementation follows best practices:

  • Clear usage description
  • Proper argument validation
  • Consistent error handling
  • Follows the established pattern of other query commands
x/dogfood/types/params.go (2)

123-132: LGTM! Efficient duplicate checking implementation.

The implementation uses a map for O(1) lookup of duplicates, which is the optimal approach. The error message is clear and helpful for debugging.


125-128: Verify ParseID compatibility with non-Ethereum assets.

Since this PR adds support for non-Ethereum assets, we should verify that assetstypes.ParseID can handle all supported asset ID formats.

✅ Verification successful

Based on the gathered information, I can now provide a final response:

ParseID implementation supports non-Ethereum assets

The ParseID function and asset ID validation system is designed to handle both Ethereum and non-Ethereum assets:

  • The asset ID format is flexible, using a simple address_chainID structure where:
    • The address part can be any hex string (not strictly Ethereum-specific)
    • The chainID part is a hex-encoded number identifying the client chain
  • The system includes specific support for:
    • Native assets (ExocoreAssetID)
    • Native staking tokens (NST with prefix "NST")
    • General staking assets (validated via IsStakingAsset)

The implementation is chain-agnostic and the validation focuses on format correctness rather than enforcing Ethereum-specific rules.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check ParseID implementation for non-Ethereum asset support

# Search for ParseID implementation and tests
ast-grep --pattern 'func ParseID($$$) {
  $$$
}'

# Look for test cases covering different asset formats
rg -A 5 "TestParseID" 

Length of output: 72


Script:

#!/bin/bash
# Let's try a different approach to locate ParseID implementation and its tests

# Search for ParseID function across the codebase
rg -g '*.go' -B 2 -A 5 'func ParseID'

# Search for any test files containing ParseID
fd -e go -x grep -l "ParseID" {}

# Look for asset ID related constants or documentation
rg -g '*.go' -g '*.md' "asset.*ID"

Length of output: 69736


Script:

#!/bin/bash
# Let's examine the ParseID implementation and its usage pattern

# Look for ParseID implementation
rg -g '*.go' -B 2 -A 10 'func ParseID.*string.*error'

# Look for ValidateID implementation which might be used for validation
rg -g '*.go' -B 2 -A 10 'func ValidateID'

# Search for any tests or documentation about supported asset ID formats
rg -g '*.go' -g '*.md' -i 'asset.*id.*format'

Length of output: 3105


Script:

#!/bin/bash
# Let's check for asset ID validation and native asset support

# Look for native/non-native asset constants and definitions
rg -g '*.go' "NativeAssetID|ExocoreAssetID|NSTIDPrefix"

# Check for any asset ID validation in the assets module
rg -g '*.go' -B 2 -A 5 "IsStakingAsset|IsNST"

# Look for asset ID format examples in tests
rg -g '*_test.go' "assetID.*:.*\".*\""

Length of output: 9902

x/delegation/keeper/delegation_state.go Outdated Show resolved Hide resolved
@MaxMustermann2
Copy link
Contributor Author

MaxMustermann2 commented Dec 6, 2024

Query test using local_node.sh

Command

exocored query delegation QueryAssociatedStakersByOperator \
    exo18cggcpvwspnd5c6ny8wrqxpffj5zmhklprtnph --output json | jq

Response

{
  "stakers": [
    "0x3e108c058e8066da635321dc3018294ca82ddedf_0x65"
  ]
}

Copy link
Contributor

@bwhour bwhour left a comment

Choose a reason for hiding this comment

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

looks good.

Copy link
Contributor

@TimmyExogenous TimmyExogenous left a comment

Choose a reason for hiding this comment

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

LGTM

@MaxMustermann2 MaxMustermann2 merged commit 27a99e2 into ExocoreNetwork:develop Dec 6, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants