diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 216206fd2..cebae5385 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -557,6 +557,46 @@ TEST(wallet_tests, cached_witnesses_chain_tip) { } } +TEST(wallet_tests, ClearNoteWitnessCache) { + TestWallet wallet; + + auto sk = libzcash::SpendingKey::random(); + wallet.AddSpendingKey(sk); + + auto wtx = GetValidReceive(sk, 10, true); + auto note = GetNote(sk, wtx, 0, 0); + auto nullifier = note.nullifier(sk); + + mapNoteData_t noteData; + JSOutPoint jsoutpt {wtx.GetHash(), 0, 0}; + JSOutPoint jsoutpt2 {wtx.GetHash(), 0, 1}; + CNoteData nd {sk.address(), nullifier}; + noteData[jsoutpt] = nd; + wtx.SetNoteData(noteData); + + // Pretend we mined the tx by adding a fake witness + ZCIncrementalMerkleTree tree; + wtx.mapNoteData[jsoutpt].witnesses.push_front(tree.witness()); + + wallet.AddToWallet(wtx, true, NULL); + + std::vector notes {jsoutpt, jsoutpt2}; + std::vector> witnesses; + uint256 anchor2; + + // Before clearing, we should have a witness for one note + wallet.GetNoteWitnesses(notes, witnesses, anchor2); + EXPECT_TRUE((bool) witnesses[0]); + EXPECT_FALSE((bool) witnesses[1]); + + // After clearing, we should not have a witness for either note + wallet.ClearNoteWitnessCache(); + witnesses.clear(); + wallet.GetNoteWitnesses(notes, witnesses, anchor2); + EXPECT_FALSE((bool) witnesses[0]); + EXPECT_FALSE((bool) witnesses[1]); +} + TEST(wallet_tests, UpdatedNoteData) { TestWallet wallet; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 2e1449fc2..85e6944c1 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -592,6 +592,17 @@ void CWallet::AddToSpends(const uint256& wtxid) } } +void CWallet::ClearNoteWitnessCache() +{ + LOCK(cs_wallet); + for (std::pair& wtxItem : mapWallet) { + for (mapNoteData_t::value_type& item : wtxItem.second.mapNoteData) { + CNoteData* nd = &(item.second); + nd->witnesses.clear(); + } + } +} + void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, const CBlock* pblockIn, ZCIncrementalMerkleTree tree) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1bfb75792..28feb872e 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -598,6 +598,8 @@ public: */ int64_t nWitnessCacheSize; + void ClearNoteWitnessCache(); + protected: void IncrementNoteWitnesses(const CBlockIndex* pindex, const CBlock* pblock,