Skip to content

Commit 861e7f9

Browse files
committed
wallet: Replace "used" destdata with its own member variable
Instead of having "used" be an opaque value in the destdata map of a CAddressBookData, use an explicit member variable that can also document what the value represents.
1 parent 5e1aab2 commit 861e7f9

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/wallet/wallet.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,10 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
24442444
{
24452445
batch.EraseDestData(strAddress, item.first);
24462446
}
2447+
2448+
// Explicitly erase "used" record since we no longer store it in destdata
2449+
batch.EraseDestData(strAddress, "used");
2450+
24472451
m_address_book.erase(address);
24482452
is_mine = IsMine(address) != ISMINE_NO;
24492453
}
@@ -2821,38 +2825,40 @@ unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx, bool rescanning_old
28212825

28222826
bool CWallet::SetAddressUsed(WalletBatch& batch, const CTxDestination& dest, bool used)
28232827
{
2824-
const std::string key{"used"};
2825-
if (std::get_if<CNoDestination>(&dest))
2828+
if (std::get_if<CNoDestination>(&dest)) {
28262829
return false;
2830+
}
2831+
2832+
// Update CAddressBookData
2833+
m_address_book[dest].SetInputUsed(used);
28272834

2828-
if (!used) {
2829-
if (auto* data = util::FindKey(m_address_book, dest)) data->destdata.erase(key);
2835+
// Update the old database records
2836+
const std::string key{"used"};
2837+
if (used) {
2838+
return batch.WriteDestData(EncodeDestination(dest), key, "1");
2839+
} else {
28302840
return batch.EraseDestData(EncodeDestination(dest), key);
28312841
}
2832-
2833-
const std::string value{"1"};
2834-
m_address_book[dest].destdata.insert(std::make_pair(key, value));
2835-
return batch.WriteDestData(EncodeDestination(dest), key, value);
28362842
}
28372843

28382844
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
28392845
{
2846+
// Special case for "used" since we no longer store it in destdata
2847+
if (key == "used") {
2848+
m_address_book[dest].SetInputUsed(value == "1");
2849+
return;
2850+
}
2851+
28402852
m_address_book[dest].destdata.insert(std::make_pair(key, value));
28412853
}
28422854

28432855
bool CWallet::IsAddressUsed(const CTxDestination& dest) const
28442856
{
2845-
const std::string key{"used"};
2846-
std::map<CTxDestination, CAddressBookData>::const_iterator i = m_address_book.find(dest);
2847-
if(i != m_address_book.end())
2848-
{
2849-
CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
2850-
if(j != i->second.destdata.end())
2851-
{
2852-
return true;
2853-
}
2857+
const CAddressBookData* entry = FindAddressBookEntry(dest, /*allow_change=*/true);
2858+
if(!entry) {
2859+
return false;
28542860
}
2855-
return false;
2861+
return entry->GetInputUsed();
28562862
}
28572863

28582864
std::vector<std::string> CWallet::GetAddressReceiveRequests() const

src/wallet/wallet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ class ReserveDestination
204204
class CAddressBookData
205205
{
206206
private:
207+
//! The address is a change addres
207208
bool m_change{true};
209+
//! The label
208210
std::string m_label;
211+
//! This address has been used in a transaction input
212+
bool m_input_used{false};
209213
public:
210214
std::string purpose;
211215

@@ -220,6 +224,9 @@ class CAddressBookData
220224
m_change = false;
221225
m_label = label;
222226
}
227+
228+
bool GetInputUsed() const { return m_input_used; }
229+
void SetInputUsed(bool value) { m_input_used = value; }
223230
};
224231

225232
struct CRecipient

0 commit comments

Comments
 (0)