From b5947f2997749500eb1d4fb766a3ca306226fbfc Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Thu, 15 Nov 2018 10:42:39 +0100 Subject: [PATCH] Implement BuildSimplifiedDiff in CDeterministicMNList And use it instead of BuildDiff in DIP4 --- src/evo/deterministicmns.cpp | 28 ++++++++++++++++++++++++++++ src/evo/deterministicmns.h | 2 ++ src/evo/simplifiedmns.cpp | 15 +-------------- src/evo/simplifiedmns.h | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 45246c285..fea319b08 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -283,6 +283,34 @@ CDeterministicMNListDiff CDeterministicMNList::BuildDiff(const CDeterministicMNL return diffRet; } +CSimplifiedMNListDiff CDeterministicMNList::BuildSimplifiedDiff(const CDeterministicMNList& to) const +{ + CSimplifiedMNListDiff diffRet; + diffRet.baseBlockHash = blockHash; + diffRet.blockHash = to.blockHash; + + to.ForEachMN(false, [&](const CDeterministicMNCPtr& toPtr) { + const auto fromPtr = mnMap.find(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) { + const auto toPtr = to.mnMap.find(fromPtr->proTxHash); + if (toPtr == nullptr) { + diffRet.deletedMNs.emplace_back(fromPtr->proTxHash); + } + }); + + return diffRet; +} + CDeterministicMNList CDeterministicMNList::ApplyDiff(const CDeterministicMNListDiff& diff) const { assert(diff.prevBlockHash == blockHash && diff.nHeight == nHeight + 1); 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;