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

Hotfix/account balances main #757

Merged
merged 3 commits into from
Apr 24, 2024
Merged

Hotfix/account balances main #757

merged 3 commits into from
Apr 24, 2024

Conversation

phamphong9981
Copy link
Contributor

@phamphong9981 phamphong9981 commented Apr 15, 2024

Summary by CodeRabbit

  • Refactor
    • Improved the account balance handling process, including updates and resets in the database system.

@phamphong9981 phamphong9981 added bug Something isn't working main labels Apr 15, 2024
@phamphong9981 phamphong9981 self-assigned this Apr 15, 2024
Copy link

coderabbitai bot commented Apr 15, 2024

Walkthrough

The recent update in the crawl_account.service.ts file enhances the account balance management system. It integrates lodash for efficient address indexing and introduces a mechanism to reset balance amounts in the database. The processing and updating of account balances have also been refined for better performance and accuracy.

Changes

File Path Change Summary
.../crawl-account/... Updated to use lodash for keying by addresses, added logic to reset balances, refined balance processing and updating.

Poem

🐰💻✨
In the burrows of code, where the data hops around,
A clever little update has just been found.
Lodash in use, where addresses align,
Balances reset, oh how divine!
Cheers to the devs, with every line refined! 🌟
🥕👩‍💻🥕


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between fd5a2fa and 91988e1.
Files selected for processing (1)
  • src/services/crawl-account/crawl_account.service.ts (3 hunks)
Additional comments not posted (1)
src/services/crawl-account/crawl_account.service.ts (1)

303-328: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [324-337]

Refactor to improve readability and reduce complexity.

The nested loops and conditionals from lines 324 to 337 make the code hard to read and maintain. Consider refactoring this part to separate functions or methods to handle the logic more cleanly. For example, you could create a separate method to process each account's balances and another to construct the list of account balances to be updated.


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

@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: 2

@@ -19,6 +19,7 @@ import {
QueryAccountRequest,
QueryAccountResponse,
} from '@aura-nw/aurajs/types/codegen/cosmos/auth/v1beta1/query';
import _ from 'lodash';
Copy link

Choose a reason for hiding this comment

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

Consider using a more specific import for lodash to reduce bundle size.

Instead of importing the entire lodash library, consider importing only the functions you need. This can help reduce the overall bundle size and improve the application's performance. For example:

import keyBy from 'lodash/keyBy';

Comment on lines 303 to 328
last_updated_height: number;
}[] = [];
await knex.transaction(async (trx) => {
const addressesWithIds = await Account.query()
.select('id', 'address')
.forUpdate()
.whereIn('address', addresses)
.transacting(trx);
const addressesWithIds = _.keyBy(
await Account.query()
.select('id', 'address')
.forUpdate()
.whereIn('address', addresses)
.transacting(trx),
'address'
);
const resetBalanceList: {
account_id: number;
last_updated_height: number;
}[] = accounts
.filter((account) => addressesWithIds[account.address])
.map((account) => ({
account_id: addressesWithIds[account.address].id,
last_updated_height: account.last_updated_height,
}));
accounts.forEach((account) => {
const accountId = addressesWithIds.find(
(addressWithId) => addressWithId.address === account.address
)?.id;
if (Array.isArray(account.balances) && accountId)
const accountId = addressesWithIds[account.address]?.id;
if (Array.isArray(account.balances) && accountId) {
account.balances.forEach((balance) => {
listAccountBalance.push({
account_id: accountId,
Copy link

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [306-347]

Ensure transactional integrity and handle potential race conditions.

The transaction block from lines 306 to 347 updates account balances and inserts new records. However, there are a few concerns:

  1. The use of raw SQL queries (lines 344-346 and subsequent lines) can be prone to SQL injection if not properly handled. Always ensure that parameters are properly sanitized.
  2. The transaction does not seem to handle potential race conditions between read and write operations. Consider using row-level locking mechanisms if applicable.
  3. The error handling within the transaction only logs the error but does not rethrow it, potentially leading to silent failures in some scenarios. Consider rethrowing the error or handling it in a way that the calling function is aware of the failure.

@peara peara merged commit 4028118 into main Apr 24, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working main
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants