wallet: Write best block record on unload

This commit is contained in:
Ava Chow 2025-03-05 17:50:52 -08:00
parent 9113e60f55
commit eceb848494
2 changed files with 16 additions and 0 deletions

View file

@ -164,6 +164,7 @@ bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet
interfaces::Chain& chain = wallet->chain(); interfaces::Chain& chain = wallet->chain();
std::string name = wallet->GetName(); std::string name = wallet->GetName();
wallet->WriteBestBlock();
// Unregister with the validation interface which also drops shared pointers. // Unregister with the validation interface which also drops shared pointers.
wallet->m_chain_notifications_handler.reset(); wallet->m_chain_notifications_handler.reset();
@ -4721,4 +4722,17 @@ std::optional<CKey> CWallet::GetKey(const CKeyID& keyid) const
} }
return std::nullopt; return std::nullopt;
} }
void CWallet::WriteBestBlock() const
{
LOCK(cs_wallet);
if (!m_last_block_processed.IsNull()) {
CBlockLocator loc;
chain().findBlock(m_last_block_processed, FoundBlock().locator(loc));
WalletBatch batch(GetDatabase());
batch.WriteBestBlock(loc);
}
}
} // namespace wallet } // namespace wallet

View file

@ -994,6 +994,8 @@ public:
} }
/** Set last block processed height, and write to database */ /** Set last block processed height, and write to database */
void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
/** Write the current best block to database */
void WriteBestBlock() const;
//! Connect the signals from ScriptPubKeyMans to the signals in CWallet //! Connect the signals from ScriptPubKeyMans to the signals in CWallet
void ConnectScriptPubKeyManNotifiers(); void ConnectScriptPubKeyManNotifiers();