Skip to content

Commit 129fdf7

Browse files
committed
wallet: Track whether a locked coin is persisted
1 parent 87ec923 commit 129fdf7

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

2625+
void CWallet::LoadLockedCoin(const COutPoint& coin, bool persistent)
2626+
{
2627+
AssertLockHeld(cs_wallet);
2628+
m_locked_coins.emplace(coin, persistent);
2629+
}
2630+
26252631
bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
26262632
{
26272633
AssertLockHeld(cs_wallet);
2628-
setLockedCoins.insert(output);
2634+
LoadLockedCoin(output, batch != nullptr);
26292635
if (batch) {
26302636
return batch->WriteLockedUTXO(output);
26312637
}
@@ -2635,7 +2641,7 @@ bool CWallet::LockCoin(const COutPoint& output, WalletBatch* batch)
26352641
bool CWallet::UnlockCoin(const COutPoint& output, WalletBatch* batch)
26362642
{
26372643
AssertLockHeld(cs_wallet);
2638-
bool was_locked = setLockedCoins.erase(output);
2644+
bool was_locked = m_locked_coins.erase(output);
26392645
if (batch && was_locked) {
26402646
return batch->EraseLockedUTXO(output);
26412647
}
@@ -2647,26 +2653,24 @@ bool CWallet::UnlockAllCoins()
26472653
AssertLockHeld(cs_wallet);
26482654
bool success = true;
26492655
WalletBatch batch(GetDatabase());
2650-
for (auto it = setLockedCoins.begin(); it != setLockedCoins.end(); ++it) {
2651-
success &= batch.EraseLockedUTXO(*it);
2656+
for (const auto& [coin, _] : m_locked_coins) {
2657+
success &= batch.EraseLockedUTXO(coin);
26522658
}
2653-
setLockedCoins.clear();
2659+
m_locked_coins.clear();
26542660
return success;
26552661
}
26562662

26572663
bool CWallet::IsLockedCoin(const COutPoint& output) const
26582664
{
26592665
AssertLockHeld(cs_wallet);
2660-
return setLockedCoins.count(output) > 0;
2666+
return m_locked_coins.count(output) > 0;
26612667
}
26622668

26632669
void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts) const
26642670
{
26652671
AssertLockHeld(cs_wallet);
2666-
for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
2667-
it != setLockedCoins.end(); it++) {
2668-
COutPoint outpt = (*it);
2669-
vOutpts.push_back(outpt);
2672+
for (const auto& [coin, _] : m_locked_coins) {
2673+
vOutpts.push_back(coin);
26702674
}
26712675
}
26722676

src/wallet/wallet.h

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

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

548550
bool IsLockedCoin(const COutPoint& output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
551+
void LoadLockedCoin(const COutPoint& coin, bool persistent) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
549552
bool LockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
550553
bool UnlockCoin(const COutPoint& output, WalletBatch* batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
551554
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
@@ -1072,7 +1072,7 @@ static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vecto
10721072
uint32_t n;
10731073
key >> hash;
10741074
key >> n;
1075-
pwallet->LockCoin(COutPoint(hash, n));
1075+
pwallet->LoadLockedCoin(COutPoint(hash, n), /*persistent=*/true);
10761076
return DBErrors::LOAD_OK;
10771077
});
10781078
result = std::max(result, locked_utxo_res.m_result);

0 commit comments

Comments
 (0)