Skip to content

Commit 22a24ab

Browse files
Merge pull request dashpay#4801 from kittywhiskers/deglobalization2
backport: bitcoin#18038, bitcoin#14193, bitcoin#17564, bitcoin#17999, bitcoin#18740, bitcoin#19096, bitcoin#16426, bitcoin#17737, bitcoin#18698: deglobalization backports (part 2)
2 parents 7e74de3 + 7f2cfae commit 22a24ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1998
-1116
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ BITCOIN_CORE_H = \
301301
util/memory.h \
302302
util/moneystr.h \
303303
util/ranges.h \
304+
util/ref.h \
304305
util/serfloat.h \
305306
util/settings.h \
306307
util/string.h \
@@ -317,6 +318,7 @@ BITCOIN_CORE_H = \
317318
walletinitinterface.h \
318319
wallet/bdb.h \
319320
wallet/coincontrol.h \
321+
wallet/context.h \
320322
wallet/crypter.h \
321323
wallet/db.h \
322324
wallet/fees.h \
@@ -480,6 +482,7 @@ libdash_wallet_a_SOURCES = \
480482
interfaces/wallet.cpp \
481483
wallet/bdb.cpp \
482484
wallet/coincontrol.cpp \
485+
wallet/context.cpp \
483486
wallet/crypter.cpp \
484487
wallet/db.cpp \
485488
wallet/fees.cpp \

src/Makefile.test.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ BITCOIN_TESTS =\
200200
test/raii_event_tests.cpp \
201201
test/random_tests.cpp \
202202
test/ratecheck_tests.cpp \
203+
test/ref_tests.cpp \
203204
test/reverselock_tests.cpp \
204205
test/rpc_tests.cpp \
205206
test/sanity_tests.cpp \
@@ -228,6 +229,7 @@ BITCOIN_TESTS =\
228229
test/uint256_tests.cpp \
229230
test/util_tests.cpp \
230231
test/validation_block_tests.cpp \
232+
test/validation_chainstatemanager_tests.cpp \
231233
test/validation_flush_tests.cpp \
232234
test/versionbits_tests.cpp
233235

src/coinjoin/client.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ bool CCoinJoinClientSession::SignFinalTransaction(const CTransaction& finalTrans
523523
if (fMasternodeMode || pnode == nullptr) return false;
524524
if (!mixingMasternode) return false;
525525

526-
LOCK(cs_main);
527526
LOCK(mixingWallet.cs_wallet);
528527
LOCK(cs_coinjoin);
529528

@@ -749,7 +748,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
749748
CAmount nBalanceNeedsAnonymized;
750749

751750
{
752-
LOCK2(cs_main, mixingWallet.cs_wallet);
751+
LOCK(mixingWallet.cs_wallet);
753752

754753
if (!fDryRun && mixingWallet.IsLocked(true)) {
755754
strAutoDenomResult = _("Wallet is locked.");
@@ -900,7 +899,7 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
900899
mixingWallet.LockCoin(txin.prevout);
901900
vecOutPointLocked.push_back(txin.prevout);
902901
}
903-
} // LOCK2(cs_main, mixingWallet.cs_wallet);
902+
} // LOCK(mixingWallet.cs_wallet);
904903

