Skip to content

Commit e9822dd

Browse files
committed
feat: new GetSML method for CDeterministicMNList
1 parent 0f3f044 commit e9822dd

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/evo/deterministicmns.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <evo/dmnstate.h>
99
#include <evo/evodb.h>
1010
#include <evo/providertx.h>
11+
#include <evo/simplifiedmns.h>
1112
#include <evo/specialtx.h>
1213
#include <llmq/commitment.h>
1314
#include <llmq/utils.h>
@@ -255,6 +256,14 @@ std::vector<CDeterministicMNCPtr> CDeterministicMNList::GetProjectedMNPayees(gsl
255256
return result;
256257
}
257258

259+
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> CDeterministicMNList::GetSML() const
260+
{
261+
if (!m_cached_sml) {
262+
m_cached_sml = std::make_shared<const CSimplifiedMNList>(*this);
263+
}
264+
return m_cached_sml;
265+
}
266+
258267
int CDeterministicMNList::CalcMaxPoSePenalty() const
259268
{
260269
// Maximum PoSe penalty is dynamic and equals the number of registered MNs
@@ -440,6 +449,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
440449

441450
mnMap = mnMap.set(dmn->proTxHash, dmn);
442451
mnInternalIdMap = mnInternalIdMap.set(dmn->GetInternalId(), dmn->proTxHash);
452+
m_cached_sml = nullptr;
443453
if (fBumpTotalCount) {
444454
// nTotalRegisteredCount acts more like a checkpoint, not as a limit,
445455
nTotalRegisteredCount = std::max(dmn->GetInternalId() + 1, (uint64_t)nTotalRegisteredCount);
@@ -510,6 +520,9 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
510520

511521
dmn->pdmnState = pdmnState;
512522
mnMap = mnMap.set(oldDmn.proTxHash, dmn);
523+
if (m_cached_sml && CSimplifiedMNListEntry{oldDmn} != CSimplifiedMNListEntry{*dmn}) {
524+
m_cached_sml = nullptr;
525+
}
513526
}
514527

515528
void CDeterministicMNList::UpdateMN(const uint256& proTxHash, const std::shared_ptr<const CDeterministicMNState>& pdmnState)
@@ -581,6 +594,7 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
581594

582595
mnMap = mnMap.erase(proTxHash);
583596
mnInternalIdMap = mnInternalIdMap.erase(dmn->GetInternalId());
597+
m_cached_sml = nullptr;
584598
}
585599

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

src/evo/deterministicmns.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CBlock;
3030
class CBlockIndex;
3131
class CCoinsViewCache;
3232
class CEvoDB;
33+
class CSimplifiedMNList;
3334
class TxValidationState;
3435

3536
extern RecursiveMutex cs_main;
@@ -171,6 +172,13 @@ class CDeterministicMNList
171172
// we keep track of this as checking for duplicates would otherwise be painfully slow
172173
MnUniquePropertyMap mnUniquePropertyMap;
173174

175+
// This SML could be null
176+
// This cache is used to improve performance and meant to be reused
177+
// for multiple CDeterministicMNList until mnMap is actually changed.
178+
// Calls of AddMN, RemoveMN and (in some cases) UpdateMN reset this cache;
179+
// it happens also for indirect calls such as ApplyDiff
180+
mutable std::shared_ptr<const CSimplifiedMNList> m_cached_sml;
181+
174182
public:
175183
CDeterministicMNList() = default;
176184
explicit CDeterministicMNList(const uint256& _blockHash, int _height, uint32_t _totalRegisteredCount) :
@@ -205,6 +213,7 @@ class CDeterministicMNList
205213
mnMap = MnMap();
206214
mnUniquePropertyMap = MnUniquePropertyMap();
207215
mnInternalIdMap = MnInternalIdMap();
216+
m_cached_sml = nullptr;
208217

209218
SerializationOpBase(s, CSerActionUnserialize());
210219

@@ -331,6 +340,11 @@ class CDeterministicMNList
331340
*/
332341
[[nodiscard]] std::vector<CDeterministicMNCPtr> GetProjectedMNPayees(gsl::not_null<const CBlockIndex* const> pindexPrev, int nCount = std::numeric_limits<int>::max()) const;
333342

343+
/**
344+
* Calculates CSimplifiedMNList for current list and cache it
345+
*/
346+
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> GetSML() const;
347+
334348
/**
335349
* Calculates the maximum penalty which is allowed at the height of this MN list. It is dynamic and might change
336350
* for every block.

test/lint/lint-circular-dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
"evo/deterministicmns -> llmq/commitment -> validation -> txmempool -> evo/deterministicmns",
5050
"evo/deterministicmns -> llmq/utils -> evo/deterministicmns",
5151
"evo/deterministicmns -> llmq/utils -> llmq/snapshot -> evo/smldiff -> evo/deterministicmns",
52-
"evo/deterministicmns -> llmq/utils -> llmq/snapshot -> evo/simplifiedmns -> evo/deterministicmns",
5352
"evo/deterministicmns -> llmq/utils -> net -> evo/deterministicmns",
5453
"evo/deterministicmns -> validationinterface -> evo/deterministicmns",
5554
"evo/deterministicmns -> validationinterface -> governance/vote -> evo/deterministicmns",
55+
"evo/deterministicmns -> evo/simplifiedmns -> evo/deterministicmns",
5656
"evo/mnhftx -> validation -> evo/mnhftx",
5757
"evo/smldiff -> llmq/blockprocessor -> llmq/utils -> llmq/snapshot -> evo/smldiff",
5858
"evo/specialtxman -> validation -> evo/specialtxman",

0 commit comments

Comments
 (0)