@@ -785,30 +785,25 @@ bool CWallet::IsSpent(const COutPoint& outpoint) const
785785 return false ;
786786}
787787
788- void CWallet::AddToSpends (const COutPoint& outpoint, const Txid& txid, WalletBatch* batch )
788+ void CWallet::AddToSpends (const COutPoint& outpoint, const Txid& txid)
789789{
790790 mapTxSpends.insert (std::make_pair (outpoint, txid));
791791
792- if (batch) {
793- UnlockCoin (outpoint, batch);
794- } else {
795- WalletBatch temp_batch (GetDatabase ());
796- UnlockCoin (outpoint, &temp_batch);
797- }
792+ UnlockCoin (outpoint);
798793
799794 std::pair<TxSpends::iterator, TxSpends::iterator> range;
800795 range = mapTxSpends.equal_range (outpoint);
801796 SyncMetaData (range);
802797}
803798
804799
805- void CWallet::AddToSpends (const CWalletTx& wtx, WalletBatch* batch )
800+ void CWallet::AddToSpends (const CWalletTx& wtx)
806801{
807802 if (wtx.IsCoinBase ()) // Coinbases don't spend anything!
808803 return ;
809804
810805 for (const CTxIn& txin : wtx.tx ->vin )
811- AddToSpends (txin.prevout , wtx.GetHash (), batch );
806+ AddToSpends (txin.prevout , wtx.GetHash ());
812807}
813808
814809bool CWallet::EncryptWallet (const SecureString& strWalletPassphrase)
@@ -1058,7 +1053,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const
10581053 wtx.nOrderPos = IncOrderPosNext (&batch);
10591054 wtx.m_it_wtxOrdered = wtxOrdered.insert (std::make_pair (wtx.nOrderPos , &wtx));
10601055 wtx.nTimeSmart = ComputeTimeSmart (wtx, rescanning_old_block);
1061- AddToSpends (wtx, &batch );
1056+ AddToSpends (wtx);
10621057
10631058 // Update birth time when tx time is older than it.
10641059 MaybeUpdateBirthTime (wtx.GetTxTime ());
@@ -2622,22 +2617,34 @@ util::Result<void> CWallet::DisplayAddress(const CTxDestination& dest)
26222617 return util::Error{_ (" There is no ScriptPubKeyManager for this address" )};
26232618}
26242619
2625- bool CWallet::LockCoin (const COutPoint& output, WalletBatch* batch )
2620+ void CWallet::LoadLockedCoin (const COutPoint& coin, bool persistent )
26262621{
26272622 AssertLockHeld (cs_wallet);
2628- setLockedCoins.insert (output);
2629- if (batch) {
2630- return batch->WriteLockedUTXO (output);
2623+ m_locked_coins.emplace (coin, persistent);
2624+ }
2625+
2626+ bool CWallet::LockCoin (const COutPoint& output, bool persist)
2627+ {
2628+ AssertLockHeld (cs_wallet);
2629+ LoadLockedCoin (output, persist);
2630+ if (persist) {
2631+ WalletBatch batch (GetDatabase ());
2632+ return batch.WriteLockedUTXO (output);
26312633 }
26322634 return true ;
26332635}
26342636
2635- bool CWallet::UnlockCoin (const COutPoint& output, WalletBatch* batch )
2637+ bool CWallet::UnlockCoin (const COutPoint& output)
26362638{
26372639 AssertLockHeld (cs_wallet);
2638- bool was_locked = setLockedCoins.erase (output);
2639- if (batch && was_locked) {
2640- return batch->EraseLockedUTXO (output);
2640+ auto locked_coin_it = m_locked_coins.find (output);
2641+ if (locked_coin_it != m_locked_coins.end ()) {
2642+ bool persisted = locked_coin_it->second ;
2643+ m_locked_coins.erase (locked_coin_it);
2644+ if (persisted) {
2645+ WalletBatch batch (GetDatabase ());
2646+ return batch.EraseLockedUTXO (output);
2647+ }
26412648 }
26422649 return true ;
26432650}
@@ -2647,26 +2654,24 @@ bool CWallet::UnlockAllCoins()
26472654 AssertLockHeld (cs_wallet);
26482655 bool success = true ;
26492656 WalletBatch batch (GetDatabase ());
2650- for (auto it = setLockedCoins. begin (); it != setLockedCoins. end (); ++it ) {
2651- success &= batch.EraseLockedUTXO (*it );
2657+ for (const auto & [coin, persistent] : m_locked_coins ) {
2658+ if (persistent) success = success && batch.EraseLockedUTXO (coin );
26522659 }
2653- setLockedCoins .clear ();
2660+ m_locked_coins .clear ();
26542661 return success;
26552662}
26562663
26572664bool CWallet::IsLockedCoin (const COutPoint& output) const
26582665{
26592666 AssertLockHeld (cs_wallet);
2660- return setLockedCoins .count (output) > 0 ;
2667+ return m_locked_coins .count (output) > 0 ;
26612668}
26622669
26632670void CWallet::ListLockedCoins (std::vector<COutPoint>& vOutpts) const
26642671{
26652672 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);
2673+ for (const auto & [coin, _] : m_locked_coins) {
2674+ vOutpts.push_back (coin);
26702675 }
26712676}
26722677
0 commit comments