Skip to content

Commit 0ad316a

Browse files
committed
wallet: Track whether a locked coin is persisted
1 parent 218db39 commit 0ad316a

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,10 +2608,16 @@ util::Result<void> CWallet::DisplayAddress(const CTxDestination& dest)
26082608
return util::Error{_("There is no ScriptPubKeyManager for this address")};
26092609
}
26102610

2611+
void CWallet::LoadLockedCoin(const COutPoint& coin, bool persistent)
2612+
{
2613+
AssertLockHeld(cs_wallet);
2614+
m_locked_coins.emplace(coin, persistent);
2615+
}
2616+
26112617
bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
26122618
{
26132619
AssertLockHeld(cs_wallet);
2614-
setLockedCoins.insert(output);
2620+
LoadLockedCoin(output, batch != nullptr);
26152621
if (batch) {
26162622
return batch->WriteLockedUTXO(output);
26172623
}
@@ -2621,7 +2627,7 @@ bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
26212627
bool CWallet::UnlockCoin(const COutPoint& output, WalletBatch* batch)
26222628
{
26232629
AssertLockHeld(cs_wallet);
2624-
bool was_locked = setLockedCoins.erase(output);
2630+
bool was_locked = m_locked_coins.erase(output);
26252631
if (batch && was_locked) {
26262632
return batch->EraseLockedUTXO(output);
26272633
}
@@ -2633,26 +2639,24 @@ bool CWallet::UnlockAllCoins()
26332639
AssertLockHeld(cs_wallet);
26342640
bool success = true;
26352641
WalletBatch batch(GetDatabase());
2636-
for (auto it = setLockedCoins.begin(); it != setLockedCoins.end(); ++it) {
2637-
success &= batch.EraseLockedUTXO(*it);
2642+
for (const auto& [coin, _] : m_locked_coins) {
2643+
success &= batch.EraseLockedUTXO(coin);
26382644
}
2639-
setLockedCoins.clear();
2645+
m_locked_coins.clear();
26402646
return success;
26412647
}
26422648

26432649
bool CWallet::IsLockedCoin(const COutPoint& output) const
26442650
{
26452651
AssertLockHeld(cs_wallet);
2646-
return setLockedCoins.count(output) > 0;
2652+
return m_locked_coins.count(output) > 0;
26472653
}
26482654

26492655
void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) const
26502656
{
26512657
AssertLockHeld(cs_wallet);
2652-
for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
2653-
it != setLockedCoins.end(); it++) {
2654-
COutPoint outpt = (*it);
2655-
vOutpts.push_back(outpt);
2658+
for (const auto& [coin, _] : m_locked_coins) {
2659+
vOutpts.push_back(coin);
26562660
}
26572661
}
26582662

src/wallet/wallet.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
495495
/** Set of Coins owned by this wallet that we won't try to spend from. A
496496
* Coin may be locked if it has already been used to fund a transaction
497497
* that hasn't confirmed yet. We wouldn't consider the Coin spent already,
498-
* but also shouldn't try to use it again. */
499-
std::set<COutPoint> setLockedCoins GUARDED_BY(cs_wallet);
498+
* but also shouldn't try to use it again.
499+
* bool to track whether this locked coin is persisted to disk.
500+
*/
501+
std::map<COutPoint, bool> m_locked_coins GUARDED_BY(cs_wallet);
500502

501503
/** Registered interfaces::Chain::Notifications handler. */
502504
std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
@@ -544,6 +546,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
544546
util::Result<void> DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
545547

546548
bool IsLockedCoin(const COutPoint& output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
549+
void LoadLockedCoin(const COutPoint& coin, bool persistent) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
547550
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
548551
bool UnlockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
549552
bool UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

src/wallet/walletdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto
10711071
uint32_t n;
10721072
key >> hash;
10731073
key >> n;
1074-
pwallet->LockCoin(COutPoint(hash, n));
1074+
pwallet->LoadLockedCoin(COutPoint(hash, n), /*persistent=*/true);
10751075
return DBErrors::LOAD_OK;
10761076
});
10771077
result = std::max(result, locked_utxo_res.m_result);

0 commit comments

Comments
 (0)