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.blockHash = to.blockHash;
diffRet.nHeight = to.nHeight; diffRet.nHeight = to.nHeight;
for (const auto& p : to.mnMap) { to.ForEachMN(false, [&](const CDeterministicMNCPtr& toPtr) {
const auto fromPtr = mnMap.find(p.first); auto fromPtr = GetMN(toPtr->proTxHash);
if (fromPtr == nullptr) { if (fromPtr == nullptr) {
diffRet.addedMNs.emplace(p.first, p.second); diffRet.addedMNs.emplace(toPtr->proTxHash, toPtr);
} else if (*p.second->pdmnState != *(*fromPtr)->pdmnState) { } else if (*toPtr->pdmnState != *fromPtr->pdmnState) {
diffRet.updatedMNs.emplace(p.first, p.second->pdmnState); diffRet.updatedMNs.emplace(toPtr->proTxHash, toPtr->pdmnState);
} }
} });
for (const auto& p : mnMap) { ForEachMN(false, [&](const CDeterministicMNCPtr& fromPtr) {
const auto toPtr = to.mnMap.find(p.first); auto toPtr = to.GetMN(fromPtr->proTxHash);
if (toPtr == nullptr) { 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; return diffRet;
} }

View File

@ -10,6 +10,7 @@
#include "dbwrapper.h" #include "dbwrapper.h"
#include "evodb.h" #include "evodb.h"
#include "providertx.h" #include "providertx.h"
#include "simplifiedmns.h"
#include "sync.h" #include "sync.h"
#include "immer/map.hpp" #include "immer/map.hpp"
@ -307,6 +308,7 @@ public:
std::vector<std::pair<arith_uint256, CDeterministicMNCPtr>> CalculateScores(const uint256& modifier) const; std::vector<std::pair<arith_uint256, CDeterministicMNCPtr>> CalculateScores(const uint256& modifier) const;
CDeterministicMNListDiff BuildDiff(const CDeterministicMNList& to) const; CDeterministicMNListDiff BuildDiff(const CDeterministicMNList& to) const;
CSimplifiedMNListDiff BuildSimplifiedDiff(const CDeterministicMNList& to) const;
CDeterministicMNList ApplyDiff(const CDeterministicMNListDiff& diff) const; CDeterministicMNList ApplyDiff(const CDeterministicMNListDiff& diff) const;
void AddMN(const CDeterministicMNCPtr& dmn); 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 baseDmnList = deterministicMNManager->GetListForBlock(baseBlockHash);
auto dmnList = deterministicMNManager->GetListForBlock(blockHash); auto dmnList = deterministicMNManager->GetListForBlock(blockHash);
auto dmnDiff = baseDmnList.BuildDiff(dmnList); mnListDiffRet = baseDmnList.BuildSimplifiedDiff(dmnList);
// TODO store coinbase TX in CBlockIndex // TODO store coinbase TX in CBlockIndex
CBlock block; CBlock block;
@ -153,8 +153,6 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc
return false; return false;
} }
mnListDiffRet.baseBlockHash = baseBlockHash;
mnListDiffRet.blockHash = blockHash;
mnListDiffRet.cbTx = block.vtx[0]; mnListDiffRet.cbTx = block.vtx[0];
std::vector<uint256> vHashes; std::vector<uint256> vHashes;
@ -164,17 +162,6 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc
} }
vMatch[0] = true; // only coinbase matches vMatch[0] = true; // only coinbase matches
mnListDiffRet.cbTxMerkleTree = CPartialMerkleTree(vHashes, vMatch); 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; return true;
} }

View File

@ -29,6 +29,21 @@ public:
CSimplifiedMNListEntry() {} CSimplifiedMNListEntry() {}
CSimplifiedMNListEntry(const CDeterministicMN& dmn); 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: public:
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;