Skip to content

Commit 186c81d

Browse files
UdjinM6PastaPastaPasta
authored andcommitted
refactor: add CacheTipHeight, improve cache API safety
Add CacheTipHeight() helper to eliminate code duplication when updating tip height cache. Refactor cache methods to accept CBlockIndex* instead of hash/height pairs for type safety. Handle nullptr cases properly.
1 parent a431512 commit 186c81d

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

src/instantsend/instantsend.cpp

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

441434
void 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

747736
std::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+
767767
int 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

787782
bool CInstantSendManager::IsInstantSendEnabled() const

src/instantsend/instantsend.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
9999
GUARDED_BY(cs_height_cache);
100100
mutable int m_cached_tip_height GUARDED_BY(cs_height_cache){-1};
101101

102-
void CacheBlockHeightInternal(const uint256& hash, int height) const EXCLUSIVE_LOCKS_REQUIRED(cs_height_cache);
102+
void CacheBlockHeightInternal(const CBlockIndex* const block_index) const EXCLUSIVE_LOCKS_REQUIRED(cs_height_cache);
103103

104104
public:
105105
CInstantSendManager() = delete;
@@ -179,8 +179,9 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
179179

180180
size_t GetInstantSendLockCount() const;
181181

182-
void CacheBlockHeight(const uint256& hash, int height) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
182+
void CacheBlockHeight(const CBlockIndex* const block_index) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
183183
std::optional<int> GetBlockHeight(const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
184+
void CacheTipHeight(const CBlockIndex* const tip) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
184185
int GetTipHeight() const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
185186

186187
bool IsInstantSendEnabled() const override;

0 commit comments

Comments
 (0)