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

Gauges queries #8563

Merged
merged 14 commits into from
Sep 9, 2024
Merged

Gauges queries #8563

merged 14 commits into from
Sep 9, 2024

Conversation

najeal
Copy link
Contributor

@najeal najeal commented Jul 29, 2024

Closes: #6314

What is the purpose of the change

The purpose of the change is to make external integration easier by adding new queries:

  • External Gauges
  • Internal Gauges
  • GaugesByPoolID.

Testing and Verifying

This change added tests and can be verified as follows:

  • Added unit test that validates queries responses

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

Copy link
Contributor

coderabbitai bot commented Jul 29, 2024

Walkthrough

The changes enhance the querying capabilities of the incentives module by introducing new RPC methods to retrieve internal and external gauges, as well as gauges linked to specific pool IDs. Additionally, new filtering functions and comprehensive tests accompany these features, improving overall functionality and usability for external integrators.

Changes

Files Change Summary
proto/osmosis/incentives/query.proto Added RPC methods for InternalGauges, ExternalGauges, and GaugesByPoolID, including corresponding request and response message types.
x/incentives/keeper/gauge.go Introduced a new filtering function getGaugesFromIteratorAndFilter and modified existing methods to utilize this feature, enhancing gauge retrieval capabilities.
x/incentives/keeper/grpc_query.go Added new methods for querying gauges by pool ID, internal status, and external status, utilizing a new filtering function for flexible data access.
x/incentives/keeper/grpc_query_test.go Added TestGRPCSpecificGauges to validate the behavior of new gRPC queries for internal and external gauges and gauges by pool ID.
x/incentives/types/gauge.go Enhanced the Gauge struct with new methods for identifying gauge types based on locking mechanisms and pool ID associations.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant QueryService
    participant Keeper
    participant Database

    User->>CLI: Request for Internal Gauges
    CLI->>QueryService: Call InternalGauges()
    QueryService->>Keeper: GetInternalGauges()
    Keeper->>Database: Fetch internal gauges
    Database-->>Keeper: Return internal gauges
    Keeper-->>QueryService: Return gauges
    QueryService-->>CLI: Return gauge data
    CLI-->>User: Display internal gauges
Loading
sequenceDiagram
    participant User
    participant CLI
    participant QueryService
    participant Keeper
    participant Database

    User->>CLI: Request for Gauges By Pool ID
    CLI->>QueryService: Call GaugesByPoolID()
    QueryService->>Keeper: GetGaugesFromPoolID(poolID)
    Keeper->>Database: Fetch gauges by pool ID
    Database-->>Keeper: Return gauges
    Keeper-->>QueryService: Return gauges
    QueryService-->>CLI: Return gauge data
    CLI-->>User: Display gauges for pool ID
Loading

Assessment against linked issues

