mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-13 07:28:59 +01:00
Require CBlockIndex::GetBlockPos() to hold mutex cs_main
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <consensus/params.h>
|
#include <consensus/params.h>
|
||||||
#include <flatfile.h>
|
#include <flatfile.h>
|
||||||
#include <primitives/block.h>
|
#include <primitives/block.h>
|
||||||
|
#include <sync.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
|
|
||||||
@@ -37,6 +38,8 @@ static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
|
|||||||
*/
|
*/
|
||||||
static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
|
static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60;
|
||||||
|
|
||||||
|
extern RecursiveMutex cs_main;
|
||||||
|
|
||||||
class CBlockFileInfo
|
class CBlockFileInfo
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -223,8 +226,9 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
FlatFilePos GetBlockPos() const
|
FlatFilePos GetBlockPos() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
|
||||||
{
|
{
|
||||||
|
AssertLockHeld(::cs_main);
|
||||||
FlatFilePos ret;
|
FlatFilePos ret;
|
||||||
if (nStatus & BLOCK_HAVE_DATA) {
|
if (nStatus & BLOCK_HAVE_DATA) {
|
||||||
ret.nFile = nFile;
|
ret.nFile = nFile;
|
||||||
|
|||||||
@@ -59,7 +59,9 @@ bool TxIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
|
|||||||
// Exclude genesis block transaction because outputs are not spendable.
|
// Exclude genesis block transaction because outputs are not spendable.
|
||||||
if (pindex->nHeight == 0) return true;
|
if (pindex->nHeight == 0) return true;
|
||||||
|
|
||||||
CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
|
CDiskTxPos pos{
|
||||||
|
WITH_LOCK(::cs_main, return pindex->GetBlockPos()),
|
||||||
|
GetSizeOfCompactSize(block.vtx.size())};
|
||||||
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
|
std::vector<std::pair<uint256, CDiskTxPos>> vPos;
|
||||||
vPos.reserve(block.vtx.size());
|
vPos.reserve(block.vtx.size());
|
||||||
for (const auto& tx : block.vtx) {
|
for (const auto& tx : block.vtx) {
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ FUZZ_TARGET(chain)
|
|||||||
|
|
||||||
const uint256 zero{};
|
const uint256 zero{};
|
||||||
disk_block_index->phashBlock = &zero;
|
disk_block_index->phashBlock = &zero;
|
||||||
|
{
|
||||||
|
LOCK(::cs_main);
|
||||||
(void)disk_block_index->GetBlockHash();
|
(void)disk_block_index->GetBlockHash();
|
||||||
(void)disk_block_index->GetBlockPos();
|
(void)disk_block_index->GetBlockPos();
|
||||||
(void)disk_block_index->GetBlockTime();
|
(void)disk_block_index->GetBlockTime();
|
||||||
@@ -30,6 +32,7 @@ FUZZ_TARGET(chain)
|
|||||||
(void)disk_block_index->HaveTxsDownloaded();
|
(void)disk_block_index->HaveTxsDownloaded();
|
||||||
(void)disk_block_index->IsValid();
|
(void)disk_block_index->IsValid();
|
||||||
(void)disk_block_index->ToString();
|
(void)disk_block_index->ToString();
|
||||||
|
}
|
||||||
|
|
||||||
const CBlockHeader block_header = disk_block_index->GetBlockHeader();
|
const CBlockHeader block_header = disk_block_index->GetBlockHeader();
|
||||||
(void)CDiskBlockIndex{*disk_block_index};
|
(void)CDiskBlockIndex{*disk_block_index};
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ using node::UndoReadFromDisk;
|
|||||||
|
|
||||||
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index, BlockFilter& filter)
|
bool ComputeFilter(BlockFilterType filter_type, const CBlockIndex* block_index, BlockFilter& filter)
|
||||||
{
|
{
|
||||||
|
LOCK(::cs_main);
|
||||||
|
|
||||||
CBlock block;
|
CBlock block;
|
||||||
if (!ReadBlockFromDisk(block, block_index->GetBlockPos(), Params().GetConsensus())) {
|
if (!ReadBlockFromDisk(block, block_index->GetBlockPos(), Params().GetConsensus())) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -140,11 +140,13 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prune the older block file.
|
// Prune the older block file.
|
||||||
|
int file_number;
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile);
|
file_number = oldTip->GetBlockPos().nFile;
|
||||||
|
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
|
||||||
}
|
}
|
||||||
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
|
UnlinkPrunedFiles({file_number});
|
||||||
|
|
||||||
// Verify ScanForWalletTransactions only picks transactions in the new block
|
// Verify ScanForWalletTransactions only picks transactions in the new block
|
||||||
// file.
|
// file.
|
||||||
@@ -169,9 +171,10 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup)
|
|||||||
// Prune the remaining block file.
|
// Prune the remaining block file.
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(newTip->GetBlockPos().nFile);
|
file_number = newTip->GetBlockPos().nFile;
|
||||||
|
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
|
||||||
}
|
}
|
||||||
UnlinkPrunedFiles({newTip->GetBlockPos().nFile});
|
UnlinkPrunedFiles({file_number});
|
||||||
|
|
||||||
// Verify ScanForWalletTransactions scans no blocks.
|
// Verify ScanForWalletTransactions scans no blocks.
|
||||||
{
|
{
|
||||||
@@ -202,11 +205,13 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
|
|||||||
CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
|
CBlockIndex* newTip = m_node.chainman->ActiveChain().Tip();
|
||||||
|
|
||||||
// Prune the older block file.
|
// Prune the older block file.
|
||||||
|
int file_number;
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(oldTip->GetBlockPos().nFile);
|
file_number = oldTip->GetBlockPos().nFile;
|
||||||
|
Assert(m_node.chainman)->m_blockman.PruneOneBlockFile(file_number);
|
||||||
}
|
}
|
||||||
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile});
|
UnlinkPrunedFiles({file_number});
|
||||||
|
|
||||||
// Verify importmulti RPC returns failure for a key whose creation time is
|
// Verify importmulti RPC returns failure for a key whose creation time is
|
||||||
// before the missing block, and success for a key whose creation time is
|
// before the missing block, and success for a key whose creation time is
|
||||||
|
|||||||
Reference in New Issue
Block a user