silent payments: file-backed wallet store - #60
Conversation
|
Although we will probably want to move this way at some point in the future, given how simple our use case is, this might be overkill (thanks for demonstrating regardless). The way it integrates with the node and wallet appears awkward to me. I believe you had a PR that was more in line with what I'm about to recommend, so hopefully there's some code already present for these changes
|
|
Got it. I didn't have a clear sense of scale so I was making trade-offs that went beyond what we actually need. The separation between wallet code and file code, and the cost of rewriting the file, both matter less than I assumed. Easy enough to redo using the save/load pattern from the keys file PR. |
|
Pushed the rework. Updated the PR description to reflect the changes. |
The wallet keeps scan progress and found coins only in memory, so a node restart re-scans from genesis. Add a `wallet_store` module that persists the wallet to disk. `Wallet` implements the file extension trait. Save serializes the whole wallet to a small binary file. Load reads it back. Each save writes to a `.tmp` file, flushes it, then renames it over the wallet file so a crash mid-write leaves the previous file intact. `WalletStore` pairs the wallet file path with the configured network. Save and load delegate to `Wallet::save` / `Wallet::load`.
Wire the wallet save/load through the node binary so wallet state actually survives restarts. At startup, the node builds a `WalletStore` from `<datadir>/wallet.bin` and the configured network. If the file exists, it is loaded through the store. Otherwise a fresh wallet is created. Any load failure exits with an error rather than scanning into the wrong file. The scan thread saves the wallet through the store after every block it processes. If a save fails, the thread escalates through `FatalShutdown` so the node goes down before the next block can advance `scan_height` past data that never reached disk.
|
Is this ready for review? |
I believe so, the only major design I was stuck on was wallet updates and that's clarified now. I did some end to end tests locally and it appears to work. |
rustaceanrob
left a comment
There was a problem hiding this comment.
Sent a payment and did a few startup/shutdowns. Works as expected.
Updated design:
Walletgainssaveandloadmethods throughFileExt. Each save writes the whole wallet to a binary file. Toavoid leaving a half-written file if the node crashes during a save, the save writes to a temporary file first and then renames it over the real one.
WalletStoreis type that holds the wallet file's path and the network the file should be for. It calls the wallet's save and load methods.When the node starts, it loads the wallet. The scan thread saves the wallet through the store after every
block it processes. If a save fails, the node shuts down through
FatalShutdownso it cannot keep scanning past data that never reached disk.I have the
sp-wallet-store-backupbranch on my fork for reference.