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

Staking-squid v24 #836

Merged
merged 8 commits into from
Sep 4, 2024
Merged

Conversation

marc-aurele-besner
Copy link
Collaborator

@marc-aurele-besner marc-aurele-besner commented Aug 28, 2024

User description

Staking-squid v24

  • Separate OperatorProfile in a separate table (to avoid the case where a operator edit his row while the indexer is also editing his row and therefore clear the field (if the indexer cache had not the new values)
  • Improve logic around profitability

PR Type

enhancement, bug fix


Description

  • Separated OperatorProfile into a new table and GraphQL type to prevent conflicts during updates.
  • Improved logic for calculating domain's current share price and updated related properties.
  • Refactored event processing functions to remove unused code and optimize cache handling.
  • Implemented batch saving to avoid large queries that could fail.
  • Renamed RewardEvent to Reward and updated related database schema and model exports.

Changes walkthrough 📝

Relevant files
Enhancement
11 files
epoch.ts
Update domain runtime and share calculation logic               

indexers/staking-squid/src/events/epoch.ts

  • Changed string comparison from "AutoId" to "autoId".
  • Updated logic for calculating domain's current share price.
  • Added new properties for domain's current total shares and share
    price.
  • +12/-3   
    operator.ts
    Update operator reward event handling                                       

    indexers/staking-squid/src/events/operator.ts

  • Changed event handling to use operatorRewards instead of
    operatorRewardedEvents.
  • +2/-5     
    unlock.ts
    Refactor unlock event processing and cache updates             

    indexers/staking-squid/src/events/unlock.ts

  • Updated cache handling for domain, account, operator, and nominator.
  • Removed redundant code for processing unlocked events.
  • +16/-35 
    withdraw.ts
    Update nominator pending action on withdrawal                       

    indexers/staking-squid/src/events/withdraw.ts

    • Added pending action update for nominator during stake withdrawal.
    +2/-0     
    operator.ts
    Enhance nominator updates during operator deregistration 

    indexers/staking-squid/src/extrinsics/operator.ts

  • Added pending action and estimated withdrawals update for nominator
    during operator deregistration.
  • +2/-0     
    index.ts
    Update model exports and rename reward event                         

    indexers/staking-squid/src/model/generated/index.ts

  • Renamed RewardEvent to Reward.
  • Added export for operatorProfile.model.
  • +2/-1     
    bundle.ts
    Modify bundle author ID generation                                             

    indexers/staking-squid/src/storage/bundle.ts

    • Changed bundle author ID generation logic.
    +1/-1     
    operator.ts
    Refactor operator creation and reward event handling         

    indexers/staking-squid/src/storage/operator.ts

  • Removed operator profile fields from operator creation.
  • Updated reward event creation to use event ID.
  • +7/-17   
    cache.ts
    Optimize cache handling and batch saving                                 

    indexers/staking-squid/src/utils/cache.ts

  • Changed cache structure for operator rewards.
  • Implemented batch saving to prevent large queries.
  • +12/-6   
    1724269616356-Data.js
    Database schema changes for operator profile and rewards 

    indexers/staking-squid/db/migrations/1724269616356-Data.js

  • Added new table for operator_profile.
  • Renamed reward_event table to reward.
  • +28/-18 
    schema.graphql
    Update GraphQL schema for operator profile and rewards     

    indexers/staking-squid/schema.graphql

  • Moved operator profile fields to a new OperatorProfile type.
  • Renamed RewardEvent type to Reward.
  • +23/-13 
    Cleanup
    1 files
    index.ts
    Clean up unused event processing functions                             

    indexers/staking-squid/src/events/index.ts

    • Removed unused event processing functions.
    +0/-6     
    Configuration changes
    1 files
    squid.yaml
    Update staking-squid version to 24                                             

    indexers/staking-squid/squid.yaml

    • Updated version from 21 to 24.
    +1/-1     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    netlify bot commented Aug 28, 2024

    Deploy Preview for dev-astral canceled.

    Name Link
    🔨 Latest commit 48fae87
    🔍 Latest deploy log https://app.netlify.com/sites/dev-astral/deploys/66cf0879951d9a0008dc6ada

    Copy link

    netlify bot commented Aug 28, 2024

    Deploy Preview for dev-astral canceled.

    Name Link
    🔨 Latest commit 36fdbcc
    🔍 Latest deploy log https://app.netlify.com/sites/dev-astral/deploys/66d5df3d49c50900070fa300

    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Logic Change
    The logic for determining _domain.runtime has been changed from checking for "AutoId" to "autoId". Ensure that this change aligns with expected data formats and that all dependent systems or data sources have been updated to accommodate this case sensitivity change.

    Calculation Update
    The calculation for domain.currentSharePrice has been updated. It's crucial to verify the correctness of this new formula, especially how it handles cases where domain.currentTotalShares is zero to prevent division by zero errors or other logical flaws.

    State Management
    Significant changes have been made to how state is managed and saved, particularly with the addition of multiple cache.set calls for domains, accounts, operators, and nominators. It's important to ensure that these changes do not introduce any race conditions or inconsistencies in state management, especially under high load.

    Database Schema Changes
    New tables and indices have been created, especially for operator_profile. Review the schema changes to ensure they meet the application's requirements and that they are optimized for the queries they will support. Also, ensure that foreign keys and relationships are correctly defined.

    Copy link

    github-actions bot commented Aug 28, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Add error handling for division by zero in share price calculation

    Ensure that the division operation does not throw an error when
    domain.currentTotalShares is zero by checking the condition before performing the
    division.

    indexers/staking-squid/src/events/epoch.ts [196-200]

    -domain.currentSharePrice =
    -  domain.currentTotalShares > BigInt(0)
    -    ? (domain.currentTotalStake * SHARES_CALCULATION_MULTIPLIER) /
    -      domain.currentTotalShares
    -    : BigInt(0);
    +if (domain.currentTotalShares > BigInt(0)) {
    +  domain.currentSharePrice = (domain.currentTotalStake * SHARES_CALCULATION_MULTIPLIER) / domain.currentTotalShares;
    +} else {
    +  domain.currentSharePrice = BigInt(0);
    +}
     
    Suggestion importance[1-10]: 9

    Why: This suggestion addresses a potential division by zero error, which is a critical issue that could cause runtime exceptions. The proposed change improves code robustness.

    9
    Possible issue
    Use case-insensitive string comparison for "autoId"

    Consider using a case-insensitive comparison for the string "autoId" to ensure
    consistent behavior regardless of the case used in the input data.

    indexers/staking-squid/src/events/epoch.ts [93-95]

    -_domain.runtime = stringifiedRuntime.includes("autoId")
    +_domain.runtime = stringifiedRuntime.toLowerCase().includes("autoid")
       ? DomainRuntime.AutoId
       : DomainRuntime.EVM;
     
    Suggestion importance[1-10]: 8

    Why: The suggestion to use a case-insensitive comparison ensures consistent behavior regardless of input case, which is crucial for avoiding potential bugs related to string case sensitivity.

    8
    Maintainability
    Refactor repeated withdrawal updates into a single function

    Refactor the repeated code for setting total withdrawals and updating caches into a
    function to improve code maintainability and reduce duplication.

    indexers/staking-squid/src/events/unlock.ts [126-137]

    -domain.totalWithdrawals += amountBigInt;
    -cache.domains.set(domain.id, domain);
    -account.totalWithdrawals += amountBigInt;
    -cache.accounts.set(account.id, account);
    -operator.totalWithdrawals += amountBigInt;
    -cache.operators.set(operator.id, operator);
    -nominator.totalWithdrawals += amountBigInt;
    -cache.nominators.set(nominator.id, nominator);
    +function updateTotalWithdrawals(entity, cache, id, amount) {
    +  entity.totalWithdrawals += amount;
    +  cache.set(id, entity);
    +}
    +updateTotalWithdrawals(domain, cache.domains, domain.id, amountBigInt);
    +updateTotalWithdrawals(account, cache.accounts, account.id, amountBigInt);
    +updateTotalWithdrawals(operator, cache.operators, operator.id, amountBigInt);
    +updateTotalWithdrawals(nominator, cache.nominators, nominator.id, amountBigInt);
     
    Suggestion importance[1-10]: 7

    Why: Refactoring repeated code into a function enhances maintainability and reduces duplication, making the codebase easier to manage and less error-prone.

    7
    Improve variable naming for clarity in createRewardEvent call

    Use a more descriptive variable name than event for the createRewardEvent function
    to avoid confusion with global objects or other event handlers in the context.

    indexers/staking-squid/src/events/operator.ts [198-202]

    -const operatorRewardedEvent = createRewardEvent(block, extrinsic, event, {
    +const operatorRewardedEvent = createRewardEvent(block, extrinsic, rewardEventDetails, {
       operatorId: operator.id,
       domainId: operator.domainId,
       amount,
     });
     
    Suggestion importance[1-10]: 6

    Why: The suggestion improves code readability and maintainability by using a more descriptive variable name, reducing potential confusion with other event-related variables.

    6

    @marc-aurele-besner marc-aurele-besner marked this pull request as ready for review September 2, 2024 15:53
    Copy link
    Contributor

    @clostao clostao left a comment

    Choose a reason for hiding this comment

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

    lgtm

    @marc-aurele-besner marc-aurele-besner merged commit 1120ad1 into main Sep 4, 2024
    13 checks passed
    @marc-aurele-besner marc-aurele-besner deleted the feat/keepImprovingStakingSquid branch September 4, 2024 13:02
    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.

    2 participants