905904
// Always attempt to join an existing queue
906905
if (JoinExistingQueue(nBalanceNeedsAnonymized, connman)) {
@@ -1211,7 +1210,7 @@ bool CCoinJoinClientManager::MarkAlreadyJoinedQueueAsTried(CCoinJoinQueue& dsq)
12111210

12121211
bool CCoinJoinClientSession::SubmitDenominate(CConnman& connman)
12131212
{
1214-
LOCK2(cs_main, mixingWallet.cs_wallet);
1213+
LOCK(mixingWallet.cs_wallet);
12151214

12161215
std::string strError;
12171216
std::vector<CTxDSIn> vecTxDSIn;
@@ -1288,7 +1287,6 @@ bool CCoinJoinClientSession::SelectDenominate(std::string& strErrorRet, std::vec
12881287

12891288
bool CCoinJoinClientSession::PrepareDenominate(int nMinRounds, int nMaxRounds, std::string& strErrorRet, const std::vector<CTxDSIn>& vecTxDSIn, std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsRet, bool fDryRun)
12901289
{
1291-
AssertLockHeld(cs_main);
12921290
AssertLockHeld(mixingWallet.cs_wallet);
12931291

12941292
if (!CCoinJoin::IsValidDenomination(nSessionDenom)) {
@@ -1354,7 +1352,7 @@ bool CCoinJoinClientSession::MakeCollateralAmounts()
13541352
{
13551353
if (!CCoinJoinClientOptions::IsEnabled()) return false;
13561354

1357-
LOCK2(cs_main, mixingWallet.cs_wallet);
1355+
LOCK(mixingWallet.cs_wallet);
13581356

13591357
// NOTE: We do not allow txes larger than 100 kB, so we have to limit number of inputs here.
13601358
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
@@ -1391,7 +1389,6 @@ bool CCoinJoinClientSession::MakeCollateralAmounts()
13911389
// Split up large inputs or create fee sized inputs
13921390
bool CCoinJoinClientSession::MakeCollateralAmounts(const CompactTallyItem& tallyItem, bool fTryDenominated)
13931391
{
1394-
AssertLockHeld(cs_main);
13951392
AssertLockHeld(mixingWallet.cs_wallet);
13961393

13971394
if (!CCoinJoinClientOptions::IsEnabled()) return false;
@@ -1484,14 +1481,13 @@ bool CCoinJoinClientSession::MakeCollateralAmounts(const CompactTallyItem& tally
14841481

14851482
bool CCoinJoinClientSession::CreateCollateralTransaction(CMutableTransaction& txCollateral, std::string& strReason)
14861483
{
1487-
auto locked_chain = mixingWallet.chain().lock();
1488-
LOCK(mixingWallet.cs_wallet);
1484+
AssertLockHeld(mixingWallet.cs_wallet);
14891485

14901486
std::vector<COutput> vCoins;
14911487
CCoinControl coin_control;
14921488
coin_control.nCoinType = CoinType::ONLY_COINJOIN_COLLATERAL;
14931489

1494-
mixingWallet.AvailableCoins(*locked_chain, vCoins, true, &coin_control);
1490+
mixingWallet.AvailableCoins(vCoins, true, &coin_control);
14951491

14961492
if (vCoins.empty()) {
14971493
strReason = strprintf("%s requires a collateral transaction and could not locate an acceptable input!", gCoinJoinName);
@@ -1537,7 +1533,7 @@ bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate)
15371533
{
15381534
if (!CCoinJoinClientOptions::IsEnabled()) return false;
15391535

1540-
LOCK2(cs_main, mixingWallet.cs_wallet);
1536+
LOCK(mixingWallet.cs_wallet);
15411537

15421538
// NOTE: We do not allow txes larger than 100 kB, so we have to limit number of inputs here.
15431539
// We still want to consume a lot of inputs to avoid creating only smaller denoms though.
@@ -1568,7 +1564,6 @@ bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate)
15681564
// Create denominations
15691565
bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate, const CompactTallyItem& tallyItem, bool fCreateMixingCollaterals)
15701566
{
1571-
AssertLockHeld(cs_main);
15721567
AssertLockHeld(mixingWallet.cs_wallet);
15731568

15741569
if (!CCoinJoinClientOptions::IsEnabled()) return false;

src/coinjoin/coinjoin.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ std::string CCoinJoinBaseSession::GetStateString() const
214214

215215
bool CCoinJoinBaseSession::IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const
216216
{
217-
AssertLockHeld(cs_main);
218-
219217
std::set<CScript> setScripPubKeys;
220218
nMessageIDRet = MSG_NOERR;
221219
if (fConsumeCollateralRet) *fConsumeCollateralRet = false;
@@ -262,7 +260,7 @@ bool CCoinJoinBaseSession::IsValidInOuts(const std::vector<CTxIn>& vin, const st
262260
nFees -= txout.nValue;
263261
}
264262

265-
CCoinsViewMemPool viewMemPool(&::ChainstateActive().CoinsTip(), mempool);
263+
CCoinsViewMemPool viewMemPool(WITH_LOCK(cs_main, return &::ChainstateActive().CoinsTip()), mempool);
266264

267265
for (const auto& txin : vin) {
268266
LogPrint(BCLog::COINJOIN, "CCoinJoinBaseSession::%s -- txin=%s\n", __func__, txin.ToString());

src/coinjoin/coinjoin.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class CConnman;
2323
class CBLSPublicKey;
2424
class CBlockIndex;
2525

26-
extern CCriticalSection cs_main;
27-
2826
// timeouts
2927
static constexpr int COINJOIN_AUTO_TIMEOUT_MIN = 5;
3028
static constexpr int COINJOIN_AUTO_TIMEOUT_MAX = 15;
@@ -321,7 +319,7 @@ class CCoinJoinBaseSession
321319

322320
void SetNull() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin);
323321

324-
bool IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
322+
bool IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const;
325323

326324
public:
327325
int nSessionDenom{0}; // Users must submit a denom matching this

src/coinjoin/server.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,6 @@ bool CCoinJoinServer::AddEntry(CConnman& connman, const CCoinJoinEntry& entry, P
586586
vin.emplace_back(txin);
587587
}
588588

589-
LOCK(cs_main);
590-
591589
bool fConsumeCollateral{false};
592590
if (!IsValidInOuts(vin, entry.vecTxOut, nMessageIDRet, &fConsumeCollateral)) {
593591
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR! IsValidInOuts() failed: %s\n", __func__, CCoinJoin::GetMessageByID(nMessageIDRet).translated);

src/coinjoin/util.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,11 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
271271
}
272272

273273
CTransactionRef tx;
274-
if (!pwallet->CreateTransaction(*pwallet->chain().lock(), vecSend, tx, nFeeRet, nChangePosRet, strResult, coinControl)) {
275-
return false;
274+
{
275+
LOCK2(pwallet->cs_wallet, cs_main);
276+
if (!pwallet->CreateTransaction(vecSend, tx, nFeeRet, nChangePosRet, strResult, coinControl)) {
277+
return false;
278+
}
276279
}
277280

278281
CAmount nAmountLeft = GetAmountLeft();
@@ -306,7 +309,10 @@ bool CTransactionBuilder::Commit(bilingual_str& strResult)
306309
return false;
307310
}
308311

309-
pwallet->CommitTransaction(tx, {}, {});
312+
{
313+
LOCK2(pwallet->cs_wallet, cs_main);
314+
pwallet->CommitTransaction(tx, {}, {});
315+
}
310316

311317
fKeepKeys = true;
312318

src/dashd.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <noui.h>
1818
#include <shutdown.h>
1919
#include <ui_interface.h>
20+
#include <util/ref.h>
2021
#include <util/system.h>
2122
#include <util/strencodings.h>
2223
#include <util/threadnames.h>
@@ -83,6 +84,7 @@ static bool AppInit(int argc, char* argv[])
8384
return true;
8485
}
8586

87+
util::Ref context{node};
8688
try
8789
{
8890
bool datadirFromCmdLine = gArgs.IsArgSet("-datadir");
@@ -158,7 +160,7 @@ static bool AppInit(int argc, char* argv[])
158160
// If locking the data directory failed, exit immediately
159161
return false;
160162
}
161-
fRet = AppInitMain(node);
163+
fRet = AppInitMain(context, node);
162164
} catch (...) {
163165
PrintExceptionContinue(std::current_exception(), "AppInit()");
164166
}

src/dsnotificationinterface.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
void CDSNotificationInterface::InitializeCurrentBlockTip()
2424
{
25-
LOCK(cs_main);
2625
SynchronousUpdatedBlockTip(::ChainActive().Tip(), nullptr, ::ChainstateActive().IsInitialBlockDownload());
2726
UpdatedBlockTip(::ChainActive().Tip(), nullptr, ::ChainstateActive().IsInitialBlockDownload());
2827
}

src/governance/governance.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ void CGovernanceManager::ProcessMessage(CNode* pfrom, const std::string& msg_typ
143143
return;
144144
}
145145

146-
LOCK2(cs_main, cs);
146+
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
147+
LOCK(cs);
147148

148149
if (mapObjects.count(nHash) || mapPostponedObjects.count(nHash) || mapErasedGovernanceObjects.count(nHash)) {
149150
// TODO - print error code? what if it's GOVOBJ_ERROR_IMMATURE?
@@ -262,7 +263,8 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman
262263

263264
govobj.UpdateSentinelVariables(); //this sets local vars in object
264265

265-
LOCK2(cs_main, cs);
266+
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
267+
LOCK(cs);
266268
std::string strError;
267269

268270
// MAKE SURE THIS OBJECT IS OK
@@ -321,7 +323,8 @@ void CGovernanceManager::UpdateCachesAndClean()
321323

322324
std::vector<uint256> vecDirtyHashes = mmetaman.GetAndClearDirtyGovernanceObjectHashes();
323325

324-
LOCK2(cs_main, cs);
326+
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
327+
LOCK(cs);
325328

326329
for (const uint256& nHash : vecDirtyHashes) {
327330
auto it = mapObjects.find(nHash);
@@ -836,7 +839,8 @@ void CGovernanceManager::CheckPostponedObjects(CConnman& connman)
836839
{
837840
if (!masternodeSync.IsSynced()) return;
838841

839-
LOCK2(cs_main, cs);
842+
LOCK2(cs_main, ::mempool.cs); // Lock mempool because of GetTransaction deep inside
843+
LOCK(cs);
840844

841845
// Check postponed proposals
842846
for (auto it = mapPostponedObjects.begin(); it != mapPostponedObjects.end();) {

0 commit comments

Comments
 (0)