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

[ZCash] Implement ShardTree sync process #27255

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

[ZCash] Implement ShardTree sync process #27255

wants to merge 5 commits into from

Conversation

cypt4
Copy link
Collaborator

@cypt4 cypt4 commented Jan 15, 2025

Part of #27018

Implements shard tree state sync process.
Entry point for the sync process is the per-account ZCashShieldSyncService class.
Sync process includes:
Verifying chain state. In some cases chain reorg may happen so we need to check whether latest scanned block hash hasn't been changed. Otherwise we need to clean OrchardStorage to remove notes, nullifiers and commitments that are not valid.
Preparing a set of scan ranges to scan. Each scan range ends with updating of OrchardSyncState with the discovered data.
Scanning ranges one-by-one. This includes reading compact blocks from the gRPC node and extracting related incoming notes and nullifiers for spends along with a set of commitment tree leafs to be inserted to the shard tree in the future. Extracting is done on a separate sequence. This is done using OrchardBlockScanner class.
Inserting extracted leafs to the shard tree(OrchardSyncState::ApplyScanResults).

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

@cypt4 cypt4 requested review from a team as code owners January 15, 2025 20:30
@github-actions github-actions bot added CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet feature/web3/wallet/core labels Jan 15, 2025
Copy link
Contributor

[puLL-Merge] - brave/brave-core@27255

Description

This pull request implements significant changes to the Brave Wallet's ZCash functionality, particularly focusing on enhancing the Orchard protocol support and improving the shield sync process. The changes include new tasks for batch scanning blocks, verifying chain state, and managing sync operations more efficiently.

Possible Issues

  1. The introduction of new complex tasks and processes may increase the potential for race conditions or synchronization issues.
  2. The changes to the sync process might temporarily impact performance or user experience during the transition.

Security Hotspots

  1. The ZCashBlocksBatchScanTask and ZCashScanBlocksTask handle sensitive blockchain data. Ensure proper input validation and error handling to prevent potential exploits.
  2. The ZCashVerifyChainStateTask is critical for maintaining the integrity of the sync process. Any vulnerabilities here could lead to syncing incorrect or malicious data.
Changes

Changes

  1. components/brave_wallet/browser/BUILD.gn:

    • Added new source files for Zcash-related tasks and contexts.
  2. components/brave_wallet/browser/internal/hd_key_zip32.h and .cc:

    • Added GetSpendingKey() method to HDKeyZip32 class.
  3. components/brave_wallet/browser/internal/orchard_bundle_manager.h and .cc:

    • Modified Create method to include OrchardSpendsBundle.
  4. components/brave_wallet/browser/keyring_service.cc and .h:

    • Added GetOrchardSpendingKey method.
  5. components/brave_wallet/browser/zcash/zcash_action_context.h and .cc:

    • New files introducing ZCashActionContext struct for Zcash operations.
  6. components/brave_wallet/browser/zcash/zcash_blocks_batch_scan_task.h and .cc:

    • New files implementing block scanning in batches.
  7. components/brave_wallet/browser/zcash/zcash_get_zcash_chain_tip_status_task.h and .cc:

    • New files for retrieving chain tip status.
  8. components/brave_wallet/browser/zcash/zcash_scan_blocks_task.h and .cc:

    • New files implementing a task for scanning multiple block ranges.
  9. components/brave_wallet/browser/zcash/zcash_shield_sync_service.cc and .h:

    • Significant refactoring of the shield sync process.
  10. components/brave_wallet/browser/zcash/zcash_verify_chain_state_task.h and .cc:

    • New files for verifying the chain state during sync.
  11. components/brave_wallet/browser/zcash/zcash_wallet_service.cc and .h:

    • Updated to use new tasks and processes for Zcash operations.
  12. components/brave_wallet/common/brave_wallet.mojom:

    • Updated ZCash-related interfaces and structs.
  13. Various test files:

    • Added or updated unit tests for new functionality.
sequenceDiagram
    participant User
    participant ZCashWalletService
    participant ZCashScanBlocksTask
    participant ZCashBlocksBatchScanTask
    participant ZCashVerifyChainStateTask
    participant OrchardSyncState
    participant ZCashRpc

    User->>ZCashWalletService: StartShieldSync
    ZCashWalletService->>ZCashVerifyChainStateTask: VerifyChainState
    ZCashVerifyChainStateTask->>ZCashRpc: GetLatestBlock
    ZCashRpc-->>ZCashVerifyChainStateTask: BlockInfo
    ZCashVerifyChainStateTask->>OrchardSyncState: GetAccountMeta
    OrchardSyncState-->>ZCashVerifyChainStateTask: AccountMeta
    ZCashVerifyChainStateTask->>ZCashRpc: GetTreeState
    ZCashRpc-->>ZCashVerifyChainStateTask: TreeState
    ZCashVerifyChainStateTask-->>ZCashWalletService: ChainStateVerified
    ZCashWalletService->>ZCashScanBlocksTask: StartBlockScanning
    loop For each block range
        ZCashScanBlocksTask->>ZCashBlocksBatchScanTask: ScanRange
        ZCashBlocksBatchScanTask->>ZCashRpc: GetCompactBlocks
        ZCashRpc-->>ZCashBlocksBatchScanTask: CompactBlocks
        ZCashBlocksBatchScanTask->>OrchardSyncState: ApplyScanResults
        OrchardSyncState-->>ZCashBlocksBatchScanTask: ScanResultsApplied
        ZCashBlocksBatchScanTask-->>ZCashScanBlocksTask: RangeScanned
    end
    ZCashScanBlocksTask-->>ZCashWalletService: ScanCompleted
    ZCashWalletService-->>User: SyncCompleted
Loading

Copy link
Contributor

@Douglashdaniel Douglashdaniel left a comment

Choose a reason for hiding this comment

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

++ Wallet Front-end

@brave-builds
Copy link
Collaborator

A Storybook has been deployed to preview UI for the latest push

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/storybook-url Deploy storybook and provide a unique URL for each build feature/web3/wallet/core feature/web3/wallet puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants