mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 06:49:38 -04:00
refactor: Use MockableSteadyClock in ReportHeadersPresync
This allows the clock to be mockable in tests. Also, replace cs_main with GetMutex() while touching this function. Also, use the ElapseSteady test helper in the p2p_headers_presync fuzz target to make it more deterministic. The m_last_presync_update variable is a global that is not reset in ResetAndInitialize. However, it is only used for logging, so completely disable it for now. Without this patch, the tool would report a diff: cargo run --manifest-path ./contrib/devtools/deterministic-fuzz-coverage/Cargo.toml -- $PWD/bld-cmake/ $PWD/../qa-assets/fuzz_corpora/ p2p_headers_presync 32 ... 4468| 81| auto now = std::chrono::steady_clock::now(); 4469| 81| if (now < m_last_presync_update + std::chrono::milliseconds{250}) return; - ^80 + ^79 ...
This commit is contained in:
parent
fa9c38794e
commit
fad22149f4
3 changed files with 9 additions and 5 deletions
|
@ -15,6 +15,7 @@
|
|||
#include <test/util/net.h>
|
||||
#include <test/util/script.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <test/util/time.h>
|
||||
#include <uint256.h>
|
||||
#include <validation.h>
|
||||
|
||||
|
@ -158,6 +159,9 @@ FUZZ_TARGET(p2p_headers_presync, .init = initialize)
|
|||
{
|
||||
SeedRandomStateForTest(SeedRand::ZEROS);
|
||||
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
|
||||
// The steady clock is currently only used for logging, so a constant
|
||||
// time-point seems acceptable for now.
|
||||
ElapseSteady elapse_steady{};
|
||||
SetMockTime(ConsumeTime(fuzzed_data_provider));
|
||||
|
||||
ChainstateManager& chainman = *g_testing_setup->m_node.chainman;
|
||||
|
|
|
@ -4456,16 +4456,16 @@ bool ChainstateManager::ProcessNewBlockHeaders(std::span<const CBlockHeader> hea
|
|||
|
||||
void ChainstateManager::ReportHeadersPresync(const arith_uint256& work, int64_t height, int64_t timestamp)
|
||||
{
|
||||
AssertLockNotHeld(cs_main);
|
||||
AssertLockNotHeld(GetMutex());
|
||||
{
|
||||
LOCK(cs_main);
|
||||
LOCK(GetMutex());
|
||||
// Don't report headers presync progress if we already have a post-minchainwork header chain.
|
||||
// This means we lose reporting for potentially legitimate, but unlikely, deep reorgs, but
|
||||
// prevent attackers that spam low-work headers from filling our logs.
|
||||
if (m_best_header->nChainWork >= UintToArith256(GetConsensus().nMinimumChainWork)) return;
|
||||
// Rate limit headers presync updates to 4 per second, as these are not subject to DoS
|
||||
// protection.
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto now = MockableSteadyClock::now();
|
||||
if (now < m_last_presync_update + std::chrono::milliseconds{250}) return;
|
||||
m_last_presync_update = now;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
||||
// Copyright (c) 2009-present The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
|
@ -933,7 +933,7 @@ private:
|
|||
friend Chainstate;
|
||||
|
||||
/** Most recent headers presync progress update, for rate-limiting. */
|
||||
std::chrono::time_point<std::chrono::steady_clock> m_last_presync_update GUARDED_BY(::cs_main) {};
|
||||
MockableSteadyClock::time_point m_last_presync_update GUARDED_BY(GetMutex()){};
|
||||
|
||||
std::array<ThresholdConditionCache, VERSIONBITS_NUM_BITS> m_warningcache GUARDED_BY(::cs_main);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue