Skip to content

Conversation

@UdjinM6
Copy link

@UdjinM6 UdjinM6 commented Nov 5, 2025

Issue being fixed or feature implemented

Previously, LoadHDChain() would fail if CRYPTED_HDCHAIN records were
read before MASTER_KEY records during wallet loading, because the
check m_storage.HasEncryptionKeys() != chain.IsCrypted() would
incorrectly fail when mapMasterKeys was still empty.

This PR fixes the issue by:

  • Adding an optional fSkipEncryptionCheck parameter to LoadHDChain()
  • Skipping the encryption check during wallet loading in ReadKeyValue()
  • Adding comprehensive validation in LoadWallet() after all records
    are loaded to ensure HD chain encryption consistency

What was done?

How Has This Been Tested?

run tests

Breaking Changes

n/a

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

UdjinM6 and others added 2 commits November 5, 2025 22:18
Previously, LoadHDChain() would fail if CRYPTED_HDCHAIN records were
read before MASTER_KEY records during wallet loading, because the
check `m_storage.HasEncryptionKeys() != chain.IsCrypted()` would
incorrectly fail when mapMasterKeys was still empty.

This commit fixes the issue by:
- Adding an optional fSkipEncryptionCheck parameter to LoadHDChain()
- Skipping the encryption check during wallet loading in ReadKeyValue()
- Adding comprehensive validation in LoadWallet() after all records
  are loaded to ensure HD chain encryption consistency

This preserves the safety check for encryption operations while
allowing wallet loading to succeed regardless of database record
ordering.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@UdjinM6 UdjinM6 added this to the 23.1 milestone Nov 5, 2025
@github-actions
Copy link

github-actions bot commented Nov 5, 2025

✅ No Merge Conflicts Detected

This PR currently has no conflicts with other open PRs.

@coderabbitai
Copy link

coderabbitai bot commented Nov 5, 2025

Walkthrough

The changes refactor HD chain loading validation timing by introducing a skip_encryption_check parameter to LegacyScriptPubKeyMan::LoadHDChain. During wallet loading in walletdb.cpp, the HD chain is loaded with the check skipped, and a new post-load validation is added after the wallet data is fully initialized. This validation ensures encryption state consistency between the wallet and HD chain, deferring the check from the initial load to after complete wallet setup.

Sequence Diagram

sequenceDiagram
    participant LoadWallet
    participant LoadHDChain
    participant HDChainValidation
    
    LoadWallet->>LoadHDChain: LoadHDChain(chain, skip_encryption_check=true)
    Note over LoadHDChain: Skips encryption consistency check
    LoadHDChain-->>LoadWallet: Return true
    
    Note over LoadWallet: Complete wallet data loading...
    
    rect rgb(230, 245, 250)
        Note over LoadWallet: Post-load validation phase
        LoadWallet->>HDChainValidation: Check encryption state<br/>storage.HasEncryptionKeys() vs<br/>chain.IsCrypted()
        alt Mismatch detected
            HDChainValidation-->>LoadWallet: Return CORRUPT
        else State consistent
            HDChainValidation-->>LoadWallet: Proceed normally
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • src/wallet/scriptpubkeyman.h: Signature change with new optional parameter requiring understanding of backward compatibility
  • src/wallet/scriptpubkeyman.cpp: Conditional logic added around the encryption consistency check; reviewer should verify the logic preserves existing behavior when skip_encryption_check=false
  • src/wallet/walletdb.cpp: New validation logic added post-load; reviewer should confirm the validation correctly mirrors the skipped check and error handling path returns appropriate wallet status (CORRUPT on mismatch)
  • Key attention areas: Ensure the deferred validation in walletdb.cpp covers all encryption inconsistency scenarios previously caught during initial load; verify error message updates are complete

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing an ordering issue with HD chain encryption checks during wallet loading.
Description check ✅ Passed The description is directly related to the changeset, explaining the issue being fixed, the specific changes made to LoadHDChain(), and the validation approach.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98600eb and 35dc258.

