Skip to content

Commit 0b75a7f

Browse files
committed
wallet: Reuse existing batch in CWallet::SetUsedDestinationState
1 parent 01f45dd commit 0b75a7f

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

src/interfaces/wallet.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ class WalletImpl : public Wallet
170170
bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
171171
{
172172
LOCK(m_wallet->cs_wallet);
173-
return m_wallet->AddDestData(dest, key, value);
173+
WalletBatch batch{m_wallet->GetDatabase()};
174+
return m_wallet->AddDestData(batch, dest, key, value);
174175
}
175176
bool eraseDestData(const CTxDestination& dest, const std::string& key) override
176177
{
177178
LOCK(m_wallet->cs_wallet);
178-
return m_wallet->EraseDestData(dest, key);
179+
WalletBatch batch{m_wallet->GetDatabase()};
180+
return m_wallet->EraseDestData(batch, dest, key);
179181
}
180182
std::vector<std::string> getDestValues(const std::string& prefix) override
181183
{

src/wallet/test/wallet_tests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
338338
{
339339
CTxDestination dest = PKHash();
340340
LOCK(m_wallet.cs_wallet);
341-
m_wallet.AddDestData(dest, "misc", "val_misc");
342-
m_wallet.AddDestData(dest, "rr0", "val_rr0");
343-
m_wallet.AddDestData(dest, "rr1", "val_rr1");
341+
WalletBatch batch{m_wallet.GetDatabase()};
342+
m_wallet.AddDestData(batch, dest, "misc", "val_misc");
343+
m_wallet.AddDestData(batch, dest, "rr0", "val_rr0");
344+
m_wallet.AddDestData(batch, dest, "rr1", "val_rr1");
344345

345346
auto values = m_wallet.GetDestValues("rr");
346347
BOOST_CHECK_EQUAL(values.size(), 2U);

src/wallet/wallet.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
691691
return success;
692692
}
693693

694-
void CWallet::SetUsedDestinationState(const uint256& hash, unsigned int n, bool used)
694+
void CWallet::SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used)
695695
{
696696
AssertLockHeld(cs_wallet);
697697
const CWalletTx* srctx = GetWalletTx(hash);
@@ -701,9 +701,9 @@ void CWallet::SetUsedDestinationState(const uint256& hash, unsigned int n, bool
701701
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
702702
if (IsMine(dst)) {
703703
if (used && !GetDestData(dst, "used", nullptr)) {
704-
AddDestData(dst, "used", "p"); // p for "present", opposite of absent (null)
704+
AddDestData(batch, dst, "used", "p"); // p for "present", opposite of absent (null)
705705
} else if (!used && GetDestData(dst, "used", nullptr)) {
706-
EraseDestData(dst, "used");
706+
EraseDestData(batch, dst, "used");
707707
}
708708
}
709709
}
@@ -734,7 +734,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
734734
// Mark used destinations
735735
for (const CTxIn& txin : wtxIn.tx->vin) {
736736
const COutPoint& op = txin.prevout;
737-
SetUsedDestinationState(op.hash, op.n, true);
737+
SetUsedDestinationState(batch, op.hash, op.n, true);
738738
}
739739
}
740740

@@ -3410,20 +3410,20 @@ unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
34103410
return nTimeSmart;
34113411
}
34123412

3413-
bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
3413+
bool CWallet::AddDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key, const std::string &value)
34143414
{
34153415
if (boost::get<CNoDestination>(&dest))
34163416
return false;
34173417

34183418
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
3419-
return WalletBatch(*database).WriteDestData(EncodeDestination(dest), key, value);
3419+
return batch.WriteDestData(EncodeDestination(dest), key, value);
34203420
}
34213421

3422-
bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
3422+
bool CWallet::EraseDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key)
34233423
{
34243424
if (!mapAddressBook[dest].destdata.erase(key))
34253425
return false;
3426-
return WalletBatch(*database).EraseDestData(EncodeDestination(dest), key);
3426+
return batch.EraseDestData(EncodeDestination(dest), key);
34273427
}
34283428

34293429
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
795795
// Whether this or any UTXO with the same CTxDestination has been spent.
796796
bool IsUsedDestination(const CTxDestination& dst) const;
797797
bool IsUsedDestination(const uint256& hash, unsigned int n) const;
798-
void SetUsedDestinationState(const uint256& hash, unsigned int n, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
798+
void SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
799799

800800
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool single_coin) const;
801801

@@ -820,9 +820,9 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
820820
bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; }
821821

822822
//! Adds a destination data tuple to the store, and saves it to disk
823-
bool AddDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
823+
bool AddDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
824824
//! Erases a destination data tuple in the store and on disk
825-
bool EraseDestData(const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
825+
bool EraseDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
826826
//! Adds a destination data tuple to the store, without saving it to disk
827827
void LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
828828
//! Look up a destination data tuple in the store, return true if found false otherwise

0 commit comments

Comments
 (0)