Repository navigation
Conversation
importsaplingviewingkey kept the key in memory only, while the notes it
found were written to the wallet, tagged with the key's ivk. After a
restart the key was gone, in one of two ways:
- If a rescan reached one of those notes, mapSaplingFullViewingKeys.at()
threw std::out_of_range and pivxd died with "map::at" in AppInit.
- If none did, the node started normally and logged nothing. It found
no new notes for the key and missed its spends, while the old notes
still counted in the balance.
Write the key to a new "sapextfvk" record and load it with the wallet.
Older clients ignore the record. It is not encrypted: it cannot spend,
and encrypted Sapling keys store their extended full viewing key in the
clear too. The record counts as a key for -salvagewallet. A viewing key
has no birthday, so loading one sets the wallet's to the start of time.
Wallets written before this change hold such notes and no key. Skip
those notes instead of throwing, so the node starts and the key can be
imported again; a rescan with the import finds what the wallet missed.
ScanForWalletTransactions also wrote the notes it touched, with their
witnesses, but not nWitnessCacheSize, which SetBestChain normally writes
with them. A wallet created on a running chain has none saved, so it
loads as 0. After importing a key (viewing or spending) with a rescan
over its notes, the next startup failed
nWitnessCacheSize >= (int64_t) nd->witnesses.size()
in CopyPreviousWitnesses until the first periodic SetBestChain, up to
an hour later. Write the size in the same batch as the notes. A wallet
already in this state recovers by starting once with -rescan.
sapling_wallet_persistence.py now covers these cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
importsaplingviewingkeyadds the key in memory only. The notes the import finds are saved, taggedwith the key's ivk, but the key itself is not. After a restart a wallet that holds only viewing keys
breaks in one of two ways:
mapSaplingFullViewingKeys.at(ivk)throws and pivxd exits during init with
map::at.the node starts normally and logs nothing, but the address is gone from
listshieldaddresses.New notes are not found and spends are missed, while the old notes still count in the balance.
Which of the two happens depends on how long the node ran, because the wallet's best block is only
saved periodically.
A second bug affects any key imported with a rescan into a wallet created on a running chain.
ScanForWalletTransactionswrites the notes it touched with their witnesses, but notnWitnessCacheSize. The wallet has no size saved yet, so it loads as 0, and the next start aborts onin
CopyPreviousWitnesses. This keeps happening until the first periodicSetBestChainwrites thesize, up to an hour later. Wallets created at block 0, as in most tests, hide the bug: the startup
rescan raises the saved size.
Both bugs are in v5.6.1 and on master. We found them while running a node that watches shielded
addresses with viewing keys only.
Changes
sapextfvk(key: ivk, value: extended full viewing key).AddViewingKeyToWalletwrites it before adding the key in memory, andLoadWalletloads it.csapzkeyalready stores the extfvk in the clear.-salvagewallet.watch-only scripts.
UpdateSaplingNullifierNoteMapWithTxskips a note whose viewing key is missing, instead ofthrowing. This only happens in wallets written before this change. The node starts, and importing
the key again with a rescan restores the address.
ScanForWalletTransactionswritesnWitnessCacheSizein the same batch as the notes.Recovering an affected wallet
Start once with
-rescan, which rebuilds the witnesses. Then import the viewing key again:importsaplingviewingkey "<key>" "yes" <start height>.Tests
sapling_wallet_persistence.pynow runs two nodes and covers:the address;
encryptwallet;followed by a restart.
Without the fix the new cases fail: pivxd exits with
map::atat the first restart.Tested on master 4b3bc73, built in ubuntu:20.04 with depends, as in CI. All 87 unit test suites
pass (each in its own process), as do every
sapling_*andwallet_*functional test andtiertwo_deterministicmns.py. The same change also passes on v5.6.1.