Skip to content

Commit d61f44e

Browse files
PastaPastaPastaknst
authored andcommitted
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 53190a5 commit d61f44e

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/evo/deterministicmns.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
468468

469469
mnMap = mnMap.set(dmn->proTxHash, dmn);
470470
mnInternalIdMap = mnInternalIdMap.set(dmn->GetInternalId(), dmn->proTxHash);
471-
{
472-
LOCK(m_cached_sml_mutex);
473-
m_cached_sml = nullptr;
474-
}
471+
InvalidateSMLCache();
475472
if (fBumpTotalCount) {
476473
// nTotalRegisteredCount acts more like a checkpoint, not as a limit,
477474
nTotalRegisteredCount = std::max(dmn->GetInternalId() + 1, (uint64_t)nTotalRegisteredCount);
@@ -543,11 +540,9 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
543540

544541
dmn->pdmnState = pdmnState;
545542
mnMap = mnMap.set(oldDmn.proTxHash, dmn);
546-
{
547-
LOCK(m_cached_sml_mutex);
548-
if (m_cached_sml && oldDmn.to_sml_entry() != dmn->to_sml_entry()) {
549-
m_cached_sml = nullptr;
550-
}
543+
LOCK(m_cached_sml_mutex);
544+
if (m_cached_sml && oldDmn.to_sml_entry() != dmn->to_sml_entry()) {
545+
m_cached_sml = nullptr;
551546
}
552547
}
553548

@@ -620,10 +615,7 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
620615

621616
mnMap = mnMap.erase(proTxHash);
622617
mnInternalIdMap = mnInternalIdMap.erase(dmn->GetInternalId());
623-
{
624-
LOCK(m_cached_sml_mutex);
625-
m_cached_sml = nullptr;
626-
}
618+
InvalidateSMLCache();
627619
}
628620

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

src/evo/deterministicmns.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ class CDeterministicMNList
157157
mutable Mutex m_cached_sml_mutex;
158158
mutable std::shared_ptr<const CSimplifiedMNList> m_cached_sml GUARDED_BY(m_cached_sml_mutex);
159159

160+
// Private helper method to invalidate SML cache
161+
void InvalidateSMLCache()
162+
{
163+
LOCK(m_cached_sml_mutex);
164+
m_cached_sml = nullptr;
165+
}
166+
160167
public:
161168
CDeterministicMNList() = default;
162169
explicit CDeterministicMNList(const uint256& _blockHash, int _height, uint32_t _totalRegisteredCount) :
@@ -237,10 +244,7 @@ class CDeterministicMNList
237244
mnMap = MnMap();
238245
mnUniquePropertyMap = MnUniquePropertyMap();
239246
mnInternalIdMap = MnInternalIdMap();
240-
{
241-
LOCK(m_cached_sml_mutex);
242-
m_cached_sml = nullptr;
243-
}
247+
InvalidateSMLCache();
244248
}
245249

246250
[[nodiscard]] size_t GetAllMNsCount() const

0 commit comments

Comments
 (0)