wallet: Write best block record on unload

This commit is contained in:
Ava Chow 2025-03-05 17:50:52 -08:00
parent 02a4a97bdd
commit 59e880eff4
2 changed files with 12 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();
@ -4708,4 +4709,13 @@ std::optional<CKey> CWallet::GetKey(const CKeyID& keyid) const
} }
return std::nullopt; return std::nullopt;
} }
void CWallet::WriteBestBlock() const
{
LOCK(cs_wallet);
if (!m_best_block.IsNull()) {
WalletBatch batch(GetDatabase());
batch.WriteBestBlock(m_best_block);
}
}
} // namespace wallet } // namespace wallet

View file

@ -1027,6 +1027,8 @@ public:
/** Set last block processed height, and write to database */ /** Set last block processed height, and write to database */
void SetBestBlock(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); void SetBestBlock(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
void LoadBestBlock(const BestBlock& best_block) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet); void LoadBestBlock(const BestBlock& best_block) 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();