From 83d7b5b67b88d08653a1a5c366d7164f93b2b2c3 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Sat, 15 Oct 2016 12:26:22 -0500 Subject: [PATCH 1/4] Add more asserts to track down the bug --- src/wallet/wallet.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 8a80f2db6..8c41c5313 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -675,6 +675,8 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, for (std::pair& wtxItem : mapWallet) { for (mapNoteData_t::value_type& item : wtxItem.second.mapNoteData) { CNoteData* nd = &(item.second); + // Check the validity of the cache + assert(nWitnessCacheSize >= nd->witnesses.size()); if (nd->witnesses.size() > 0) { nd->witnesses.front().append(note_commitment); } @@ -695,6 +697,13 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, if (nWitnessCacheSize < WITNESS_CACHE_SIZE) { nWitnessCacheSize += 1; } + for (std::pair& wtxItem : mapWallet) { + for (mapNoteData_t::value_type& item : wtxItem.second.mapNoteData) { + CNoteData* nd = &(item.second); + // Check the validity of the cache + assert(nWitnessCacheSize >= nd->witnesses.size()); + } + } if (fFileBacked) { CWalletDB walletdb(strWalletFile); WriteWitnessCache(walletdb); @@ -709,12 +718,21 @@ void CWallet::DecrementNoteWitnesses() for (std::pair& wtxItem : mapWallet) { for (mapNoteData_t::value_type& item : wtxItem.second.mapNoteData) { CNoteData* nd = &(item.second); + // Check the validity of the cache + assert(nWitnessCacheSize >= nd->witnesses.size()); if (nd->witnesses.size() > 0) { nd->witnesses.pop_front(); } } } nWitnessCacheSize -= 1; + for (std::pair& wtxItem : mapWallet) { + for (mapNoteData_t::value_type& item : wtxItem.second.mapNoteData) { + CNoteData* nd = &(item.second); + // Check the validity of the cache + assert(nWitnessCacheSize >= nd->witnesses.size()); + } + } // TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302) assert(nWitnessCacheSize > 0); if (fFileBacked) { From 8a7d37befda5fb06a4acddbadf6ec1d68fdad67c Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Sun, 16 Oct 2016 15:29:20 +0100 Subject: [PATCH 2/4] Move the increment of nWitnessCacheSize to make the later assertions correct. Signed-off-by: Daira Hopwood --- src/wallet/wallet.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 8c41c5313..35e99328f 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -654,6 +654,9 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, } } } + if (nWitnessCacheSize < WITNESS_CACHE_SIZE) { + nWitnessCacheSize += 1; + } const CBlock* pblock {pblockIn}; CBlock block; @@ -694,9 +697,6 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, } } } - if (nWitnessCacheSize < WITNESS_CACHE_SIZE) { - nWitnessCacheSize += 1; - } for (std::pair& wtxItem : mapWallet) { for (mapNoteData_t::value_type& item : wtxItem.second.mapNoteData) { CNoteData* nd = &(item.second); From 8e41408aa72e90858a10179d75eec3b9ca879f1f Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Sun, 16 Oct 2016 16:00:45 +0100 Subject: [PATCH 3/4] Add another assertion to narrow down where the bug occurs. Signed-off-by: Daira Hopwood --- src/wallet/wallet.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 35e99328f..e857a23f8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -690,8 +690,10 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, if (txIsOurs) { JSOutPoint jsoutpt {hash, i, j}; if (mapWallet[hash].mapNoteData.count(jsoutpt)) { - mapWallet[hash].mapNoteData[jsoutpt].witnesses.push_front( - tree.witness()); + CNoteData* nd = &(mapWallet[hash].mapNoteData[jsoutpt]); + nd->witnesses.push_front(tree.witness()); + // Check the validity of the cache + assert(nWitnessCacheSize >= nd->witnesses.size()); } } } From 95e32d88b020567db36af667e969070c22e2ea32 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Mon, 17 Oct 2016 01:49:28 +0100 Subject: [PATCH 4/4] Add another assertion about the witness cache. Signed-off-by: Daira Hopwood --- src/wallet/wallet.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index e857a23f8..7723c034f 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -691,6 +691,7 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex, JSOutPoint jsoutpt {hash, i, j}; if (mapWallet[hash].mapNoteData.count(jsoutpt)) { CNoteData* nd = &(mapWallet[hash].mapNoteData[jsoutpt]); + assert(nd->witnesses.size() == 0); nd->witnesses.push_front(tree.witness()); // Check the validity of the cache assert(nWitnessCacheSize >= nd->witnesses.size());