Skip to content

Commit f30063b

Browse files
committed
wallet: Track whether a locked coin is persisted
1 parent f17945b commit f30063b

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
@@ -2611,10 +2611,16 @@ util::Result<void> CWallet::DisplayAddress(const CTxDestination& dest)
26112611
return util::Error{_("There is no ScriptPubKeyManager for this address")};
26122612
}
26132613

2614+
void CWallet::LoadLockedCoin(const COutPoint& coin, bool persistent)
2615+
{
2616+
AssertLockHeld(cs_wallet);
2617+
m_locked_coins.emplace(coin, persistent);
2618+
}
2619+
26142620
bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
26152621
{
26162622
AssertLockHeld(cs_wallet);
2617-
setLockedCoins.insert(output);
2623+
LoadLockedCoin(output, batch != nullptr);
26182624
if (batch) {
26192625
return batch->WriteLockedUTXO(output);
26202626
}
@@ -2624,7 +2630,7 @@ bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
26242630
bool CWallet::UnlockCoin(const COutPoint& output, WalletBatch* batch)
26252631
{
26262632
AssertLockHeld(cs_wallet);
2627-
bool was_locked = setLockedCoins.erase(output);
2633+
bool was_locked = m_locked_coins.erase(output);
26282634
if (batch && was_locked) {
26292635
return batch->EraseLockedUTXO(output);
26302636
}
@@ -2636,26 +2642,24 @@ bool CWallet::UnlockAllCoins()
26362642
AssertLockHeld(cs_wallet);
26372643
bool success = true;
26382644
WalletBatch batch(GetDatabase());
2639-
for (auto it = setLockedCoins.begin(); it != setLockedCoins.end(); ++it) {
2640-
success &= batch.EraseLockedUTXO(*it);
2645+
for (const auto& [coin, _] : m_locked_coins) {
2646+
success &= batch.EraseLockedUTXO(coin);
26412647
}
2642-
setLockedCoins.clear();
2648+
m_locked_coins.clear();
26432649
return success;
26442650
}
26452651

26462652
bool CWallet::IsLockedCoin(const COutPoint& output) const
26472653
{
26482654
AssertLockHeld(cs_wallet);
2649-
return setLockedCoins.count(output) > 0;
2655+
return m_locked_coins.count(output) > 0;
26502656
}
26512657

26522658
void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) const
26532659
{
26542660
AssertLockHeld(cs_wallet);
2655-
for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
2656-
it != setLockedCoins.end(); it++) {
2657-
COutPoint outpt = (*it);
2658-
vOutpts.push_back(outpt);
2661+
for (const auto& [coin, _] : m_locked_coins) {
2662+
vOutpts.push_back(coin);
26592663
}
26602664
}
26612665

src/wallet/wallet.h

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

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

545547
bool IsLockedCoin(const COutPoint& output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
548+
void LoadLockedCoin(const COutPoint& coin, bool persistent) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
546549
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
547550
bool UnlockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
548551
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
@@ -1070,7 +1070,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto
10701070
uint32_t n;
10711071
key >> hash;
10721072
key >> n;
1073-
pwallet->LockCoin(COutPoint(hash, n));
1073+
pwallet->LoadLockedCoin(COutPoint(hash, n), /*persistent=*/true);
10741074
return DBErrors::LOAD_OK;
10751075
});
10761076
result = std::max(result, locked_utxo_res.m_result);

0 commit comments

Comments
 (0)