Skip to content

Conversation

kingpinXD
Copy link

@kingpinXD kingpinXD commented Sep 3, 2025

Description

The pr refactors the delegationToDelegationResponse function to use TokensFromSharesRoundUp instead of TokensFromShares.

Closes: #25263

Summary by CodeRabbit

  • Bug Fixes

    • Updated delegation balance calculation to round up when converting shares to tokens, improving accuracy in Delegation query responses.
  • Tests

    • Added a test to verify Delegation gRPC query returns the expected balance with the updated rounding behavior.

Copy link
Contributor

coderabbitai bot commented Sep 3, 2025

📝 Walkthrough

Walkthrough

Delegation query rounding behavior changed by using TokensFromSharesRoundUp when converting shares to tokens in delegationToDelegationResponse. A new GRPC test validates the Delegation query returns the expected integer amount without precision loss.

Changes

Cohort / File(s) Change Summary
Staking query rounding
x/staking/keeper/grpc_query.go
Switched bond amount computation from TokensFromShares(...).TruncateInt() to TokensFromSharesRoundUp(...).TruncateInt() in delegationToDelegationResponse.
Staking GRPC tests
x/staking/keeper/grpc_query_test.go
Added TestGRPCQueryDelegation validating Delegation query returns expected integer balance; introduced math imports and setup for validator/delegation state.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix precision loss in Delegation query display by rounding result in query layer (#25263)

Assessment against linked issues: Out-of-scope changes

(empty)

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

🧹 Nitpick comments (5)
x/staking/keeper/grpc_query.go (1)

641-647: Align redelegation query rounding with delegation query
Replace the floor-based token calculation in redelegationsToRedelegationResponses with a round-up variant to avoid under-reporting and match delegationToDelegationResponse.

@@ x/staking/keeper/grpc_query.go:646
-               val.TokensFromShares(entry.SharesDst).TruncateInt(),
+               val.TokensFromSharesRoundUp(entry.SharesDst).TruncateInt(),
x/staking/keeper/grpc_query_test.go (4)

83-83: Assert parsing success for delegation amount.

Check ok to avoid silently using a zero value on malformed input.

-        amount, ok := math.NewIntFromString(delegationAmount)
+        amount, ok := math.NewIntFromString(delegationAmount)
+        require.True(ok)

77-77: Prefer non-panicking decimal parse in tests.

Use the non-Must variant with an assertion for clearer failure reporting.

-        shares := math.LegacyMustNewDecFromStr(validatorDelegatorShares)
+        shares, err := math.LegacyNewDecFromStr(validatorDelegatorShares)
+        require.NoError(err)

75-80: Naming nit: avoid reusing shares for two different concepts.

Using shares for validator’s DelegatorShares and again for the delegator’s computed shares is confusing. The earlier diff renames the latter to delegationShares; keep that rename to improve clarity.

Also applies to: 84-84


65-99: Broaden coverage of the rounding change.

Add parallel assertions for:

  • DelegatorDelegations and ValidatorDelegations endpoints (they flow through delegationsToDelegationResponses) to ensure consistent round-up behavior.
  • A case where the pre-fix output underflowed by < 1 uatom to prove the fix.

I can draft these tests if helpful.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Knowledge Base: Disabled due to Reviews > Disable Knowledge Base setting

📥 Commits

Reviewing files that changed from the base of the PR and between 13d51f0 and 369f406.

📒 Files selected for processing (2)
  • x/staking/keeper/grpc_query.go (1 hunks)
  • x/staking/keeper/grpc_query_test.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Summary
🔇 Additional comments (2)
x/staking/keeper/grpc_query.go (1)

597-598: Correct fix: round up before truncation to eliminate underflow in query output.

Switching to TokensFromSharesRoundUp(...).TruncateInt() resolves the precision loss described in #25263 without touching core math.

x/staking/keeper/grpc_query_test.go (1)

74-75: Ensure correct argument type for NewValidator.

testutil.NewValidator typically expects sdk.ValAddress; passing raw []byte may not compile across SDK versions. Cast explicitly for safety.

-        validator := testutil.NewValidator(s.T(), PKs[0].Address().Bytes(), PKs[0])
+        validator := testutil.NewValidator(s.T(), sdk.ValAddress(PKs[0].Address().Bytes()), PKs[0])

@aljo242
Copy link
Contributor

aljo242 commented Sep 15, 2025

needs a changelog!

@aljo242
Copy link
Contributor

aljo242 commented Sep 25, 2025

@kingpinXD lint failing

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.

Precision Loss in Delegation Queries Causes Incorrect Amount Display
3 participants