2018-04-06 11:00:10 +02:00
|
|
|
// Copyright (c) 2017 The Dash Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "cbtx.h"
|
|
|
|
#include "specialtx.h"
|
|
|
|
#include "deterministicmns.h"
|
2018-04-09 10:35:43 +02:00
|
|
|
#include "simplifiedmns.h"
|
2018-04-06 11:00:10 +02:00
|
|
|
|
|
|
|
#include "validation.h"
|
|
|
|
#include "univalue.h"
|
|
|
|
|
|
|
|
bool CheckCbTx(const CTransaction& tx, const CBlockIndex* pindexPrev, CValidationState& state)
|
|
|
|
{
|
|
|
|
AssertLockHeld(cs_main);
|
|
|
|
|
2018-10-26 07:03:14 +02:00
|
|
|
if (!tx.IsCoinBase()) {
|
2018-04-06 11:00:10 +02:00
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-cbtx-invalid");
|
2018-10-26 07:03:14 +02:00
|
|
|
}
|
2018-04-06 11:00:10 +02:00
|
|
|
|
|
|
|
CCbTx cbTx;
|
2018-10-26 07:03:14 +02:00
|
|
|
if (!GetTxPayload(tx, cbTx)) {
|
2018-04-06 11:00:10 +02:00
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-tx-payload");
|
2018-10-26 07:03:14 +02:00
|
|
|
}
|
2018-04-06 11:00:10 +02:00
|
|
|
|
2018-10-26 07:03:14 +02:00
|
|
|
if (cbTx.nVersion > CCbTx::CURRENT_VERSION) {
|
2018-04-06 11:00:10 +02:00
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-cbtx-version");
|
2018-10-26 07:03:14 +02:00
|
|
|
}
|
2018-04-06 11:00:10 +02:00
|
|
|
|
2018-10-26 07:03:14 +02:00
|
|
|
if (pindexPrev && pindexPrev->nHeight + 1 != cbTx.nHeight) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-cbtx-height");
|
2018-04-06 11:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-09 10:35:43 +02:00
|
|
|
// This can only be done after the block has been fully processed, as otherwise we won't have the finished MN list
|
|
|
|
bool CheckCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindex, CValidationState& state)
|
|
|
|
{
|
|
|
|
AssertLockHeld(cs_main);
|
|
|
|
|
2018-10-26 07:03:14 +02:00
|
|
|
if (block.vtx[0]->nType != TRANSACTION_COINBASE) {
|
2018-04-09 10:35:43 +02:00
|
|
|
return true;
|
2018-10-26 07:03:14 +02:00
|
|
|
}
|
2018-04-09 10:35:43 +02:00
|
|
|
|
|
|
|
CCbTx cbTx;
|
2018-10-26 07:03:14 +02:00
|
|
|
if (!GetTxPayload(*block.vtx[0], cbTx)) {
|
2018-04-09 10:35:43 +02:00
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-tx-payload");
|
2018-10-26 07:03:14 +02:00
|
|
|
}
|
2018-04-09 10:35:43 +02:00
|
|
|
|
|
|
|
if (pindex) {
|
|
|
|
uint256 calculatedMerkleRoot;
|
|
|
|
if (!CalcCbTxMerkleRootMNList(block, pindex->pprev, calculatedMerkleRoot, state)) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-cbtx-mnmerkleroot");
|
|
|
|
}
|
|
|
|
if (calculatedMerkleRoot != cbTx.merkleRootMNList) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-cbtx-mnmerkleroot");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev, uint256& merkleRootRet, CValidationState& state)
|
|
|
|
{
|
|
|
|
AssertLockHeld(cs_main);
|
|
|
|
LOCK(deterministicMNManager->cs);
|
|
|
|
|
|
|
|
CDeterministicMNList tmpMNList;
|
|
|
|
if (!deterministicMNManager->BuildNewListFromBlock(block, pindexPrev, state, tmpMNList)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimplifiedMNList sml(tmpMNList);
|
|
|
|
bool mutated = false;
|
|
|
|
merkleRootRet = sml.CalcMerkleRoot(&mutated);
|
|
|
|
return !mutated;
|
|
|
|
}
|
|
|
|
|
2018-04-06 11:00:10 +02:00
|
|
|
std::string CCbTx::ToString() const
|
|
|
|
{
|
|
|
|
return strprintf("CCbTx(nHeight=%d, nVersion=%d, merkleRootMNList=%s)",
|
|
|
|
nVersion, nHeight, merkleRootMNList.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCbTx::ToJson(UniValue& obj) const
|
|
|
|
{
|
|
|
|
obj.clear();
|
|
|
|
obj.setObject();
|
|
|
|
obj.push_back(Pair("version", (int)nVersion));
|
|
|
|
obj.push_back(Pair("height", (int)nHeight));
|
|
|
|
obj.push_back(Pair("merkleRootMNList", merkleRootMNList.ToString()));
|
|
|
|
}
|