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 Accounts Sub Query Node #854

Merged
merged 12 commits into from
Sep 24, 2024
Merged

Conversation

marc-aurele-besner
Copy link
Collaborator

@marc-aurele-besner marc-aurele-besner commented Sep 23, 2024

User description

Fix Accounts Sub Query Node

This adds/fix the logic around indexing accounts, transfer and account balance changes

I also added Redis, Express and BullMQ to have a tasks board, allowing to create a task that will then be executed outside of the indexer worker

(especially useful for updating existing db entry, or to run long process


PR Type

enhancement, bug_fix


Description

  • Refactored event handlers in project.ts to streamline logic and improve efficiency.
  • Added new helper functions in db.ts for creating and saving account, balance history, and transfer records.
  • Implemented utility functions in utils.ts for JSON handling and date management.
  • Updated GraphQL schema to include new types and indices for better data management.
  • Defined new table schemas and permissions in YAML configuration files for accounts, balance histories, and transfers.
  • Added new dependencies to support account management and balance updates.

Changes walkthrough 📝

Relevant files
Enhancement
6 files
project.ts
Refactor event handlers and add call handler                         

indexers/gemini-3h/accounts/project.ts

  • Removed multiple event handlers for various modules and methods.
  • Added a new handler for SubstrateHandlerKind.Call.
  • Updated handlers for VoteReward and BlockReward events.
  • +8/-52   
    db.ts
    Add database helper functions for accounts and transfers 

    indexers/gemini-3h/accounts/src/mappings/db.ts

  • Added functions to create and save account, balance history, and
    transfer records.
  • Implemented logic to check existence before saving new records.
  • +82/-0   
    helper.ts
    Implement account balance update helper function                 

    indexers/gemini-3h/accounts/src/mappings/helper.ts

  • Added a function to update account balance.
  • Utilized external balance API for fetching account balance.
  • +19/-0   
    mappingHandlers.ts
    Refactor mapping handlers and add new event handlers         

    indexers/gemini-3h/accounts/src/mappings/mappingHandlers.ts

  • Refactored handleTransferEvent to use new helper functions.
  • Added new handlers for extrinsic and reward events.
  • Removed redundant functions for account and transfer checks.
  • +108/-80
    utils.ts
    Add utility functions for JSON and date handling                 

    indexers/gemini-3h/accounts/src/mappings/utils.ts

  • Added utility functions for JSON stringification and date entry
    creation.
  • +9/-0     
    schema.graphql
    Update GraphQL schema with new types and indices                 

    indexers/gemini-3h/accounts/schema.graphql

  • Updated Account type with new fields and indices.
  • Added BalanceHistory type with fields and indices.
  • Updated Transfer type with new indices.
  • +13/-3   
    Configuration changes
    4 files
    accounts_accounts.yaml
    Define accounts table schema and permissions                         

    indexers/db/metadata/databases/gemini_3h/tables/accounts_accounts.yaml

    • Defined table schema and select permissions for accounts.
    +20/-0   
    accounts_balance_histories.yaml
    Define balance histories table schema and permissions       

    indexers/db/metadata/databases/gemini_3h/tables/accounts_balance_histories.yaml

    • Defined table schema and select permissions for balance histories.
    +18/-0   
    accounts_transfers.yaml
    Define transfers table schema and permissions                       

    indexers/db/metadata/databases/gemini_3h/tables/accounts_transfers.yaml

    • Defined table schema and select permissions for transfers.
    +23/-0   
    tables.yaml
    Update tables configuration to include new schemas             

    indexers/db/metadata/databases/gemini_3h/tables/tables.yaml

  • Included new tables for accounts, balance histories, and transfers.
  • +3/-0     
    Dependencies
    1 files
    package.json
    Add new dependencies for account management                           

    indexers/gemini-3h/accounts/package.json

  • Added new dependencies @autonomys/auto-consensus and
    @autonomys/auto-utils.
  • +2/-0     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    @github-actions github-actions bot added enhancement New feature or request bug_fix labels Sep 23, 2024
    Copy link

    PR Reviewer Guide 🔍

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

    Handler Redundancy
    The new handlers handleFarmerVoteRewardEvent and handleFarmerBlockRewardEvent seem to perform similar operations as handleTransferEvent. Consider refactoring to a common function to avoid code duplication.

    Error Handling
    The functions createAndSaveAccountIfNotExists, createAndSaveBalanceHistory, and createAndSaveTransfer do not handle errors that might occur during database operations. Consider adding try-catch blocks to handle potential exceptions.

    Timestamp Handling
    The calculation of timestamp in handleTransferEvent might lead to incorrect values due to the multiplication by 1000. Verify if the original timestamp is in seconds and needs conversion to milliseconds.

    Copy link

    github-actions bot commented Sep 23, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Error handling
    Add error handling for asynchronous operations in the handleTransferEvent function

    Consider handling potential exceptions for the updateAccountBalance and
    createAndSaveAccountIfNotExists calls within the handleTransferEvent function. This
    can be done using try-catch blocks to ensure that errors are caught and handled
    appropriately, preventing the function from failing silently.

    indexers/gemini-3h/accounts/src/mappings/mappingHandlers.ts [19-38]

    -const fromBalance = await updateAccountBalance(from, blockNumber);
    -const toBalance = await updateAccountBalance(to, blockNumber);
    -...
    -await createAndSaveAccountIfNotExists(
    -  from,
    -  blockNumber,
    -  BigInt(0),
    -  fromBalance.free,
    -  fromBalance.reserved,
    -  fromBalance.free + fromBalance.reserved
    -);
    -await createAndSaveAccountIfNotExists(
    -  to,
    -  blockNumber,
    -  BigInt(0),
    -  toBalance.free,
    -  toBalance.reserved,
    -  toBalance.free + toBalance.reserved
    -);
    +try {
    +  const fromBalance = await updateAccountBalance(from, blockNumber);
    +  const toBalance = await updateAccountBalance(to, blockNumber);
    +  ...
    +  await createAndSaveAccountIfNotExists(
    +    from,
    +    blockNumber,
    +    BigInt(0),
    +    fromBalance.free,
    +    fromBalance.reserved,
    +    fromBalance.free + fromBalance.reserved
    +  );
    +  await createAndSaveAccountIfNotExists(
    +    to,
    +    blockNumber,
    +    BigInt(0),
    +    toBalance.free,
    +    toBalance.reserved,
    +    toBalance.free + toBalance.reserved
    +  );
    +} catch (error) {
    +  console.error('Failed to handle transfer event:', error);
    +}
     
    Suggestion importance[1-10]: 8

    Why: The suggestion to add error handling using try-catch blocks for asynchronous operations in the handleTransferEvent function is valid and important. It improves the robustness of the code by ensuring that any errors during these operations are caught and logged, preventing silent failures.

    8

    Copy link

    socket-security bot commented Sep 23, 2024

    👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

    This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

    View full report↗︎

    @marc-aurele-besner marc-aurele-besner changed the title Fix Consensus Sub Query Node Fix Accounts Sub Query Node Sep 23, 2024
    Base automatically changed from feat/fix-consensus-sub-query to main September 23, 2024 23:32
    Copy link

    netlify bot commented Sep 24, 2024

    Deploy Preview for dev-astral canceled.

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

    Copy link

    New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

    Package New capabilities Transitives Size Publisher
    npm/@bull-board/express@5.23.0 None 0 11.7 kB felixmosh
    npm/@bull-board/ui@5.23.0 None 0 3.36 MB felixmosh
    npm/@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.3 None 0 54.2 kB kriszyp
    npm/@msgpackr-extract/msgpackr-extract-darwin-x64@3.0.3 None 0 53.7 kB kriszyp
    npm/@msgpackr-extract/msgpackr-extract-linux-arm@3.0.3 None 0 26 kB kriszyp
    npm/@msgpackr-extract/msgpackr-extract-linux-arm64@3.0.3 None 0 28.6 kB kriszyp
    npm/@msgpackr-extract/msgpackr-extract-linux-x64@3.0.3 None 0 61.3 kB kriszyp
    npm/@msgpackr-extract/msgpackr-extract-win32-x64@3.0.3 None 0 228 kB kriszyp
    npm/async@3.2.6 None 0 808 kB aearly
    npm/body-parser@1.20.3 network 0 62.6 kB ulisesgascon
    npm/bullmq@4.17.0 environment, filesystem, network, shell 0 1.47 MB manast
    npm/connect-ensure-login@0.1.1 None 0 7.5 kB jaredhanson
    npm/cron-parser@4.9.0 filesystem 0 54.2 kB harrisiirak
    npm/detect-libc@2.0.3 filesystem, shell 0 23.6 kB lovell
    npm/ejs@3.1.10 eval, filesystem 0 143 kB mde
    npm/encodeurl@2.0.0 None 0 6.98 kB blakeembrey
    npm/express-session@1.18.0 environment 0 86.3 kB dougwilson
    npm/express@4.21.0 environment, filesystem, network 0 221 kB wesleytodd
    npm/filelist@1.0.4 filesystem 0 18.6 kB mde
    npm/finalhandler@1.3.1 environment 0 19 kB dougwilson, wesleytodd
    npm/glob@8.1.0 filesystem 0 56.2 kB isaacs
    npm/ioredis@5.4.1 network 0 698 kB ioredis-robot
    npm/jake@10.9.2 environment, filesystem, shell 0 175 kB mde
    npm/lodash@4.17.21 None 0 1.41 MB bnjmnt4n
    npm/luxon@3.5.0 None 0 4.48 MB icambron
    npm/merge-descriptors@1.0.3 None 0 5.08 kB sindresorhus
    npm/minimatch@5.1.6 None 0 38.9 kB isaacs
    npm/msgpackr-extract@3.0.3 None 0 15.5 kB kriszyp
    npm/msgpackr@1.11.0 environment, eval, unsafe 0 1.92 MB kriszyp
    npm/node-gyp-build-optional-packages@5.2.2 environment, filesystem, unsafe 0 14.8 kB kriszyp
    npm/object-inspect@1.13.2 None 0 99.1 kB ljharb
    npm/on-headers@1.0.2 None 0 7.54 kB dougwilson
    npm/passport-local@1.0.0 None 0 8.07 kB jaredhanson
    npm/passport-strategy@1.0.0 None 0 5.1 kB jaredhanson
    npm/passport@0.6.0 network 0 81.6 kB jaredhanson
    npm/path-to-regexp@0.1.10 None 0 6.38 kB blakeembrey
    npm/pause@0.0.1 None 0 2.17 kB
    npm/pg@8.13.0 environment, network +3 344 kB brianc
    npm/qs@6.13.0 None 0 254 kB ljharb
    npm/random-bytes@1.0.0 None 0 6.24 kB dougwilson
    npm/redis-info@3.1.0 None 0 21.2 kB fgribreau
    npm/semver@7.6.3 None 0 95.8 kB npm-cli-ops
    npm/send@0.19.0 filesystem, network 0 50.2 kB ulisesgascon
    npm/serve-static@1.16.2 None 0 25.4 kB wesleytodd
    npm/tslib@2.7.0 None 0 86.2 kB typescript-bot
    npm/uid-safe@2.1.5 None 0 7.47 kB dougwilson

    🚮 Removed packages: npm/@polkadot/extension-dapp@0.46.7, npm/@polkadot/extension-inject@0.46.7, npm/@polkadot/react-identicon@3.6.5, npm/@polkadot/ui-settings@3.6.5, npm/@polkadot/ui-shared@3.6.5, npm/@react-spring/animated@9.7.3, npm/@react-spring/core@9.7.3, npm/@react-spring/shared@9.7.3, npm/@react-spring/types@9.7.3, npm/@react-spring/web@9.7.3, npm/@repeaterjs/repeater@3.0.5, npm/@resvg/resvg-wasm@2.4.0, npm/@rollup/plugin-babel@5.3.1, npm/@rollup/plugin-node-resolve@11.2.1, npm/@rollup/plugin-replace@2.4.2, npm/@rollup/pluginutils@3.1.0, npm/@sapphire/async-queue@1.5.2, npm/@sapphire/snowflake@3.5.3, npm/@scure/base@1.1.5, npm/@shuding/opentype.js@1.4.0-beta.0, npm/@sinclair/typebox@0.27.8, npm/@spruceid/siwe-parser@2.0.2, npm/@stablelib/binary@1.0.1, npm/@stablelib/int@1.0.1, npm/@stablelib/random@1.0.2, npm/@stablelib/wipe@1.0.1, npm/@substrate/connect-known-chains@1.1.8, npm/@substrate/ss58-registry@1.46.0, npm/@subwallet/wallet-connect@0.2.7, npm/@surma/rollup-plugin-off-main-thread@2.2.3, npm/@svgr/babel-plugin-add-jsx-attribute@8.0.0, npm/@svgr/babel-plugin-remove-jsx-attribute@8.0.0, npm/@svgr/babel-plugin-remove-jsx-empty-expression@8.0.0, npm/@svgr/babel-plugin-replace-jsx-attribute-value@8.0.0, npm/@svgr/babel-plugin-svg-dynamic-title@8.0.0, npm/@svgr/babel-plugin-svg-em-dimensions@8.0.0, npm/@svgr/babel-plugin-transform-react-native-svg@8.1.0, npm/@svgr/babel-plugin-transform-svg-component@8.0.0, npm/@svgr/babel-preset@8.1.0, npm/@svgr/core@8.1.0, npm/@svgr/hast-util-to-babel-ast@8.0.0, npm/@svgr/plugin-jsx@8.1.0, npm/@svgr/plugin-svgo@8.1.0, npm/@svgr/webpack@8.1.0, npm/@swc/helpers@0.5.2, npm/@tailwindcss/forms@0.5.7, npm/@tanstack/match-sorter-utils@8.11.8, npm/@tanstack/react-table@8.13.2, npm/@tanstack/react-virtual@3.1.3, npm/@tanstack/table-core@8.13.2, npm/@tanstack/virtual-core@3.1.3, npm/@testing-library/dom@9.3.4, npm/@testing-library/jest-dom@6.4.2, npm/@testing-library/react@14.2.1, npm/@testing-library/user-event@14.5.2, npm/@trysound/sax@0.2.0, npm/@types/aria-query@5.0.4, npm/@types/d3-color@2.0.6, npm/@types/d3-delaunay@5.3.4, npm/@types/d3-format@1.4.5, npm/@types/d3-path@2.0.4, npm/@types/d3-scale-chromatic@2.0.4, npm/@types/d3-scale@3.3.5, npm/@types/d3-shape@2.1.7, npm/@types/d3-time-format@3.0.4, npm/@types/d3-time@2.1.4, npm/@types/estree@0.0.39, npm/@types/glob@7.2.0, npm/@types/hoist-non-react-statics@3.3.5, npm/@types/istanbul-lib-coverage@2.0.6, npm/@types/istanbul-lib-report@3.0.3, npm/@types/istanbul-reports@3.0.4, npm/@types/jest@29.5.12, npm/@types/js-yaml@4.0.9, npm/@types/json-schema@7.0.15, npm/@types/json-stable-stringify@1.0.36, npm/@types/json5@0.0.29, npm/@types/jsonwebtoken@9.0.6, npm/@types/lodash@4.14.202, npm/@types/minimatch@5.1.2, npm/@types/node@20.11.25, npm/@types/prop-types@15.7.11, npm/@types/react-dom@18.2.21, npm/@types/react-lifecycles-compat@3.0.4, npm/@types/react@18.2.64, npm/@types/resolve@1.17.1, npm/@types/scheduler@0.16.8, npm/@types/semver@7.5.8, npm/@types/stack-utils@2.0.3, npm/@types/stylis@4.2.0, npm/@types/trusted-types@2.0.7, npm/@types/ws@8.5.10, npm/@types/yargs-parser@21.0.3, npm/@types/yargs@17.0.32, npm/@typescript-eslint/eslint-plugin@7.1.1, npm/@typescript-eslint/parser@6.21.0, npm/@typescript-eslint/scope-manager@7.1.1, npm/@typescript-eslint/type-utils@7.1.1, npm/@typescript-eslint/types@7.1.1, npm/@typescript-eslint/typescript-estree@7.1.1, npm/@typescript-eslint/utils@7.1.1

    View full report↗︎

    @marc-aurele-besner marc-aurele-besner merged commit de09818 into main Sep 24, 2024
    13 checks passed
    @marc-aurele-besner marc-aurele-besner deleted the feat/fix-accounts-sub-query branch September 24, 2024 20:38
    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