Skip to content

Commit 63031da

Browse files
committed
refactor: drop dependency SML on DMN
1 parent 7bed080 commit 63031da

File tree

10 files changed

+68
-62
lines changed

10 files changed

+68
-62
lines changed

src/evo/deterministicmns.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ uint64_t CDeterministicMN::GetInternalId() const
3737
return internalId;
3838
}
3939

40+
CSimplifiedMNListEntry CDeterministicMN::to_sml_entry() const
41+
{
42+
const CDeterministicMNState& state{*pdmnState};
43+
return CSimplifiedMNListEntry(proTxHash, state.confirmedHash, state.netInfo, state.pubKeyOperator,
44+
state.keyIDVoting, !state.IsBanned(), state.platformHTTPPort, state.platformNodeID,
45+
state.scriptPayout, state.scriptOperatorPayout, state.nVersion, nType);
46+
}
47+
4048
std::string CDeterministicMN::ToString() const
4149
{
4250
return strprintf("CDeterministicMN(proTxHash=%s, collateralOutpoint=%s, nOperatorReward=%f, state=%s", proTxHash.ToString(), collateralOutpoint.ToStringShort(), (double)nOperatorReward / 100, pdmnState->ToString());
@@ -259,11 +267,18 @@ std::vector<CDeterministicMNCPtr> CDeterministicMNList::GetProjectedMNPayees(gsl
259267
return result;
260268
}
261269

262-
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> CDeterministicMNList::GetSML() const
270+
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> CDeterministicMNList::to_sml() const
263271
{
264272
if (!m_cached_sml) {
265-
m_cached_sml = std::make_shared<const CSimplifiedMNList>(*this);
273+
std::vector<std::unique_ptr<CSimplifiedMNListEntry>> sml_entries;
274+
sml_entries.reserve(mnMap.size());
275+
276+
ForEachMN(false, [&sml_entries](auto& dmn) {
277+
sml_entries.emplace_back(std::make_unique<CSimplifiedMNListEntry>(dmn.to_sml_entry()));
278+
});
279+
m_cached_sml = std::make_shared<CSimplifiedMNList>(std::move(sml_entries));
266280
}
281+
267282
return m_cached_sml;
268283
}
269284

@@ -524,7 +539,7 @@ void CDeterministicMNList::UpdateMN(const CDeterministicMN& oldDmn, const std::s
524539

525540
dmn->pdmnState = pdmnState;
526541
mnMap = mnMap.set(oldDmn.proTxHash, dmn);
527-
if (m_cached_sml && CSimplifiedMNListEntry{oldDmn} != CSimplifiedMNListEntry{*dmn}) {
542+
if (m_cached_sml && oldDmn.to_sml_entry() != dmn->to_sml_entry()) {
528543
m_cached_sml = nullptr;
529544
}
530545
}
@@ -618,7 +633,7 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, gsl::not_null<co
618633
int nHeight = pindex->nHeight;
619634

620635
try {
621-
newList.GetSML(); // to fullfill cache of SML
636+
newList.to_sml(); // to fullfill cache of SML
622637

623638
LOCK(cs);
624639

src/evo/deterministicmns.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CBlockIndex;
3131
class CCoinsViewCache;
3232
class CEvoDB;
3333
class CSimplifiedMNList;
34+
class CSimplifiedMNListEntry;
3435
class TxValidationState;
3536

3637
extern RecursiveMutex cs_main;
@@ -80,6 +81,7 @@ class CDeterministicMN
8081

8182
[[nodiscard]] uint64_t GetInternalId() const;
8283

84+
[[nodiscard]] CSimplifiedMNListEntry to_sml_entry() const;
8385
[[nodiscard]] std::string ToString() const;
8486
[[nodiscard]] UniValue ToJson() const;
8587
};
@@ -326,7 +328,7 @@ class CDeterministicMNList
326328
/**
327329
* Calculates CSimplifiedMNList for current list and cache it
328330
*/
329-
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> GetSML() const;
331+
gsl::not_null<std::shared_ptr<const CSimplifiedMNList>> to_sml() const;
330332

331333
/**
332334
* Calculates the maximum penalty which is allowed at the height of this MN list. It is dynamic and might change

src/evo/simplifiedmns.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,37 @@
44

55
#include <evo/simplifiedmns.h>
66

7+
#include <clientversion.h>
78
#include <consensus/merkle.h>
8-
#include <evo/deterministicmns.h>
99
#include <evo/netinfo.h>
1010
#include <key_io.h>
1111
#include <logging.h>
1212
#include <pubkey.h>
1313
#include <serialize.h>
14+
#include <sync.h>
1415
#include <univalue.h>
16+
#include <util/time.h>
1517
#include <util/underlying.h>
1618
#include <version.h>
1719

18-
CSimplifiedMNListEntry::CSimplifiedMNListEntry(const CDeterministicMN& dmn) :
19-
proRegTxHash(dmn.proTxHash),
20-
confirmedHash(dmn.pdmnState->confirmedHash),
21-
netInfo(dmn.pdmnState->netInfo),
22-
pubKeyOperator(dmn.pdmnState->pubKeyOperator),
23-
keyIDVoting(dmn.pdmnState->keyIDVoting),
24-
isValid(!dmn.pdmnState->IsBanned()),
25-
platformHTTPPort(dmn.pdmnState->platformHTTPPort),
26-
platformNodeID(dmn.pdmnState->platformNodeID),
27-
scriptPayout(dmn.pdmnState->scriptPayout),
28-
scriptOperatorPayout(dmn.pdmnState->scriptOperatorPayout),
29-
nVersion(dmn.pdmnState->nVersion),
30-
nType(dmn.nType)
20+
CSimplifiedMNListEntry::CSimplifiedMNListEntry(const uint256& proreg_tx_hash, const uint256& confirmed_hash,
21+
const std::shared_ptr<NetInfoInterface>& net_info,
22+
const CBLSLazyPublicKey& pubkey_operator, const CKeyID& keyid_voting,
23+
bool is_valid, uint16_t platform_http_port,
24+
const uint160& platform_node_id, const CScript& script_payout,
25+
const CScript& script_operator_payout, uint16_t version, MnType type) :
26+
proRegTxHash(proreg_tx_hash),
27+
confirmedHash(confirmed_hash),
28+
netInfo(net_info),
29+
pubKeyOperator(pubkey_operator),
30+
keyIDVoting(keyid_voting),
31+
isValid(is_valid),
32+
platformHTTPPort(platform_http_port),
33+
platformNodeID(platform_node_id),
34+
scriptPayout(script_payout),
35+
scriptOperatorPayout(script_operator_payout),
36+
nVersion(version),
37+
nType(type)
3138
{
3239
}
3340

@@ -59,24 +66,9 @@ std::string CSimplifiedMNListEntry::ToString() const
5966
operatorPayoutAddress, platformHTTPPort, platformNodeID.ToString(), netInfo->ToString());
6067
}
6168

62-
CSimplifiedMNList::CSimplifiedMNList(const std::vector<CSimplifiedMNListEntry>& smlEntries)
69+
CSimplifiedMNList::CSimplifiedMNList(std::vector<std::unique_ptr<CSimplifiedMNListEntry>>&& smlEntries)
6370
{
64-
mnList.reserve(smlEntries.size());
65-
for (const auto& entry : smlEntries) {
66-
mnList.emplace_back(std::make_unique<CSimplifiedMNListEntry>(entry));
67-
}
68-
69-
std::sort(mnList.begin(), mnList.end(), [&](const std::unique_ptr<CSimplifiedMNListEntry>& a, const std::unique_ptr<CSimplifiedMNListEntry>& b) {
70-
return a->proRegTxHash.Compare(b->proRegTxHash) < 0;
71-
});
72-
}
73-
74-
CSimplifiedMNList::CSimplifiedMNList(const CDeterministicMNList& dmnList)
75-
{
76-
mnList.reserve(dmnList.GetAllMNsCount());
77-
dmnList.ForEachMN(false, [this](auto& dmn) {
78-
mnList.emplace_back(std::make_unique<CSimplifiedMNListEntry>(dmn));
79-
});
71+
mnList = std::move(smlEntries);
8072

8173
std::sort(mnList.begin(), mnList.end(), [&](const std::unique_ptr<CSimplifiedMNListEntry>& a, const std::unique_ptr<CSimplifiedMNListEntry>& b) {
8274
return a->proRegTxHash.Compare(b->proRegTxHash) < 0;
@@ -104,7 +96,8 @@ bool CSimplifiedMNList::operator==(const CSimplifiedMNList& rhs) const
10496
);
10597
}
10698

107-
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, const CDeterministicMNList& mn_list, BlockValidationState& state)
99+
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, std::shared_ptr<const CSimplifiedMNList> sml,
100+
BlockValidationState& state)
108101
{
109102
try {
110103
static std::atomic<int64_t> nTimeMerkle = 0;
@@ -117,7 +110,6 @@ bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, const CDeterministicMNList
117110
static uint256 merkleRootCached GUARDED_BY(cached_mutex);
118111
static bool mutatedCached GUARDED_BY(cached_mutex){false};
119112

120-
std::shared_ptr<const CSimplifiedMNList> sml{mn_list.GetSML()};
121113
LOCK(cached_mutex);
122114
if (sml == cached_sml || *sml == *cached_sml) {
123115
merkleRootRet = merkleRootCached;

src/evo/simplifiedmns.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@
1414
#include <pubkey.h>
1515
#include <util/pointer.h>
1616

17-
class UniValue;
18-
class CDeterministicMN;
19-
class CDeterministicMNList;
20-
class ChainstateManager;
17+
#include <memory>
18+
#include <vector>
2119

22-
namespace llmq {
23-
class CFinalCommitment;
24-
class CQuorumBlockProcessor;
25-
class CQuorumManager;
26-
} // namespace llmq
20+
class UniValue;
2721

2822
class CSimplifiedMNListEntry
2923
{
@@ -42,7 +36,11 @@ class CSimplifiedMNListEntry
4236
MnType nType{MnType::Regular};
4337

4438
CSimplifiedMNListEntry() = default;
45-
explicit CSimplifiedMNListEntry(const CDeterministicMN& dmn);
39+
CSimplifiedMNListEntry(const uint256& proreg_tx_hash, const uint256& confirmed_hash,
40+
const std::shared_ptr<NetInfoInterface>& net_info, const CBLSLazyPublicKey& pubkey_operator,
41+
const CKeyID& keyid_voting, bool is_valid, uint16_t platform_http_port,
42+
const uint160& platform_node_id, const CScript& script_payout,
43+
const CScript& script_operator_payout, uint16_t version, MnType type);
4644

4745
bool operator==(const CSimplifiedMNListEntry& rhs) const
4846
{
@@ -101,15 +99,15 @@ class CSimplifiedMNList
10199
std::vector<std::unique_ptr<CSimplifiedMNListEntry>> mnList;
102100

103101
CSimplifiedMNList() = default;
104-
explicit CSimplifiedMNList(const CDeterministicMNList& dmnList);
105102

106103
// This constructor from std::vector is used in unit-tests
107-
explicit CSimplifiedMNList(const std::vector<CSimplifiedMNListEntry>& smlEntries);
104+
explicit CSimplifiedMNList(std::vector<std::unique_ptr<CSimplifiedMNListEntry>>&& smlEntries);
108105

109106
uint256 CalcMerkleRoot(bool* pmutated = nullptr) const;
110107
bool operator==(const CSimplifiedMNList& rhs) const;
111108
};
112109

113-
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, const CDeterministicMNList& sml, BlockValidationState& state);
110+
bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, std::shared_ptr<const CSimplifiedMNList> sml,
111+
BlockValidationState& state);
114112

115113
#endif // BITCOIN_EVO_SIMPLIFIEDMNS_H

src/evo/smldiff.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ CSimplifiedMNListDiff BuildSimplifiedDiff(const CDeterministicMNList& from, cons
127127
to.ForEachMN(false, [&](const auto& toPtr) {
128128
auto fromPtr = from.GetMN(toPtr.proTxHash);
129129
if (fromPtr == nullptr) {
130-
CSimplifiedMNListEntry sme(toPtr);
130+
CSimplifiedMNListEntry sme{toPtr.to_sml_entry()};
131131
diffRet.mnList.push_back(std::move(sme));
132132
} else {
133-
CSimplifiedMNListEntry sme1(toPtr);
134-
CSimplifiedMNListEntry sme2(*fromPtr);
133+
CSimplifiedMNListEntry sme1{toPtr.to_sml_entry()};
134+
CSimplifiedMNListEntry sme2(fromPtr->to_sml_entry());
135135
if ((sme1 != sme2) || (extended && (sme1.scriptPayout != sme2.scriptPayout ||
136136
sme1.scriptOperatorPayout != sme2.scriptOperatorPayout))) {
137137
diffRet.mnList.push_back(std::move(sme1));

src/evo/specialtxman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(const CBlock& block, const CB
608608
nTimeDMN * 0.000001);
609609

610610
uint256 calculatedMerkleRoot;
611-
if (!CalcCbTxMerkleRootMNList(calculatedMerkleRoot, mn_list, state)) {
611+
if (!CalcCbTxMerkleRootMNList(calculatedMerkleRoot, mn_list.to_sml(), state)) {
612612
// pass the state returned by the function above
613613
return false;
614614
}

src/node/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
291291
if (!m_chain_helper.special_tx->BuildNewListFromBlock(*pblock, pindexPrev, m_chainstate.CoinsTip(), true, state, mn_list)) {
292292
throw std::runtime_error(strprintf("%s: BuildNewListFromBlock failed: %s", __func__, state.ToString()));
293293
}
294-
if (!CalcCbTxMerkleRootMNList(cbTx.merkleRootMNList, mn_list, state)) {
294+
if (!CalcCbTxMerkleRootMNList(cbTx.merkleRootMNList, mn_list.to_sml(), state)) {
295295
throw std::runtime_error(strprintf("%s: CalcCbTxMerkleRootMNList failed: %s", __func__, state.ToString()));
296296
}
297297
if (fDIP0008Active_context) {

src/test/evo_simplifiedmns_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE(simplifiedmns_merkleroots)
1616
{
1717
//TODO: Provide raw data for basic scheme as well
1818
bls::bls_legacy_scheme.store(true);
19-
std::vector<CSimplifiedMNListEntry> entries;
19+
std::vector<std::unique_ptr<CSimplifiedMNListEntry>> entries;
2020
for (size_t i = 1; i < 16; i++) {
2121
CSimplifiedMNListEntry smle;
2222
smle.nVersion = ProTxVersion::GetMax(!bls::bls_legacy_scheme, /*is_extended_addr=*/false);
@@ -33,7 +33,7 @@ BOOST_AUTO_TEST_CASE(simplifiedmns_merkleroots)
3333
smle.keyIDVoting.SetHex(strprintf("%040x", i));
3434
smle.isValid = true;
3535

36-
entries.emplace_back(smle);
36+
entries.emplace_back(std::make_unique<CSimplifiedMNListEntry>(smle));
3737
}
3838

3939
std::vector<std::string> expectedHashes = {
@@ -56,13 +56,13 @@ BOOST_AUTO_TEST_CASE(simplifiedmns_merkleroots)
5656
std::vector<std::string> calculatedHashes;
5757

5858
for (auto& smle : entries) {
59-
calculatedHashes.emplace_back(smle.CalcHash().ToString());
59+
calculatedHashes.emplace_back(smle->CalcHash().ToString());
6060
//printf("\"%s\",\n", calculatedHashes.back().c_str());
6161
}
6262

6363
BOOST_CHECK(expectedHashes == calculatedHashes);
6464

65-
CSimplifiedMNList sml(entries);
65+
CSimplifiedMNList sml{std::move(entries)};
6666

6767
std::string expectedMerkleRoot = "0bae2176078cf42fa3e1fda761d4255d1c1c54777c6a793d0ab2b07c85ed4022";
6868
std::string calculatedMerkleRoot = sml.CalcMerkleRoot(nullptr).ToString();

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ CBlock TestChainSetup::CreateBlock(
519519
if (!chainstate.ChainHelper().special_tx->BuildNewListFromBlock(block, chainstate.m_chain.Tip(), chainstate.CoinsTip(), true, state, mn_list)) {
520520
Assert(false);
521521
}
522-
if (!CalcCbTxMerkleRootMNList(cbTx->merkleRootMNList, mn_list, state)) {
522+
if (!CalcCbTxMerkleRootMNList(cbTx->merkleRootMNList, mn_list.to_sml(), state)) {
523523
Assert(false);
524524
}
525525
if (!CalcCbTxMerkleRootQuorums(block, chainstate.m_chain.Tip(), *m_node.llmq_ctx->quorum_block_processor, cbTx->merkleRootQuorums, state)) {

test/lint/lint-circular-dependencies.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
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",
4140
"evo/netinfo -> evo/providertx -> evo/netinfo",
4241
"evo/smldiff -> llmq/blockprocessor -> llmq/utils -> llmq/snapshot -> evo/smldiff",
4342
"core_io -> evo/assetlocktx -> llmq/signing -> net_processing -> evo/smldiff -> core_io",

0 commit comments

Comments
 (0)