Skip to content

Commit bfce85d

Browse files
committed
Merge bitcoin/bitcoin#30466: refactor: Make m_last_notified_header private
fa92705 refactor: Make m_last_notified_header private (MarcoFalke) Pull request description: Seems brittle to expose mutable fields public. Fix it by making it private. Fixes bitcoin/bitcoin#30425 (comment) ACKs for top commit: dergoegge: utACK fa92705 Tree-SHA512: d9841c42571144ced0edeaa4bb1d96a177a011dca37c8342c66513477c37278602a1b88beb93068b94fc4443b1552c8fc9f98bcf0bda7d0fc101e61e90c33944
2 parents 3799224 + fa92705 commit bfce85d

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/validation.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,24 +3405,24 @@ static SynchronizationState GetSynchronizationState(bool init, bool blockfiles_i
34053405
return SynchronizationState::INIT_DOWNLOAD;
34063406
}
34073407

3408-
static bool NotifyHeaderTip(ChainstateManager& chainman) LOCKS_EXCLUDED(cs_main)
3408+
bool ChainstateManager::NotifyHeaderTip()
34093409
{
34103410
bool fNotify = false;
34113411
bool fInitialBlockDownload = false;
34123412
CBlockIndex* pindexHeader = nullptr;
34133413
{
3414-
LOCK(cs_main);
3415-
pindexHeader = chainman.m_best_header;
3414+
LOCK(GetMutex());
3415+
pindexHeader = m_best_header;
34163416

3417-
if (pindexHeader != chainman.m_last_notified_header) {
3417+
if (pindexHeader != m_last_notified_header) {
34183418
fNotify = true;
3419-
fInitialBlockDownload = chainman.IsInitialBlockDownload();
3420-
chainman.m_last_notified_header = pindexHeader;
3419+
fInitialBlockDownload = IsInitialBlockDownload();
3420+
m_last_notified_header = pindexHeader;
34213421
}
34223422
}
3423-
// Send block tip changed notifications without cs_main
3423+
// Send block tip changed notifications without the lock held
34243424
if (fNotify) {
3425-
chainman.GetNotifications().headerTip(GetSynchronizationState(fInitialBlockDownload, chainman.m_blockman.m_blockfiles_indexed), pindexHeader->nHeight, pindexHeader->nTime, false);
3425+
GetNotifications().headerTip(GetSynchronizationState(fInitialBlockDownload, m_blockman.m_blockfiles_indexed), pindexHeader->nHeight, pindexHeader->nTime, false);
34263426
}
34273427
return fNotify;
34283428
}
@@ -4378,7 +4378,7 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>&
43784378
}
43794379
}
43804380
}
4381-
if (NotifyHeaderTip(*this)) {
4381+
if (NotifyHeaderTip()) {
43824382
if (IsInitialBlockDownload() && ppindex && *ppindex) {
43834383
const CBlockIndex& last_accepted{**ppindex};
43844384
int64_t blocks_left{(NodeClock::now() - last_accepted.Time()) / GetConsensus().PowTargetSpacing()};
@@ -4549,7 +4549,7 @@ bool ChainstateManager::ProcessNewBlock(const std::shared_ptr<const CBlock>& blo
45494549
}
45504550
}
45514551

4552-
NotifyHeaderTip(*this);
4552+
NotifyHeaderTip();
45534553

45544554
BlockValidationState state; // Only used to report errors, not invalidity - ignore it
45554555
if (!ActiveChainstate().ActivateBestChain(state, block)) {
@@ -5126,7 +5126,7 @@ void ChainstateManager::LoadExternalBlockFile(
51265126
}
51275127
}
51285128

5129-
NotifyHeaderTip(*this);
5129+
NotifyHeaderTip();
51305130

51315131
if (!blocks_with_unknown_parent) continue;
51325132

@@ -5152,7 +5152,7 @@ void ChainstateManager::LoadExternalBlockFile(
51525152
}
51535153
range.first++;
51545154
blocks_with_unknown_parent->erase(it);
5155-
NotifyHeaderTip(*this);
5155+
NotifyHeaderTip();
51565156
}
51575157
}
51585158
} catch (const std::exception& e) {

src/validation.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,11 @@ class ChainstateManager
906906

907907
CBlockIndex* m_best_invalid GUARDED_BY(::cs_main){nullptr};
908908

909+
/** The last header for which a headerTip notification was issued. */
910+
CBlockIndex* m_last_notified_header GUARDED_BY(GetMutex()){nullptr};
911+
912+
bool NotifyHeaderTip() LOCKS_EXCLUDED(GetMutex());
913+
909914
//! Internal helper for ActivateSnapshot().
910915
//!
911916
//! De-serialization of a snapshot that is created with
@@ -1063,9 +1068,6 @@ class ChainstateManager
10631068
/** Best header we've seen so far (used for getheaders queries' starting points). */
10641069
CBlockIndex* m_best_header GUARDED_BY(::cs_main){nullptr};
10651070

1066-
/** The last header for which a headerTip notification was issued. */
1067-
CBlockIndex* m_last_notified_header GUARDED_BY(::cs_main){nullptr};
1068-
10691071
//! The total number of bytes available for us to use across all in-memory
10701072
//! coins caches. This will be split somehow across chainstates.
10711073
int64_t m_total_coinstip_cache{0};

0 commit comments

Comments
 (0)