Skip to content

Commit 3b4a1a0

Browse files
committed
feat: new GetSML method for CDeterministicMNList
1 parent bbd8731 commit 3b4a1a0

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/evo/deterministicmns.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <evo/dmnstate.h>
88
#include <evo/evodb.h>
99
#include <evo/providertx.h>
10+
#include <evo/simplifiedmns.h>
1011
#include <evo/specialtx.h>
1112
#include <index/txindex.h>
1213

@@ -258,6 +259,14 @@ std::vector<CDeterministicMNCPtr> CDeterministicMNList::GetProjectedMNPayees(gsl
258259
return result;
259260
}
260261

262+
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> CDeterministicMNList::GetSML() const
263+
{
264+
if (!m_cached_sml) {
265+
m_cached_sml = std::make_shared<const CSimplifiedMNList>(*this);
266+
}
267+
return m_cached_sml;
268+
}
269+
261270
int CDeterministicMNList::CalcMaxPoSePenalty() const
262271
{
263272
// Maximum PoSe penalty is dynamic and equals the number of registered MNs
@@ -443,6 +452,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
443452

444453
mnMap = mnMap.set(dmn->proTxHash, dmn);
445454
mnInternalIdMap = mnInternalIdMap.set(dmn->GetInternalId(), dmn->proTxHash);
455+
m_cached_sml = nullptr;
446456
if (fBumpTotalCount) {
447457
// nTotalRegisteredCount acts more like a checkpoint, not as a limit,
448458
nTotalRegisteredCount = std::max(dmn->GetInternalId() + 1, (uint64_t)nTotalRegisteredCount);
@@ -514,6 +524,9 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
514524

515525
dmn->pdmnState = pdmnState;
516526
mnMap = mnMap.set(oldDmn.proTxHash, dmn);
527+
if (m_cached_sml && CSimplifiedMNListEntry{oldDmn} != CSimplifiedMNListEntry{*dmn}) {
528+
m_cached_sml = nullptr;
529+
}
517530
}
518531

519532
void CDeterministicMNList::UpdateMN(const uint256& proTxHash, const std::shared_ptr<const CDeterministicMNState>& pdmnState)
@@ -585,6 +598,7 @@ void CDeterministicMNList::RemoveMN(const uint256& proTxHash)
585598

586599
mnMap = mnMap.erase(proTxHash);
587600
mnInternalIdMap = mnInternalIdMap.erase(dmn->GetInternalId());
601+
m_cached_sml = nullptr;
588602
}
589603

590604
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;
@@ -145,6 +146,13 @@ class CDeterministicMNList
145146
// we keep track of this as checking for duplicates would otherwise be painfully slow
146147
MnUniquePropertyMap mnUniquePropertyMap;
147148

149+
// This SML could be null
150+
// This cache is used to improve performance and meant to be reused
151+
// for multiple CDeterministicMNList until mnMap is actually changed.
152+
// Calls of AddMN, RemoveMN and (in some cases) UpdateMN reset this cache;
153+
// it happens also for indirect calls such as ApplyDiff
154+
mutable std::shared_ptr<const CSimplifiedMNList> m_cached_sml;
155+
148156
public:
149157
CDeterministicMNList() = default;
150158
explicit CDeterministicMNList(const uint256& _blockHash, int _height, uint32_t _totalRegisteredCount) :
@@ -195,6 +203,7 @@ class CDeterministicMNList
195203
mnMap = MnMap();
196204
mnUniquePropertyMap = MnUniquePropertyMap();
197205
mnInternalIdMap = MnInternalIdMap();
206+
m_cached_sml = nullptr;
198207
}
199208

200209
[[nodiscard]] size_t GetAllMNsCount() const
@@ -314,6 +323,11 @@ class CDeterministicMNList
314323
*/
315324
[[nodiscard]] std::vector<CDeterministicMNCPtr> GetProjectedMNPayees(gsl::not_null<const CBlockIndex* const> pindexPrev, int nCount = std::numeric_limits<int>::max()) const;
316325

326+
/**
327+
* Calculates CSimplifiedMNList for current list and cache it
328+
*/
329+
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> GetSML() const;
330+
317331
/**
318332
* Calculates the maximum penalty which is allowed at the height of this MN list. It is dynamic and might change
319333
* for every block.

test/lint/lint-circular-dependencies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"evo/chainhelper -> evo/specialtxman -> validation -> evo/chainhelper",
3838
"evo/deterministicmns -> index/txindex -> validation -> evo/deterministicmns",
3939
"evo/deterministicmns -> index/txindex -> validation -> txmempool -> evo/deterministicmns",
40+
"evo/deterministicmns -> evo/simplifiedmns -> evo/deterministicmns",
4041
"evo/netinfo -> evo/providertx -> evo/netinfo",
4142
"evo/smldiff -> llmq/blockprocessor -> llmq/utils -> llmq/snapshot -> evo/smldiff",
4243
"core_io -> evo/assetlocktx -> llmq/signing -> net_processing -> evo/smldiff -> core_io",

0 commit comments

Comments
 (0)