mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
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.
This commit is contained in:
parent
62252c95e5
commit
f5ba424cd4
3 changed files with 21 additions and 29 deletions
|
@ -390,7 +390,7 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
|
|||
CTxDestination dest = PKHash();
|
||||
LOCK(m_wallet.cs_wallet);
|
||||
WalletBatch batch{m_wallet.GetDatabase()};
|
||||
m_wallet.AddDestData(batch, dest, "misc", "val_misc");
|
||||
m_wallet.SetAddressUsed(batch, dest, true);
|
||||
m_wallet.SetAddressReceiveRequest(batch, dest, "0", "val_rr0");
|
||||
m_wallet.SetAddressReceiveRequest(batch, dest, "1", "val_rr1");
|
||||
|
||||
|
|
|
@ -813,12 +813,11 @@ void CWallet::SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned
|
|||
CTxDestination dst;
|
||||
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
|
||||
if (IsMine(dst)) {
|
||||
if (used && !GetDestData(dst, "used", nullptr)) {
|
||||
if (AddDestData(batch, dst, "used", "p")) { // p for "present", opposite of absent (null)
|
||||
if (used != IsAddressUsed(dst)) {
|
||||
if (used) {
|
||||
tx_destinations.insert(dst);
|
||||
}
|
||||
} else if (!used && GetDestData(dst, "used", nullptr)) {
|
||||
EraseDestData(batch, dst, "used");
|
||||
SetAddressUsed(batch, dst, used);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -834,7 +833,7 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
|
|||
if (!ExtractDestination(srctx->tx->vout[n].scriptPubKey, dest)) {
|
||||
return false;
|
||||
}
|
||||
if (GetDestData(dest, "used", nullptr)) {
|
||||
if (IsAddressUsed(dest)) {
|
||||
return true;
|
||||
}
|
||||
if (IsLegacy()) {
|
||||
|
@ -842,15 +841,15 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
|
|||
assert(spk_man != nullptr);
|
||||
for (const auto& keyid : GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
|
||||
WitnessV0KeyHash wpkh_dest(keyid);
|
||||
if (GetDestData(wpkh_dest, "used", nullptr)) {
|
||||
if (IsAddressUsed(wpkh_dest)) {
|
||||
return true;
|
||||
}
|
||||
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
|
||||
if (GetDestData(sh_wpkh_dest, "used", nullptr)) {
|
||||
if (IsAddressUsed(sh_wpkh_dest)) {
|
||||
return true;
|
||||
}
|
||||
PKHash pkh_dest(keyid);
|
||||
if (GetDestData(pkh_dest, "used", nullptr)) {
|
||||
if (IsAddressUsed(pkh_dest)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -3761,20 +3760,20 @@ unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
|
|||
return nTimeSmart;
|
||||
}
|
||||
|
||||
bool CWallet::AddDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key, const std::string &value)
|
||||
bool CWallet::SetAddressUsed(WalletBatch& batch, const CTxDestination& dest, bool used)
|
||||
{
|
||||
const std::string key{"used"};
|
||||
if (std::get_if<CNoDestination>(&dest))
|
||||
return false;
|
||||
|
||||
m_address_book[dest].destdata.insert(std::make_pair(key, value));
|
||||
return batch.WriteDestData(EncodeDestination(dest), key, value);
|
||||
if (!used) {
|
||||
if (auto* data = util::FindKey(m_address_book, dest)) data->destdata.erase(key);
|
||||
return batch.EraseDestData(EncodeDestination(dest), key);
|
||||
}
|
||||
|
||||
bool CWallet::EraseDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key)
|
||||
{
|
||||
if (!m_address_book[dest].destdata.erase(key))
|
||||
return false;
|
||||
return batch.EraseDestData(EncodeDestination(dest), key);
|
||||
const std::string value{"1"};
|
||||
m_address_book[dest].destdata.insert(std::make_pair(key, value));
|
||||
return batch.WriteDestData(EncodeDestination(dest), key, value);
|
||||
}
|
||||
|
||||
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
|
||||
|
@ -3782,16 +3781,15 @@ void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, c
|
|||
m_address_book[dest].destdata.insert(std::make_pair(key, value));
|
||||
}
|
||||
|
||||
bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
|
||||
bool CWallet::IsAddressUsed(const CTxDestination& dest) const
|
||||
{
|
||||
const std::string key{"used"};
|
||||
std::map<CTxDestination, CAddressBookData>::const_iterator i = m_address_book.find(dest);
|
||||
if(i != m_address_book.end())
|
||||
{
|
||||
CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
|
||||
if(j != i->second.destdata.end())
|
||||
{
|
||||
if(value)
|
||||
*value = j->second;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -865,17 +865,8 @@ public:
|
|||
|
||||
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; return true; }
|
||||
|
||||
/**
|
||||
* Adds a destination data tuple to the store, and saves it to disk
|
||||
* When adding new fields, take care to consider how DelAddressBook should handle it!
|
||||
*/
|
||||
bool AddDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
//! Erases a destination data tuple in the store and on disk
|
||||
bool EraseDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
//! Adds a destination data tuple to the store, without saving it to disk
|
||||
void LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
//! Look up a destination data tuple in the store, return true if found false otherwise
|
||||
bool GetDestData(const CTxDestination& dest, const std::string& key, std::string* value) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
//! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
|
||||
int64_t nRelockTime GUARDED_BY(cs_wallet){0};
|
||||
|
@ -1082,6 +1073,9 @@ public:
|
|||
|
||||
bool DelAddressBook(const CTxDestination& address);
|
||||
|
||||
bool IsAddressUsed(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
bool SetAddressUsed(WalletBatch& batch, const CTxDestination& dest, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
std::vector<std::string> GetAddressReceiveRequests() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue