Skip to content

Commit 40c66bb

Browse files
author
MarcoFalke
committed
Merge #15855: [refactor] interfaces: Add missing LockAnnotation for cs_main
fa3c651 [refactor] interfaces: Add missing LockAnnotation for cs_main (MarcoFalke) Pull request description: This adds missing `LockAnnotation lock(::cs_main);` to `src/interfaces/chain.cpp` (as well as tests and benchmarks) ACKs for commit fa3c65: practicalswift: utACK fa3c651 ryanofsky: utACK fa3c651 Tree-SHA512: b67082fe3718c94b4addf7f2530593915225c25080f20c3ffa4ff7e08f1f49548f255fb285f89a8feff84be3f6c91e1792495ced9f6bf396732396d1356d597a
2 parents 667a861 + fa3c651 commit 40c66bb

File tree

11 files changed

+55
-20
lines changed

11 files changed

+55
-20
lines changed

src/bench/bench.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ void benchmark::BenchRunner::RunAll(Printer& printer, uint64_t num_evals, double
114114
for (const auto& p : benchmarks()) {
115115
TestingSetup test{CBaseChainParams::REGTEST};
116116
{
117+
LOCK(cs_main);
117118
assert(::ChainActive().Height() == 0);
118119
const bool witness_enabled{IsWitnessEnabled(::ChainActive().Tip(), Params().GetConsensus())};
119120
assert(witness_enabled);

src/bench/duplicate_inputs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static void DuplicateInputs(benchmark::State& state)
2929
CMutableTransaction coinbaseTx{};
3030
CMutableTransaction naughtyTx{};
3131

32+
LOCK(cs_main);
3233
CBlockIndex* pindexPrev = ::ChainActive().Tip();
3334
assert(pindexPrev != nullptr);
3435
block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());

src/interfaces/chain.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LockImpl : public Chain::Lock
4141
{
4242
Optional<int> getHeight() override
4343
{
44+
LockAnnotation lock(::cs_main);
4445
int height = ::ChainActive().Height();
4546
if (height >= 0) {
4647
return height;
@@ -49,6 +50,7 @@ class LockImpl : public Chain::Lock
4950
}
5051
Optional<int> getBlockHeight(const uint256& hash) override
5152
{
53+
LockAnnotation lock(::cs_main);
5254
CBlockIndex* block = LookupBlockIndex(hash);
5355
if (block && ::ChainActive().Contains(block)) {
5456
return block->nHeight;
@@ -63,29 +65,34 @@ class LockImpl : public Chain::Lock
6365
}
6466
uint256 getBlockHash(int height) override
6567
{
68+
LockAnnotation lock(::cs_main);
6669
CBlockIndex* block = ::ChainActive()[height];
6770
assert(block != nullptr);
6871
return block->GetBlockHash();
6972
}
7073
int64_t getBlockTime(int height) override
7174
{
75+
LockAnnotation lock(::cs_main);
7276
CBlockIndex* block = ::ChainActive()[height];
7377
assert(block != nullptr);
7478
return block->GetBlockTime();
7579
}
7680
int64_t getBlockMedianTimePast(int height) override
7781
{
82+
LockAnnotation lock(::cs_main);
7883
CBlockIndex* block = ::ChainActive()[height];
7984
assert(block != nullptr);
8085
return block->GetMedianTimePast();
8186
}
8287
bool haveBlockOnDisk(int height) override
8388
{
89+
LockAnnotation lock(::cs_main);
8490
CBlockIndex* block = ::ChainActive()[height];
8591
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
8692
}
8793
Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) override
8894
{
95+
LockAnnotation lock(::cs_main);
8996
CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time, height);
9097
if (block) {
9198
if (hash) *hash = block->GetBlockHash();
@@ -95,6 +102,7 @@ class LockImpl : public Chain::Lock
95102
}
96103
Optional<int> findPruned(int start_height, Optional<int> stop_height) override
97104
{
105+
LockAnnotation lock(::cs_main);
98106
if (::fPruneMode) {
99107
CBlockIndex* block = stop_height ? ::ChainActive()[*stop_height] : ::ChainActive().Tip();
100108
while (block && block->nHeight >= start_height) {
@@ -108,6 +116,7 @@ class LockImpl : public Chain::Lock
108116
}
109117
Optional<int> findFork(const uint256& hash, Optional<int>* height) override
110118
{
119+
LockAnnotation lock(::cs_main);
111120
const CBlockIndex* block = LookupBlockIndex(hash);
112121
const CBlockIndex* fork = block ? ::ChainActive().FindFork(block) : nullptr;
113122
if (height) {
@@ -122,7 +131,11 @@ class LockImpl : public Chain::Lock
122131
}
123132
return nullopt;
124133
}
125-
CBlockLocator getTipLocator() override { return ::ChainActive().GetLocator(); }
134+
CBlockLocator getTipLocator() override
135+
{
136+
LockAnnotation lock(::cs_main);
137+
return ::ChainActive().GetLocator();
138+
}
126139
Optional<int> findLocatorFork(const CBlockLocator& locator) override
127140
{
128141
LockAnnotation lock(::cs_main);

src/qt/test/wallettests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ void TestGUI()
145145
}
146146
{
147147
auto locked_chain = wallet->chain().lock();
148+
LockAnnotation lock(::cs_main);
149+
148150
WalletRescanReserver reserver(wallet.get());
149151
reserver.reserve();
150152
CWallet::ScanResult result = wallet->ScanForWalletTransactions(locked_chain->getBlockHash(0), {} /* stop_block */, reserver, true /* fUpdate */);

src/test/miner_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
#include <consensus/merkle.h>
99
#include <consensus/tx_verify.h>
1010
#include <consensus/validation.h>
11-
#include <validation.h>
1211
#include <miner.h>
1312
#include <policy/policy.h>
1413
#include <pubkey.h>
1514
#include <script/standard.h>
1615
#include <txmempool.h>
1716
#include <uint256.h>
18-
#include <util/system.h>
1917
#include <util/strencodings.h>
18+
#include <util/system.h>
19+
#include <validation.h>
2020

2121
#include <test/setup_common.h>
2222

@@ -82,7 +82,7 @@ struct {
8282
{2, 0xbbbeb305}, {2, 0xfe1c810a},
8383
};
8484

85-
static CBlockIndex CreateBlockIndex(int nHeight)
85+
static CBlockIndex CreateBlockIndex(int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
8686
{
8787
CBlockIndex index;
8888
index.nHeight = nHeight;

src/test/txvalidationcache_tests.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,38 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
6666

6767
// Test 1: block with both of those transactions should be rejected.
6868
block = CreateAndProcessBlock(spends, scriptPubKey);
69-
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
69+
{
70+
LOCK(cs_main);
71+
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
72+
}
7073

7174
// Test 2: ... and should be rejected if spend1 is in the memory pool
7275
BOOST_CHECK(ToMemPool(spends[0]));
7376
block = CreateAndProcessBlock(spends, scriptPubKey);
74-
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
77+
{
78+
LOCK(cs_main);
79+
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
80+
}
7581
mempool.clear();
7682

7783
// Test 3: ... and should be rejected if spend2 is in the memory pool
7884
BOOST_CHECK(ToMemPool(spends[1]));
7985
block = CreateAndProcessBlock(spends, scriptPubKey);
80-
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
86+
{
87+
LOCK(cs_main);
88+
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() != block.GetHash());
89+
}
8190
mempool.clear();
8291

8392
// Final sanity test: first spend in mempool, second in block, that's OK:
8493
std::vector<CMutableTransaction> oneSpend;
8594
oneSpend.push_back(spends[0]);
8695
BOOST_CHECK(ToMemPool(spends[1]));
8796
block = CreateAndProcessBlock(oneSpend, scriptPubKey);
88-
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() == block.GetHash());
97+
{
98+
LOCK(cs_main);
99+
BOOST_CHECK(::ChainActive().Tip()->GetBlockHash() == block.GetHash());
100+
}
89101
// spends[1] should have been removed from the mempool when the
90102
// block with spends[0] is accepted:
91103
BOOST_CHECK_EQUAL(mempool.size(), 0U);

src/test/util.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
8484
.CreateNewBlock(coinbase_scriptPubKey)
8585
->block);
8686

87+
LOCK(cs_main);
8788
block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1;
8889
block->hashMerkleRoot = BlockMerkleRoot(*block);
8990

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
181181

182182
UnregisterValidationInterface(&sub);
183183

184+
LOCK(cs_main);
184185
BOOST_CHECK_EQUAL(sub.m_expected_tip, ::ChainActive().Tip()->GetBlockHash());
185186
}
186187

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,7 +3224,7 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
32243224
}
32253225

32263226
//! Returns last CBlockIndex* that is a checkpoint
3227-
static CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
3227+
static CBlockIndex* GetLastCheckpoint(const CCheckpointData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
32283228
{
32293229
const MapCheckpoints& checkpoints = data.mapCheckpoints;
32303230

@@ -3248,7 +3248,7 @@ static CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
32483248
* in ConnectBlock().
32493249
* Note that -reindex-chainstate skips the validation that happens here!
32503250
*/
3251-
static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
3251+
static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
32523252
{
32533253
assert(pindexPrev != nullptr);
32543254
const int nHeight = pindexPrev->nHeight + 1;

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ class CVerifyDB {
412412
/** Replay blocks that aren't fully applied to the database. */
413413
bool ReplayBlocks(const CChainParams& params, CCoinsView* view);
414414

415-
inline CBlockIndex* LookupBlockIndex(const uint256& hash)
415+
inline CBlockIndex* LookupBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
416416
{
417417
AssertLockHeld(cs_main);
418418
BlockMap::const_iterator it = mapBlockIndex.find(hash);

0 commit comments

Comments
 (0)