Skip to content

Commit f5ba424

Browse files
committed
wallet: Add IsAddressUsed / SetAddressUsed methods
This simplifies code and adds a less cumbersome interface for accessing address used information than CWallet AddDestData / EraseDestData / GetDestData methods. There is no change in behavior. Lower-level walletdb DestData methods are also still available and not affected by this change. If there is interest in consolidating destdata logic more and making it internal to walletdb, #18608 could be considered as a followup.
1 parent 62252c9 commit f5ba424

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
390390
CTxDestination dest = PKHash();
391391
LOCK(m_wallet.cs_wallet);
392392
WalletBatch batch{m_wallet.GetDatabase()};
393-
m_wallet.AddDestData(batch, dest, "misc", "val_misc");
393+
m_wallet.SetAddressUsed(batch, dest, true);
394394
m_wallet.SetAddressReceiveRequest(batch, dest, "0", "val_rr0");
395395
m_wallet.SetAddressReceiveRequest(batch, dest, "1", "val_rr1");
396396

src/wallet/wallet.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -813,12 +813,11 @@ void CWallet::SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned
813813
CTxDestination dst;
814814
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
815815
if (IsMine(dst)) {
816-
if (used && !GetDestData(dst, "used", nullptr)) {
817-
if (AddDestData(batch, dst, "used", "p")) { // p for "present", opposite of absent (null)
816+
if (used != IsAddressUsed(dst)) {
817+
if (used) {
818818
tx_destinations.insert(dst);
819819
}
820-
} else if (!used && GetDestData(dst, "used", nullptr)) {
821-
EraseDestData(batch, dst, "used");
820+
SetAddressUsed(batch, dst, used);
822821
}
823822
}
824823
}
@@ -834,23 +833,23 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
834833
if (!ExtractDestination(srctx->tx->vout[n].scriptPubKey, dest)) {
835834
return false;
836835
}
837-
if (GetDestData(dest, "used", nullptr)) {
836+
if (IsAddressUsed(dest)) {
838837
return true;
839838
}
840839
if (IsLegacy()) {
841840
LegacyScriptPubKeyMan* spk_man = GetLegacyScriptPubKeyMan();
842841
assert(spk_man != nullptr);
843842
for (const auto& keyid : GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
844843
WitnessV0KeyHash wpkh_dest(keyid);
845-
if (GetDestData(wpkh_dest, "used", nullptr)) {
844+
if (IsAddressUsed(wpkh_dest)) {
846845
return true;
847846
}
848847
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
849-
if (GetDestData(sh_wpkh_dest, "used", nullptr)) {
848+
if (IsAddressUsed(sh_wpkh_dest)) {
850849
return true;
851850
}
852851
PKHash pkh_dest(keyid);
853-
if (GetDestData(pkh_dest, "used", nullptr)) {
852+
if (IsAddressUsed(pkh_dest)) {
854853
return true;
855854
}
856855
}
@@ -3761,37 +3760,36 @@ unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
37613760
return nTimeSmart;
37623761
}
37633762

3764-
bool CWallet::AddDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key, const std::string &value)
3763+
bool CWallet::SetAddressUsed(WalletBatch& batch, const CTxDestination& dest, bool used)
37653764
{
3765+
const std::string key{"used"};
37663766
if (std::get_if<CNoDestination>(&dest))
37673767
return false;
37683768

3769+
if (!used) {
3770+
if (auto* data = util::FindKey(m_address_book, dest)) data->destdata.erase(key);
3771+
return batch.EraseDestData(EncodeDestination(dest), key);
3772+
}
3773+
3774+
const std::string value{"1"};
37693775
m_address_book[dest].destdata.insert(std::make_pair(key, value));
37703776
return batch.WriteDestData(EncodeDestination(dest), key, value);
37713777
}
37723778

3773-
bool CWallet::EraseDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key)
3774-
{
3775-
if (!m_address_book[dest].destdata.erase(key))
3776-
return false;
3777-
return batch.EraseDestData(EncodeDestination(dest), key);
3778-
}
3779-
37803779
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
37813780
{
37823781
m_address_book[dest].destdata.insert(std::make_pair(key, value));
37833782
}
37843783

3785-
bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
3784+
bool CWallet::IsAddressUsed(const CTxDestination& dest) const
37863785
{
3786+
const std::string key{"used"};
37873787
std::map<CTxDestination, CAddressBookData>::const_iterator i = m_address_book.find(dest);
37883788
if(i != m_address_book.end())
37893789
{
37903790
CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
37913791
if(j != i->second.destdata.end())
37923792
{
3793-
if(value)
3794-
*value = j->second;
37953793
return true;
37963794
}
37973795
}

src/wallet/wallet.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -865,17 +865,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
865865

866866
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; return true; }
867867

868-
/**
869-
* Adds a destination data tuple to the store, and saves it to disk
870-
* When adding new fields, take care to consider how DelAddressBook should handle it!
871-
*/
872-
bool AddDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
873-
//! Erases a destination data tuple in the store and on disk
874-
bool EraseDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
875868
//! Adds a destination data tuple to the store, without saving it to disk
876869
void LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
877-
//! Look up a destination data tuple in the store, return true if found false otherwise
878-
bool GetDestData(const CTxDestination& dest, const std::string& key, std::string* value) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
879870

880871
//! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
881872
int64_t nRelockTime GUARDED_BY(cs_wallet){0};
@@ -1082,6 +1073,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10821073

10831074
bool DelAddressBook(const CTxDestination& address);
10841075

1076+
bool IsAddressUsed(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1077+
bool SetAddressUsed(WalletBatch& batch, const CTxDestination& dest, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1078+
10851079
std::vector<std::string> GetAddressReceiveRequests() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10861080
bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10871081

0 commit comments

Comments
 (0)