Skip to content

Commit 6fd4341

Browse files
committed
Require CBlockIndex::GetBlockPos() to hold mutex cs_main
1 parent 39d9bbe commit 6fd4341

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

src/chain.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <consensus/params.h>
1111
#include <flatfile.h>
1212
#include <primitives/block.h>
13+
#include <sync.h>
1314
#include <tinyformat.h>
1415
#include <uint256.h>
1516

@@ -37,6 +38,8 @@ static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
3738
*/
3839
static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
3940

41+
extern RecursiveMutex cs_main;
42+
4043
class CBlockFileInfo
4144
{
4245
public:
@@ -223,8 +226,9 @@ class CBlockIndex
223226
{
224227
}
225228

226-
FlatFilePos GetBlockPos() const
229+
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
227230
{
231+
AssertLockHeld(::cs_main);
228232
FlatFilePos ret;
229233
if (nStatus & BLOCK_HAVE_DATA) {
230234
ret.nFile = nFile;

src/index/txindex.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
5959
// Exclude genesis block transaction because outputs are not spendable.
6060
if (pindex->nHeight == 0) return true;
6161

62-
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
62+
CDiskTxPos pos{
63+
WITH_LOCK(::cs_main, return pindex->GetBlockPos()),
64+
GetSizeOfCompactSize(block.vtx.size())};
6365
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
6466
vPos.reserve(block.vtx.size());
6567
for (const auto& tx : block.vtx) {

src/test/fuzz/chain.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ FUZZ_TARGET(chain)
2121

2222
const uint256 zero{};
2323
disk_block_index->phashBlock = &zero;
24-
(void)disk_block_index->GetBlockHash();
25-
(void)disk_block_index->GetBlockPos();
26-
(void)disk_block_index->GetBlockTime();
27-
(void)disk_block_index->GetBlockTimeMax();
28-
(void)disk_block_index->GetMedianTimePast();
29-
(void)disk_block_index->GetUndoPos();
30-
(void)disk_block_index->HaveTxsDownloaded();
31-
(void)disk_block_index->IsValid();
32-
(void)disk_block_index->ToString();
24+
{
25+
LOCK(::cs_main);
26+
(void)disk_block_index->GetBlockHash();
27+
(void)disk_block_index->GetBlockPos();
28+
(void)disk_block_index->GetBlockTime();
29+
(void)disk_block_index->GetBlockTimeMax();
30+
(void)disk_block_index->GetMedianTimePast();
31+
(void)disk_block_index->GetUndoPos();
32+
(void)disk_block_index->HaveTxsDownloaded();
33+
(void)disk_block_index->IsValid();
34+
(void)disk_block_index->ToString();
35+
}
3336

3437
const CBlockHeader block_header = disk_block_index->GetBlockHeader();
3538
(void)CDiskBlockIndex{*disk_block_index};

src/test/util/blockfilter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ using node::UndoReadFromDisk;
1313

1414
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index, BlockFilter& filter)
1515
{
16+
LOCK(::cs_main);
17+
1618
CBlock block;
1719
if (!ReadBlockFromDisk(block, block_index->GetBlockPos(), Params().GetConsensus())) {
1820
return false;

src/wallet/test/wallet_tests.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
140140
}
141141

142142
// Prune the older block file.
143+
int file_number;
143144
{
144145
LOCK(cs_main);
145-
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile);
146+
file_number = oldTip->GetBlockPos().nFile;
147+
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
146148
}
147-
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
149+
UnlinkPrunedFiles({file_number});
148150

149151
// Verify ScanForWalletTransactions only picks transactions in the new block
150152
// file.
@@ -169,9 +171,10 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
169171
// Prune the remaining block file.
170172
{
171173
LOCK(cs_main);
172-
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(newTip->GetBlockPos().nFile);
174+
file_number = newTip->GetBlockPos().nFile;
175+
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
173176
}
174-
UnlinkPrunedFiles({newTip->GetBlockPos().nFile});
177+
UnlinkPrunedFiles({file_number});
175178

176179
// Verify ScanForWalletTransactions scans no blocks.
177180
{
@@ -202,11 +205,13 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
202205
CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
203206

204207
// Prune the older block file.
208+
int file_number;
205209
{
206210
LOCK(cs_main);
207-
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile);
211+
file_number = oldTip->GetBlockPos().nFile;
212+
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
208213
}
209-
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
214+
UnlinkPrunedFiles({file_number});
210215

211216
// Verify importmulti RPC returns failure for a key whose creation time is
212217
// before the missing block, and success for a key whose creation time is

0 commit comments

Comments
 (0)