Objective Addressed Explanation
Create queries for internal and external gauges (#[6314])
Query gauges linked to specific pool IDs (#[6314])
Enhance usability of gauge querying for external integrators (#[6314])

Tip

We have updated our review workflow to use the Anthropic's Claude family of models. Please share any feedback in the discussion post on our Discord.


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ad3ec0f and 1fe715f.

Files selected for processing (1)
  • proto/osmosis/incentives/query.proto (2 hunks)
Additional comments not posted (9)
proto/osmosis/incentives/query.proto (9)

99-103: LGTM!

The InternalGauges RPC method is correctly defined, following the established pattern in the file. The naming and message types are appropriate for querying internal gauges.


104-108: LGTM!

The ExternalGauges RPC method is correctly defined, following the established pattern in the file. The naming and message types are appropriate for querying external gauges.


109-113: LGTM!

The GaugesByPoolID RPC method is correctly defined, following the established pattern in the file. The naming and message types are appropriate for querying gauges by pool ID. The path parameter id is correctly specified in the HTTP endpoint.


261-264: LGTM!

The QueryInternalGaugesRequest message type is correctly defined, following the established pattern in the file. The inclusion of the pagination field allows for paginated requests when querying internal gauges.


265-269: LGTM!

The QueryInternalGaugesResponse message type is correctly defined, following the established pattern in the file. The gauges field holds the list of internal gauges, and the pagination field allows for paginated responses. The (gogoproto.nullable) = false option ensures that the gauges field is not nullable.


271-274: LGTM!

The QueryExternalGaugesRequest message type is correctly defined, following the established pattern in the file. The inclusion of the pagination field allows for paginated requests when querying external gauges.


275-279: LGTM!

The QueryExternalGaugesResponse message type is correctly defined, following the established pattern in the file. The gauges field holds the list of external gauges, and the pagination field allows for paginated responses. The (gogoproto.nullable) = false option ensures that the gauges field is not nullable.


281-285: LGTM!

The QueryGaugesByPoolIDRequest message type is correctly defined, following the established pattern in the file. The id field represents the pool ID for which gauges are being queried, and the pagination field allows for paginated requests.


287-291: LGTM!

The QueryGaugesByPoolIDResponse message type is correctly defined, following the established pattern in the file. The gauges field holds the list of gauges associated with the specified pool ID, and the pagination field allows for paginated responses. The (gogoproto.nullable) = false option ensures that the gauges field is not nullable.


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.

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
Member

@mattverse mattverse left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! I like the idea, but left some comments! Please take a look

proto/osmosis/incentives/query.proto Outdated Show resolved Hide resolved
proto/osmosis/incentives/query.proto Outdated Show resolved Hide resolved
proto/osmosis/incentives/query.proto Show resolved Hide resolved
Comment on lines 74 to 79
func (gauge Gauge) IsInternalGauge() bool {
return gauge.IsNoLockGauge() && strings.HasPrefix(gauge.DistributeTo.GetDenom(), NoLockInternalPrefix)
}

func (gauge Gauge) IsExternalGauge() bool {
return gauge.IsNoLockGauge() && strings.HasPrefix(gauge.DistributeTo.GetDenom(), NoLockExternalPrefix)
Copy link
Member

Choose a reason for hiding this comment

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

AFAIR internal / external gauges does not have to be no lock gauge, (no lock gauge for only CL Pools)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's always InternalNoLock or ExternalNoLock from what I understand in Keeper.CreateGauge

func (k Keeper) CreateGauge(ctx sdk.Context, isPerpetual bool, owner sdk.AccAddress, coins sdk.Coins, distrTo lockuptypes.QueryCondition, startTime time.Time, numEpochsPaidOver uint64, poolId uint64) (uint64, error) {

But I may be wrong

Copy link
Member

Choose a reason for hiding this comment

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

if isNoLockGauge || distrTo.LockQueryType == lockuptypes.ByDuration {

I may also be wrong, but please look at this line

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 may have missunderstood what defines an External/Internal Gauge.
Hence my filtering on DistrTo.Denom looks wrong.
If I can have more information of what defines External/Internal Gauge I could correct it. Do you have this information?

The same for IsLinkToPool function, I am not sure to filter the right way.
Can you give me your opinion? 🙂

x/incentives/keeper/grpc_query.go Show resolved Hide resolved
@mattverse mattverse self-assigned this Jul 29, 2024
@mattverse
Copy link
Member

Also a kindly reminder to re generate protobuf

Copy link
Contributor

github-actions bot commented Aug 8, 2024

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!

@github-actions github-actions bot added the Stale label Aug 8, 2024
@najeal
Copy link
Contributor Author

najeal commented Aug 8, 2024

I updated the way to fetch internal gauges, taking into account the Balancer pools.

From what I have understood:
Balancer Pool: can have only internal gauges created at the same time than the pool.
Stableswap Pool: it does not have gauge linked
Concentrated PoolType: can have an internal gauge created at the same time than the pool. Then it can also have internal & external gauges created anytime.
CosmWasm Pool: it does not have gauge linked

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

Outside diff range, codebase verification and nitpick comments (1)
x/incentives/keeper/gauge.go (1)

Line range hint 36-53:
Review: getGaugesFromIteratorAndFilter Function.

The implementation of getGaugesFromIteratorAndFilter correctly iterates over the iterator and applies the filter function to each gauge. Error handling is done using panic, which might be appropriate for the context, but consider logging the error for better traceability.

Consider adding logging for errors to improve traceability:

if err != nil {
  ctx.Logger().Error(fmt.Sprintf("Error unmarshalling gauge IDs: %v", err))
  panic(err)
}

@github-actions github-actions bot removed the Stale label Aug 9, 2024
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!

@github-actions github-actions bot added the Stale label Aug 18, 2024
@czarcas7ic czarcas7ic removed the Stale label Aug 18, 2024
@mattverse mattverse closed this Aug 26, 2024
@mattverse mattverse reopened this Aug 26, 2024
type GaugeFilterFn func(*types.Gauge) bool

// getGaugesFromIterator iterates over everything in a gauge's iterator, until it reaches the end. Return all gauges iterated over.
func (k Keeper) getGaugesFromIteratorAndFilter(ctx sdk.Context, iterator db.Iterator, filter GaugeFilterFn) []types.Gauge {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this change right now? It seems like we're not calling this anymore

store := ctx.KVStore(q.Keeper.storeKey)
valStore := prefix.NewStore(store, keyPrefix)

pageRes, err := query.FilteredPaginate(valStore, pageReq, func(key []byte, value []byte, accumulate bool) (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we not filter than paginate? I'm curious if we paginate then filter inside, we wouldn't be getting correct pagination (e.g user wants 100 results, we do a filter inside and only returns 30 results). I might be wrong on this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huum the way I understand how FilteredPaginate works it will continue to iterate until it reaches the 100 results.

I just missed one important point: to return true at the end of the closure when new element is added to the result.
When the closure returns true the FilteredPaginate function increments the numbered of hit elements and verifies that num_hit < request.Limit before continuing to iterate.
I will make the update.

x/incentives/types/gauge.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@najeal najeal left a comment

Choose a reason for hiding this comment

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

Few points I want you to take a look @mattverse

x/incentives/keeper/grpc_query.go Show resolved Hide resolved
}
return pageRes, gauges, nil
}

// filterByPrefixAndDenom filters gauges based on a given key prefix and denom
func (q Querier) filterByPrefixAndDenom(ctx sdk.Context, prefixType []byte, denom string, pagination *query.PageRequest) (*query.PageResponse, []types.Gauge, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function looks not correct if I am right about the way FilteredPaginate works. What do you think @mattverse

Copy link
Member

Choose a reason for hiding this comment

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

which part are you sus about?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Iterating over newGauges the closure returns false if the denom is not the expected one.
In some situations we can miss a gauge
If len(newGauges) == 2
Index 0 : denom is not expecting one
Index 1 : denom is the expecting one

we are going to exit at first iteration without adding the second gauge in the response.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, this is an edge case as mentioned in the comment that we need to fix :(

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

Outside diff range, codebase verification and nitpick comments (2)
x/incentives/types/gauge.go (1)

69-79: Method implementation is correct but consider enhancing documentation.

The IsInternalGauge method correctly checks for internal gauge prefixes. Consider adding a comment to clarify that the method can check multiple prefixes, enhancing understanding for future maintainers.

x/incentives/keeper/grpc_query.go (1)

328-356: Method implementation is correct but consider enhancing documentation.

The getGaugesWithFilter method effectively retrieves gauges based on a filter function and pagination parameters. Consider adding a comment to clarify the edge case handling when multiple gauges start at the same time.

Copy link
Member

@mattverse mattverse 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. Let's merge this once conflicts are resolved 👍

@najeal
Copy link
Contributor Author

najeal commented Sep 3, 2024

@mattverse conflicts are resolved 👍

@mattverse mattverse closed this Sep 9, 2024
@mattverse mattverse reopened this Sep 9, 2024
@mattverse
Copy link
Member

mattverse commented Sep 9, 2024

We don't have a changelog for this, but for the sake of faster iterations merging this for now and I'll create a PR with the changelog right after

@mattverse mattverse merged commit bfa80e3 into osmosis-labs:main Sep 9, 2024
1 check 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.

Create Internal / External Queries for Incentives module
3 participants