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();
wallet.GetNoteWitnesses(notes, witnesses, anchor3);
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
uint256 anchor4;

28
src/wallet/wallet.cpp

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

7
src/wallet/wallet.h

@ -580,10 +580,11 @@ private:
public:
/*
* Cached anchors corresponding to the cached incremental witnesses for the
* notes in our wallet.
* Size of the incremental witness cache for the 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:
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);
}
bool CWalletDB::WriteAnchorCache(const std::list<uint256>& vAnchorCache)
bool CWalletDB::WriteWitnessCacheSize(int64_t nWitnessCacheSize)
{
nWalletDBUpdated++;
return Write(std::string("anchorcache"), vAnchorCache);
return Write(std::string("witnesscachesize"), nWitnessCacheSize);
}
bool CWalletDB::ReadPool(int64_t nPool, CKeyPool& keypool)
@ -637,9 +637,9 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
return false;
}
}
else if (strType == "anchorcache")
else if (strType == "witnesscachesize")
{
ssValue >> pwallet->vAnchorCache;
ssValue >> pwallet->nWitnessCacheSize;
}
} catch (...)
{

2
src/wallet/walletdb.h

@ -106,7 +106,7 @@ public:
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 WritePool(int64_t nPool, const CKeyPool& keypool);

Loading…
Cancel
Save