autonomys / astral

Home of our Block Explorer
https://explorer.subspace.network
11 stars 9 forks source link

Add Hasura action to update account balance #933

Closed marc-aurele-besner closed 4 days ago

marc-aurele-besner commented 6 days ago

User description

Add Hasura action to update account balance


PR Type

Enhancement, Configuration changes


Description


Changes walkthrough ๐Ÿ“

Relevant files
Enhancement
7 files
index.js
Add and enable `updateAccount` queue in taskboard               

indexers/taskboard/constants/index.js
  • Added a new queue named updateAccount.
  • Enabled the updateAccount queue.
  • +3/-3     
    index.js
    Modify task addition logic and server port configuration 

    indexers/taskboard/index.js
  • Modified task addition logic to support Hasura actions.
  • Changed server port to use environment variable BULL_PORT.
  • +6/-2     
    index.js
    Add `updateAccount` task handler                                                 

    indexers/taskboard/tasks/index.js - Added `updateAccount` task handler.
    +5/-0     
    updateAccount.js
    Implement `updateAccount` function for account balance update

    indexers/taskboard/tasks/updateAccount.js
  • Implemented updateAccount function to update account balance.
  • Utilized database connection and transaction management.
  • +58/-0   
    db.js
    Add upsert query for account data in database                       

    indexers/taskboard/utils/db.js - Added `consensusUpsertAccountQuery` for upserting account data.
    +13/-0   
    actions.graphql
    Define `updateAccount` mutation in GraphQL                             

    indexers/db/metadata/actions.graphql - Defined `updateAccount` mutation and related input/output types.
    +21/-0   
    actions.yaml
    Add `updateAccount` action definition in Hasura metadata 

    indexers/db/metadata/actions.yaml
  • Added updateAccount action definition.
  • Defined custom input and output types for the action.
  • +12/-3   
    Configuration changes
    2 files
    docker-compose.yml
    Update service port mappings and environment configurations

    docker-compose.yml
  • Updated Caddy service port mappings.
  • Added environment variable for Hasura task board action endpoint.
  • Modified taskboard service port configuration.
  • +10/-7   
    Dockerfile
    Update Node.js version in Dockerfile                                         

    indexers/taskboard/Dockerfile - Updated Node.js version to 18-alpine.
    +1/-1     
    Dependencies
    1 files
    package.json
    Add new dependencies for taskboard                                             

    indexers/taskboard/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

    netlify[bot] commented 6 days ago

    Deploy Preview for dev-astral canceled.

    Name Link
    Latest commit 409e332eb0694e6c289611dde7768be904c2191a
    Latest deploy log https://app.netlify.com/sites/dev-astral/deploys/672e7add42f0f30008bfaf61
    github-actions[bot] commented 6 days ago

    PR Reviewer Guide ๐Ÿ”

    Here are some key observations to aid the review process:

    โฑ๏ธ Estimated effort to review: 4 ๐Ÿ”ต๐Ÿ”ต๐Ÿ”ต๐Ÿ”ตโšช
    ๐Ÿงช No relevant tests
    ๐Ÿ”’ No security concerns identified
    โšก Recommended focus areas for review

    Error Handling
    The error handling in the updateAccount function could be improved by providing more specific error messages and possibly categorizing errors for better debugging and maintenance. Code Duplication
    The request body parsing logic in the POST endpoint seems redundant and could be simplified or abstracted to reduce duplication and improve code maintainability. Configuration Hardcoding
    The queue configuration for 'updateAccount' is hardcoded, which might limit flexibility. Consider externalizing such configurations or providing a way to manage them dynamically.
    github-actions[bot] commented 6 days ago

    PR Code Suggestions โœจ

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Implement error handling for initialization steps in the updateAccount function ___ **Add error handling for potential failures in asynchronous operations such as
    activate and connectToDB to prevent unhandled promise rejections and improve the
    robustness of the updateAccount function.** [indexers/taskboard/tasks/updateAccount.js [14-15]](https://github.com/autonomys/astral/pull/933/files#diff-5720ceb4ec01e72ede4b4971aa76d1f81edc9d0b3d0f2d7ceecac1eb8c92fd6cR14-R15) ```diff -const api = await activate({ networkId }); -const pool = await connectToDB(); +let api, pool; +try { + api = await activate({ networkId }); + pool = await connectToDB(); +} catch (err) { + console.error("Failed to initialize dependencies:", err); + throw err; +} ```
    Suggestion importance[1-10]: 8 Why: The suggestion to add error handling for the `activate` and `connectToDB` functions is crucial as it addresses potential unhandled promise rejections, which can improve the robustness and reliability of the `updateAccount` function.
    8
    Use a specific environment variable for the taskboard service port ___ **Replace the hardcoded port number in the app.listen method with a dynamic
    environment variable to enhance configuration flexibility and consistency with other
    service configurations.** [indexers/taskboard/index.js [171]](https://github.com/autonomys/astral/pull/933/files#diff-77210285feed36e41153995d3f6882e1958c371640a2eeb112a7e435889a9627R171-R171) ```diff -app.listen(process.env.BULL_PORT || 3020, () => { +app.listen(process.env.TASKBOARD_PORT || 3020, () => { ```
    Suggestion importance[1-10]: 3 Why: While the suggestion to use a specific environment variable for the taskboard service port might enhance configuration flexibility, it is not critical since the existing code already uses an environment variable (`BULL_PORT`). This change would provide minimal impact unless there is a specific need to differentiate the ports in the environment configuration.
    3
    Possible bug
    Add checks to prevent overriding variable values with undefined if parts of req.body are missing ___ **Ensure that the destructuring assignment in the conditional block does not override
    the initial values of queueName, taskName, data, opts, and jobId if req.body.action
    is not present or malformed. This can lead to runtime errors or unexpected behavior.** [indexers/taskboard/index.js [111-114]](https://github.com/autonomys/astral/pull/933/files#diff-77210285feed36e41153995d3f6882e1958c371640a2eeb112a7e435889a9627R111-R114) ```diff let { queueName, taskName, data, opts, jobId } = req.body; -if (req.body.action) { +if (req.body.action && req.body.input && req.body.input.args) { taskName = req.body.action.name; ({ queueName, data, opts, jobId } = req.body.input.args); } ```
    Suggestion importance[1-10]: 7 Why: This suggestion correctly identifies a potential bug where the destructuring assignment might override variables with undefined values if parts of `req.body` are missing. Adding checks enhances the robustness of the code by preventing runtime errors.
    7
    Ensure the SQL query handles null values to prevent database errors ___ **Modify the SQL query in consensusUpsertAccountQuery to handle potential null values
    in the accountState.data fields to avoid inserting null values into the database
    which might violate constraints or lead to incorrect data handling.** [indexers/taskboard/utils/db.js [115-117]](https://github.com/autonomys/astral/pull/933/files#diff-0eb7ec60df4af52ee0866bca1a4bff4e8533870a6ca76c2ca7bbe1431b4c4e31R115-R117) ```diff -accountState.data.free, -accountState.data.reserved, -accountState.data.free + accountState.data.reserved, +accountState.data.free || 0, +accountState.data.reserved || 0, +(accountState.data.free || 0) + (accountState.data.reserved || 0), ```
    Suggestion importance[1-10]: 6 Why: This suggestion is valid as it prevents potential database errors by ensuring that null values are handled correctly in the SQL query. This can avoid violations of database constraints and incorrect data handling.
    6