Skip to content

Commit

Permalink
init: improve dmn collateral locking.
Browse files Browse the repository at this point in the history
1) Walk-through the DMN list only once instead of one-time per wallet.
2) Remove wallet dependency on evo/deterministicmns.h.
  • Loading branch information
furszy committed Jan 19, 2022
1 parent 918db4e commit 38b92b9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
9 changes: 6 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 6 additions & 12 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down
9 changes: 3 additions & 6 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class ScriptPubKeyMan;
class SaplingScriptPubKeyMan;
class SaplingNoteData;
struct SaplingNoteEntry;
class CDeterministicMNList;

/** (client) version numbers for particular wallet features */
enum WalletFeature {
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion test/lint/lint-circular-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 38b92b9

Please sign in to comment.