From 38b92b99752e26f0422f1995ff836671149e9b3f Mon Sep 17 00:00:00 2001 From: furszy Date: Fri, 14 Jan 2022 12:51:15 -0300 Subject: [PATCH] init: improve dmn collateral locking. 1) Walk-through the DMN list only once instead of one-time per wallet. 2) Remove wallet dependency on evo/deterministicmns.h. --- src/init.cpp | 9 ++++++--- src/wallet/wallet.cpp | 18 ++++++------------ src/wallet/wallet.h | 9 +++------ test/lint/lint-circular-dependencies.sh | 1 - 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 57a874866382e..d5f1097cb88e9 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1750,10 +1750,13 @@ bool AppInitMain() // automatic lock for DMN if (gArgs.GetBoolArg("-mnconflock", DEFAULT_MNCONFLOCK)) { + LogPrintf("Locking masternode collaterals...\n"); const auto& mnList = deterministicMNManager->GetListAtChainTip(); - for (CWallet* pwallet : vpwallets) { - pwallet->ScanMasternodeCollateralsAndLock(mnList); - } + mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) { + for (CWallet* pwallet : vpwallets) { + pwallet->LockOutpointIfMineWithMutex(nullptr, dmn->collateralOutpoint); + } + }); } #endif diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5d953d1b55422..32331ce3a57c0 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -13,7 +13,7 @@ #include "checkpoints.h" #include "coincontrol.h" -#include "evo/deterministicmns.h" +#include "evo/providertx.h" #include "guiinterfaceutil.h" #include "masternode.h" #include "policy/policy.h" @@ -4121,6 +4121,11 @@ void CWallet::AutoCombineDust(CConnman* connman) } } +void CWallet::LockOutpointIfMineWithMutex(const CTransactionRef& ptx, const COutPoint& c) +{ + WITH_LOCK(cs_wallet, LockOutpointIfMine(ptx, c)); +} + void CWallet::LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c) { AssertLockHeld(cs_wallet); @@ -4140,17 +4145,6 @@ void CWallet::LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c) } } -// Called during Init -void CWallet::ScanMasternodeCollateralsAndLock(const CDeterministicMNList& mnList) -{ - LOCK(cs_wallet); - - LogPrintf("Locking masternode collaterals...\n"); - mnList.ForEachMN(false, [&](const CDeterministicMNCPtr& dmn) { - LockOutpointIfMine(nullptr, dmn->collateralOutpoint); - }); -} - // Called from AddToWalletIfInvolvingMe void CWallet::LockIfMyCollateral(const CTransactionRef& ptx) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index c6370891f5d9e..b6da321ee222c 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -99,7 +99,6 @@ class ScriptPubKeyMan; class SaplingScriptPubKeyMan; class SaplingNoteData; struct SaplingNoteEntry; -class CDeterministicMNList; /** (client) version numbers for particular wallet features */ enum WalletFeature { @@ -886,13 +885,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface * -- If ptx is null, c is the output of a transaction in mapWallet */ void LockOutpointIfMine(const CTransactionRef& ptx, const COutPoint& c); - /* - * Locks cs_wallet - * Called during Init. If a DMN collateral is found in the wallet, - * lock the corresponding coin, to prevent accidental spending. + * Same functionality as above but locking the cs_wallet mutex internally. + * future: add capability to lock the mutex from outside of this class without exposing it. */ - void ScanMasternodeCollateralsAndLock(const CDeterministicMNList& mnList); + void LockOutpointIfMineWithMutex(const CTransactionRef& ptx, const COutPoint& c); /* * Requires cs_wallet lock. diff --git a/test/lint/lint-circular-dependencies.sh b/test/lint/lint-circular-dependencies.sh index e6b0515317a90..8cdbe498b2cff 100755 --- a/test/lint/lint-circular-dependencies.sh +++ b/test/lint/lint-circular-dependencies.sh @@ -43,7 +43,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=( "chain -> legacy/stakemodifier -> stakeinput -> chain" "chain -> legacy/stakemodifier -> validation -> chain" "chainparamsbase -> util/system -> logging -> chainparamsbase" - "evo/deterministicmns -> masternode -> wallet/wallet -> evo/deterministicmns" "kernel -> stakeinput -> wallet/wallet -> kernel" "legacy/validation_zerocoin_legacy -> wallet/wallet -> validation -> legacy/validation_zerocoin_legacy" "qt/askpassphrasedialog -> qt/pivx/pivxgui -> qt/pivx/topbar -> qt/askpassphrasedialog"