Changeset View
Changeset View
Standalone View
Standalone View
src/policy/block/rtt.cpp
// Copyright (c) 2024 The Bitcoin developers | // Copyright (c) 2024 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <policy/block/rtt.h> | #include <policy/block/rtt.h> | ||||
#include <arith_uint256.h> | #include <arith_uint256.h> | ||||
#include <blockindex.h> | #include <blockindex.h> | ||||
#include <common/args.h> | |||||
#include <consensus/params.h> | #include <consensus/params.h> | ||||
#include <logging.h> | |||||
#include <pow/pow.h> | #include <pow/pow.h> | ||||
#include <timedata.h> | |||||
#include <tinyformat.h> | |||||
#include <algorithm> | #include <algorithm> | ||||
#include <cmath> | #include <cmath> | ||||
bool RTTPolicy::operator()(BlockPolicyValidationState &state) { | |||||
if (!gArgs.GetBoolArg("-enablertt", DEFAULT_ENABLE_RTT)) { | |||||
return true; | |||||
} | |||||
if (!m_blockIndex.pprev) { | |||||
return true; | |||||
} | |||||
auto rttWork = GetNextRTTWorkRequired(m_blockIndex.pprev, | |||||
m_blockIndex.GetHeaderReceivedTime(), | |||||
m_consensusParams); | |||||
if (!rttWork.has_value()) { | |||||
return true; | |||||
} | |||||
if (!CheckProofOfWork(m_blockIndex.GetBlockHash(), *rttWork, | |||||
m_consensusParams)) { | |||||
return state.Invalid( | |||||
BlockPolicyValidationResult::POLICY_VIOLATION, "policy-bad-rtt", | |||||
strprintf( | |||||
"Block %s at height %d violates the real time target %08x (%s " | |||||
"> %s, time diff %ds)\n", | |||||
m_blockIndex.GetBlockHash().ToString(), m_blockIndex.nHeight, | |||||
*rttWork, m_blockIndex.GetBlockHash().ToString(), | |||||
arith_uint256().SetCompact(*rttWork).ToString(), | |||||
m_blockIndex.GetHeaderReceivedTime() - | |||||
m_blockIndex.pprev->GetHeaderReceivedTime())); | |||||
} | |||||
return true; | |||||
} | |||||
/** | /** | ||||
* target(t) = target(prev_block) * RTT_CONSTANT_FACTOR * t^(RTT_K - 1) | * target(t) = target(prev_block) * RTT_CONSTANT_FACTOR * t^(RTT_K - 1) | ||||
* Where t is the time since the previous block timestamp and RTT_K is an | * Where t is the time since the previous block timestamp and RTT_K is an | ||||
* arbitrary integer >= 1. | * arbitrary integer >= 1. | ||||
* The factor is computed as: | * The factor is computed as: | ||||
* RTT_K * gamma(1 + 1/RTT_K)^RTT_K / (T^(RTT_K-1)) | * RTT_K * gamma(1 + 1/RTT_K)^RTT_K / (T^(RTT_K-1)) | ||||
* Where T is the target time between blocks, aka 600s. | * Where T is the target time between blocks, aka 600s. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |