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.
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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 (...)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user