Merge pull request #2447 from codablock/pr_dip4_fixdiff

Fix "protx diff" and MNLISTDIFF P2P message to not always return all MNs
This commit is contained in:
Alexander Block 2018-11-16 09:37:35 +01:00 committed by GitHub
commit 58b7041279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 24 deletions

View File

@ -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;
}

View File

@ -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<std::pair<arith_uint256, CDeterministicMNCPtr>> 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);

View File

@ -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<uint256> 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;
}

View File

@ -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;