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 <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> | |||||
| 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->nBits, m_blockIndex.pprev->GetHeaderReceivedTime(), | |||||
| m_blockIndex.GetHeaderReceivedTime(), m_consensusParams); | |||||
| if (!rttWork.has_value()) { | |||||
| return true; | |||||
| } | |||||
| const int64_t diffTime = m_blockIndex.GetHeaderReceivedTime() - | |||||
| m_blockIndex.pprev->GetHeaderReceivedTime(); | |||||
| if (!CheckProofOfWork(m_blockIndex.GetBlockHash(), rttWork->GetCompact(), | |||||
| m_consensusParams)) { | |||||
| return state.Invalid( | |||||
| BlockPolicyValidationResult::POLICY_VIOLATION, "policy-bad-rtt", | |||||
| strprintf("Block %s at height %d violates the " | |||||
| "real time target (%s > %s, time diff %ds)\n", | |||||
| m_blockIndex.GetBlockHash().ToString(), | |||||
| m_blockIndex.nHeight, | |||||
| m_blockIndex.GetBlockHash().ToString(), | |||||
| rttWork->ToString(), diffTime)); | |||||
| } | |||||
| 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 41 Lines • Show Last 20 Lines | |||||