Skip to content

Commit 4687832

Browse files
committed
Merge bitcoin/bitcoin#30425: kernel: De-globalize static validation variables
51fa262 refactor: Mark some static global vars as const (TheCharlatan) 39f9b80 refactor: De-globalize last notified header index (TheCharlatan) 3443943 refactor: De-globalize validation benchmark timekeeping (TheCharlatan) Pull request description: In future, users of the kernel library might run multiple chainstates in parallel, or create and destroy multiple chainstates over the lifetime of a process. Having static, mutable variables could lead to state inconsistencies in these scenarios. --- This pull request is part of the [libbitcoinkernel project](bitcoin/bitcoin#27587). ACKs for top commit: dergoegge: Code review ACK 51fa262 maflcko: ACK 51fa262 🍚 tdb3: code review ACK 51fa262 Tree-SHA512: da91aa7ffa343325cabb8764ef03c8358845662cf0ba8a6cc1dd38e40e5462d88734f2b459c2de8e7a041551eda9143d92487842609f7f30636f61a0cd3c57ee
2 parents 1db0be8 + 51fa262 commit 4687832

File tree

4 files changed

+58
-55
lines changed

4 files changed

+58
-55
lines changed

src/core_read.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ bool ParseHashStr(const std::string& strHex, uint256& result)
245245

246246
util::Result<int> SighashFromStr(const std::string& sighash)
247247
{
248-
static std::map<std::string, int> map_sighash_values = {
248+
static const std::map<std::string, int> map_sighash_values = {
249249
{std::string("DEFAULT"), int(SIGHASH_DEFAULT)},
250250
{std::string("ALL"), int(SIGHASH_ALL)},
251251
{std::string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY)},

src/script/descriptor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ std::string DescriptorChecksum(const Span<const char>& span)
116116
* As a result, within-group-of-32 errors count as 1 symbol, as do cross-group errors that don't affect
117117
* the position within the groups.
118118
*/
119-
static std::string INPUT_CHARSET =
119+
static const std::string INPUT_CHARSET =
120120
"0123456789()[],'/*abcdefgh@:$%{}"
121121
"IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~"
122122
"ijklmnopqrstuvwxyzABCDEFGH`#\"\\ ";
123123

124124
/** The character set for the checksum itself (same as bech32). */
125-
static std::string CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
125+
static const std::string CHECKSUM_CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
126126

127127
uint64_t c = 1;
128128
int cls = 0;

src/validation.cpp

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,15 +2399,6 @@ static unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Ch
23992399
}
24002400

24012401

2402-
static SteadyClock::duration time_check{};
2403-
static SteadyClock::duration time_forks{};
2404-
static SteadyClock::duration time_connect{};
2405-
static SteadyClock::duration time_verify{};
2406-
static SteadyClock::duration time_undo{};
2407-
static SteadyClock::duration time_index{};
2408-
static SteadyClock::duration time_total{};
2409-
static int64_t num_blocks_total = 0;
2410-
24112402
/** Apply the effects of this block (with given index) on the UTXO set represented by coins.
24122403
* Validity checks that depend on the UTXO set are also done; ConnectBlock()
24132404
* can fail if those validity checks fail (among other reasons). */
@@ -2452,7 +2443,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
24522443
uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
24532444
assert(hashPrevBlock == view.GetBestBlock());
24542445

2455-
num_blocks_total++;
2446+
m_chainman.num_blocks_total++;
24562447

24572448
// Special case for the genesis block, skipping connection of its transactions
24582449
// (its coinbase is unspendable)
@@ -2494,11 +2485,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
24942485
}
24952486

24962487
const auto time_1{SteadyClock::now()};
2497-
time_check += time_1 - time_start;
2488+
m_chainman.time_check += time_1 - time_start;
24982489
LogPrint(BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n",
24992490
Ticks<MillisecondsDouble>(time_1 - time_start),
2500-
Ticks<SecondsDouble>(time_check),
2501-
Ticks<MillisecondsDouble>(time_check) / num_blocks_total);
2491+
Ticks<SecondsDouble>(m_chainman.time_check),
2492+
Ticks<MillisecondsDouble>(m_chainman.time_check) / m_chainman.num_blocks_total);
25022493

