Only check nWitnessCacheSize on rewind if we've ever witnessed a Sprout or Sapling note.

This allows "rollback" for empty Sapling wallets injected into nonempty
chain state that is then rolled back.
This commit is contained in:
Kris Nuttycombe 2022-04-04 15:31:19 -06:00
parent afb503503d
commit 8e4dd1e964
1 changed files with 11 additions and 3 deletions

View File

@ -2726,13 +2726,21 @@ void DecrementNoteWitnesses(NoteDataMap& noteDataMap, int indexHeight, int64_t n
void CWallet::DecrementNoteWitnesses(const Consensus::Params& consensus, const CBlockIndex* pindex) void CWallet::DecrementNoteWitnesses(const Consensus::Params& consensus, const CBlockIndex* pindex)
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
bool hasSprout = false;
bool hasSapling = false;
for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) { for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) {
hasSprout |= !wtxItem.second.mapSproutNoteData.empty();
::DecrementNoteWitnesses(wtxItem.second.mapSproutNoteData, pindex->nHeight, nWitnessCacheSize); ::DecrementNoteWitnesses(wtxItem.second.mapSproutNoteData, pindex->nHeight, nWitnessCacheSize);
hasSapling |= !wtxItem.second.mapSaplingNoteData.empty();
::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, pindex->nHeight, nWitnessCacheSize); ::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, pindex->nHeight, nWitnessCacheSize);
} }
nWitnessCacheSize -= 1; if (nWitnessCacheSize > 0) {
// TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302) nWitnessCacheSize -= 1;
assert(nWitnessCacheSize > 0); }
// TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302);
// however, if we have never observed Sprout or Sapling notes, this is okay
// because then the witness cache size can remain at 0.
assert(!(hasSprout || hasSapling) || nWitnessCacheSize > 0);
// ORCHARD: rewind to the last checkpoint. // ORCHARD: rewind to the last checkpoint.
if (consensus.NetworkUpgradeActive(pindex->nHeight, Consensus::UPGRADE_NU5)) { if (consensus.NetworkUpgradeActive(pindex->nHeight, Consensus::UPGRADE_NU5)) {