mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 15:04:44 +01:00
Make adjusted time type safe
This commit is contained in:
parent
fa3be799fe
commit
eeee5ada23
8 changed files with 19 additions and 16 deletions
|
@ -82,7 +82,7 @@ int main(int argc, char* argv[])
|
|||
// SETUP: Chainstate
|
||||
const ChainstateManager::Options chainman_opts{
|
||||
.chainparams = chainparams,
|
||||
.adjusted_time_callback = static_cast<int64_t (*)()>(GetTime),
|
||||
.adjusted_time_callback = NodeClock::now,
|
||||
};
|
||||
ChainstateManager chainman{chainman_opts};
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
|
||||
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
|
||||
|
||||
#include <util/time.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
|
@ -19,7 +21,7 @@ namespace kernel {
|
|||
*/
|
||||
struct ChainstateManagerOpts {
|
||||
const CChainParams& chainparams;
|
||||
const std::function<int64_t()> adjusted_time_callback{nullptr};
|
||||
const std::function<NodeClock::time_point()> adjusted_time_callback{nullptr};
|
||||
};
|
||||
|
||||
} // namespace kernel
|
||||
|
|
|
@ -1145,7 +1145,7 @@ bool PeerManagerImpl::TipMayBeStale()
|
|||
|
||||
bool PeerManagerImpl::CanDirectFetch()
|
||||
{
|
||||
return m_chainman.ActiveChain().Tip()->GetBlockTime() > GetAdjustedTime() - m_chainparams.GetConsensus().nPowTargetSpacing * 20;
|
||||
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
|
||||
}
|
||||
|
||||
static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
|
||||
|
@ -4886,7 +4886,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
|||
|
||||
if (!state.fSyncStarted && CanServeBlocks(*peer) && !fImporting && !fReindex) {
|
||||
// Only actively request headers from a single peer, unless we're close to today.
|
||||
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
|
||||
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->Time() > GetAdjustedTime() - 24h) {
|
||||
const CBlockIndex* pindexStart = m_chainman.m_best_header;
|
||||
/* If possible, start at the block preceding the currently
|
||||
best known header. This ensures that we always get a
|
||||
|
@ -4906,7 +4906,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
|||
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
|
||||
// to maintain precision
|
||||
std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
|
||||
(GetAdjustedTime() - m_chainman.m_best_header->GetBlockTime()) / consensusParams.nPowTargetSpacing
|
||||
Ticks<std::chrono::seconds>(GetAdjustedTime() - m_chainman.m_best_header->Time()) / consensusParams.nPowTargetSpacing
|
||||
);
|
||||
nSyncStarted++;
|
||||
}
|
||||
|
@ -5223,7 +5223,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
|||
// Check for headers sync timeouts
|
||||
if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
|
||||
// Detect whether this is a stalling initial-headers-sync peer
|
||||
if (m_chainman.m_best_header->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) {
|
||||
if (m_chainman.m_best_header->Time() <= GetAdjustedTime() - 24h) {
|
||||
if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (m_num_preferred_download_peers - state.fPreferredDownload >= 1)) {
|
||||
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
|
||||
// and we have others we could be using instead.
|
||||
|
|
|
@ -30,7 +30,7 @@ namespace node {
|
|||
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
|
||||
{
|
||||
int64_t nOldTime = pblock->nTime;
|
||||
int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast() + 1, GetAdjustedTime());
|
||||
int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime()))};
|
||||
|
||||
if (nOldTime < nNewTime) {
|
||||
pblock->nTime = nNewTime;
|
||||
|
@ -133,7 +133,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
|
|||
pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
|
||||
}
|
||||
|
||||
pblock->nTime = GetAdjustedTime();
|
||||
pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(GetAdjustedTime());
|
||||
m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
|
||||
|
||||
int nPackagesSelected = 0;
|
||||
|
|
|
@ -32,9 +32,9 @@ int64_t GetTimeOffset()
|
|||
return nTimeOffset;
|
||||
}
|
||||
|
||||
int64_t GetAdjustedTime()
|
||||
NodeClock::time_point GetAdjustedTime()
|
||||
{
|
||||
return GetTime() + GetTimeOffset();
|
||||
return NodeClock::now() + std::chrono::seconds{GetTimeOffset()};
|
||||
}
|
||||
|
||||
#define BITCOIN_TIMEDATA_MAX_SAMPLES 200
|
||||
|
|
|
@ -75,7 +75,7 @@ public:
|
|||
|
||||
/** Functions to keep track of adjusted P2P time */
|
||||
int64_t GetTimeOffset();
|
||||
int64_t GetAdjustedTime();
|
||||
NodeClock::time_point GetAdjustedTime();
|
||||
void AddTimeData(const CNetAddr& ip, int64_t nTime);
|
||||
|
||||
/**
|
||||
|
|
|
@ -3442,7 +3442,7 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
|
|||
* in ConnectBlock().
|
||||
* Note that -reindex-chainstate skips the validation that happens here!
|
||||
*/
|
||||
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, int64_t nAdjustedTime) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
|
||||
static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidationState& state, BlockManager& blockman, const ChainstateManager& chainman, const CBlockIndex* pindexPrev, NodeClock::time_point now) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
|
||||
{
|
||||
AssertLockHeld(::cs_main);
|
||||
assert(pindexPrev != nullptr);
|
||||
|
@ -3470,8 +3470,9 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
|
|||
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early");
|
||||
|
||||
// Check timestamp
|
||||
if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
|
||||
if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
|
||||
return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");
|
||||
}
|
||||
|
||||
// Reject blocks with outdated version
|
||||
if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB)) ||
|
||||
|
@ -3832,7 +3833,7 @@ bool TestBlockValidity(BlockValidationState& state,
|
|||
CChainState& chainstate,
|
||||
const CBlock& block,
|
||||
CBlockIndex* pindexPrev,
|
||||
const std::function<int64_t()>& adjusted_time_callback,
|
||||
const std::function<NodeClock::time_point()>& adjusted_time_callback,
|
||||
bool fCheckPOW,
|
||||
bool fCheckMerkleRoot)
|
||||
{
|
||||
|
|
|
@ -336,7 +336,7 @@ bool TestBlockValidity(BlockValidationState& state,
|
|||
CChainState& chainstate,
|
||||
const CBlock& block,
|
||||
CBlockIndex* pindexPrev,
|
||||
const std::function<int64_t()>& adjusted_time_callback,
|
||||
const std::function<NodeClock::time_point()>& adjusted_time_callback,
|
||||
bool fCheckPOW = true,
|
||||
bool fCheckMerkleRoot = true) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||
|
||||
|
@ -840,7 +840,7 @@ private:
|
|||
|
||||
const CChainParams m_chainparams;
|
||||
|
||||
const std::function<int64_t()> m_adjusted_time_callback;
|
||||
const std::function<NodeClock::time_point()> m_adjusted_time_callback;
|
||||
|
||||
//! Internal helper for ActivateSnapshot().
|
||||
[[nodiscard]] bool PopulateAndValidateSnapshot(
|
||||
|
|
Loading…
Add table
Reference in a new issue