25032494
// Do not allow blocks that contain transactions which 'overwrite' older transactions,
25042495
// unless those are already completely spent.
@@ -2596,11 +2587,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
25962587
unsigned int flags{GetBlockScriptFlags(*pindex, m_chainman)};
25972588

25982589
const auto time_2{SteadyClock::now()};
2599-
time_forks += time_2 - time_1;
2590+
m_chainman.time_forks += time_2 - time_1;
26002591
LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n",
26012592
Ticks<MillisecondsDouble>(time_2 - time_1),
2602-
Ticks<SecondsDouble>(time_forks),
2603-
Ticks<MillisecondsDouble>(time_forks) / num_blocks_total);
2593+
Ticks<SecondsDouble>(m_chainman.time_forks),
2594+
Ticks<MillisecondsDouble>(m_chainman.time_forks) / m_chainman.num_blocks_total);
26042595

26052596
CBlockUndo blockundo;
26062597

@@ -2687,12 +2678,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
26872678
UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
26882679
}
26892680
const auto time_3{SteadyClock::now()};
2690-
time_connect += time_3 - time_2;
2681+
m_chainman.time_connect += time_3 - time_2;
26912682
LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(),
26922683
Ticks<MillisecondsDouble>(time_3 - time_2), Ticks<MillisecondsDouble>(time_3 - time_2) / block.vtx.size(),
26932684
nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_3 - time_2) / (nInputs - 1),
2694-
Ticks<SecondsDouble>(time_connect),
2695-
Ticks<MillisecondsDouble>(time_connect) / num_blocks_total);
2685+
Ticks<SecondsDouble>(m_chainman.time_connect),
2686+
Ticks<MillisecondsDouble>(m_chainman.time_connect) / m_chainman.num_blocks_total);
26962687

26972688
CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, params.GetConsensus());
26982689
if (block.vtx[0]->GetValueOut() > blockReward) {
@@ -2705,12 +2696,12 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
27052696
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "block-validation-failed");
27062697
}
27072698
const auto time_4{SteadyClock::now()};
2708-
time_verify += time_4 - time_2;
2699+
m_chainman.time_verify += time_4 - time_2;
27092700
LogPrint(BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1,
27102701
Ticks<MillisecondsDouble>(time_4 - time_2),
27112702
nInputs <= 1 ? 0 : Ticks<MillisecondsDouble>(time_4 - time_2) / (nInputs - 1),
2712-
Ticks<SecondsDouble>(time_verify),
2713-
Ticks<MillisecondsDouble>(time_verify) / num_blocks_total);
2703+
Ticks<SecondsDouble>(m_chainman.time_verify),
2704+
Ticks<MillisecondsDouble>(m_chainman.time_verify) / m_chainman.num_blocks_total);
27142705

27152706
if (fJustCheck)
27162707
return true;
@@ -2720,11 +2711,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
27202711
}
27212712

27222713
const auto time_5{SteadyClock::now()};
2723-
time_undo += time_5 - time_4;
2714+
m_chainman.time_undo += time_5 - time_4;
27242715
LogPrint(BCLog::BENCH, " - Write undo data: %.2fms [%.2fs (%.2fms/blk)]\n",
27252716
Ticks<MillisecondsDouble>(time_5 - time_4),
2726-
Ticks<SecondsDouble>(time_undo),
2727-
Ticks<MillisecondsDouble>(time_undo) / num_blocks_total);
2717+
Ticks<SecondsDouble>(m_chainman.time_undo),
2718+
Ticks<MillisecondsDouble>(m_chainman.time_undo) / m_chainman.num_blocks_total);
27282719

