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

convert UpdateBalance to use relative amount instead of absolute amount change #219

Closed

Conversation

leonz789
Copy link
Contributor

@leonz789 leonz789 commented Oct 17, 2024

Description

Use relative balance change compared to previous amount, instead of absolute change compared to 32ETH as base.\

Pro: This will suites all assets, don't need to hard code NSTETH assetID

Cons: Introduce complexity of synchronization for price-feeder

  • use hash to set event to indicate the exactly price update
  • query onchain balance infos for alls takers every time before feed balancechange of NST

Conclusion: If there's no too many chains for NST-restaking senario, the existing ETH-specified version is prefered.


Closes #XXX

Summary by CodeRabbit

  • New Features

    • Introduced enhanced staker management for native-restaking assets, including methods for updating validator lists and retrieving total balance changes.
    • Added a mechanism to hash prices exceeding a specified length for improved data handling.
  • Bug Fixes

    • Corrected balance calculations and assertions in tests to align with updated logic for staker operations.
  • Documentation

    • Updated comments to clarify the purpose and functionality of new and modified methods.

Copy link
Contributor

coderabbitai bot commented Oct 17, 2024

Walkthrough

The pull request introduces significant modifications to the keeper package, specifically in the CreatePrice function and staker management for native tokens. A new constant, maxEventPriceSize, is added to enforce a limit on price lengths, prompting a hashing mechanism for longer prices. Additionally, new methods for updating validator lists and retrieving total index counts for stakers are introduced, alongside adjustments to balance calculations in tests to align with the revised logic.

Changes

File Path Change Summary
x/oracle/keeper/msg_server_create_price.go Added constant maxEventPriceSize, updated CreatePrice function to hash long prices, modified event emission format.
x/oracle/keeper/native_token.go Added methods UpdateNSTValidatorListForStaker and GetNSTTotalIndex, adjusted deposit/withdrawal logic, removed maxEffectiveBalance.
x/oracle/keeper/native_token_test.go Updated TestNSTLifeCycleOneStaker for balance calculations and assertions, reflecting new staker management logic.

Possibly related PRs

Suggested labels

enhancement, C:Proto, C:Types

Suggested reviewers

  • bwhour
  • trestinlsd
  • MaxMustermann2
  • cloud8little

🐇 In the land of code, where changes bloom,
A price is hashed, dispelling the gloom.
Validators updated, stakers rejoice,
With balance corrections, we lift up our voice.
So hop along, dear devs, with glee,
For every change brings harmony! 🌟


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

🧹 Outside diff range and nitpick comments (5)
x/oracle/keeper/native_token_test.go (1)

⚠️ Remaining Absolute Balance Changes Detected

The verification script identified absolute balance changes in x/oracle/keeper/native_token_test.go:

  • amount32 := sdkmath.NewIntWithDecimal(32, 18)
  • amount22 := sdkmath.NewIntWithDecimal(22, 18)
  • amount54 := sdkmath.NewIntWithDecimal(54, 18)
  • amount49 := sdkmath.NewIntWithDecimal(49, 18)

Action Required:

  • Refactor these instances to use relative amount changes instead of absolute values to ensure consistency with the PR objectives.
🔗 Analysis chain

Line range hint 1-300: Overall assessment: Changes align well with PR objectives

The modifications to the TestNSTLifeCycleOneStaker function successfully implement the PR objective of converting UpdateBalance to use relative amount instead of absolute amount change. The consistent updates to expected balances across various operations (deposits, balance changes, withdrawals) demonstrate a thorough implementation of the new approach.

These changes enhance the test coverage for the new balance calculation logic while maintaining the overall structure and flow of the test function. This ensures that the test continues to validate the lifecycle of a non-staking token (NST) for a single staker accurately under the new relative balance change system.

The implementation appears to address the PR's goal of enhancing compatibility across various assets by eliminating hard-coded asset IDs. However, as mentioned in the PR description, this approach may introduce additional complexity, particularly concerning synchronization for the price feeder.

To ensure that these changes are consistently applied throughout the codebase, please run the following verification script:

This script will help identify any remaining instances of absolute balance changes in test files that might need to be updated to use relative amounts.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining instances of absolute balance changes in test files

# Search for patterns that might indicate absolute balance changes in test files
rg --type go -g '*_test.go' '(?i)(balance.*?=.*?[0-9]+e18|sdkmath\.NewIntWithDecimal\([0-9]+, 18\))' x/

Length of output: 441

x/oracle/keeper/msg_server_create_price.go (1)

