Skip to content

Commit ffd84b6

Browse files
committed
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 0733d4f commit ffd84b6

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

@@ -395,7 +395,7 @@ MessageProcessingResult CInstantSendManager::ProcessInstantSendLock(NodeId from,
395395
if (!minedHeight.has_value()) {
396396
const CBlockIndex* pindexMined = WITH_LOCK(::cs_main, return m_chainstate.m_blockman.LookupBlockIndex(hashBlock));
397397
if (pindexMined != nullptr) {
398-
CacheBlockHeight(pindexMined->GetBlockHash(), pindexMined->nHeight);
398+
CacheBlockHeight(pindexMined);
399399
minedHeight = pindexMined->nHeight;
400400
}
401401
}
@@ -508,11 +508,7 @@ void CInstantSendManager::BlockConnected(const std::shared_ptr<const CBlock>& pb
508508
return;
509509
}
510510

511-
{
512-
LOCK(cs_height_cache);
513-
CacheBlockHeightInternal(pindex->GetBlockHash(), pindex->nHeight);
514-
m_cached_tip_height = pindex->nHeight;
515-
}
511+
CacheTipHeight(pindex);
516512

517513
if (m_mn_sync.IsBlockchainSynced()) {
518514
const bool has_chainlock = clhandler.HasChainLock(pindex->nHeight, pindex->GetBlockHash());
@@ -544,13 +540,10 @@ void CInstantSendManager::BlockDisconnected(const std::shared_ptr<const CBlock>&
544540
{
545541
LOCK(cs_height_cache);
546542
m_cached_block_heights.erase(pindexDisconnected->GetBlockHash());
547-
const CBlockIndex* const new_tip = pindexDisconnected->pprev;
548-
m_cached_tip_height = new_tip ? new_tip->nHeight : -1;
549-
if (new_tip) {
550-
CacheBlockHeightInternal(new_tip->GetBlockHash(), new_tip->nHeight);
551-
}
552543
}
553544

545+
CacheTipHeight(pindexDisconnected->pprev);
546+
554547
db.RemoveBlockInstantSendLocks(pblock, pindexDisconnected);
555548
}
556549

@@ -672,11 +665,7 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock)
672665

673666
void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew)
674667
{
675-
{
676-
LOCK(cs_height_cache);
677-
CacheBlockHeightInternal(pindexNew->GetBlockHash(), pindexNew->nHeight);
678-
m_cached_tip_height = pindexNew->nHeight;
679-
}
668+
CacheTipHeight(pindexNew);
680669

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

@@ -964,16 +953,16 @@ size_t CInstantSendManager::GetInstantSendLockCount() const
964953
return db.GetInstantSendLockCount();
965954
}
966955

967-
void CInstantSendManager::CacheBlockHeightInternal(const uint256& hash, int height) const
956+
void CInstantSendManager::CacheBlockHeightInternal(const CBlockIndex* const block_index) const
968957
{
969958
AssertLockHeld(cs_height_cache);
970-
m_cached_block_heights.insert(hash, height);
959+
m_cached_block_heights.insert(block_index->GetBlockHash(), block_index->nHeight);
971960
}
972961

973-
void CInstantSendManager::CacheBlockHeight(const uint256& hash, int height) const
962+
void CInstantSendManager::CacheBlockHeight(const CBlockIndex* const block_index) const
974963
{
975964
LOCK(cs_height_cache);
976-
CacheBlockHeightInternal(hash, height);
965+
CacheBlockHeightInternal(block_index);
977966
}
978967

979968
std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) const
@@ -992,10 +981,21 @@ std::optional<int> CInstantSendManager::GetBlockHeight(const uint256& hash) cons
992981
return std::nullopt;
993982
}
994983

995-
CacheBlockHeight(pindex->GetBlockHash(), pindex->nHeight);
984+
CacheBlockHeight(pindex);
996985
return pindex->nHeight;
997986
}
998987

988+
void CInstantSendManager::CacheTipHeight(const CBlockIndex* const tip) const
989+
{
990+
LOCK(cs_height_cache);
991+
if (tip) {
992+
CacheBlockHeightInternal(tip);
993+
m_cached_tip_height = tip->nHeight;
994+
} else {
995+
m_cached_tip_height = -1;
996+
}
997+
}
998+
999999
int CInstantSendManager::GetTipHeight() const
10001000
{
10011001
{
@@ -1006,14 +1006,9 @@ int CInstantSendManager::GetTipHeight() const
10061006
}
10071007

10081008
const CBlockIndex* tip = WITH_LOCK(::cs_main, return m_chainstate.m_chain.Tip());
1009-
assert(tip != nullptr);
10101009

1011-
{
1012-
LOCK(cs_height_cache);
1013-
CacheBlockHeightInternal(tip->GetBlockHash(), tip->nHeight);
1014-
m_cached_tip_height = tip->nHeight;
1015-
return m_cached_tip_height;
1016-
}
1010+
CacheTipHeight(tip);
1011+
return tip ? tip->nHeight : -1;
10171012
}
10181013

10191014
void CInstantSendManager::WorkThreadMain(PeerManager& peerman)

src/instantsend/instantsend.h

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

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

110110
public:
111111
CInstantSendManager() = delete;
@@ -192,8 +192,9 @@ class CInstantSendManager final : public instantsend::InstantSendSignerParent
192192

193193
size_t GetInstantSendLockCount() const;
194194

195-
void CacheBlockHeight(const uint256& hash, int height) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
195+
void CacheBlockHeight(const CBlockIndex* const block_index) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
196196
std::optional<int> GetBlockHeight(const uint256& hash) const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
197+
void CacheTipHeight(const CBlockIndex* const tip) const EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
197198
int GetTipHeight() const override EXCLUSIVE_LOCKS_REQUIRED(!cs_height_cache);
198199

199200
bool IsInstantSendEnabled() const override;

0 commit comments

Comments
 (0)