27292720
if (!pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
27302721
pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
@@ -2735,11 +2726,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
27352726
view.SetBestBlock(pindex->GetBlockHash());
27362727

27372728
const auto time_6{SteadyClock::now()};
2738-
time_index += time_6 - time_5;
2729+
m_chainman.time_index += time_6 - time_5;
27392730
LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n",
27402731
Ticks<MillisecondsDouble>(time_6 - time_5),
2741-
Ticks<SecondsDouble>(time_index),
2742-
Ticks<MillisecondsDouble>(time_index) / num_blocks_total);
2732+
Ticks<SecondsDouble>(m_chainman.time_index),
2733+
Ticks<MillisecondsDouble>(m_chainman.time_index) / m_chainman.num_blocks_total);
27432734

27442735
TRACE6(validation, block_connected,
27452736
block_hash.data(),
@@ -3097,11 +3088,6 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
30973088
return true;
30983089
}
30993090

3100-
static SteadyClock::duration time_connect_total{};
3101-
static SteadyClock::duration time_flush{};
3102-
static SteadyClock::duration time_chainstate{};
3103-
static SteadyClock::duration time_post_connect{};
3104-
31053091
struct PerBlockConnectTrace {
31063092
CBlockIndex* pindex = nullptr;
31073093
std::shared_ptr<const CBlock> pblock;
@@ -3188,31 +3174,31 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
31883174
return false;
31893175
}
31903176
time_3 = SteadyClock::now();
3191-
time_connect_total += time_3 - time_2;
3192-
assert(num_blocks_total > 0);
3177+
m_chainman.time_connect_total += time_3 - time_2;
3178+
assert(m_chainman.num_blocks_total > 0);
31933179
LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n",
31943180
Ticks<MillisecondsDouble>(time_3 - time_2),
3195-
Ticks<SecondsDouble>(time_connect_total),
3196-
Ticks<MillisecondsDouble>(time_connect_total) / num_blocks_total);
3181+
Ticks<SecondsDouble>(m_chainman.time_connect_total),
3182+
Ticks<MillisecondsDouble>(m_chainman.time_connect_total) / m_chainman.num_blocks_total);
31973183
bool flushed = view.Flush();
31983184
assert(flushed);
31993185
}
32003186
const auto time_4{SteadyClock::now()};
3201-
time_flush += time_4 - time_3;
3187+
m_chainman.time_flush += time_4 - time_3;
32023188
LogPrint(BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n",
32033189
Ticks<MillisecondsDouble>(time_4 - time_3),
3204-
Ticks<SecondsDouble>(time_flush),
3205-
Ticks<MillisecondsDouble>(time_flush) / num_blocks_total);
3190+
Ticks<SecondsDouble>(m_chainman.time_flush),
3191+
Ticks<MillisecondsDouble>(m_chainman.time_flush) / m_chainman.num_blocks_total);
32063192
// Write the chain state to disk, if necessary.
32073193
if (!FlushStateToDisk(state, FlushStateMode::IF_NEEDED)) {
32083194
return false;
32093195
}
32103196
const auto time_5{SteadyClock::now()};
3211-
time_chainstate += time_5 - time_4;
3197+
m_chainman.time_chainstate += time_5 - time_4;
32123198
LogPrint(BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n",
32133199
Ticks<MillisecondsDouble>(time_5 - time_4),
3214-
Ticks<SecondsDouble>(time_chainstate),
3215-
Ticks<MillisecondsDouble>(time_chainstate) / num_blocks_total);
3200+
Ticks<SecondsDouble>(m_chainman.time_chainstate),
3201+
Ticks<MillisecondsDouble>(m_chainman.time_chainstate) / m_chainman.num_blocks_total);
32163202
// Remove conflicting transactions from the mempool.;
32173203
if (m_mempool) {
32183204
m_mempool->removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
@@ -3223,16 +3209,16 @@ bool Chainstate::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew,
32233209
UpdateTip(pindexNew);
32243210

32253211
const auto time_6{SteadyClock::now()};
3226-
time_post_connect += time_6 - time_5;
3227-
time_total += time_6 - time_1;
3212+
m_chainman.time_post_connect += time_6 - time_5;
3213+
m_chainman.time_total += time_6 - time_1;
32283214
LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n",
32293215
Ticks<MillisecondsDouble>(time_6 - time_5),
3230-
Ticks<SecondsDouble>(time_post_connect),
3231-
Ticks<MillisecondsDouble>(time_post_connect) / num_blocks_total);
3216+
Ticks<SecondsDouble>(m_chainman.time_post_connect),
3217+
Ticks<MillisecondsDouble>(m_chainman.time_post_connect) / m_chainman.num_blocks_total);
32323218
LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n",
32333219
Ticks<MillisecondsDouble>(time_6 - time_1),
3234-
Ticks<SecondsDouble>(time_total),
3235-
Ticks<MillisecondsDouble>(time_total) / num_blocks_total);
3220+
Ticks<SecondsDouble>(m_chainman.time_total),
3221+
Ticks<MillisecondsDouble>(m_chainman.time_total) / m_chainman.num_blocks_total);
32363222

