Skip to content

Commit 4a3b6f4

Browse files
committed
Merge #17354: wallet: Tidy CWallet::SetUsedDestinationState
0b75a7f wallet: Reuse existing batch in CWallet::SetUsedDestinationState (João Barbosa) 01f45dd wallet: Avoid recursive lock in CWallet::SetUsedDestinationState (João Barbosa) Pull request description: This PR makes 2 distinct changes around `CWallet::SetUsedDestinationState`: - 1st the recursive lock is removed and now it requires the lock to be held; - 2nd change is to support, in the best case, just a wallet database flush when transaction is added to the wallet. ACKs for top commit: achow101: ACK 0b75a7f MarcoFalke: ACK 0b75a7f ryanofsky: Code review ACK 0b75a7f. Code changes looks fine but PR description should be updated to say what benefits of the change are. I might have missed something, but I didn't see a place where multiple batches were used previously and a single batch was used now. So the main benefit of this change appears to be removing a recursive lock? And maybe moving toward a consistent convention for passing batch instances? Tree-SHA512: abcf23a5850d29990668db20d6f624cca3e89629cc9ed003e0d05cde1b58ab2ff365034f156684ad13e55764b54c6c0c2bc7d5f96b8af7dc5e45a3be955d6b15
2 parents 99ab3a7 + 0b75a7f commit 4a3b6f4

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

src/interfaces/wallet.cpp

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

src/wallet/test/wallet_tests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,10 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
357357
{
358358
CTxDestination dest = PKHash();
359359
LOCK(m_wallet.cs_wallet);
360-
m_wallet.AddDestData(dest, "misc", "val_misc");
361-
m_wallet.AddDestData(dest, "rr0", "val_rr0");
362-
m_wallet.AddDestData(dest, "rr1", "val_rr1");
360+
WalletBatch batch{m_wallet.GetDatabase()};
361+
m_wallet.AddDestData(batch, dest, "misc", "val_misc");
362+
m_wallet.AddDestData(batch, dest, "rr0", "val_rr0");
363+
m_wallet.AddDestData(batch, dest, "rr1", "val_rr1");
363364

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

src/wallet/wallet.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -701,19 +701,19 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
701701
return success;
702702
}
703703

704-
void CWallet::SetUsedDestinationState(const uint256& hash, unsigned int n, bool used)
704+
void CWallet::SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used)
705705
{
706+
AssertLockHeld(cs_wallet);
706707
const CWalletTx* srctx = GetWalletTx(hash);
707708
if (!srctx) return;
708709

709710
CTxDestination dst;
710711
if (ExtractDestination(srctx->tx->vout[n].scriptPubKey, dst)) {
711712
if (IsMine(dst)) {
712-
LOCK(cs_wallet);
713713
if (used && !GetDestData(dst, "used", nullptr)) {
714-
AddDestData(dst, "used", "p"); // p for "present", opposite of absent (null)
714+
AddDestData(batch, dst, "used", "p"); // p for "present", opposite of absent (null)
715715
} else if (!used && GetDestData(dst, "used", nullptr)) {
716-
EraseDestData(dst, "used");
716+
EraseDestData(batch, dst, "used");
717717
}
718718
}
719719
}
@@ -744,7 +744,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
744744
// Mark used destinations
745745
for (const CTxIn& txin : wtxIn.tx->vin) {
746746
const COutPoint& op = txin.prevout;
747-
SetUsedDestinationState(op.hash, op.n, true);
747+
SetUsedDestinationState(batch, op.hash, op.n, true);
748748
}
749749
}
750750

@@ -3460,20 +3460,20 @@ unsigned int CWallet::ComputeTimeSmart(const CWalletTx& wtx) const
34603460
return nTimeSmart;
34613461
}
34623462

3463-
bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
3463+
bool CWallet::AddDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key, const std::string &value)
34643464
{
34653465
if (boost::get<CNoDestination>(&dest))
34663466
return false;
34673467

34683468
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
3469-
return WalletBatch(*database).WriteDestData(EncodeDestination(dest), key, value);
3469+
return batch.WriteDestData(EncodeDestination(dest), key, value);
34703470
}
34713471

3472-
bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
3472+
bool CWallet::EraseDestData(WalletBatch& batch, const CTxDestination &dest, const std::string &key)
34733473
{
34743474
if (!mapAddressBook[dest].destdata.erase(key))
34753475
return false;
3476-
return WalletBatch(*database).EraseDestData(EncodeDestination(dest), key);
3476+
return batch.EraseDestData(EncodeDestination(dest), key);
34773477
}
34783478

34793479
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
@@ -813,7 +813,7 @@ class CWallet final : public WalletStorage, private interfaces::Chain::Notificat
813813
// Whether this or any UTXO with the same CTxDestination has been spent.
814814
bool IsUsedDestination(const CTxDestination& dst) const;
815815
bool IsUsedDestination(const uint256& hash, unsigned int n) const;
816-
void SetUsedDestinationState(const uint256& hash, unsigned int n, bool used);
816+
void SetUsedDestinationState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
817817

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

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

840840
//! Adds a destination data tuple to the store, and saves it to disk
841-
bool AddDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
841+
bool AddDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
842842
//! Erases a destination data tuple in the store and on disk
843-
bool EraseDestData(const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
843+
bool EraseDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
844844
//! Adds a destination data tuple to the store, without saving it to disk
845845
void LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
846846
//! Look up a destination data tuple in the store, return true if found false otherwise

0 commit comments

Comments
 (0)