Dash Core Source Documentation (0.16.0.1)
Find detailed information regarding the Dash Core source code.
deterministicmns.cpp
Go to the documentation of this file.
38 return strprintf("CDeterministicMNState(nRegisteredHeight=%d, nLastPaidHeight=%d, nPoSePenalty=%d, nPoSeRevivedHeight=%d, nPoSeBanHeight=%d, nRevocationReason=%d, "
39 "ownerAddress=%s, pubKeyOperator=%s, votingAddress=%s, addr=%s, payoutAddress=%s, operatorPayoutAddress=%s)",
40 nRegisteredHeight, nLastPaidHeight, nPoSePenalty, nPoSeRevivedHeight, nPoSeBanHeight, nRevocationReason,
41 EncodeDestination(keyIDOwner), pubKeyOperator.Get().ToString(), EncodeDestination(keyIDVoting), addr.ToStringIPPort(false), payoutAddress, operatorPayoutAddress);
77 return strprintf("CDeterministicMN(proTxHash=%s, collateralOutpoint=%s, nOperatorReward=%f, state=%s", proTxHash.ToString(), collateralOutpoint.ToStringShort(), (double)nOperatorReward / 100, pdmnState->ToString());
162 CDeterministicMNCPtr CDeterministicMNList::GetMNByCollateral(const COutPoint& collateralOutpoint) const
167 CDeterministicMNCPtr CDeterministicMNList::GetValidMNByCollateral(const COutPoint& collateralOutpoint) const
244 std::sort(result.begin(), result.end(), [&](const CDeterministicMNCPtr& a, const CDeterministicMNCPtr& b) {
253 std::vector<CDeterministicMNCPtr> CDeterministicMNList::CalculateQuorum(size_t maxSize, const uint256& modifier) const
258 std::sort(scores.rbegin(), scores.rend(), [](const std::pair<arith_uint256, CDeterministicMNCPtr>& a, std::pair<arith_uint256, CDeterministicMNCPtr>& b) {
260 // this should actually never happen, but we should stay compatible with how the non deterministic MNs did the sorting
275 std::vector<std::pair<arith_uint256, CDeterministicMNCPtr>> CDeterministicMNList::CalculateScores(const uint256& modifier) const
281 // we only take confirmed MNs into account to avoid hash grinding on the ProRegTxHash to sneak MNs into a
288 // TODO When https://github.com/bitcoin/bitcoin/pull/13191 gets backported, implement something that is similar but for single-sha256
291 sha256.Write(dmn->pdmnState->confirmedHashWithProRegTxHash.begin(), dmn->pdmnState->confirmedHashWithProRegTxHash.size());
321 throw(std::runtime_error(strprintf("%s: Can't find a masternode with proTxHash=%s", __func__, proTxHash.ToString())));
332 __func__, proTxHash.ToString(), dmn->pdmnState->nPoSePenalty, newState->nPoSePenalty, maxPenalty);
349 throw(std::runtime_error(strprintf("%s: Can't find a masternode with proTxHash=%s", __func__, proTxHash.ToString())));
380 // added MNs need to be sorted by internalId so that these are added in correct order when the diff is applied later
382 std::sort(diffRet.addedMNs.begin(), diffRet.addedMNs.end(), [](const CDeterministicMNCPtr& a, const CDeterministicMNCPtr& b) {
389 CSimplifiedMNListDiff CDeterministicMNList::BuildSimplifiedDiff(const CDeterministicMNList& to) const
417 CDeterministicMNList CDeterministicMNList::ApplyDiff(const CBlockIndex* pindex, const CDeterministicMNListDiff& diff) const
426 throw(std::runtime_error(strprintf("%s: can't find a removed masternode, id=%d", __func__, id)));
446 throw(std::runtime_error(strprintf("%s: can't add a duplicate masternode with the same proTxHash=%s", __func__, dmn->proTxHash.ToString())));
449 throw(std::runtime_error(strprintf("%s: can't add a duplicate masternode with the same internalId=%d", __func__, dmn->GetInternalId())));
452 throw(std::runtime_error(strprintf("%s: can't add a masternode with a duplicate address %s", __func__, dmn->pdmnState->addr.ToStringIPPort(false))));
454 if (HasUniqueProperty(dmn->pdmnState->keyIDOwner) || HasUniqueProperty(dmn->pdmnState->pubKeyOperator)) {
455 throw(std::runtime_error(strprintf("%s: can't add a masternode with a duplicate key (%s or %s)", __func__, EncodeDestination(dmn->pdmnState->keyIDOwner), dmn->pdmnState->pubKeyOperator.Get().ToString())));
474 void CDeterministicMNList::UpdateMN(const CDeterministicMNCPtr& oldDmn, const CDeterministicMNStateCPtr& pdmnState)
478 if (HasUniqueProperty(oldDmn->pdmnState->addr) && GetUniquePropertyMN(oldDmn->pdmnState->addr)->proTxHash != oldDmn->proTxHash) {
479 throw(std::runtime_error(strprintf("%s: can't update a masternode with a duplicate address %s", __func__, oldDmn->pdmnState->addr.ToStringIPPort(false))));
492 void CDeterministicMNList::UpdateMN(const uint256& proTxHash, const CDeterministicMNStateCPtr& pdmnState)
496 throw(std::runtime_error(strprintf("%s: Can't find a masternode with proTxHash=%s", __func__, proTxHash.ToString())));
501 void CDeterministicMNList::UpdateMN(const CDeterministicMNCPtr& oldDmn, const CDeterministicMNStateDiff& stateDiff)
514 throw(std::runtime_error(strprintf("%s: Can't find a masternode with proTxHash=%s", __func__, proTxHash.ToString())));
533 bool CDeterministicMNManager::ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& _state, bool fJustCheck)
573 LogPrintf("CDeterministicMNManager::%s -- Wrote snapshot. nHeight=%d, mapCurMNs.allMNsCount=%d\n",
591 if (!consensusParams.DIP0003EnforcementHash.IsNull() && consensusParams.DIP0003EnforcementHash != pindex->GetBlockHash()) {
592 LogPrintf("CDeterministicMNManager::%s -- DIP3 enforcement block has wrong hash: hash=%s, expected=%s, nHeight=%d\n", __func__,
596 LogPrintf("CDeterministicMNManager::%s -- DIP3 is enforced now. nHeight=%d\n", __func__, nHeight);
635 LogPrintf("CDeterministicMNManager::%s -- DIP3 is not enforced anymore. nHeight=%d\n", __func__, nHeight);
648 bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, CValidationState& _state, CDeterministicMNList& mnListRet, bool debugLogs)
656 newList.SetBlockHash(uint256()); // we can't know the final block hash, so better not return a (invalid) block hash
662 // this is only valid as long these have not diverged at this point, which is the case as long as we don't add
669 // this works on the previous block, so confirmation will happen one block after nMasternodeMinimumConfirmations
670 // has been reached, but the block hash will then point to the block at nMasternodeMinimumConfirmations
707 if (!proTx.collateralOutpoint.hash.IsNull() && (!GetUTXOCoin(dmn->collateralOutpoint, coin) || coin.out.nValue != 1000 * COIN)) {
717 // and the new one is added like a completely fresh one, which is also at the bottom of the payment list
720 LogPrintf("CDeterministicMNManager::%s -- MN %s removed from list because collateral was used for a new ProRegTx. collateralOutpoint=%s, nHeight=%d, mapCurMNs.allMNsCount=%d\n",
721 __func__, replacedDmn->proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort(), nHeight, newList.GetAllMNsCount());
728 if (newList.HasUniqueProperty(proTx.keyIDOwner) || newList.HasUniqueProperty(proTx.pubKeyOperator)) {
754 if (newList.HasUniqueProperty(proTx.addr) && newList.GetUniquePropertyMN(proTx.addr)->proTxHash != proTx.proTxHash) {
768 if (newState->pubKeyOperator.Get().IsValid() && !newState->keyIDVoting.IsNull() && !newState->keyIDOwner.IsNull()) {
797 // reset all operator related fields and put MN into PoSe-banned state in case the operator key changes
842 // we should actually never get into this case as validation should have catched it...but lets be sure
862 LogPrintf("CDeterministicMNManager::%s -- MN %s removed from list because collateral was spent. collateralOutpoint=%s, nHeight=%d, mapCurMNs.allMNsCount=%d\n",
863 __func__, dmn->proTxHash.ToString(), dmn->collateralOutpoint.ToStringShort(), nHeight, newList.GetAllMNsCount());
869 // The payee for the current block was determined by the previous block's list but it might have disappeared in the
872 auto newState = std::make_shared<CDeterministicMNState>(*newList.GetMN(payee->proTxHash)->pdmnState);
882 void CDeterministicMNManager::HandleQuorumCommitment(llmq::CFinalCommitment& qc, const CBlockIndex* pindexQuorum, CDeterministicMNList& mnList, bool debugLogs)
886 auto members = llmq::CLLMQUtils::GetAllQuorumMembers((Consensus::LLMQType)qc.llmqType, pindexQuorum);
894 // The idea is to immediately ban a MN when it fails 2 DKG sessions with only a few blocks in-between
895 // If there were enough blocks between failures, the MN has a chance to recover as he reduces his penalty by 1 for every block
896 // If it however fails 3 times in the timespan of a single payment cycle, it should definitely get banned
978 if ((snapshot.GetHeight() % p_llmq.second.dkgInterval == 0) && (snapshot.GetHeight() + p_llmq.second.dkgInterval * (p_llmq.second.keepOldConnections + 1) >= tipIndex->nHeight)) {
1044 if ((p.second.GetHeight() % p_llmq.second.dkgInterval == 0) && (p.second.GetHeight() + p_llmq.second.dkgInterval * (p_llmq.second.keepOldConnections + 1) >= nHeight)) {
1078 void CDeterministicMNManager::UpgradeDiff(CDBBatch& batch, const CBlockIndex* pindexNext, const CDeterministicMNList& curMNList, CDeterministicMNList& newMNList)
1081 if (!evoDb.GetRawDB().ReadDataStream(std::make_pair(DB_LIST_DIFF, pindexNext->GetBlockHash()), oldDiffData)) {
1082 LogPrintf("CDeterministicMNManager::%s -- no diff found for %s\n", __func__, pindexNext->GetBlockHash().ToString());
1095 auto dmn = std::make_shared<CDeterministicMN>(*p.second, curMNList.GetTotalRegisteredCount() + addedCount);
1111 throw(std::runtime_error(strprintf("%s: Can't find an old masternode with proTxHash=%s", __func__, p.first.ToString())));
1116 throw(std::runtime_error(strprintf("%s: Can't find a new masternode with proTxHash=%s", __func__, p.first.ToString())));
1142 // Removing the old EVODB_BEST_BLOCK value early results in older version to crash immediately, even if the upgrade
1143 // process is cancelled in-between. But if the new version sees that the old EVODB_BEST_BLOCK is already removed,
1144 // then we must assume that the upgrade process was already running before but was interrupted.
1164 curMNList.SetBlockHash(chainActive[Params().GetConsensus().DIP0003Height - 1]->GetBlockHash());
1166 for (int nHeight = Params().GetConsensus().DIP0003Height; nHeight <= chainActive.Height(); nHeight++) {
int CalcMaxPoSePenalty() const
Calculates the maximum penalty which is allowed at the height of this MN list.
Definition: deterministicmns.cpp:301
bool GetTxPayload(const std::vector< unsigned char > &payload, T &obj)
Definition: specialtx.h:21
CDeterministicMNList GetListAtChainTip()
Definition: deterministicmns.cpp:989
bool GetBlockHash(uint256 &hashRet, int nBlockHeight)
Return true if hash can be found in chainActive at nBlockHeight height.
Definition: validation.cpp:1894
CDeterministicMNManager(CEvoDB &_evoDb)
Definition: deterministicmns.cpp:528
CDeterministicMNCPtr GetValidMN(const uint256 &proTxHash) const
Definition: deterministicmns.cpp:143
Definition: providertx.h:187
void SetBlockHash(const uint256 &_blockHash)
Definition: deterministicmns.h:380
void PoSePunish(const uint256 &proTxHash, int penalty, bool debugLogs)
Punishes a MN for misbehavior.
Definition: deterministicmns.cpp:315
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:80
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:158
void UpdatedBlockTip(const CBlockIndex *pindex)
Definition: deterministicmns.cpp:641
CDeterministicMNCPtr GetUniquePropertyMN(const T &v) const
Definition: deterministicmns.h:489
Definition: quorums_commitment.h:24
Definition: simplifiedmns.h:106
Definition: evodb.h:32
Definition: deterministicmns.h:547
Definition: block.h:72
CDeterministicMNCPtr GetMN(const uint256 &proTxHash) const
Definition: deterministicmns.cpp:134
size_t GetValidMNsCount() const
Definition: deterministicmns.h:354
Definition: transaction.h:17
CScript scriptOperatorPayout
Definition: deterministicmns.h:52
void UpdateMN(const CDeterministicMNCPtr &oldDmn, const CDeterministicMNStateCPtr &pdmnState)
Definition: deterministicmns.cpp:474
void ApplyToState(CDeterministicMNState &target) const
Definition: deterministicmns.h:181
std::shared_ptr< const CDeterministicMNState > CDeterministicMNStateCPtr
Definition: deterministicmns.h:118
std::vector< CDeterministicMNCPtr > GetProjectedMNPayees(int nCount) const
Calculates the projected MN payees for the next count blocks.
Definition: deterministicmns.cpp:232
void UpgradeDiff(CDBBatch &batch, const CBlockIndex *pindexNext, const CDeterministicMNList &curMNList, CDeterministicMNList &newMNList)
Definition: deterministicmns.cpp:1078
void AddMN(const CDeterministicMNCPtr &dmn, bool fBumpTotalCount=true)
Definition: deterministicmns.cpp:441
Definition: univalue.h:20
bool DoS(int level, bool ret=false, unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="", bool corruptionIn=false, const std::string &strDebugMessageIn="")
Definition: validation.h:36
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
bool IsMNValid(const uint256 &proTxHash) const
Definition: deterministicmns.cpp:104
std::shared_ptr< const CDeterministicMN > CDeterministicMNCPtr
Definition: deterministicmns.h:249
CDeterministicMNListDiff BuildDiff(const CDeterministicMNList &to) const
Definition: deterministicmns.cpp:358
bool IsDIP3Enforced(int nHeight=-1)
Definition: deterministicmns.cpp:1020
void AddUniqueProperty(const CDeterministicMNCPtr &dmn, const T &v)
Definition: deterministicmns.h:500
void ForEachMN(bool onlyValid, Callback &&cb) const
Definition: deterministicmns.h:366
std::unique_ptr< CDeterministicMNManager > deterministicMNManager
Definition: deterministicmns.cpp:24
bool ReadDataStream(const K &key, CDataStream &ssValue) const
Definition: dbwrapper.h:246
void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList &oldMNList, const CDeterministicMNListDiff &diff)
Definition: validationinterface.cpp:246
uint32_t nTotalRegisteredCount
Definition: deterministicmns.h:298
Definition: simplifiedmns.h:24
Definition: transaction.h:22
std::vector< CDeterministicMNCPtr > CalculateQuorum(size_t maxSize, const uint256 &modifier) const
Calculate a quorum based on the modifier.
Definition: deterministicmns.cpp:253
CDeterministicMNCPtr GetMNByInternalId(uint64_t internalId) const
Definition: deterministicmns.cpp:181
CDeterministicMNCPtr GetValidMNByCollateral(const COutPoint &collateralOutpoint) const
Definition: deterministicmns.cpp:167
CDeterministicMNCPtr GetMNPayee() const
Definition: deterministicmns.cpp:216
static const int DISK_SNAPSHOT_PERIOD
Definition: deterministicmns.h:639
Definition: deterministicmns.h:288
const uint256 & GetBlockHash() const
Definition: deterministicmns.h:376
std::unordered_map< uint256, CDeterministicMNList, StaticSaltedHasher > mnListsCache
Definition: deterministicmns.h:649
CFinalCommitment commitment
Definition: quorums_commitment.h:117
Definition: bls.h:263
void HandleQuorumCommitment(llmq::CFinalCommitment &qc, const CBlockIndex *pindexQuorum, CDeterministicMNList &mnList, bool debugLogs)
Definition: deterministicmns.cpp:882
void UpdateUniqueProperty(const CDeterministicMNCPtr &dmn, const T &oldValue, const T &newValue)
Definition: deterministicmns.h:530
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
static const int LIST_DIFFS_CACHE_SIZE
Definition: deterministicmns.h:641
void PoSeDecrease(const uint256 &proTxHash)
Decrease penalty score of MN by 1.
Definition: deterministicmns.cpp:345
boost::signals2::signal< void(const CDeterministicMNList &)> NotifyMasternodeListChanged
Masternode list has changed.
Definition: ui_interface.h:111
uint32_t GetTotalRegisteredCount() const
Definition: deterministicmns.h:392
Definition: deterministicmns.h:189
std::unique_ptr< CEvoDBScopedCommitter > BeginTransaction()
Definition: evodb.h:49
static std::pair< std::string, UniValue > Pair(const char *cKey, const char *cVal)
Definition: univalue.h:185
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
bool BuildNewListFromBlock(const CBlock &block, const CBlockIndex *pindexPrev, CValidationState &state, CDeterministicMNList &mnListRet, bool debugLogs)
Definition: deterministicmns.cpp:648
bool IsProTxWithCollateral(const CTransactionRef &tx, uint32_t n)
Definition: deterministicmns.cpp:998
bool IsMNPoSeBanned(const uint256 &proTxHash) const
Definition: deterministicmns.cpp:113
Definition: deterministicmns.h:31
void DeleteUniqueProperty(const CDeterministicMNCPtr &dmn, const T &oldValue)
Definition: deterministicmns.h:515
int DIP0003EnforcementHeight
Block height at which DIP0003 becomes enforced.
Definition: params.h:160
Definition: providertx.h:135
CBLSLazyPublicKey pubKeyOperator
Definition: deterministicmns.h:48
Definition: deterministicmns.h:120
CDeterministicMNCPtr GetMNByOperatorKey(const CBLSPublicKey &pubKey)
Definition: deterministicmns.cpp:152
void CleanupCache(int nHeight)
Definition: deterministicmns.cpp:1031
std::string EncodeDestination(const CTxDestination &dest)
Definition: base58.cpp:329
bool HasMN(const uint256 &proTxHash) const
Definition: deterministicmns.h:402
static int CompareByLastPaid_GetHeight(const CDeterministicMN &dmn)
Definition: deterministicmns.cpp:190
CSimplifiedMNListDiff BuildSimplifiedDiff(const CDeterministicMNList &to) const
Definition: deterministicmns.cpp:389
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:947
bool UpgradeDBIfNeeded()
Definition: deterministicmns.cpp:1130
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:453
void DecreasePoSePenalties(CDeterministicMNList &mnList)
Definition: deterministicmns.cpp:902
bool HasUniqueProperty(const T &v) const
Definition: deterministicmns.h:484
bool ProcessBlock(const CBlock &block, const CBlockIndex *pindex, CValidationState &state, bool fJustCheck)
Definition: deterministicmns.cpp:533
std::string ToStringIPPort(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:572
MnInternalIdMap mnInternalIdMap
Definition: deterministicmns.h:300
std::vector< CDeterministicMNCPtr > addedMNs
Definition: deterministicmns.h:552
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:157
CDeterministicMNList ApplyDiff(const CBlockIndex *pindex, const CDeterministicMNListDiff &diff) const
Definition: deterministicmns.cpp:417
bool UndoBlock(const CBlock &block, const CBlockIndex *pindex)
Definition: deterministicmns.cpp:605
Definition: providertx.h:88
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
CDeterministicMNList GetListForBlock(const CBlockIndex *pindex)
Definition: deterministicmns.cpp:919
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:54
std::set< uint64_t > removedMns
Definition: deterministicmns.h:555
Definition: providertx.h:19
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:110
int CalcPenalty(int percent) const
Returns a the given percentage from the max penalty for this MN list.
Definition: deterministicmns.cpp:309
CChain & chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:217
std::vector< std::pair< arith_uint256, CDeterministicMNCPtr > > CalculateScores(const uint256 &modifier) const
Definition: deterministicmns.cpp:275
AssertLockHeld(g_cs_orphans)
CDeterministicMNCPtr GetMNByCollateral(const COutPoint &collateralOutpoint) const
Definition: deterministicmns.cpp:162
static const int CLIENT_VERSION
dashd-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:38
std::map< uint64_t, CDeterministicMNStateDiff > updatedMNs
Definition: deterministicmns.h:554
static bool CompareByLastPaid(const CDeterministicMN &_a, const CDeterministicMN &_b)
Definition: deterministicmns.cpp:201
void RemoveMN(const uint256 &proTxHash)
Definition: deterministicmns.cpp:510
std::vector< bool > validMembers
Definition: quorums_commitment.h:34
std::unordered_map< uint256, CDeterministicMNListDiff, StaticSaltedHasher > mnListDiffsCache
Definition: deterministicmns.h:650
std::vector< CSimplifiedMNListEntry > mnList
Definition: simplifiedmns.h:114
int nMasternodeMinimumConfirmations
Definition: params.h:147
static std::vector< CDeterministicMNCPtr > GetAllQuorumMembers(Consensus::LLMQType llmqType, const CBlockIndex *pindexQuorum)
Definition: quorums_utils.cpp:18
Definition: serialize.h:161
CDeterministicMNCPtr GetMNByService(const CService &service) const
Definition: deterministicmns.cpp:176