32373223
// If we are the background validation chainstate, check to see if we are done
32383224
// validating the snapshot (i.e. our tip has reached the snapshot's base block).
@@ -3423,16 +3409,15 @@ static bool NotifyHeaderTip(ChainstateManager& chainman) LOCKS_EXCLUDED(cs_main)
34233409
{
34243410
bool fNotify = false;
34253411
bool fInitialBlockDownload = false;
3426-
static CBlockIndex* pindexHeaderOld = nullptr;
34273412
CBlockIndex* pindexHeader = nullptr;
34283413
{
34293414
LOCK(cs_main);
34303415
pindexHeader = chainman.m_best_header;
34313416

3432-
if (pindexHeader != pindexHeaderOld) {
3417+
if (pindexHeader != chainman.m_last_notified_header) {
34333418
fNotify = true;
34343419
fInitialBlockDownload = chainman.IsInitialBlockDownload();
3435-
pindexHeaderOld = pindexHeader;
3420+
chainman.m_last_notified_header = pindexHeader;
34363421
}
34373422
}
34383423
// Send block tip changed notifications without cs_main

src/validation.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,21 @@ class ChainstateManager
949949
//! A queue for script verifications that have to be performed by worker threads.
950950
CCheckQueue<CScriptCheck> m_script_check_queue;
951951

952+
//! Timers and counters used for benchmarking validation in both background
953+
//! and active chainstates.
954+
SteadyClock::duration GUARDED_BY(::cs_main) time_check{};
955+
SteadyClock::duration GUARDED_BY(::cs_main) time_forks{};
956+
SteadyClock::duration GUARDED_BY(::cs_main) time_connect{};
957+
SteadyClock::duration GUARDED_BY(::cs_main) time_verify{};
958+
SteadyClock::duration GUARDED_BY(::cs_main) time_undo{};
959+
SteadyClock::duration GUARDED_BY(::cs_main) time_index{};
960+
SteadyClock::duration GUARDED_BY(::cs_main) time_total{};
961+
int64_t GUARDED_BY(::cs_main) num_blocks_total{0};
962+
SteadyClock::duration GUARDED_BY(::cs_main) time_connect_total{};
963+
SteadyClock::duration GUARDED_BY(::cs_main) time_flush{};
964+
SteadyClock::duration GUARDED_BY(::cs_main) time_chainstate{};
965+
SteadyClock::duration GUARDED_BY(::cs_main) time_post_connect{};
966+
952967
public:
953968
using Options = kernel::ChainstateManagerOpts;
954969

@@ -1048,6 +1063,9 @@ class ChainstateManager
10481063
/** Best header we've seen so far (used for getheaders queries' starting points). */
10491064
CBlockIndex* m_best_header GUARDED_BY(::cs_main){nullptr};
10501065

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

0 commit comments

Comments
 (0)