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: db cache clear #6394

Merged
merged 2 commits into from
Dec 23, 2024
Merged

fix: db cache clear #6394

merged 2 commits into from
Dec 23, 2024

Conversation

sidmorizon
Copy link
Contributor

@sidmorizon sidmorizon commented Dec 22, 2024

Summary by CodeRabbit

  • 新功能
    • 引入了 URL_ACCOUNT_ID 常量,供外部使用。
  • 改进
    • 优化了 LocalDbBaseLocalDbBaseContainer 类中的数据库事务处理和缓存逻辑。
  • 文档
    • 更新了方法签名以反映逻辑变化。

Copy link

codesandbox bot commented Dec 22, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link
Contributor

coderabbitai bot commented Dec 22, 2024

概述

代码变更概览

本次代码变更主要涉及三个文件:LocalDbBase.tsLocalDbBaseContainer.tsaccountUtils.ts,重点关注数据库操作、事务处理和性能优化。

变更

文件路径 变更摘要
packages/kit-bg/src/dbs/local/LocalDbBase.ts 重构数据库事务处理逻辑,简化异步调用,优化错误处理
packages/kit-bg/src/dbs/local/LocalDbBaseContainer.ts 调整事务处理方法,引入条件缓存机制
packages/shared/src/utils/accountUtils.ts 新增 URL_ACCOUNT_ID 导出常量

详细变更

LocalDbBase.ts

  • 移除冗余的数据库就绪检查
  • 简化事务方法调用
  • 改进错误处理机制

LocalDbBaseContainer.ts

  • 修改 withTransaction 方法实现
  • 优化 getRecordById 方法的缓存逻辑
  • 引入条件缓存策略

accountUtils.ts

  • 新增 URL_ACCOUNT_ID 导出常量

技术亮点

  1. 代码简化:减少不必要的异步调用
  2. 性能优化:引入条件缓存机制
  3. 错误处理:改进异常捕获和处理流程

潜在影响

  • 提高数据库操作效率
  • 减少不必要的异步开销
  • 增强代码可读性和维护性

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


🪧 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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 or 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.

@sidmorizon sidmorizon marked this pull request as ready for review December 22, 2024 07:50
@sidmorizon sidmorizon enabled auto-merge (squash) December 22, 2024 07:50
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: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 033f85b and f0fdcf2.

📒 Files selected for processing (3)
  • packages/kit-bg/src/dbs/local/LocalDbBase.ts (53 hunks)
  • packages/kit-bg/src/dbs/local/LocalDbBaseContainer.ts (3 hunks)
  • packages/shared/src/utils/accountUtils.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/kit-bg/src/dbs/local/LocalDbBaseContainer.ts

[error] 64-64: This let declares a variable that is only assigned once.

'shouldUseCache' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)

🔇 Additional comments (52)
packages/kit-bg/src/dbs/local/LocalDbBase.ts (50)

225-225: 建议
这里对 Context 记录进行更新,逻辑看起来挺好。


234-234: 提示
重置上下文时,使用 transaction 可以避免中途出错时出现不一致。


253-253: 提示
生成新 backupUUID 并写入,流程合理。


310-310: 提示
重置密码时,记得测试特殊场景,确保不会误操作生产环境。


335-335: 提示
一次性获取所有 Credential 记录,后续使用记得考虑潜在性能问题。
[approve]


392-392: 提示
更新 context 的 verifyString,很直观。


431-431: 建议
更新密码时,检查逻辑完整度,确保 oldPassword 校验与新密码加密都安全。
[approve]


450-450: 提示
getAllCredentials 逻辑简明,没发现明显问题。


461-461: 提示
批量删除 Credentials,配合事务流程安全性更高。


519-519: 提示
这里先拿到所有钱包,再根据 refillWalletInfo 条件补充数据,流程清晰。


822-822: 提醒
更新 walletOrder 要注意并发情况,谨防其他地方并行写入时产生冲突。


843-843: 提示
更新 IndexedAccount 心中序号,保持排序准确即可。


998-998: 提示
在 transaction 中添加 indexedAccount,确保原子性。


1130-1130: 提示
添加下一 HD 索引,注意循环创建时的冲突检测。
[approve]


1336-1336: 提示
更新固件校验标志,这里需要留意 firmware 版本比对逻辑。
[approve]


1372-1373: 提示
用了 transaction 更新设备信息,保证写入一致性。


1394-1394: 提示
通过 transaction 修改设备 label,这里要注意 label 的格式或长度检查。
[approve]


1500-1500: 提示
创建 QR 钱包时,一并处理 airGapAccounts 数据,很好用 transaction 保证一致性。


1668-1668: 提示
在 transaction 里添加设备记录,便于后续自动回滚或提交。


1720-1720: 提示
临时钱包状态恢复,逻辑简单明了。


1781-1781: 提示
创建硬件钱包,设备信息与钱包信息一起写入,流程清晰。


1912-1912: 提示
清空气隙钱包的账号信息,用 transaction 很安全。


2052-2052: 提示
为钱包设置 isTemp 状态,对热切换临时钱包场景有帮助。


2088-2088: 提示
批量更新钱包名称和头像时,小心 JSON 序列化异常。
[approve]


2311-2312: 提示
这里先获取 Address,再根据情况决定更新或新增,逻辑不错,别忘了性能方面的考虑。

Also applies to: 2317-2317


2404-2404: 提示
给钱包添加账号的事务处理,注意 large batch 时的性能影响。
[approve]


2438-2438: 提示
获取现有账号列表后再操作,挺稳妥。


2450-2450: 提示
先移除旧账号,再添加新账号,可避免数据冲突。


2481-2481: 提示
将新账号记录插入数据库,很好地利用 skipIfExists 参数。


2588-2589: 提示
保存 TON 导入助记词时,安全性一定要做好验证。


2737-2737: 提示
更新账号顺序,记得校验传入 order 的合理性。


2772-2772: 提示
批量删除 IndexedAccounts,逻辑直观。


2789-2789: 提示
移除单个 IndexedAccount,不忘捕获异常以防找不到记录。
[approve]


2801-2801: 提示
批量删除账号并记录对应 walletId,这一步做得细心周到。


2824-2824: 提示
移除后更新钱包内的账号列表,很好,避免残留引用。


2852-2852: 提示
从钱包移除单个账号时,也要删除 Credential,保持数据同步。


2899-2899: 提示
更新 ExternalAccount 的信息,灵活易扩展。


3000-3000: 提示
设置账号的名称,注意重复名的检查逻辑就比较周全。
[approve]


3167-3167: 提示
更新设备设置时,把 settingsRaw 改成自定义 JSON 即可,无明显缺陷。


3182-3182: 提示
添加签名消息,记得处理 ctx.nextSignatureMessageId 的溢出问题。
[approve]


3208-3208: 提示
添加签名交易时,用 JSON.dumps 存储 data,别忘了大小限制。
[approve]


3233-3233: 提示
添加连接站点记录,也要考虑站点重复判断。
[approve]


3257-3257: 提示
先获取所有 SignedMessage,然后一起删除。建议日志记录删除数量。
[approve]

Also applies to: 3260-3260


3270-3270: 提示
删除所有 SignedTransaction,操作与上一部分一致,保持良好的流程。

Also applies to: 3273-3273


3283-3283: 提示
清空所有 ConnectedSite 时,也利用事务保障一致性。

Also applies to: 3286-3286


3299-3299: 提示
演示获取上下文的示例,方便测试 DB 事务。


3323-3323: 提示
演示更新 UUID,要避免过多随机写入。
[approve]


3364-3364: 提示
通过直接更新记录的方式写死 backupUUID,仅用于演示注意别在正式环境误用。
[approve]


3390-3390: 提示
插入新的 Credential。测试容量限制时可多插几条做压测。


3454-3454: 提示
批量更新 Credential,用于演示长事务。请确保性能不受影响。

packages/kit-bg/src/dbs/local/LocalDbBaseContainer.ts (1)

32-36: 提示
删除了原本的异常抛出,改为直接调用 db.withTransaction,代码可用性更好。但需要确保异常场景能正确上抛或处理。

packages/shared/src/utils/accountUtils.ts (1)

764-764: 提示
新增常量 URL_ACCOUNT_ID,用于处理特殊全局地址,命名直观好理解。

@sidmorizon sidmorizon merged commit c299053 into x Dec 23, 2024
8 checks passed
@sidmorizon sidmorizon deleted the fix/db-cache-clear branch December 23, 2024 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants