From 45f34e130f91cf139f376046b81e019929160894 Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Mon, 17 Dec 2018 14:24:48 +0100 Subject: [PATCH] Implement HasValidMN, HasValidMNByCollateral and GetValidMNByCollateral --- src/evo/deterministicmns.cpp | 23 +++++++++-------------- src/evo/deterministicmns.h | 17 +++++++++++++---- src/masternodeman.cpp | 2 +- src/wallet/wallet.cpp | 11 ++++++++--- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 8f1140fd97ab1..0f79db6667b01 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -151,6 +151,15 @@ CDeterministicMNCPtr CDeterministicMNList::GetMNByCollateral(const COutPoint& co return GetUniquePropertyMN(collateralOutpoint); } +CDeterministicMNCPtr CDeterministicMNList::GetValidMNByCollateral(const COutPoint& collateralOutpoint) const +{ + auto dmn = GetMNByCollateral(collateralOutpoint); + if (dmn && !IsMNValid(dmn)) { + return nullptr; + } + return dmn; +} + static int CompareByLastPaid_GetHeight(const CDeterministicMN& dmn) { int height = dmn.pdmnState->nLastPaidHeight; @@ -826,20 +835,6 @@ CDeterministicMNList CDeterministicMNManager::GetListAtChainTip() return GetListForBlock(tipBlockHash); } -bool CDeterministicMNManager::HasValidMNCollateralAtChainTip(const COutPoint& outpoint) -{ - auto mnList = GetListAtChainTip(); - auto dmn = mnList.GetMNByCollateral(outpoint); - return dmn && mnList.IsMNValid(dmn); -} - -bool CDeterministicMNManager::HasMNCollateralAtChainTip(const COutPoint& outpoint) -{ - auto mnList = GetListAtChainTip(); - auto dmn = mnList.GetMNByCollateral(outpoint); - return dmn != nullptr; -} - bool CDeterministicMNManager::IsProTxWithCollateral(const CTransactionRef& tx, uint32_t n) { if (tx->nVersion != 3 || tx->nType != TRANSACTION_PROVIDER_REGISTER) { diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index 48729c0b92473..6a8469b117193 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -289,10 +289,23 @@ class CDeterministicMNList { return GetMN(proTxHash) != nullptr; } + bool HasValidMN(const uint256& proTxHash) const + { + return GetValidMN(proTxHash) != nullptr; + } + bool HasMNByCollateral(const COutPoint& collateralOutpoint) const + { + return GetMNByCollateral(collateralOutpoint) != nullptr; + } + bool HasValidMNByCollateral(const COutPoint& collateralOutpoint) const + { + return GetValidMNByCollateral(collateralOutpoint) != nullptr; + } CDeterministicMNCPtr GetMN(const uint256& proTxHash) const; CDeterministicMNCPtr GetValidMN(const uint256& proTxHash) const; CDeterministicMNCPtr GetMNByOperatorKey(const CBLSPublicKey& pubKey); CDeterministicMNCPtr GetMNByCollateral(const COutPoint& collateralOutpoint) const; + CDeterministicMNCPtr GetValidMNByCollateral(const COutPoint& collateralOutpoint) const; CDeterministicMNCPtr GetMNPayee() const; /** @@ -479,10 +492,6 @@ class CDeterministicMNManager CDeterministicMNList GetListForBlock(const uint256& blockHash); CDeterministicMNList GetListAtChainTip(); - // TODO remove after removal of old non-deterministic lists - bool HasValidMNCollateralAtChainTip(const COutPoint& outpoint); - bool HasMNCollateralAtChainTip(const COutPoint& outpoint); - // Test if given TX is a ProRegTx which also contains the collateral at index n bool IsProTxWithCollateral(const CTransactionRef& tx, uint32_t n); diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index a6d4890164faa..9fb09bae5153d 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -643,7 +643,7 @@ bool CMasternodeMan::Has(const COutPoint& outpoint) { LOCK(cs); if (deterministicMNManager->IsDIP3Active()) { - return deterministicMNManager->HasValidMNCollateralAtChainTip(outpoint); + return deterministicMNManager->GetListAtChainTip().HasValidMNByCollateral(outpoint); } else { return mapMasternodes.find(outpoint) != mapMasternodes.end(); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 553a912d7beba..d0982ba94a2e8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1121,10 +1121,11 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose) } AddToSpends(hash); + auto mnList = deterministicMNManager->GetListAtChainTip(); for(unsigned int i = 0; i < wtx.tx->vout.size(); ++i) { if (IsMine(wtx.tx->vout[i]) && !IsSpent(hash, i)) { setWalletUTXO.insert(COutPoint(hash, i)); - if (deterministicMNManager->IsProTxWithCollateral(wtx.tx, i) || deterministicMNManager->HasMNCollateralAtChainTip(COutPoint(hash, i))) { + if (deterministicMNManager->IsProTxWithCollateral(wtx.tx, i) || mnList.HasMNByCollateral(COutPoint(hash, i))) { LockCoin(COutPoint(hash, i)); } } @@ -3989,11 +3990,13 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet) // This avoids accidential spending of collaterals. They can still be unlocked manually if a spend is really intended. void CWallet::AutoLockMasternodeCollaterals() { + auto mnList = deterministicMNManager->GetListAtChainTip(); + LOCK2(cs_main, cs_wallet); for (const auto& pair : mapWallet) { for (unsigned int i = 0; i < pair.second.tx->vout.size(); ++i) { if (IsMine(pair.second.tx->vout[i]) && !IsSpent(pair.first, i)) { - if (deterministicMNManager->IsProTxWithCollateral(pair.second.tx, i) || deterministicMNManager->HasMNCollateralAtChainTip(COutPoint(pair.first, i))) { + if (deterministicMNManager->IsProTxWithCollateral(pair.second.tx, i) || mnList.HasMNByCollateral(COutPoint(pair.first, i))) { LockCoin(COutPoint(pair.first, i)); } } @@ -4643,11 +4646,13 @@ void CWallet::ListLockedCoins(std::vector& vOutpts) void CWallet::ListProTxCoins(std::vector& vOutpts) { + auto mnList = deterministicMNManager->GetListAtChainTip(); + AssertLockHeld(cs_wallet); for (const auto &o : setWalletUTXO) { if (mapWallet.count(o.hash)) { const auto &p = mapWallet[o.hash]; - if (deterministicMNManager->IsProTxWithCollateral(p.tx, o.n) || deterministicMNManager->HasMNCollateralAtChainTip(o)) { + if (deterministicMNManager->IsProTxWithCollateral(p.tx, o.n) || mnList.HasMNByCollateral(o)) { vOutpts.emplace_back(o); } }