Skip to content

Commit 99fac53

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 13727fb commit 99fac53

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

src/instantsend/instantsend.cpp

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ MessageProcessingResult CInstantSendManager::ProcessMessage(NodeId from, std::st
136136
ret.m_error = MisbehavingError{1};
137137
return ret;
138138
}
139-
CacheBlockHeight(blockIndex->GetBlockHash(), blockIndex->nHeight);
139+
CacheBlockHeight(blockIndex);
140140
cycleHeightOpt = blockIndex->nHeight;
141141
}
142142

@@ -393,7 +393,7 @@ MessageProcessingResult CInstantSendManager::ProcessInstantSendLock(NodeId from,
393393
if (!minedHeight.has_value()) {
394394
const CBlockIndex* pindexMined = WITH_LOCK(::cs_main, return m_chainstate.m_blockman.LookupBlockIndex(hashBlock));
395395
if (pindexMined != nullptr) {
396-
CacheBlockHeight(pindexMined->GetBlockHash(), pindexMined->nHeight);
396+
CacheBlockHeight(pindexMined);
397397
minedHeight = pindexMined->nHeight;
398398
}
399399
}
@@ -506,11 +506,7 @@ void CInstantSendManager::BlockConnected(const std::shared_ptr<const CBlock>& pb
506506
return;
507507
}
508508

509-
{
510-
LOCK(cs_height_cache);
511-
CacheBlockHeightInternal(pindex->GetBlockHash(), pindex->nHeight);
512-
m_cached_tip_height = pindex->nHeight;
513-
}
509+
CacheTipHeight(pindex);
514510

515511
if (m_mn_sync.IsBlockchainSynced()) {
516512
const bool has_chainlock = clhandler.HasChainLock(pindex->nHeight, pindex->GetBlockHash());
@@ -542,13 +538,10 @@ void CInstantSendManager::BlockDisconnected(const std::shared_ptr<const CBlock>&
542538
{
543539
LOCK(cs_height_cache);
544540
m_cached_block_heights.erase(pindexDisconnected->GetBlockHash());
545-
const CBlockIndex* const new_tip = pindexDisconnected->pprev;
546-
m_cached_tip_height = new_tip ? new_tip->nHeight : -1;
547-
if (new_tip) {
548-
CacheBlockHeightInternal(new_tip->GetBlockHash(), new_tip->nHeight);
549-
}
550541
}
551542

543+
CacheTipHeight(pindexDisconnected->pprev);
544+
552545
db.RemoveBlockInstantSendLocks(pblock, pindexDisconnected);
553546
}
554547

@@ -670,11 +663,7 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock)
670663

671664
void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew)
672665
{
673-
{
674-
LOCK(cs_height_cache);
675-
CacheBlockHeightInternal(pindexNew->GetBlockHash(), pindexNew->nHeight);
676-
m_cached_tip_height = pindexNew->nHeight;
677-
}
666+
CacheTipHeight(pindexNew);
678667

679668
bool fDIP0008Active = pindexNew->pprev && pindexNew->pprev->nHeight >= Params().GetConsensus().DIP0008Height;
680669

@@ -962,16 +951,16 @@ size_t CInstantSendManager::GetInstantSendLockCount() const
962951
return db.GetInstantSendLockCount();
963952
}
964953

965-
void CInstantSendManager::CacheBlockHeightInternal(const uint256& hash, int height) const
954+
void CInstantSendManager::CacheBlockHeightInternal(const CBlockIndex* const block_index) const
966955
{
967956
AssertLockHeld(cs_height_cache);
968-
m_cached_block_heights.insert(hash, height);
957+
m_cached_block_heights.insert(block_index->GetBlockHash(), block_index->nHeight);
969958
}
970959

971-
void CInstantSendManager::CacheBlockHeight(const uint256& hash, int height) const
960+
void CInstantSendManager::CacheBlockHeight(const CBlockIndex* const block_index) const
972961
{
973962
LOCK(cs_height_cache);
974-
CacheBlockHeightInternal(hash, height);
963+
CacheBlockHeightInternal(block_index);
975964
}
976965

977966
std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) const
@@ -990,10 +979,21 @@ std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) cons
990979
return std::nullopt;
991980
}
992981

993-
CacheBlockHeight(pindex->GetBlockHash(), pindex->nHeight);
982+
CacheBlockHeight(pindex);
994983
return pindex->nHeight;
995984
}
996985

986+
void CInstantSendManager::CacheTipHeight(const CBlockIndex* const tip) const
987+
{
988+
LOCK(cs_height_cache);
989+
if (tip) {
990+
CacheBlockHeightInternal(tip);
991+
m_cached_tip_height = tip->nHeight;
992+
} else {
993+
m_cached_tip_height = -1;
994+
}
995+
}
996+
997997
int CInstantSendManager::GetTipHeight() const
998998
{
999999
{
@@ -1004,14 +1004,9 @@ int CInstantSendManager::GetTipHeight() const
10041004
}
10051005

10061006
const CBlockIndex* tip = WITH_LOCK(::cs_main, return m_chainstate.m_chain.Tip());
1007-
assert(tip != nullptr);
10081007

1009-
{
1010-
LOCK(cs_height_cache);
1011-
CacheBlockHeightInternal(tip->GetBlockHash(), tip->nHeight);
1012-
m_cached_tip_height = tip->nHeight;
1013-
return m_cached_tip_height;
1014-
}
1008+
CacheTipHeight(tip);
1009+
return tip ? tip->nHeight : -1;
10151010
}
10161011

10171012
void CInstantSendManager::WorkThreadMain(PeerManager& peerman)

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;
@@ -186,8 +186,9 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
186186

187187
size_t GetInstantSendLockCount() const;
188188

189-
void CacheBlockHeight(const uint256& hash, int height) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
189+
void CacheBlockHeight(const CBlockIndex* const block_index) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
190190
std::optional<int> GetBlockHeight(const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
191+
void CacheTipHeight(const CBlockIndex* const tip) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
191192
int GetTipHeight() const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
192193

193194
bool IsInstantSendEnabled() const override;

0 commit comments

Comments
 (0)