mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-18 16:42:54 +01: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:
@@ -390,7 +390,7 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
|
|||||||
CTxDestination dest = PKHash();
|
CTxDestination dest = PKHash();
|
||||||
LOCK(m_wallet.cs_wallet);
|
LOCK(m_wallet.cs_wallet);
|
||||||
WalletBatch batch{m_wallet.GetDatabase()};
|
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, "0", "val_rr0");
|
||||||
m_wallet.SetAddressReceiveRequest(batch, dest, "1", "val_rr1");
|
m_wallet.SetAddressReceiveRequest(batch, dest, "1", "val_rr1");
|
||||||
|
|
||||||
|
|||||||
@@ -813,12 +813,11 @@ void CWallet::SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned
|
|||||||
CTxDestination dst;
|
CTxDestination dst;
|
||||||
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
|
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
|
||||||
if (IsMine(dst)) {
|
if (IsMine(dst)) {
|
||||||
if (used && !GetDestData(dst, "used", nullptr)) {
|
if (used != IsAddressUsed(dst)) {
|
||||||
if (AddDestData(batch, dst, "used", "p")) { // p for "present", opposite of absent (null)
|
if (used) {
|
||||||
tx_destinations.insert(dst);
|
tx_destinations.insert(dst);
|
||||||
}
|
}
|
||||||
} else if (!used && GetDestData(dst, "used", nullptr)) {
|
SetAddressUsed(batch, dst, used);
|
||||||
EraseDestData(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)) {
|
if (!ExtractDestination(srctx->tx->vout[n].scriptPubKey, dest)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (GetDestData(dest, "used", nullptr)) {
|
if (IsAddressUsed(dest)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (IsLegacy()) {
|
if (IsLegacy()) {
|
||||||
@@ -842,15 +841,15 @@ bool CWallet::IsSpentKey(const uint256& hash, unsigned int n) const
|
|||||||
assert(spk_man != nullptr);
|
assert(spk_man != nullptr);
|
||||||
for (const auto& keyid : GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
|
for (const auto& keyid : GetAffectedKeys(srctx->tx->vout[n].scriptPubKey, *spk_man)) {
|
||||||
WitnessV0KeyHash wpkh_dest(keyid);
|
WitnessV0KeyHash wpkh_dest(keyid);
|
||||||
if (GetDestData(wpkh_dest, "used", nullptr)) {
|
if (IsAddressUsed(wpkh_dest)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
|
ScriptHash sh_wpkh_dest(GetScriptForDestination(wpkh_dest));
|
||||||
if (GetDestData(sh_wpkh_dest, "used", nullptr)) {
|
if (IsAddressUsed(sh_wpkh_dest)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
PKHash pkh_dest(keyid);
|
PKHash pkh_dest(keyid);
|
||||||
if (GetDestData(pkh_dest, "used", nullptr)) {
|
if (IsAddressUsed(pkh_dest)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3761,37 +3760,36 @@ unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
|
|||||||
return nTimeSmart;
|
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))
|
if (std::get_if<CNoDestination>(&dest))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (!used) {
|
||||||
|
if (auto* data = util::FindKey(m_address_book, dest)) data->destdata.erase(key);
|
||||||
|
return batch.EraseDestData(EncodeDestination(dest), key);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string value{"1"};
|
||||||
m_address_book[dest].destdata.insert(std::make_pair(key, value));
|
m_address_book[dest].destdata.insert(std::make_pair(key, value));
|
||||||
return batch.WriteDestData(EncodeDestination(dest), key, value);
|
return batch.WriteDestData(EncodeDestination(dest), key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
|
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
|
||||||
{
|
{
|
||||||
m_address_book[dest].destdata.insert(std::make_pair(key, value));
|
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);
|
std::map<CTxDestination, CAddressBookData>::const_iterator i = m_address_book.find(dest);
|
||||||
if(i != m_address_book.end())
|
if(i != m_address_book.end())
|
||||||
{
|
{
|
||||||
CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
|
CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
|
||||||
if(j != i->second.destdata.end())
|
if(j != i->second.destdata.end())
|
||||||
{
|
{
|
||||||
if(value)
|
|
||||||
*value = j->second;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -865,17 +865,8 @@ public:
|
|||||||
|
|
||||||
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; return true; }
|
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
|
//! 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);
|
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().
|
//! 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};
|
int64_t nRelockTime GUARDED_BY(cs_wallet){0};
|
||||||
@@ -1082,6 +1073,9 @@ public:
|
|||||||
|
|
||||||
bool DelAddressBook(const CTxDestination& address);
|
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);
|
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);
|
bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user