79-80: Use fmt.Sprintf for string concatenation to enhance readability.

Concatenating strings using the + operator can be less readable and more error-prone. Utilizing fmt.Sprintf improves clarity and maintainability of the code.

Apply the following changes:

 // [tokenIDStr]_[roundIDStr]_[price]_[decimal]_price/hash
-sdk.NewAttribute(types.AttributeKeyFinalPrice, tokenIDStr+"_"+roundIDStr+"_"+newItem.PriceTR.Price+"_"+decimalStr+"_"+priceFormat),
+sdk.NewAttribute(
+    types.AttributeKeyFinalPrice,
+    fmt.Sprintf("%s_%s_%s_%s_%s", tokenIDStr, roundIDStr, newItem.PriceTR.Price, decimalStr, priceFormat),
+),

Ensure the fmt package is imported if not already done (as shown in the previous comment).

x/oracle/keeper/native_token.go (3)

Line range hint 157-171: Implement check for complete validator withdrawal before removal

The TODO comment indicates that additional checks are needed to verify if the validator has withdrawn all assets before removing it from ValidatorPubkeyList. Implementing this check will ensure correctness.

Would you like assistance in implementing this functionality?


Line range hint 197-202: Correct error message for clarity

The error message "remove unexist validator" is grammatically incorrect.

Proposed code change:

 if amountInt64 <= 0 {
-    return errors.New("remove unexist validator")
+    return errors.New("attempt to remove a non-existent validator")
 }

229-230: Consider upgrading to support other chains

The TODO comments indicate that the current limitation may not be suitable for other chains like Solana. Consider upgrading the implementation to be compatible with additional chains.

Would you like assistance in designing an upgrade to support other chains?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between bf6efa5 and 42ee670.

📒 Files selected for processing (3)
  • x/oracle/keeper/msg_server_create_price.go (2 hunks)
  • x/oracle/keeper/native_token.go (9 hunks)
  • x/oracle/keeper/native_token_test.go (2 hunks)
🧰 Additional context used
📓 Learnings (1)
x/oracle/keeper/native_token.go (2)
Learnt from: leonz789
PR: ExocoreNetwork/exocore#189
File: x/oracle/keeper/native_token.go:0-0
Timestamp: 2024-09-20T00:02:14.496Z
Learning: The function `UpdateNativeTokenByDepositOrWithdraw` is invoked through the assets module, and data validation is properly handled there.
Learnt from: leonz789
PR: ExocoreNetwork/exocore#189
File: x/oracle/keeper/native_token.go:0-0
Timestamp: 2024-10-09T01:26:20.424Z
Learning: The function `UpdateNativeTokenByDepositOrWithdraw` is invoked through the assets module, and data validation is properly handled there.
🔇 Additional comments (8)
x/oracle/keeper/native_token_test.go (3)

141-143: LGTM: Balance calculation correction

The change from 59 to 49 in the expected balance aligns with the PR objective of using relative amount changes instead of absolute amounts. This correction ensures that the test accurately reflects the new balance calculation logic after the UpdateNSTByBalanceChange operation.


149-152: LGTM: Consistent balance update in assets module

The change from 59e18 to 49e18 in the expected total deposit and withdrawable amounts is consistent with the previous balance change in the oracle module. This modification ensures that the assets module's state remains in sync with the oracle module's calculations, maintaining consistency across the system.


162-162: LGTM: Consistent balance update after withdrawal

The change from 29 to 19 in the expected balance after withdrawal is consistent with the previous balance changes and reflects the cumulative effect of all operations (deposits, balance changes, and withdrawals) on the staker's balance. This modification aligns with the PR objective of using relative amount changes and ensures the test accurately represents the new balance calculation logic.

x/oracle/keeper/msg_server_create_price.go (2)

9-9: Importing "crypto/sha256" is appropriate.

The addition of the crypto/sha256 package is necessary for hashing the price when it exceeds the maximum allowed size.


16-18: Constants are defined and named appropriately.

The new constant maxEventPriceSize and existing constants follow consistent naming conventions and improve code readability.

x/oracle/keeper/native_token.go (3)

172-173: Clarify need for extra checks on amount units

The TODO comment suggests uncertainty about whether the caller needs to perform extra checks to ensure the amount is in integer units. Consider clarifying whether validation should occur here or at the caller level.

Do you need assistance in determining where this validation should be implemented?


Line range hint 246-252: Logic correctly updates staker balances

The code correctly iterates over stakerChanges and updates the staker balances accordingly.


269-277: Balance updates and delegationKeeper call appear correct

