Browse Source

Replace vAnchorCache with a cache size counter

The anchor is obtained from the returned witnesses; since all witnesses are to
the same point (the latest blockchain tip), they all have the same root.
metaverse
Jack Grigg 8 years ago
parent
commit
4086e5ce98
  1. 3
      src/wallet/gtest/test_wallet.cpp
  2. 28
      src/wallet/wallet.cpp
  3. 7
      src/wallet/wallet.h
  4. 8
      src/wallet/walletdb.cpp
  5. 2
      src/wallet/walletdb.h

3
src/wallet/gtest/test_wallet.cpp

@ -402,7 +402,8 @@ TEST(wallet_tests, cached_witnesses_chain_tip) {
witnesses.clear(); witnesses.clear();
wallet.GetNoteWitnesses(notes, witnesses, anchor3); wallet.GetNoteWitnesses(notes, witnesses, anchor3);
EXPECT_FALSE((bool) witnesses[0]); EXPECT_FALSE((bool) witnesses[0]);
EXPECT_EQ(anchor1, anchor3); // Should not equal first anchor because none of these notes had witnesses
EXPECT_NE(anchor1, anchor3);
// Re-incrementing with the same block should give the same result // Re-incrementing with the same block should give the same result
uint256 anchor4; uint256 anchor4;

28
src/wallet/wallet.cpp

@ -649,12 +649,11 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex,
} }
} }
} }
vAnchorCache.push_front(tree.root()); if (nWitnessCacheSize < WITNESS_CACHE_SIZE) {
if (vAnchorCache.size() > WITNESS_CACHE_SIZE) { nWitnessCacheSize += 1;
vAnchorCache.pop_back();
} }
if (fFileBacked) { if (fFileBacked) {
CWalletDB(strWalletFile).WriteAnchorCache(vAnchorCache); CWalletDB(strWalletFile).WriteWitnessCacheSize(nWitnessCacheSize);
} }
} }
} }
@ -671,12 +670,11 @@ void CWallet::DecrementNoteWitnesses()
} }
} }
} }
if (vAnchorCache.size() > 0) { nWitnessCacheSize -= 1;
vAnchorCache.pop_front(); // TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302)
} assert(nWitnessCacheSize > 0);
// TODO: If vAnchorCache is empty, we need to regenerate the caches (#1302)
if (fFileBacked) { if (fFileBacked) {
CWalletDB(strWalletFile).WriteAnchorCache(vAnchorCache); CWalletDB(strWalletFile).WriteWitnessCacheSize(nWitnessCacheSize);
} }
} }
} }
@ -1070,18 +1068,24 @@ void CWallet::GetNoteWitnesses(std::vector<JSOutPoint> notes,
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
witnesses.resize(notes.size()); witnesses.resize(notes.size());
boost::optional<uint256> rt;
int i = 0; int i = 0;
for (JSOutPoint note : notes) { for (JSOutPoint note : notes) {
if (mapWallet.count(note.hash) && if (mapWallet.count(note.hash) &&
mapWallet[note.hash].mapNoteData.count(note) && mapWallet[note.hash].mapNoteData.count(note) &&
mapWallet[note.hash].mapNoteData[note].witnesses.size() > 0) { mapWallet[note.hash].mapNoteData[note].witnesses.size() > 0) {
witnesses[i] = mapWallet[note.hash].mapNoteData[note].witnesses.front(); witnesses[i] = mapWallet[note.hash].mapNoteData[note].witnesses.front();
if (!rt) {
rt = witnesses[i]->root();
} else {
assert(*rt == witnesses[i]->root());
}
} }
i++; i++;
} }
// vAnchorCache should only be empty here before the genesis block (so, never) // All returned witnesses have the same anchor
if (vAnchorCache.size() > 0) { if (rt) {
final_anchor = vAnchorCache.front(); final_anchor = *rt;
} }
} }
} }

7
src/wallet/wallet.h

@ -580,10 +580,11 @@ private:
public: public:
/* /*
* Cached anchors corresponding to the cached incremental witnesses for the * Size of the incremental witness cache for the notes in our wallet.
* notes in our wallet. * This will always be greater than or equal to the size of the largest
* incremental witness cache in any transaction in mapWallet.
*/ */
std::list<uint256> vAnchorCache; int64_t nWitnessCacheSize;
protected: protected:
void IncrementNoteWitnesses(const CBlockIndex* pindex, void IncrementNoteWitnesses(const CBlockIndex* pindex,

8
src/wallet/walletdb.cpp

@ -162,10 +162,10 @@ bool CWalletDB::WriteDefaultKey(const CPubKey& vchPubKey)
return Write(std::string("defaultkey"), vchPubKey); return Write(std::string("defaultkey"), vchPubKey);
} }
bool CWalletDB::WriteAnchorCache(const std::list<uint256>& vAnchorCache) bool CWalletDB::WriteWitnessCacheSize(int64_t nWitnessCacheSize)
{ {
nWalletDBUpdated++; nWalletDBUpdated++;
return Write(std::string("anchorcache"), vAnchorCache); return Write(std::string("witnesscachesize"), nWitnessCacheSize);
} }
bool CWalletDB::ReadPool(int64_t nPool, CKeyPool& keypool) bool CWalletDB::ReadPool(int64_t nPool, CKeyPool& keypool)
@ -637,9 +637,9 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
return false; return false;
} }
} }
else if (strType == "anchorcache") else if (strType == "witnesscachesize")
{ {
ssValue >> pwallet->vAnchorCache; ssValue >> pwallet->nWitnessCacheSize;
} }
} catch (...) } catch (...)
{ {

2
src/wallet/walletdb.h

@ -106,7 +106,7 @@ public:
bool WriteDefaultKey(const CPubKey& vchPubKey); bool WriteDefaultKey(const CPubKey& vchPubKey);
bool WriteAnchorCache(const std::list<uint256>& vAnchorCache); bool WriteWitnessCacheSize(int64_t nWitnessCacheSize);
bool ReadPool(int64_t nPool, CKeyPool& keypool); bool ReadPool(int64_t nPool, CKeyPool& keypool);
bool WritePool(int64_t nPool, const CKeyPool& keypool); bool WritePool(int64_t nPool, const CKeyPool& keypool);

Loading…
Cancel
Save