Skip to content

Commit e07c943

Browse files
committed
Add AssertLockHeld for cs_main to ChainActive-using functions
All functions that use ChainActive but do not aquire the cs_main lock themselves, need to be called with the cs_main lock held. This commit adds assertions to all externally callable functions that use chainActive or chainMostWork. This will flag usages when built with -DDEBUG_LOCKORDER.
1 parent 2f3308f commit e07c943

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
474474

475475
bool IsStandardTx(const CTransaction& tx, string& reason)
476476
{
477+
AssertLockHeld(cs_main);
477478
if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) {
478479
reason = "version";
479480
return false;
@@ -556,6 +557,7 @@ bool IsStandardTx(const CTransaction& tx, string& reason)
556557

557558
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
558559
{
560+
AssertLockHeld(cs_main);
559561
// Time based nLockTime implemented in 0.1.6
560562
if (tx.nLockTime == 0)
561563
return true;
@@ -667,6 +669,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, CCoinsViewCache& inputs)
667669

668670
int CMerkleTx::SetMerkleBranch(const CBlock* pblock)
669671
{
672+
AssertLockHeld(cs_main);
670673
CBlock blockTmp;
671674

672675
if (pblock == NULL) {
@@ -813,6 +816,7 @@ int64_t GetMinFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree,
813816
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
814817
bool* pfMissingInputs, bool fRejectInsaneFee)
815818
{
819+
AssertLockHeld(cs_main);
816820
if (pfMissingInputs)
817821
*pfMissingInputs = false;
818822

@@ -958,6 +962,7 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
958962
{
959963
if (hashBlock == 0 || nIndex == -1)
960964
return 0;
965+
AssertLockHeld(cs_main);
961966

962967
// Find the block it claims to be in
963968
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(hashBlock);
@@ -981,6 +986,7 @@ int CMerkleTx::GetDepthInMainChainINTERNAL(CBlockIndex* &pindexRet) const
981986

982987
int CMerkleTx::GetDepthInMainChain(CBlockIndex* &pindexRet) const
983988
{
989+
AssertLockHeld(cs_main);
984990
int nResult = GetDepthInMainChainINTERNAL(pindexRet);
985991
if (nResult == 0 && !mempool.exists(GetHash()))
986992
return -1; // Not in chain, not in mempool
@@ -1304,6 +1310,7 @@ int GetNumBlocksOfPeers()
13041310

13051311
bool IsInitialBlockDownload()
13061312
{
1313+
AssertLockHeld(cs_main);
13071314
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
13081315
return true;
13091316
static int64_t nLastUpdate;
@@ -1323,6 +1330,7 @@ CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
13231330

13241331
void CheckForkWarningConditions()
13251332
{
1333+
AssertLockHeld(cs_main);
13261334
// Before we get past initial download, we cannot reliably alert about forks
13271335
// (we assume we don't get stuck on a fork before the last checkpoint)
13281336
if (IsInitialBlockDownload())
@@ -1368,6 +1376,7 @@ void CheckForkWarningConditions()
13681376

13691377
void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
13701378
{
1379+
AssertLockHeld(cs_main);
13711380
// If we are on a fork that is sufficiently large, set a warning flag
13721381
CBlockIndex* pfork = pindexNewForkTip;
13731382
CBlockIndex* plonger = chainActive.Tip();
@@ -2078,6 +2087,7 @@ void static FindMostWorkChain() {
20782087

20792088
// Try to activate to the most-work chain (thereby connecting it).
20802089
bool ActivateBestChain(CValidationState &state) {
2090+
AssertLockHeld(cs_main);
20812091
CBlockIndex *pindexOldTip = chainActive.Tip();
20822092
bool fComplete = false;
20832093
while (!fComplete) {
@@ -2126,6 +2136,7 @@ bool ActivateBestChain(CValidationState &state) {
21262136

21272137
bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos& pos)
21282138
{
2139+
AssertLockHeld(cs_main);
21292140
// Check for duplicate
21302141
uint256 hash = block.GetHash();
21312142
if (mapBlockIndex.count(hash))
@@ -2344,6 +2355,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
23442355

23452356
bool AcceptBlock(CBlock& block, CValidationState& state, CDiskBlockPos* dbp)
23462357
{
2358+
AssertLockHeld(cs_main);
23472359
// Check for duplicate
23482360
uint256 hash = block.GetHash();
23492361
if (mapBlockIndex.count(hash))
@@ -2455,6 +2467,7 @@ bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, uns
24552467

24562468
int64_t CBlockIndex::GetMedianTime() const
24572469
{
2470+
AssertLockHeld(cs_main);
24582471
const CBlockIndex* pindex = this;
24592472
for (int i = 0; i < nMedianTimeSpan/2; i++)
24602473
{
@@ -2467,6 +2480,7 @@ int64_t CBlockIndex::GetMedianTime() const
24672480

24682481
void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd)
24692482
{
2483+
AssertLockHeld(cs_main);
24702484
// Filter out duplicate requests
24712485
if (pindexBegin == pnode->pindexLastGetBlocksBegin && hashEnd == pnode->hashLastGetBlocksEnd)
24722486
return;
@@ -2983,6 +2997,7 @@ bool InitBlockIndex() {
29832997

29842998
void PrintBlockTree()
29852999
{
3000+
AssertLockHeld(cs_main);
29863001
// pre-compute tree structure
29873002
map<CBlockIndex*, vector<CBlockIndex*> > mapNext;
29883003
for (map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.begin(); mi != mapBlockIndex.end(); ++mi)

src/qt/transactiondesc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
QString TransactionDesc::FormatTxStatus(const CWalletTx& wtx)
2222
{
23+
AssertLockHeld(cs_main);
2324
if (!IsFinalTx(wtx, chainActive.Height() + 1))
2425
{
2526
if (wtx.nLockTime < LOCKTIME_THRESHOLD)

src/qt/transactionrecord.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
150150

151151
void TransactionRecord::updateStatus(const CWalletTx &wtx)
152152
{
153+
AssertLockHeld(cs_main);
153154
// Determine transaction status
154155

155156
// Find the block the tx is in
@@ -234,6 +235,7 @@ void TransactionRecord::updateStatus(const CWalletTx &wtx)
234235

235236
bool TransactionRecord::statusUpdateNeeded()
236237
{
238+
AssertLockHeld(cs_main);
237239
return status.cur_num_blocks != chainActive.Height();
238240
}
239241

0 commit comments

Comments
 (0)