@@ -152,7 +152,7 @@ std::variant<uint256, CTransactionRef, std::monostate> CInstantSendManager::Proc
152152 if (!minedHeight.has_value ()) {
153153 const CBlockIndex* pindexMined = WITH_LOCK (::cs_main, return m_chainstate.m_blockman .LookupBlockIndex (hashBlock));
154154 if (pindexMined != nullptr ) {
155- CacheBlockHeight (pindexMined-> GetBlockHash (), pindexMined-> nHeight );
155+ CacheBlockHeight (pindexMined);
156156 minedHeight = pindexMined->nHeight ;
157157 }
158158 }
@@ -258,11 +258,7 @@ void CInstantSendManager::BlockConnected(const std::shared_ptr<const CBlock>& pb
258258 return ;
259259 }
260260
261- {
262- LOCK (cs_height_cache);
263- CacheBlockHeightInternal (pindex->GetBlockHash (), pindex->nHeight );
264- m_cached_tip_height = pindex->nHeight ;
265- }
261+ CacheTipHeight (pindex);
266262
267263 if (m_mn_sync.IsBlockchainSynced ()) {
268264 const bool has_chainlock = clhandler.HasChainLock (pindex->nHeight , pindex->GetBlockHash ());
@@ -294,13 +290,10 @@ void CInstantSendManager::BlockDisconnected(const std::shared_ptr<const CBlock>&
294290 {
295291 LOCK (cs_height_cache);
296292 m_cached_block_heights.erase (pindexDisconnected->GetBlockHash ());
297- const CBlockIndex* const new_tip = pindexDisconnected->pprev ;
298- m_cached_tip_height = new_tip ? new_tip->nHeight : -1 ;
299- if (new_tip) {
300- CacheBlockHeightInternal (new_tip->GetBlockHash (), new_tip->nHeight );
301- }
302293 }
303294
295+ CacheTipHeight (pindexDisconnected->pprev );
296+
304297 db.RemoveBlockInstantSendLocks (pblock, pindexDisconnected);
305298}
306299
@@ -440,11 +433,7 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock)
440433
441434void CInstantSendManager::UpdatedBlockTip (const CBlockIndex* pindexNew)
442435{
443- {
444- LOCK (cs_height_cache);
445- CacheBlockHeightInternal (pindexNew->GetBlockHash (), pindexNew->nHeight );
446- m_cached_tip_height = pindexNew->nHeight ;
447- }
436+ CacheTipHeight (pindexNew);
448437
449438 bool fDIP0008Active = pindexNew->pprev && pindexNew->pprev ->nHeight >= Params ().GetConsensus ().DIP0008Height ;
450439
@@ -732,16 +721,16 @@ size_t CInstantSendManager::GetInstantSendLockCount() const
732721 return db.GetInstantSendLockCount ();
733722}
734723
735- void CInstantSendManager::CacheBlockHeightInternal (const uint256& hash, int height ) const
724+ void CInstantSendManager::CacheBlockHeightInternal (const CBlockIndex* const block_index ) const
736725{
737726 AssertLockHeld (cs_height_cache);
738- m_cached_block_heights.insert (hash, height );
727+ m_cached_block_heights.insert (block_index-> GetBlockHash (), block_index-> nHeight );
739728}
740729
741- void CInstantSendManager::CacheBlockHeight (const uint256& hash, int height ) const
730+ void CInstantSendManager::CacheBlockHeight (const CBlockIndex* const block_index ) const
742731{
743732 LOCK (cs_height_cache);
744- CacheBlockHeightInternal (hash, height );
733+ CacheBlockHeightInternal (block_index );
745734}
746735
747736std::optional<int > CInstantSendManager::GetBlockHeight (const uint256& hash) const
@@ -760,10 +749,21 @@ std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) cons
760749 return std::nullopt ;
761750 }
762751
763- CacheBlockHeight (pindex-> GetBlockHash (), pindex-> nHeight );
752+ CacheBlockHeight (pindex);
764753 return pindex->nHeight ;
765754}
766755
756+ void CInstantSendManager::CacheTipHeight (const CBlockIndex* const tip) const
757+ {
758+ LOCK (cs_height_cache);
759+ if (tip) {
760+ CacheBlockHeightInternal (tip);
761+ m_cached_tip_height = tip->nHeight ;
762+ } else {
763+ m_cached_tip_height = -1 ;
764+ }
765+ }
766+
767767int CInstantSendManager::GetTipHeight () const
768768{
769769 {
@@ -774,14 +774,9 @@ int CInstantSendManager::GetTipHeight() const
774774 }
775775
776776 const CBlockIndex* tip = WITH_LOCK (::cs_main, return m_chainstate.m_chain .Tip ());
777- assert (tip != nullptr );
778777
779- {
780- LOCK (cs_height_cache);
781- CacheBlockHeightInternal (tip->GetBlockHash (), tip->nHeight );
782- m_cached_tip_height = tip->nHeight ;
783- return m_cached_tip_height;
784- }
778+ CacheTipHeight (tip);
779+ return tip ? tip->nHeight : -1 ;
785780}
786781
787782bool CInstantSendManager::IsInstantSendEnabled () const
0 commit comments