diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 724b33cd3..9808f6494 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -265,20 +265,48 @@ CDeterministicMNListDiff CDeterministicMNList::BuildDiff(const CDeterministicMNL diffRet.blockHash = to.blockHash; diffRet.nHeight = to.nHeight; - for (const auto& p : to.mnMap) { - const auto fromPtr = mnMap.find(p.first); + to.ForEachMN(false, [&](const CDeterministicMNCPtr& toPtr) { + auto fromPtr = GetMN(toPtr->proTxHash); if (fromPtr == nullptr) { - diffRet.addedMNs.emplace(p.first, p.second); - } else if (*p.second->pdmnState != *(*fromPtr)->pdmnState) { - diffRet.updatedMNs.emplace(p.first, p.second->pdmnState); + diffRet.addedMNs.emplace(toPtr->proTxHash, toPtr); + } else if (*toPtr->pdmnState != *fromPtr->pdmnState) { + diffRet.updatedMNs.emplace(toPtr->proTxHash, toPtr->pdmnState); } - } - for (const auto& p : mnMap) { - const auto toPtr = to.mnMap.find(p.first); + }); + ForEachMN(false, [&](const CDeterministicMNCPtr& fromPtr) { + auto toPtr = to.GetMN(fromPtr->proTxHash); if (toPtr == nullptr) { - diffRet.removedMns.insert(p.first); + diffRet.removedMns.insert(fromPtr->proTxHash); } - } + }); + + return diffRet; +} + +CSimplifiedMNListDiff CDeterministicMNList::BuildSimplifiedDiff(const CDeterministicMNList& to) const +{ + CSimplifiedMNListDiff diffRet; + diffRet.baseBlockHash = blockHash; + diffRet.blockHash = to.blockHash; + + to.ForEachMN(false, [&](const CDeterministicMNCPtr& toPtr) { + auto fromPtr = GetMN(toPtr->proTxHash); + if (fromPtr == nullptr) { + diffRet.mnList.emplace_back(*toPtr); + } else { + CSimplifiedMNListEntry sme1(*toPtr); + CSimplifiedMNListEntry sme2(*fromPtr); + if (sme1 != sme2) { + diffRet.mnList.emplace_back(*toPtr); + } + } + }); + ForEachMN(false, [&](const CDeterministicMNCPtr& fromPtr) { + auto toPtr = to.GetMN(fromPtr->proTxHash); + if (toPtr == nullptr) { + diffRet.deletedMNs.emplace_back(fromPtr->proTxHash); + } + }); return diffRet; } diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index 70f3fcc87..bc9fadaa3 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -10,6 +10,7 @@ #include "dbwrapper.h" #include "evodb.h" #include "providertx.h" +#include "simplifiedmns.h" #include "sync.h" #include "immer/map.hpp" @@ -307,6 +308,7 @@ public: std::vector> CalculateScores(const uint256& modifier) const; CDeterministicMNListDiff BuildDiff(const CDeterministicMNList& to) const; + CSimplifiedMNListDiff BuildSimplifiedDiff(const CDeterministicMNList& to) const; CDeterministicMNList ApplyDiff(const CDeterministicMNListDiff& diff) const; void AddMN(const CDeterministicMNCPtr& dmn); diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index e71d1168b..26d26eb47 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -144,7 +144,7 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc auto baseDmnList = deterministicMNManager->GetListForBlock(baseBlockHash); auto dmnList = deterministicMNManager->GetListForBlock(blockHash); - auto dmnDiff = baseDmnList.BuildDiff(dmnList); + mnListDiffRet = baseDmnList.BuildSimplifiedDiff(dmnList); // TODO store coinbase TX in CBlockIndex CBlock block; @@ -153,8 +153,6 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc return false; } - mnListDiffRet.baseBlockHash = baseBlockHash; - mnListDiffRet.blockHash = blockHash; mnListDiffRet.cbTx = block.vtx[0]; std::vector vHashes; @@ -164,17 +162,6 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc } vMatch[0] = true; // only coinbase matches mnListDiffRet.cbTxMerkleTree = CPartialMerkleTree(vHashes, vMatch); - mnListDiffRet.deletedMNs.assign(dmnDiff.removedMns.begin(), dmnDiff.removedMns.end()); - - for (const auto& p : dmnDiff.addedMNs) { - mnListDiffRet.mnList.emplace_back(*p.second); - } - for (const auto& p : dmnDiff.updatedMNs) { - const auto& dmn = dmnList.GetMN(p.first); - CDeterministicMN newDmn(*dmn); - newDmn.pdmnState = p.second; - mnListDiffRet.mnList.emplace_back(newDmn); - } return true; } diff --git a/src/evo/simplifiedmns.h b/src/evo/simplifiedmns.h index 1487cd64e..426665219 100644 --- a/src/evo/simplifiedmns.h +++ b/src/evo/simplifiedmns.h @@ -29,6 +29,21 @@ public: CSimplifiedMNListEntry() {} CSimplifiedMNListEntry(const CDeterministicMN& dmn); + bool operator==(const CSimplifiedMNListEntry& rhs) const + { + return proRegTxHash == rhs.proRegTxHash && + confirmedHash == rhs.confirmedHash && + service == rhs.service && + pubKeyOperator == rhs.pubKeyOperator && + keyIDVoting == rhs.keyIDVoting && + isValid == rhs.isValid; + } + + bool operator!=(const CSimplifiedMNListEntry& rhs) const + { + return !(rhs == *this); + } + public: ADD_SERIALIZE_METHODS;