Skip to content

Commit f1e1312

Browse files
committed
merge bitcoin#16426: Reverse cs_main, cs_wallet lock order and reduce cs_main locking
Co-authored-by: "UdjinM6 <UdjinM6@users.noreply.github.com>"
1 parent 89dfdd7 commit f1e1312

24 files changed

+398
-562
lines changed

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/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/init.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -966,12 +966,7 @@ static void ThreadImport(std::vector<fs::path> vImportFiles)
966966

967967
if (fMasternodeMode) {
968968
assert(activeMasternodeManager);
969-
const CBlockIndex* pindexTip;
970-
{
971-
LOCK(cs_main);
972-
pindexTip = ::ChainActive().Tip();
973-
}
974-
activeMasternodeManager->Init(pindexTip);
969+
activeMasternodeManager->Init(::ChainActive().Tip());
975970
}
976971

977972
g_wallet_init_interface.AutoLockMasternodeCollaterals();
@@ -999,12 +994,7 @@ void PeriodicStats()
999994
}
1000995

1001996
// short version of GetNetworkHashPS(120, -1);
1002-
CBlockIndex *tip;
1003-
{
1004-
LOCK(cs_main);
1005-
tip = ::ChainActive().Tip();
1006-
assert(tip);
1007-
}
997+
CBlockIndex *tip = ::ChainActive().Tip();
1008998
CBlockIndex *pindex = tip;
1009999
int64_t minTime = pindex->GetBlockTime();
10101000
int64_t maxTime = minTime;
@@ -2284,14 +2274,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
22842274

22852275
// LOAD SERIALIZED DAT FILES INTO DATA CACHES FOR INTERNAL USE
22862276

2287-
bool fLoadCacheFiles = !(fReindex || fReindexChainState);
2288-
{
2289-
LOCK(cs_main);
2290-
// was blocks/chainstate deleted?
2291-
if (::ChainActive().Tip() == nullptr) {
2292-
fLoadCacheFiles = false;
2293-
}
2294-
}
2277+
bool fLoadCacheFiles = !(fReindex || fReindexChainState) && (::ChainActive().Tip() != nullptr);
22952278
fs::path pathDB = GetDataDir();
22962279
std::string strDBName;
22972280

0 commit comments

Comments
 (0)