upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/storage.md
diff options
context:
space:
mode:
authorbitflash-maintainer <noreply@bitflash.invalid>2026-09-06 01:53:03 +0000
committerbitflash-maintainer <noreply@bitflash.invalid>2026-09-06 01:53:03 +0000
commitef9f06d43fc6577c15df2256abbb25b20d4227b8 (patch)
tree7e9132aa871fdc01cb95268d2f5ea3bb11853ddf /docs/storage.md
parentbe63e83d79e7e00e6b8ca889a70e6a19886aa1d8 (diff)
parentceae3d076b5ad11ee84729ae43f14e9312896646 (diff)
Merge branch 'chore/main-catchup-2026-08-26' into 'main'HEADmain
Bring main onto the line this project actually ships (incl. the 1.2.23 compactblock port) Closes #6, #7, #8, #11, #35, #34, #29, #28, #27, and #16 See merge request !16
Diffstat (limited to 'docs/storage.md')
-rw-r--r--docs/storage.md159
1 files changed, 159 insertions, 0 deletions
diff --git a/docs/storage.md b/docs/storage.md
new file mode 100644
index 0000000..c7de4e7
--- /dev/null
+++ b/docs/storage.md
@@ -0,0 +1,159 @@
1# Bitflash Storage Inventory
2
3This document records the Berkeley DB surface that still exists in Bitflash.
4It is a migration map, not a promise that all stores should move at once.
5
6The immediate priority is `wallet.dat`: it holds spend authority and recovery
7metadata. `blkindex.dat` can be rebuilt from block files, so it is important for
8startup correctness but less dangerous than wallet storage.
9
10## Berkeley DB Environment
11
12All `CDB` stores use one Berkeley DB environment rooted at:
13
14```text
15<datadir>/database/
16```
17
18The environment creates log files such as:
19
20```text
21database/log.0000000001
22```
23
24Those logs are part of the legacy BDB risk model. They are not user-facing
25wallet files, but they can contain historical records while the environment is
26live. Wallet encryption already rewrites `wallet.dat` and purges BDB environment
27logs after the encrypted file is installed.
28
29## Common Records
30
31Every `CDB` file can carry:
32
33| Key | Value | Purpose |
34|---|---|---|
35| `"version"` | `int` | Database format version written when a DB is opened in create mode. |
36
37Keys and values are serialized with the existing `CDataStream` format. A future
38SQLite migration must preserve byte-for-byte value semantics until a higher
39level schema migration is intentionally introduced.
40
41## `wallet.dat`
42
43`wallet.dat` is accessed through `CWalletDB`.
44Read-only tooling should go through `ScanWalletRecords()` instead of opening a
45Berkeley DB cursor directly. That keeps migration checks, audits, and future
46backends behind one record-stream boundary. Wallet rewrite code should also use
47that boundary when it needs to copy existing records into a newly written
48wallet file.
49
50| Key shape | Value | Secret? | Meaning |
51|---|---|---:|---|
52| `("name", address)` | `string` | No | Address book label. |
53| `("tx", txid)` | `CWalletTx` | No | Wallet transaction state, including spend flags and metadata. |
54| `("key", pubkey)` | `CPrivKey` | Yes | Plain private key record for unencrypted wallets or old backups. |
55| `"defaultkey"` | `vector<unsigned char>` | No | Default public key shown by the wallet. |
56| `"hdmaster"` | `vector<unsigned char>` | Yes | Plain BIP32 master private key material. |
57| `"hdchaincode"` | `vector<unsigned char>` | Yes | Plain BIP32 chain code. |
58| `"hdnext"` | `unsigned int` | No | Legacy deterministic derivation next index. |
59| `"hdschema"` | `int` | No | Deterministic derivation schema (`legacy` or `bip44`). |
60| `"hdcointype"` | `unsigned int` | No | BIP44 coin type. Bitflash is `4346950`. |
61| `"hdreceivenext"` | `unsigned int` | No | BIP44 external receive-chain next index. |
62| `"hdchangenext"` | `unsigned int` | No | BIP44 internal change-chain next index. |
63| `("mkey", id)` | `CWalletMasterKey` | Sensitive | Encrypted wallet master-key metadata. |
64| `("ckey", pubkey)` | `vector<unsigned char>` | Yes | Private key encrypted under the wallet master key. |
65| `"cryptedhdmaster"` | `vector<unsigned char>` | Yes | Encrypted BIP32 master private key material. |
66| `"cryptedhdchaincode"` | `vector<unsigned char>` | Yes | Encrypted BIP32 chain code. |
67| `"walletminversion"` | `int` | No | Minimum wallet format version required to open this wallet safely. |
68| `("pool", index)` | `vector<unsigned char>` | No | Public key reserved in the key pool. The private key lives in `key`/`ckey`. |
69| `("setting", name)` | typed setting value | Usually no | Wallet/UI settings such as mining mode, fees, and backup timestamps. |
70
71### Wallet Migration Rules
72
73A future wallet backend must keep these properties:
74
751. Unknown future wallet formats fail closed with a clear message.
762. Encrypted wallets must encrypt private keys and HD seed material.
773. A locked encrypted wallet may expose public keys and balances, but not
78 private keys or decrypted HD seed material.
794. `-backupwallet` must produce a wallet file that opens in a different datadir
80 without copying `database/`, and must not leave a half-written final backup
81 path if the copy or install step fails.
825. `-recoveryaudit` must distinguish:
83 - no recovery phrase installed;
84 - recovery phrase present;
85 - recovery phrase encrypted and locked, requiring unlock before coverage can
86 be audited.
87
88## SQLite Wallet Store
89
90Since v1.2.20 SQLite is the default wallet backend. It is a byte-preserving
91record store, not a new wallet format -- the same serialized `CDataStream` bytes
92`wallet.dat` uses:
93
94```sql
95CREATE TABLE wallet_records (
96 key BLOB PRIMARY KEY NOT NULL,
97 value BLOB NOT NULL
98);
99PRAGMA user_version=1;
100```
101
102A fresh data directory starts on `wallet.sqlite`. An existing `wallet.dat` keeps
103running on Berkeley DB until it is converted: the GUI offers a one-click
104conversion on first run, and `-walletbackend=sqlite` / `-walletbackend=bdb`
105(recorded in a plain-text `wallet-backend` marker file in the datadir) select a
106backend explicitly. Conversion never modifies `wallet.dat`; it stays as a
107fallback, and the change takes effect on restart.
108
109Every wallet operation runs on either backend: read, write, erase, key-pool and
110HD-seed writes (the two halves of the seed bracketed in one SQLite transaction),
111`-backupwallet` (checkpoint then copy the self-contained file), `-encryptwallet`
112(an atomic rebuild-and-swap in one transaction), and the `-walletstorageaudit` /
113`-walletstoragecheck` diagnostics, which read the active backend's file.
114
115The migration tooling remains available for scripted use. `-walletsqliteexport=FILE`
116streams every raw `wallet.dat` record through `ScanWalletRecords()` into a SQLite
117file in one transaction, refusing to overwrite. `-walletsqliteverify=FILE`
118compares the two streams and prints only counts. `-walletsqliteloadcheck=FILE`
119parses an export with the same record types the loader uses.
120`-walletsqliterestore=FILE` rebuilds a `wallet.dat` from an export in an empty
121data directory. Failed exports and restores remove their incomplete output.
122
123## `blkindex.dat`
124
125`blkindex.dat` is accessed through `CTxDB`.
126
127| Key shape | Value | Rebuildable? | Meaning |
128|---|---|---:|---|
129| `("tx", txid)` | `CTxIndex` | Yes | Transaction disk position and spent-output index state. |
130| `("owner", hash160, CDiskTxPos)` | `int height` | Yes | Legacy owner transaction index cursor range. |
131| `("blockindex", blockhash)` | `CDiskBlockIndex` | Yes | Block index entry. |
132| `"hashBestChain"` | `uint256` | Yes | Current best-chain tip hash. |
133
134`blkindex.dat` can be rebuilt from `blk*.dat` plus consensus validation. A
135future migration may therefore prefer a rebuild path over a byte-preserving
136conversion path.
137
138## Other Legacy Stores
139
140None. Upstream still documents `reviews.dat` (`CReviewDB`) and `market.dat`
141(`CMarketDB`) here; this fork deleted the 2009 marketplace outright, along
142with `CReviewDB`, `market.cpp` and `market.h`, so neither file is opened or
143created by this tree. A datadir carrying them from an older build is simply
144ignored.
145
146The old IP address database (`addr.dat`) has been removed. Peer discovery is
147handled by Nostr, `.btf`, rendezvous relays, and direct onion peers.
148
149## Migration Order (shipped in v1.2.20)
150
1511. Storage self-tests were added around Berkeley DB behavior first.
1522. The SQLite backend landed behind an explicit flag, then behind a
153 `wallet-backend` marker and a GUI conversion wizard.
1543. BDB-to-SQLite migration is a one-way copy that leaves `wallet.dat` as a backup.
1554. SQLite became the default for new wallets only after cross-version restore,
156 encryption, backup, and recovery-audit tests passed -- and a live coin-proof
157 of receive, spend, restart, and encrypt on real mainnet funds.
1585. Treat `blkindex.dat` separately; it is safer to rebuild than to migrate under
159 pressure.