Skip to content

Commit b4185c7

Browse files
committed
CI: fix asan lock issues
1 parent c6aba4a commit b4185c7

File tree

7 files changed

+34
-16
lines changed

7 files changed

+34
-16
lines changed

src/chain.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ CBlockLocator CChain::GetLocator(const CBlockIndex *pindex) const {
5656
return CBlockLocator(vHave);
5757
}
5858

59-
void CBlockIndex::untrim() {
59+
void CBlockIndex::untrim() EXCLUSIVE_LOCKS_REQUIRED(::cs_main){
60+
AssertLockHeld(::cs_main);
6061
if (!trimmed())
6162
return;
6263
CBlockIndex tmp;
@@ -70,12 +71,10 @@ void CBlockIndex::untrim() {
7071
m_pcontext->chainman->m_blockman.m_dirty_blockindex.insert(this);
7172
}
7273

73-
const CBlockIndex *CBlockIndex::untrim_to(CBlockIndex *pindexNew) const
74+
const CBlockIndex *CBlockIndex::untrim_to(CBlockIndex *pindexNew) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
7475
{
75-
if (m_pcontext) {
76-
return m_pcontext->chainman->m_blockman.m_block_tree_db->RegenerateFullIndex(this, pindexNew);
77-
}
78-
return nullptr;
76+
AssertLockHeld(::cs_main);
77+
return m_pcontext->chainman->m_blockman.m_block_tree_db->RegenerateFullIndex(this, pindexNew);
7978
}
8079

8180
const CBlockIndex *CChain::FindFork(const CBlockIndex *pindex) const {

src/dynafed.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ bool NextBlockIsParameterTransition(const CBlockIndex* pindexPrev, const Consens
1616
for (int32_t height = next_height - 1; height >= (int32_t)(next_height - consensus.dynamic_epoch_length); --height) {
1717
const CBlockIndex* p_epoch_walk = pindexPrev->GetAncestor(height);
1818
assert(p_epoch_walk);
19-
ForceUntrimHeader(p_epoch_walk);
19+
{
20+
LOCK(cs_main);
21+
ForceUntrimHeader(p_epoch_walk);
22+
}
2023
const DynaFedParamEntry& proposal = p_epoch_walk->dynafed_params().m_proposed;
2124
const uint256 proposal_root = proposal.CalculateRoot();
2225
vote_tally[proposal_root]++;
@@ -62,7 +65,10 @@ DynaFedParamEntry ComputeNextBlockFullCurrentParameters(const CBlockIndex* pinde
6265
// may be pre-dynafed params
6366
const CBlockIndex* p_epoch_start = pindexPrev->GetAncestor(epoch_start_height);
6467
assert(p_epoch_start);
65-
ForceUntrimHeader(p_epoch_start);
68+
{
69+
LOCK(cs_main);
70+
ForceUntrimHeader(p_epoch_start);
71+
}
6672
if (p_epoch_start->dynafed_params().IsNull()) {
6773
// We need to construct the "full" current parameters of pre-dynafed
6874
// consensus
@@ -96,7 +102,10 @@ DynaFedParamEntry ComputeNextBlockCurrentParameters(const CBlockIndex* pindexPre
96102
{
97103
assert(pindexPrev);
98104

99-
ForceUntrimHeader(pindexPrev);
105+
{
106+
LOCK(cs_main);
107+
ForceUntrimHeader(pindexPrev);
108+
}
100109
DynaFedParamEntry entry = ComputeNextBlockFullCurrentParameters(pindexPrev, consensus);
101110

102111
uint32_t next_height = pindexPrev->nHeight+1;

src/pegins.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ std::vector<std::pair<CScript, CScript>> GetValidFedpegScripts(const CBlockIndex
489489
break;
490490
}
491491

492-
ForceUntrimHeader(p_epoch_start);
492+
{
493+
LOCK(cs_main);
494+
ForceUntrimHeader(p_epoch_start);
495+
}
493496
if (!p_epoch_start->dynafed_params().IsNull()) {
494497
fedpegscripts.push_back(std::make_pair(p_epoch_start->dynafed_params().m_current.m_fedpeg_program, p_epoch_start->dynafed_params().m_current.m_fedpegscript));
495498
} else {

src/rest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ static bool rest_headers(const std::any& context,
232232
case RetFormat::BINARY: {
233233
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
234234
for (const CBlockIndex *pindex : headers) {
235+
LOCK(cs_main);
235236
CBlockIndex tmpBlockIndexFull;
236237
const CBlockIndex* pindexfull=pindex->untrim_to(&tmpBlockIndexFull);
237238
ssHeader << pindexfull->GetBlockHeader();
@@ -246,6 +247,7 @@ static bool rest_headers(const std::any& context,
246247
case RetFormat::HEX: {
247248
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
248249
for (const CBlockIndex *pindex : headers) {
250+
LOCK(cs_main);
249251
CBlockIndex tmpBlockIndexFull;
250252
const CBlockIndex* pindexfull=pindex->untrim_to(&tmpBlockIndexFull);
251253
ssHeader << pindexfull->GetBlockHeader();

src/rpc/blockchain.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
199199
AssertLockNotHeld(cs_main); // For performance reasons
200200

201201
CBlockIndex tmpBlockIndexFull;
202-
const CBlockIndex* blockindex=blockindex_->untrim_to(&tmpBlockIndexFull);
202+
const CBlockIndex* blockindex;
203+
{
204+
LOCK(cs_main);
205+
blockindex = blockindex_->untrim_to(&tmpBlockIndexFull);
206+
}
203207

204208
UniValue result(UniValue::VOBJ);
205209
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
@@ -1020,6 +1024,7 @@ static RPCHelpMan getblockheader()
10201024

10211025
if (!fVerbose)
10221026
{
1027+
LOCK(cs_main);
10231028
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
10241029
CBlockIndex tmpBlockIndexFull;
10251030
const CBlockIndex* pblockindexfull=pblockindex->untrim_to(&tmpBlockIndexFull);

src/txdb.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,14 @@ bool CBlockTreeDB::WritePAKList(const std::vector<std::vector<unsigned char> >&
333333

334334
const CBlockIndex *CBlockTreeDB::RegenerateFullIndex(const CBlockIndex *pindexTrimmed, CBlockIndex *pindexNew) const
335335
{
336+
LOCK(cs_main);
337+
336338
if(!pindexTrimmed->trimmed()) {
337339
return pindexTrimmed;
338340
}
339341
CBlockHeader tmp;
340342
bool BlockRead = false;
341343
{
342-
// At this point we can either be locked or unlocked depending on where we're being called
343-
// but cs_main is a RecursiveMutex, so it doesn't matter
344-
LOCK(cs_main);
345344
// In unpruned nodes, same data could be read from blocks using ReadBlockFromDisk, but that turned out to
346345
// be about 6x slower than reading from the index
347346
std::pair<uint8_t, uint256> key(DB_BLOCK_INDEX, pindexTrimmed->GetBlockHash());

src/validation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,13 +2717,14 @@ static void UpdateTipLog(
27172717
!warning_messages.empty() ? strprintf(" warning='%s'", warning_messages) : "");
27182718
}
27192719

2720-
void ForceUntrimHeader(const CBlockIndex *pindex_)
2720+
void ForceUntrimHeader(const CBlockIndex *pindex_) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
27212721
{
2722+
AssertLockHeld(cs_main);
2723+
27222724
assert(pindex_);
27232725
if (!pindex_->trimmed()) {
27242726
return;
27252727
}
2726-
AssertLockHeld(cs_main);
27272728
CBlockIndex *pindex=const_cast<CBlockIndex*>(pindex_);
27282729
pindex->untrim();
27292730
}

0 commit comments

Comments
 (0)