Skip to content

Commit 5ec1901

Browse files
refactor: centralize SML cache invalidation logic
Add private helper methods InvalidateSMLCache() and InvalidateSMLCacheIfChanged() to centralize cache invalidation logic and reduce code duplication. This improves maintainability by: - Centralizing cache invalidation logic in dedicated methods - Reducing code duplication across AddMN, RemoveMN, UpdateMN, and Unserialize - Making cache invalidation patterns consistent and easier to maintain - Providing both unconditional and conditional invalidation helpers The conditional invalidation method preserves the optimization in UpdateMN where cache is only invalidated if the SML entry actually changed.
1 parent aef34f7 commit 5ec1901

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

src/evo/deterministicmns.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
465465

466466
mnMap = mnMap.set(dmn->proTxHash, dmn);
467467
mnInternalIdMap = mnInternalIdMap.set(dmn->GetInternalId(), dmn->proTxHash);
468-
{
469-
LOCK(m_cached_sml_mutex);
470-
m_cached_sml = nullptr;
471-
}
468+
InvalidateSMLCache();
472469
if (fBumpTotalCount) {
473470
// nTotalRegisteredCount acts more like a checkpoint, not as a limit,
474471
nTotalRegisteredCount = std::max(dmn->GetInternalId() + 1, (uint64_t)nTotalRegisteredCount);
@@ -539,12 +536,7 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
539536

540537
dmn->pdmnState = pdmnState;
541538
mnMap = mnMap.set(oldDmn.proTxHash, dmn);
542-
{
543-
LOCK(m_cached_sml_mutex);
544-
if (m_cached_sml && oldDmn.to_sml_entry() != dmn->to_sml_entry()) {
545-
m_cached_sml = nullptr;
546-
}
547-
}
539+
InvalidateSMLCacheIfChanged(oldDmn.to_sml_entry(), dmn->to_sml_entry());
548540
}
549541

550542
void CDeterministicMNList::UpdateMN(const uint256& proTxHash, const std::shared_ptr<const CDeterministicMNState>& pdmnState)
@@ -616,10 +608,7 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
616608

617609
mnMap = mnMap.erase(proTxHash);
618610
mnInternalIdMap = mnInternalIdMap.erase(dmn->GetInternalId());
619-
{
620-
LOCK(m_cached_sml_mutex);
621-
m_cached_sml = nullptr;
622-
}
611+
InvalidateSMLCache();
623612
}
624613

625614
bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null<const CBlockIndex*> pindex,

src/evo/deterministicmns.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ class CDeterministicMNList
183183
mutable Mutex m_cached_sml_mutex;
184184
mutable std::shared_ptr<const CSimplifiedMNList> m_cached_sml GUARDED_BY(m_cached_sml_mutex);
185185

186+
// Private helper method to invalidate SML cache
187+
void InvalidateSMLCache() {
188+
LOCK(m_cached_sml_mutex);
189+
m_cached_sml = nullptr;
190+
}
191+
192+
// Private helper method to conditionally invalidate SML cache if entry changed
193+
void InvalidateSMLCacheIfChanged(const CSimplifiedMNListEntry& oldEntry, const CSimplifiedMNListEntry& newEntry) {
194+
LOCK(m_cached_sml_mutex);
195+
if (m_cached_sml && oldEntry != newEntry) {
196+
m_cached_sml = nullptr;
197+
}
198+
}
199+
186200
public:
187201
CDeterministicMNList() = default;
188202
explicit CDeterministicMNList(const uint256& _blockHash, int _height, uint32_t _totalRegisteredCount) :
@@ -247,10 +261,7 @@ class CDeterministicMNList
247261
mnMap = MnMap();
248262
mnUniquePropertyMap = MnUniquePropertyMap();
249263
mnInternalIdMap = MnInternalIdMap();
250-
{
251-
LOCK(m_cached_sml_mutex);
252-
m_cached_sml = nullptr;
253-
}
264+
InvalidateSMLCache();
254265

255266
SerializationOpBase(s, CSerActionUnserialize());
256267

0 commit comments

Comments
 (0)