📒 Files selected for processing (3)
  • src/wallet/scriptpubkeyman.cpp (1 hunks)
  • src/wallet/scriptpubkeyman.h (1 hunks)
  • src/wallet/walletdb.cpp (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
src/**/*.{cpp,h,cc,cxx,hpp}

📄 CodeRabbit inference engine (CLAUDE.md)

src/**/*.{cpp,h,cc,cxx,hpp}: Dash Core C++ codebase must be written in C++20 and require at least Clang 16 or GCC 11.1
Dash uses unordered_lru_cache for efficient caching with LRU eviction

Files:

  • src/wallet/scriptpubkeyman.h
  • src/wallet/scriptpubkeyman.cpp
  • src/wallet/walletdb.cpp
🧠 Learnings (3)
📓 Common learnings
Learnt from: kwvg
Repo: dashpay/dash PR: 6761
File: src/chainlock/signing.cpp:247-250
Timestamp: 2025-07-29T14:32:48.369Z
Learning: In PR #6761, kwvg acknowledged a null pointer check issue in ChainLockSigner::Cleanup() method but deferred it to follow-up, consistent with the pattern of avoiding scope creep in refactoring PRs.
Learnt from: knst
Repo: dashpay/dash PR: 6805
File: src/wallet/rpc/wallet.cpp:357-357
Timestamp: 2025-08-08T07:01:47.332Z
Learning: In src/wallet/rpc/wallet.cpp, the upgradetohd RPC now returns a UniValue string message (RPCResult::Type::STR) instead of a boolean, including guidance about mnemonic backup and null-character passphrase handling; functional tests have been updated to assert returned strings in several cases.
📚 Learning: 2025-08-08T07:01:47.332Z
Learnt from: knst
Repo: dashpay/dash PR: 6805
File: src/wallet/rpc/wallet.cpp:357-357
Timestamp: 2025-08-08T07:01:47.332Z
Learning: In src/wallet/rpc/wallet.cpp, the upgradetohd RPC now returns a UniValue string message (RPCResult::Type::STR) instead of a boolean, including guidance about mnemonic backup and null-character passphrase handling; functional tests have been updated to assert returned strings in several cases.

Applied to files:

  • src/wallet/scriptpubkeyman.h
  • src/wallet/scriptpubkeyman.cpp
  • src/wallet/walletdb.cpp
📚 Learning: 2025-02-14T15:19:17.218Z
Learnt from: kwvg
Repo: dashpay/dash PR: 6529
File: src/wallet/rpcwallet.cpp:3002-3003
Timestamp: 2025-02-14T15:19:17.218Z
Learning: The `GetWallet()` function calls in `src/wallet/rpcwallet.cpp` are properly validated with null checks that throw appropriate RPC errors, making additional validation unnecessary.

Applied to files:

  • src/wallet/walletdb.cpp
🧬 Code graph analysis (2)
src/wallet/scriptpubkeyman.h (1)
src/wallet/scriptpubkeyman.cpp (2)
  • LoadHDChain (430-438)
  • LoadHDChain (430-430)
src/wallet/walletdb.cpp (1)
src/wallet/interfaces.cpp (1)
  • spk_man (214-221)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: arm-linux-build / Build source
  • GitHub Check: linux64_ubsan-build / Build source
  • GitHub Check: linux64_sqlite-build / Build source
  • GitHub Check: linux64-build / Build source
  • GitHub Check: linux64_fuzz-build / Build source
  • GitHub Check: mac-build / Build source
  • GitHub Check: win64-build / Build source
  • GitHub Check: linux64_nowallet-build / Build source
  • GitHub Check: linux64_tsan-build / Build source
  • GitHub Check: Lint / Run linters

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Member

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

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

utACK 35dc258

@UdjinM6 UdjinM6 modified the milestones: 23.1, 23.0.1 Nov 25, 2025
@PastaPastaPasta PastaPastaPasta merged commit 35c7990 into dashpay:develop Dec 1, 2025
55 of 58 checks passed
PastaPastaPasta added a commit to PastaPastaPasta/dash that referenced this pull request Dec 1, 2025
35dc258 chore: trivial cleanup (UdjinM6)
96de036 fix: HD chain encryption check ordering issue (UdjinM6)

Pull request description:

  ## Issue being fixed or feature implemented
  Previously, LoadHDChain() would fail if CRYPTED_HDCHAIN records were
  read before MASTER_KEY records during wallet loading, because the
  check `m_storage.HasEncryptionKeys() != chain.IsCrypted()` would
  incorrectly fail when mapMasterKeys was still empty.

  This PR fixes the issue by:
  - Adding an optional fSkipEncryptionCheck parameter to LoadHDChain()
  - Skipping the encryption check during wallet loading in ReadKeyValue()
  - Adding comprehensive validation in LoadWallet() after all records
    are loaded to ensure HD chain encryption consistency

  ## What was done?

  ## How Has This Been Tested?
  run tests

  ## Breaking Changes
  n/a

  ## Checklist:
  - [ ] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [ ] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

ACKs for top commit:
  PastaPastaPasta:
    utACK 35dc258

Tree-SHA512: f986209cb03a87559ddf417a7b0f833303968918086f125170b372d0856f5dd22d41ff3577e788ed100133d6862ca532228c857c4e862f526e53609b0324568e
PastaPastaPasta added a commit that referenced this pull request Dec 2, 2025
736bb26 chore: bump manpages for 23.0.1 (pasta)
d5c7d25 chore: bump nMinimumChainWork and defaultAssumeValid (pasta)
4f8aa71 chore: bump version to 23.0.1 (pasta)
0865b7c docs: add release notes for 23.0.1 (pasta)
2048b42 Merge #6986: test: new commandline argument -tinyblk to use blk size just 64kb instead 16Mb (pasta)
1a9b20c Merge #7013: fix: update BuildTestVectors call to adjust batch size based on output flag (pasta)
36e4679 Merge #7009: fix: include QDebug directly (pasta)
69d0c9c Merge #6999: feat: verify and repair evodb diffs automatically at node startup (pasta)
ca16437 Merge #6996: perf: reduce cs_main lock scope in evodb verify/repair operations (pasta)
207526e Merge #6977: fix: bls benchmarks crash when ran independently (pasta)
226aaf4 Merge #6969: feat: add evodb verify and repair RPC commands (pasta)
92abe9b Merge #6964: perf: remove duplicated check of same key in the instant send database (pasta)
5a1ec4c Merge #6961: fix: correct BLS scheme setting in `MigrateLegacyDiffs()` when `nVersion` is present (pasta)
bf653d3 Merge #6949: depends: Qt 5.15.18 (pasta)
faf58cd merge bitcoin#30774: Qt 5.15.16 (Kittywhiskers Van Gogh)
6a995f5 Merge #6944: fix: HD chain encryption check ordering issue (pasta)

Pull request description:

  ## Issue being fixed or feature implemented
  See commits; release v23.0.1

  ## What was done?
  see commits

  ## How Has This Been Tested?

  ## Breaking Changes

  ## Checklist:
    _Go over all the following points, and put an `x` in all the boxes that apply._
  - [ ] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

ACKs for top commit:
  kwvg:
    utACK 736bb26

Tree-SHA512: fe02c4c3e520b11af54d09c47bda94112313456f9f1cb6d14e78ef16704b2a8ec8feb80fa914c55e152db2bcac7f278291824aaa36c8079eb9e0c9bff9e554a4
PastaPastaPasta added a commit that referenced this pull request Dec 4, 2025
fe1cff3 chore: bump release to 23.0.2 (pasta)
a8f15c1 Merge #7032: fix: drop gsl usage from RebuildListFromBlock function wrapper (pasta)
736bb26 chore: bump manpages for 23.0.1 (pasta)
d5c7d25 chore: bump nMinimumChainWork and defaultAssumeValid (pasta)
4f8aa71 chore: bump version to 23.0.1 (pasta)
0865b7c docs: add release notes for 23.0.1 (pasta)
2048b42 Merge #6986: test: new commandline argument -tinyblk to use blk size just 64kb instead 16Mb (pasta)
1a9b20c Merge #7013: fix: update BuildTestVectors call to adjust batch size based on output flag (pasta)
36e4679 Merge #7009: fix: include QDebug directly (pasta)
69d0c9c Merge #6999: feat: verify and repair evodb diffs automatically at node startup (pasta)
ca16437 Merge #6996: perf: reduce cs_main lock scope in evodb verify/repair operations (pasta)
207526e Merge #6977: fix: bls benchmarks crash when ran independently (pasta)
226aaf4 Merge #6969: feat: add evodb verify and repair RPC commands (pasta)
92abe9b Merge #6964: perf: remove duplicated check of same key in the instant send database (pasta)
5a1ec4c Merge #6961: fix: correct BLS scheme setting in `MigrateLegacyDiffs()` when `nVersion` is present (pasta)
bf653d3 Merge #6949: depends: Qt 5.15.18 (pasta)
faf58cd merge bitcoin#30774: Qt 5.15.16 (Kittywhiskers Van Gogh)
6a995f5 Merge #6944: fix: HD chain encryption check ordering issue (pasta)
6fd7059 chore: mark v23 as release (pasta)
ae08f53 docs: integrate 6946 release notes into final (pasta)
74a222d Merge #6946: feat: show seed on wallet creation (pasta)
877343a Merge #6943: fix: don't treat arrays/objects as string literals for composite methods (pasta)
00368fb Merge #6940: fix: reuse best clsig to avoid potential race condition (pasta)
8eceb98 Merge #6938: fix: logic error in `CheckDecryptionKey` (pasta)
3f30664 Merge #6929: fix: repair `makeseeds.py`, `getblockchaininfo[softforks]` help text, drop extra `generate`s from test, resolve macOS GID issue (pasta)
7ba4f1c Merge #6928: docs: update man pages for 23.0 (pasta)
a6c1d6a Merge #6920: chore: update minimum chain work, tx stats, checkpoints, seeds and SECURITY.md for v23 (pasta)
84df1f0 Merge #6909: chore: Translations 2025-10 (pasta)
a6449b1 Merge #6885: fix: improve governance/proposal dialog strings (pasta)
ebf3a64 docs: typos (pasta)
4ad5533 docs: typos (pasta)
f407453 doc: Replace Bitcoin Core PR references with Dash Core backport PRs (pasta)
78d0725 docs: add note on proto bump and platformban p2p (pasta)
e0519c3 docs: fix whitespace errors (pasta)
bc8db22 docs: minor improvements to release notes (pasta)
c338511 docs: reorganize rpc updates to organize extended address changes (thephez)
700c46e style: make heading style consistent (thephez)
bd636bd docs: add contributors (pasta)
6d29bc3 docs: revert changes deferred to v24 (pasta)
615f5ff docs: make the downgrade warning more confident (pasta)
567771a Apply suggestions from code review (PastaPastaPasta)
2b3211a docs: add link to 22.1.3 release notes (pasta)
548a38a docs: remove individual release notes files (pasta)
e770c25 docs: add v23.0.0 release notes and archive v22.1.3 (pasta)

Pull request description:

  ## Issue being fixed or feature implemented
  Includes changes from 23.0.0 release too because we never merged it back.

  ## What was done?

  ## How Has This Been Tested?

  ## Breaking Changes

  ## Checklist:
  - [ ] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [ ] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_

ACKs for top commit:
  PastaPastaPasta:
    utACK 491db4a
  kwvg:
    utACK 491db4a

Tree-SHA512: 61895cd1f9d01ac7be1d407afe1ddd24b98e8242cb03229ecd586a4d2d1c43dbc62c98da52a8c715b3a5943fb40e99b23251e691f778779af9d6da94392122a3
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.

2 participants