The balance adjustments and the call to delegationKeeper.UpdateNSTBalance are implemented correctly.

Comment on lines +69 to +75
priceFormat := "price"
if len(newItem.PriceTR.Price) > maxEventPriceSize {
// This is mainly used for NST since they might have a string with big size to indicate the changes for stakers
hashPrice := sha256.Sum256([]byte(newItem.PriceTR.Price))
newItem.PriceTR.Price = string(hashPrice[:])
priceFormat = "hash"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Convert hash to a hexadecimal string for proper encoding.

Using string(hashPrice[:]) to convert the hash result into a string will produce unreadable characters because it interprets the byte array as a string of bytes. To correctly represent the hash as a readable string, it should be encoded in hexadecimal format.

Apply the following changes to fix the issue:

 priceFormat := "price"
 if len(newItem.PriceTR.Price) > maxEventPriceSize {
     // This is mainly used for NST since they might have a string with big size to indicate the changes for stakers
     hashPrice := sha256.Sum256([]byte(newItem.PriceTR.Price))
-    newItem.PriceTR.Price = string(hashPrice[:])
+    newItem.PriceTR.Price = fmt.Sprintf("%x", hashPrice)
     priceFormat = "hash"
 }

Additionally, ensure the fmt package is imported at the top of the file:

 import (
     "context"
     "errors"
     "strconv"
     "time"
+    "fmt"
     "crypto/sha256"
     "github.com/ExocoreNetwork/exocore/x/oracle/types"
     sdk "github.com/cosmos/cosmos-sdk/types"
 )
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
priceFormat := "price"
if len(newItem.PriceTR.Price) > maxEventPriceSize {
// This is mainly used for NST since they might have a string with big size to indicate the changes for stakers
hashPrice := sha256.Sum256([]byte(newItem.PriceTR.Price))
newItem.PriceTR.Price = string(hashPrice[:])
priceFormat = "hash"
}
priceFormat := "price"
if len(newItem.PriceTR.Price) > maxEventPriceSize {
// This is mainly used for NST since they might have a string with big size to indicate the changes for stakers
hashPrice := sha256.Sum256([]byte(newItem.PriceTR.Price))
newItem.PriceTR.Price = fmt.Sprintf("%x", hashPrice)
priceFormat = "hash"
}

Comment on lines +285 to +294
// TODO: set a persistent state to track this number
// GetNSTTotalIndex returns the count of how many time the NST balance of assetID has been changed including sources of deposit/withdraw, balanceChange
func (k Keeper) GetNSTTotalIndex(ctx sdk.Context, assetID string) int64 {
stakerInfos := k.GetStakerInfos(ctx, assetID)
totalIndex := int64(0)
for _, stakerInfo := range stakerInfos {
totalIndex += int64(len(stakerInfo.BalanceList))
}
return totalIndex
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Implement persistent state to track NST total index

The TODO comment suggests that storing the total index in a persistent state may improve performance by avoiding iteration over all stakers.

Would you like assistance in implementing a persistent state to track this number?

Comment on lines +255 to +260
length := len(stakerInfo.BalanceList)
// length should always be greater than 0 since the staker must deposit first, then we can update balance change
if length <= 0 {
return errors.New("UpdateBalane should not be executed on an empty balanceList")
}
newBalance = *(stakerInfo.BalanceList[length-1])
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in error message

Correct the typo in the error message: "UpdateBalane" should be "UpdateBalance".

Proposed code change:

 if length <= 0 {
-    return errors.New("UpdateBalane should not be executed on an empty balanceList")
+    return errors.New("UpdateBalance should not be executed on an empty balanceList")
}
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
length := len(stakerInfo.BalanceList)
// length should always be greater than 0 since the staker must deposit first, then we can update balance change
if length <= 0 {
return errors.New("UpdateBalane should not be executed on an empty balanceList")
}
newBalance = *(stakerInfo.BalanceList[length-1])
length := len(stakerInfo.BalanceList)
// length should always be greater than 0 since the staker must deposit first, then we can update balance change
if length <= 0 {
return errors.New("UpdateBalance should not be executed on an empty balanceList")
}
newBalance = *(stakerInfo.BalanceList[length-1])

Comment on lines +121 to +123
// UpdateValidatorListForStaker invoked when deposit/withdraw happedn for an NST asset
// deposit wiil increase the staker's balance with a new validatorPubkey added into that staker's validatorList
// withdraw will decrease the staker's balanec with a vadlidatorPubkey removed from that staker's validatorList
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct typos in function comments

There are several typographical errors in the function comments. Correcting them will improve clarity.

Proposed changes:

-// UpdateValidatorListForStaker invoked when deposit/withdraw happedn for an NST asset
+// UpdateValidatorListForStaker invoked when deposit/withdraw happen for an NST asset
-// deposit wiil increase the staker's balance with a new validatorPubkey added into that staker's validatorList
+// Deposit will increase the staker's balance with a new validatorPubkey added into the staker's ValidatorPubkeyList
-// withdraw will decrease the staker's balanec with a vadlidatorPubkey removed from that staker's validatorList
+// Withdraw will decrease the staker's balance with a validatorPubkey removed from the staker's ValidatorPubkeyList
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// UpdateValidatorListForStaker invoked when deposit/withdraw happedn for an NST asset
// deposit wiil increase the staker's balance with a new validatorPubkey added into that staker's validatorList
// withdraw will decrease the staker's balanec with a vadlidatorPubkey removed from that staker's validatorList
// UpdateValidatorListForStaker invoked when deposit/withdraw happen for an NST asset
// Deposit will increase the staker's balance with a new validatorPubkey added into the staker's ValidatorPubkeyList
// Withdraw will decrease the staker's balance with a validatorPubkey removed from the staker's ValidatorPubkeyList

Comment on lines +144 to 147
if amountInt64 > 0 {
// deopsit add a new validator into staker's validatorList
stakerInfo.ValidatorPubkeyList = append(stakerInfo.ValidatorPubkeyList, validatorPubkey)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo and prevent adding duplicate validators

Correct the typo in the comment. Additionally, to prevent duplicate validator entries, add a check to ensure validatorPubkey is not already in ValidatorPubkeyList before appending.

Proposed changes:

-// deopsit add a new validator into staker's validatorList
+// Deposit adds a new validator into the staker's ValidatorPubkeyList
+// Check if validatorPubkey already exists
+exists := false
+for _, vPubkey := range stakerInfo.ValidatorPubkeyList {
+    if vPubkey == validatorPubkey {
+        // Validator already exists, do not add
+        exists = true
+        break
+    }
+}
+if !exists {
     stakerInfo.ValidatorPubkeyList = append(stakerInfo.ValidatorPubkeyList, validatorPubkey)
+}
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if amountInt64 > 0 {
// deopsit add a new validator into staker's validatorList
stakerInfo.ValidatorPubkeyList = append(stakerInfo.ValidatorPubkeyList, validatorPubkey)
}
if amountInt64 > 0 {
// Deposit adds a new validator into the staker's ValidatorPubkeyList
// Check if validatorPubkey already exists
exists := false
for _, vPubkey := range stakerInfo.ValidatorPubkeyList {
if vPubkey == validatorPubkey {
// Validator already exists, do not add
exists = true
break
}
}
if !exists {
stakerInfo.ValidatorPubkeyList = append(stakerInfo.ValidatorPubkeyList, validatorPubkey)
}
}

Comment on lines +125 to +130
_, decimalInt, err := k.getDecimal(ctx, assetID)
if err != nil {
return err
}
// transfer amount into integer, for restaking the effective balance should always be whole unit
amountInt64 := amount.Quo(decimalInt).Int64()
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure amount is a multiple of decimalInt to prevent rounding errors

When converting amount to whole units, any fractional part will be truncated. To avoid potential loss of value, consider adding a check to confirm that amount is exactly divisible by decimalInt.

Proposed code change:

+// Ensure that amount is a multiple of decimalInt
+if !amount.Mod(decimalInt).IsZero() {
+    return errors.New("amount must be a whole number of units")
+}
 amountInt64 := amount.Quo(decimalInt).Int64()
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_, decimalInt, err := k.getDecimal(ctx, assetID)
if err != nil {
return err
}
// transfer amount into integer, for restaking the effective balance should always be whole unit
amountInt64 := amount.Quo(decimalInt).Int64()
_, decimalInt, err := k.getDecimal(ctx, assetID)
if err != nil {
return err
}
// transfer amount into integer, for restaking the effective balance should always be whole unit
// Ensure that amount is a multiple of decimalInt
if !amount.Mod(decimalInt).IsZero() {
return errors.New("amount must be a whole number of units")
}
amountInt64 := amount.Quo(decimalInt).Int64()

@leonz789 leonz789 marked this pull request as draft October 21, 2024 02:23
Copy link

github-actions bot commented Dec 6, 2024

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days-before-close if no further activity occurs.

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.

1 participant