2019-01-29 15:53:14 +01:00
|
|
|
// Copyright (c) 2017-2019 The Dash Core developers
|
2018-04-09 10:35:43 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-11-06 09:54:23 +01:00
|
|
|
#include "cbtx.h"
|
2018-10-30 10:59:32 +01:00
|
|
|
#include "core_io.h"
|
2018-11-06 09:54:23 +01:00
|
|
|
#include "deterministicmns.h"
|
2019-04-04 10:27:17 +02:00
|
|
|
#include "llmq/quorums.h"
|
|
|
|
#include "llmq/quorums_blockprocessor.h"
|
|
|
|
#include "llmq/quorums_commitment.h"
|
2018-04-09 10:35:43 +02:00
|
|
|
#include "simplifiedmns.h"
|
|
|
|
#include "specialtx.h"
|
|
|
|
|
2019-01-11 11:05:58 +01:00
|
|
|
#include "base58.h"
|
2018-04-09 14:49:34 +02:00
|
|
|
#include "chainparams.h"
|
2018-11-06 09:54:23 +01:00
|
|
|
#include "consensus/merkle.h"
|
|
|
|
#include "univalue.h"
|
|
|
|
#include "validation.h"
|
2018-04-09 10:35:43 +02:00
|
|
|
|
|
|
|
CSimplifiedMNListEntry::CSimplifiedMNListEntry(const CDeterministicMN& dmn) :
|
|
|
|
proRegTxHash(dmn.proTxHash),
|
2018-11-23 10:40:46 +01:00
|
|
|
confirmedHash(dmn.pdmnState->confirmedHash),
|
2018-04-09 10:35:43 +02:00
|
|
|
service(dmn.pdmnState->addr),
|
2018-10-21 21:45:16 +02:00
|
|
|
pubKeyOperator(dmn.pdmnState->pubKeyOperator),
|
2018-04-09 10:35:43 +02:00
|
|
|
keyIDVoting(dmn.pdmnState->keyIDVoting),
|
|
|
|
isValid(dmn.pdmnState->nPoSeBanHeight == -1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 CSimplifiedMNListEntry::CalcHash() const
|
|
|
|
{
|
|
|
|
CHashWriter hw(SER_GETHASH, CLIENT_VERSION);
|
|
|
|
hw << *this;
|
|
|
|
return hw.GetHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CSimplifiedMNListEntry::ToString() const
|
|
|
|
{
|
2019-04-30 14:48:21 +02:00
|
|
|
return strprintf("CSimplifiedMNListEntry(proRegTxHash=%s, confirmedHash=%s, service=%s, pubKeyOperator=%s, votingAddress=%s, isValid=%d)",
|
Merge #11117: Prepare for non-Base58 addresses (#3294)
* Merge #11117: Prepare for non-Base58 addresses
864cd2787 Move CBitcoinAddress to base58.cpp (Pieter Wuille)
5c8ff0d44 Introduce wrappers around CBitcoinAddress (Pieter Wuille)
Pull request description:
This patch removes the need for the intermediary Base58 type `CBitcoinAddress`, by providing {`Encode`,`Decode`,`IsValid`}`Destination` functions that directly operate on the conversion between `std::string`s and `CTxDestination`.
As a side, it also fixes a number of indentation issues, and removes probably several unnecessary implicit `CTxDestination`<->`CBitcoinAddress` conversions.
This change is far from complete. In follow-ups I'd like to:
* Split off the specific address and key encoding logic from base58.h, and move it to a address.h or so.
* Replace `CTxDestination` with a non-`boost::variant` version (which can be more efficient as `boost::variant` allocates everything on the heap, and remove the need for `boost::get<...>` and `IsValidDestination` calls everywhere).
* Do the same for `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey`.
However, I've tried to keep this patch to be minimally invasive, but still enough to support non-Base58 addresses. Perhaps a smaller patch is possible to hack Bech32 support into `CBitcoinAddress`, but I would consider that a move in the wrong direction.
Tree-SHA512: c2c77ffb57caeadf2429b1c2562ce60e8c7be8aa9f8e51b591f354b6b441162625b2efe14c023a1ae485cf2ed417263afa35c892891dfaa7844e7fbabccab85e
* CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* fix CBitcoinAddress GetKeyID check
Signed-off-by: Pasta <pasta@dashboost.org>
* fix providertx.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* hopefully fix governance-classes.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-validators.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-classes.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* fix governance-classes.h
Signed-off-by: Pasta <pasta@dashboost.org>
* DecodeTransaction -> DecodeDestination, fix governance-validators.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* More fixes for 3294
* Move GetIndexKey into rpc/misc.cpp near getAddressesFromParams
No need to have it in base58.cpp anymore as this is only used in getAddressesFromParams
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: Alexander Block <ablock84@gmail.com>
2020-01-22 11:35:04 +01:00
|
|
|
proRegTxHash.ToString(), confirmedHash.ToString(), service.ToString(false), pubKeyOperator.Get().ToString(), EncodeDestination(keyIDVoting), isValid);
|
2018-04-09 10:35:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSimplifiedMNListEntry::ToJson(UniValue& obj) const
|
|
|
|
{
|
|
|
|
obj.clear();
|
|
|
|
obj.setObject();
|
|
|
|
obj.push_back(Pair("proRegTxHash", proRegTxHash.ToString()));
|
2018-11-13 13:24:14 +01:00
|
|
|
obj.push_back(Pair("confirmedHash", confirmedHash.ToString()));
|
2018-04-09 10:35:43 +02:00
|
|
|
obj.push_back(Pair("service", service.ToString(false)));
|
2019-06-13 11:01:26 +02:00
|
|
|
obj.push_back(Pair("pubKeyOperator", pubKeyOperator.Get().ToString()));
|
Merge #11117: Prepare for non-Base58 addresses (#3294)
* Merge #11117: Prepare for non-Base58 addresses
864cd2787 Move CBitcoinAddress to base58.cpp (Pieter Wuille)
5c8ff0d44 Introduce wrappers around CBitcoinAddress (Pieter Wuille)
Pull request description:
This patch removes the need for the intermediary Base58 type `CBitcoinAddress`, by providing {`Encode`,`Decode`,`IsValid`}`Destination` functions that directly operate on the conversion between `std::string`s and `CTxDestination`.
As a side, it also fixes a number of indentation issues, and removes probably several unnecessary implicit `CTxDestination`<->`CBitcoinAddress` conversions.
This change is far from complete. In follow-ups I'd like to:
* Split off the specific address and key encoding logic from base58.h, and move it to a address.h or so.
* Replace `CTxDestination` with a non-`boost::variant` version (which can be more efficient as `boost::variant` allocates everything on the heap, and remove the need for `boost::get<...>` and `IsValidDestination` calls everywhere).
* Do the same for `CBitcoinSecret`, `CBitcoinExtKey`, and `CBitcoinExtPubKey`.
However, I've tried to keep this patch to be minimally invasive, but still enough to support non-Base58 addresses. Perhaps a smaller patch is possible to hack Bech32 support into `CBitcoinAddress`, but I would consider that a move in the wrong direction.
Tree-SHA512: c2c77ffb57caeadf2429b1c2562ce60e8c7be8aa9f8e51b591f354b6b441162625b2efe14c023a1ae485cf2ed417263afa35c892891dfaa7844e7fbabccab85e
* CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* more CBitcoinAddress -> EncodeDestination in providertx.h
Signed-off-by: Pasta <pasta@dashboost.org>
* fix CBitcoinAddress GetKeyID check
Signed-off-by: Pasta <pasta@dashboost.org>
* fix providertx.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* hopefully fix governance-classes.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-validators.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* partially fix governance-classes.cpp, unable to resolve "address.IsScript()"
Signed-off-by: Pasta <pasta@dashboost.org>
* fix governance-classes.h
Signed-off-by: Pasta <pasta@dashboost.org>
* DecodeTransaction -> DecodeDestination, fix governance-validators.cpp
Signed-off-by: Pasta <pasta@dashboost.org>
* More fixes for 3294
* Move GetIndexKey into rpc/misc.cpp near getAddressesFromParams
No need to have it in base58.cpp anymore as this is only used in getAddressesFromParams
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
Co-authored-by: Alexander Block <ablock84@gmail.com>
2020-01-22 11:35:04 +01:00
|
|
|
obj.push_back(Pair("votingAddress", EncodeDestination(keyIDVoting)));
|
2018-04-09 10:35:43 +02:00
|
|
|
obj.push_back(Pair("isValid", isValid));
|
|
|
|
}
|
|
|
|
|
2018-08-22 12:39:41 +02:00
|
|
|
CSimplifiedMNList::CSimplifiedMNList(const std::vector<CSimplifiedMNListEntry>& smlEntries)
|
|
|
|
{
|
2019-07-02 22:46:03 +02:00
|
|
|
mnList.resize(smlEntries.size());
|
|
|
|
for (size_t i = 0; i < smlEntries.size(); i++) {
|
|
|
|
mnList[i] = std::make_unique<CSimplifiedMNListEntry>(smlEntries[i]);
|
|
|
|
}
|
2018-08-22 12:39:41 +02:00
|
|
|
|
2019-07-02 22:46:03 +02:00
|
|
|
std::sort(mnList.begin(), mnList.end(), [&](const std::unique_ptr<CSimplifiedMNListEntry>& a, const std::unique_ptr<CSimplifiedMNListEntry>& b) {
|
|
|
|
return a->proRegTxHash.Compare(b->proRegTxHash) < 0;
|
2018-08-22 12:39:41 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-09 10:35:43 +02:00
|
|
|
CSimplifiedMNList::CSimplifiedMNList(const CDeterministicMNList& dmnList)
|
|
|
|
{
|
2019-07-02 22:46:03 +02:00
|
|
|
mnList.resize(dmnList.GetAllMNsCount());
|
2018-04-09 10:35:43 +02:00
|
|
|
|
2019-07-02 22:46:03 +02:00
|
|
|
size_t i = 0;
|
|
|
|
dmnList.ForEachMN(false, [this, &i](const CDeterministicMNCPtr& dmn) {
|
|
|
|
mnList[i++] = std::make_unique<CSimplifiedMNListEntry>(*dmn);
|
2018-10-02 11:03:05 +02:00
|
|
|
});
|
2018-04-09 10:35:43 +02:00
|
|
|
|
2019-07-02 22:46:03 +02:00
|
|
|
std::sort(mnList.begin(), mnList.end(), [&](const std::unique_ptr<CSimplifiedMNListEntry>& a, const std::unique_ptr<CSimplifiedMNListEntry>& b) {
|
|
|
|
return a->proRegTxHash.Compare(b->proRegTxHash) < 0;
|
2018-04-09 10:35:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:54:23 +01:00
|
|
|
uint256 CSimplifiedMNList::CalcMerkleRoot(bool* pmutated) const
|
2018-04-09 10:35:43 +02:00
|
|
|
{
|
|
|
|
std::vector<uint256> leaves;
|
|
|
|
leaves.reserve(mnList.size());
|
|
|
|
for (const auto& e : mnList) {
|
2019-07-02 22:46:03 +02:00
|
|
|
leaves.emplace_back(e->CalcHash());
|
2018-04-09 10:35:43 +02:00
|
|
|
}
|
|
|
|
return ComputeMerkleRoot(leaves, pmutated);
|
|
|
|
}
|
2018-04-09 14:49:34 +02:00
|
|
|
|
2019-04-04 10:27:17 +02:00
|
|
|
CSimplifiedMNListDiff::CSimplifiedMNListDiff()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimplifiedMNListDiff::~CSimplifiedMNListDiff()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSimplifiedMNListDiff::BuildQuorumsDiff(const CBlockIndex* baseBlockIndex, const CBlockIndex* blockIndex)
|
|
|
|
{
|
|
|
|
auto baseQuorums = llmq::quorumBlockProcessor->GetMinedAndActiveCommitmentsUntilBlock(baseBlockIndex);
|
|
|
|
auto quorums = llmq::quorumBlockProcessor->GetMinedAndActiveCommitmentsUntilBlock(blockIndex);
|
|
|
|
|
|
|
|
std::set<std::pair<Consensus::LLMQType, uint256>> baseQuorumHashes;
|
|
|
|
std::set<std::pair<Consensus::LLMQType, uint256>> quorumHashes;
|
|
|
|
for (auto& p : baseQuorums) {
|
|
|
|
for (auto& p2 : p.second) {
|
|
|
|
baseQuorumHashes.emplace(p.first, p2->GetBlockHash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& p : quorums) {
|
|
|
|
for (auto& p2 : p.second) {
|
|
|
|
quorumHashes.emplace(p.first, p2->GetBlockHash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& p : baseQuorumHashes) {
|
|
|
|
if (!quorumHashes.count(p)) {
|
|
|
|
deletedQuorums.emplace_back((uint8_t)p.first, p.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& p : quorumHashes) {
|
|
|
|
if (!baseQuorumHashes.count(p)) {
|
|
|
|
llmq::CFinalCommitment qc;
|
2019-04-04 11:11:25 +02:00
|
|
|
uint256 minedBlockHash;
|
|
|
|
if (!llmq::quorumBlockProcessor->GetMinedCommitment(p.first, p.second, qc, minedBlockHash)) {
|
2019-04-04 10:27:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
newQuorums.emplace_back(qc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-09 14:49:34 +02:00
|
|
|
void CSimplifiedMNListDiff::ToJson(UniValue& obj) const
|
|
|
|
{
|
|
|
|
obj.setObject();
|
|
|
|
|
|
|
|
obj.push_back(Pair("baseBlockHash", baseBlockHash.ToString()));
|
|
|
|
obj.push_back(Pair("blockHash", blockHash.ToString()));
|
|
|
|
|
2018-10-30 10:59:32 +01:00
|
|
|
CDataStream ssCbTxMerkleTree(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ssCbTxMerkleTree << cbTxMerkleTree;
|
|
|
|
obj.push_back(Pair("cbTxMerkleTree", HexStr(ssCbTxMerkleTree.begin(), ssCbTxMerkleTree.end())));
|
|
|
|
|
|
|
|
obj.push_back(Pair("cbTx", EncodeHexTx(*cbTx)));
|
|
|
|
|
2018-04-09 14:49:34 +02:00
|
|
|
UniValue deletedMNsArr(UniValue::VARR);
|
|
|
|
for (const auto& h : deletedMNs) {
|
|
|
|
deletedMNsArr.push_back(h.ToString());
|
|
|
|
}
|
|
|
|
obj.push_back(Pair("deletedMNs", deletedMNsArr));
|
|
|
|
|
|
|
|
UniValue mnListArr(UniValue::VARR);
|
|
|
|
for (const auto& e : mnList) {
|
|
|
|
UniValue eObj;
|
|
|
|
e.ToJson(eObj);
|
|
|
|
mnListArr.push_back(eObj);
|
|
|
|
}
|
|
|
|
obj.push_back(Pair("mnList", mnListArr));
|
|
|
|
|
2019-04-04 10:27:17 +02:00
|
|
|
UniValue deletedQuorumsArr(UniValue::VARR);
|
|
|
|
for (const auto& e : deletedQuorums) {
|
|
|
|
UniValue eObj(UniValue::VOBJ);
|
|
|
|
eObj.push_back(Pair("llmqType", e.first));
|
|
|
|
eObj.push_back(Pair("quorumHash", e.second.ToString()));
|
|
|
|
deletedQuorumsArr.push_back(eObj);
|
|
|
|
}
|
|
|
|
obj.push_back(Pair("deletedQuorums", deletedQuorumsArr));
|
|
|
|
|
|
|
|
UniValue newQuorumsArr(UniValue::VARR);
|
|
|
|
for (const auto& e : newQuorums) {
|
|
|
|
UniValue eObj;
|
|
|
|
e.ToJson(eObj);
|
|
|
|
newQuorumsArr.push_back(eObj);
|
|
|
|
}
|
|
|
|
obj.push_back(Pair("newQuorums", newQuorumsArr));
|
|
|
|
|
2018-04-09 14:49:34 +02:00
|
|
|
CCbTx cbTxPayload;
|
|
|
|
if (GetTxPayload(*cbTx, cbTxPayload)) {
|
|
|
|
obj.push_back(Pair("merkleRootMNList", cbTxPayload.merkleRootMNList.ToString()));
|
2019-04-04 10:27:17 +02:00
|
|
|
if (cbTxPayload.nVersion >= 2) {
|
|
|
|
obj.push_back(Pair("merkleRootQuorums", cbTxPayload.merkleRootQuorums.ToString()));
|
|
|
|
}
|
2018-04-09 14:49:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& blockHash, CSimplifiedMNListDiff& mnListDiffRet, std::string& errorRet)
|
|
|
|
{
|
|
|
|
AssertLockHeld(cs_main);
|
|
|
|
mnListDiffRet = CSimplifiedMNListDiff();
|
|
|
|
|
2019-02-12 20:52:06 +01:00
|
|
|
const CBlockIndex* baseBlockIndex = chainActive.Genesis();
|
2018-04-09 14:49:34 +02:00
|
|
|
if (!baseBlockHash.IsNull()) {
|
2019-02-12 20:52:06 +01:00
|
|
|
auto it = mapBlockIndex.find(baseBlockHash);
|
|
|
|
if (it == mapBlockIndex.end()) {
|
|
|
|
errorRet = strprintf("block %s not found", baseBlockHash.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
baseBlockIndex = it->second;
|
2018-04-09 14:49:34 +02:00
|
|
|
}
|
|
|
|
auto blockIt = mapBlockIndex.find(blockHash);
|
|
|
|
if (blockIt == mapBlockIndex.end()) {
|
|
|
|
errorRet = strprintf("block %s not found", blockHash.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-12 20:52:06 +01:00
|
|
|
const CBlockIndex* blockIndex = blockIt->second;
|
2018-04-09 14:49:34 +02:00
|
|
|
|
2019-02-12 20:52:06 +01:00
|
|
|
if (!chainActive.Contains(baseBlockIndex) || !chainActive.Contains(blockIndex)) {
|
2018-04-09 14:49:34 +02:00
|
|
|
errorRet = strprintf("block %s and %s are not in the same chain", baseBlockHash.ToString(), blockHash.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-12 20:52:06 +01:00
|
|
|
if (baseBlockIndex->nHeight > blockIndex->nHeight) {
|
2018-04-09 14:49:34 +02:00
|
|
|
errorRet = strprintf("base block %s is higher then block %s", baseBlockHash.ToString(), blockHash.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCK(deterministicMNManager->cs);
|
|
|
|
|
2019-07-09 07:59:57 +02:00
|
|
|
auto baseDmnList = deterministicMNManager->GetListForBlock(baseBlockIndex);
|
|
|
|
auto dmnList = deterministicMNManager->GetListForBlock(blockIndex);
|
2018-11-15 10:42:39 +01:00
|
|
|
mnListDiffRet = baseDmnList.BuildSimplifiedDiff(dmnList);
|
2018-04-09 14:49:34 +02:00
|
|
|
|
2019-07-09 07:59:57 +02:00
|
|
|
// We need to return the value that was provided by the other peer as it otherwise won't be able to recognize the
|
|
|
|
// response. This will usually be identical to the block found in baseBlockIndex. The only difference is when a
|
|
|
|
// null block hash was provided to get the diff from the genesis block.
|
|
|
|
mnListDiffRet.baseBlockHash = baseBlockHash;
|
|
|
|
|
2019-04-04 10:27:17 +02:00
|
|
|
if (!mnListDiffRet.BuildQuorumsDiff(baseBlockIndex, blockIndex)) {
|
|
|
|
errorRet = strprintf("failed to build quorums diff");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-09 14:49:34 +02:00
|
|
|
// TODO store coinbase TX in CBlockIndex
|
|
|
|
CBlock block;
|
2019-02-12 20:52:06 +01:00
|
|
|
if (!ReadBlockFromDisk(block, blockIndex, Params().GetConsensus())) {
|
2018-04-09 14:49:34 +02:00
|
|
|
errorRet = strprintf("failed to read block %s from disk", blockHash.ToString());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mnListDiffRet.cbTx = block.vtx[0];
|
|
|
|
|
|
|
|
std::vector<uint256> vHashes;
|
|
|
|
std::vector<bool> vMatch(block.vtx.size(), false);
|
|
|
|
for (const auto& tx : block.vtx) {
|
|
|
|
vHashes.emplace_back(tx->GetHash());
|
|
|
|
}
|
|
|
|
vMatch[0] = true; // only coinbase matches
|
|
|
|
mnListDiffRet.cbTxMerkleTree = CPartialMerkleTree(vHashes, vMatch);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|