Bitcoin ABC 0.30.5
P2P Digital Currency
rtt.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <policy/block/rtt.h>
6
7#include <arith_uint256.h>
8#include <blockindex.h>
9#include <common/args.h>
11#include <consensus/params.h>
12#include <logging.h>
13#include <pow/pow.h>
14#include <timedata.h>
15
16#include <tinyformat.h>
17
18#include <algorithm>
19#include <cmath>
20
22 if (!m_blockIndex.pprev) {
23 return true;
24 }
25
27 return true;
28 }
29
33 if (!rttWork.has_value()) {
34 return true;
35 }
36
39 return state.Invalid(
42 "Block %s at height %d violates the real time target %08x (%s "
43 "> %s, time diff %ds)\n",
46 arith_uint256().SetCompact(*rttWork).ToString(),
49 }
50
51 return true;
52}
53
78static constexpr double RTT_K{6.};
79
80static const double RTT_CONSTANT_FACTOR_2 =
81 RTT_K * std::pow(std::tgamma(1. + 1. / RTT_K), RTT_K) /
82 std::pow(600., RTT_K - 1.);
83static const double RTT_CONSTANT_FACTOR_5 =
84 RTT_K * std::pow(std::tgamma(1. + 1. / RTT_K), RTT_K) /
85 std::pow(2400., RTT_K - 1.);
86static const double RTT_CONSTANT_FACTOR_11 =
87 RTT_K * std::pow(std::tgamma(1. + 1. / RTT_K), RTT_K) /
88 std::pow(6000., RTT_K - 1.);
89static const double RTT_CONSTANT_FACTOR_17 =
90 RTT_K * std::pow(std::tgamma(1. + 1. / RTT_K), RTT_K) /
91 std::pow(9600., RTT_K - 1.);
92static const std::vector<double> RTT_CONSTANT_FACTOR = {
95 0., 0., 0.,
97 0., 0., 0.,
99};
100
101std::optional<uint32_t>
102GetNextRTTWorkRequired(const CBlockIndex *pprev, int64_t now,
103 const Consensus::Params &consensusParams) {
104 const CBlockIndex *previousIndex = pprev;
105 // We loop over the past 17 blocks to gather their receive time. We don't
106 // care about the receive time of the current block se we leave it at zero.
107 std::vector<int64_t> prevHeaderReceivedTime(18, 0);
108 for (size_t i = 1; i < 18; i++) {
109 if (!previousIndex) {
110 return std::nullopt;
111 }
112
113 prevHeaderReceivedTime[i] = previousIndex->GetHeaderReceivedTime();
114 if (prevHeaderReceivedTime[i] == 0) {
115 // If the reception time is zero, this past block is read from the
116 // disk, and we have no reason to apply RTT on the current block.
117 return std::nullopt;
118 }
119
120 previousIndex = previousIndex->pprev;
121 }
122
123 arith_uint256 prevTarget;
124 if (!NBitsToTarget(consensusParams, pprev->nBits, prevTarget)) {
125 return std::nullopt;
126 }
127 const double prevTargetDouble = prevTarget.getdouble();
128
129 double minTarget = UintToArith256(consensusParams.powLimit).getdouble();
130 for (size_t i : {2, 5, 11, 17}) {
131 // Zero (or negative) difftime are not possible, so clamp to minimum 1s
132 const int64_t diffTime =
133 std::max<int64_t>(1, now - prevHeaderReceivedTime[i]);
134
135 double target = prevTargetDouble * RTT_CONSTANT_FACTOR[i] *
136 std::pow(double(diffTime), RTT_K - 1.);
137
138 minTarget = std::min(minTarget, target);
139 }
140
141 arith_uint256 nextTarget = arith_uint256::fromDouble(minTarget);
142 if (minTarget < 1. ||
143 nextTarget > UintToArith256(consensusParams.powLimit)) {
144 return std::nullopt;
145 }
146
147 return nextTarget.GetCompact();
148}
149
150bool isRTTEnabled(const Consensus::Params &params, const CBlockIndex *pprev) {
151 return IsAugustoEnabled(params, pprev) &&
152 gArgs.GetBoolArg("-enablertt", DEFAULT_ENABLE_RTT);
153}
bool IsAugustoEnabled(const Consensus::Params &params, int64_t nMedianTimePast)
Check if May 15th, 2024 protocol upgrade has activated.
Definition: activation.cpp:117
ArgsManager gArgs
Definition: args.cpp:38
arith_uint256 UintToArith256(const uint256 &a)
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:556
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:32
int64_t GetHeaderReceivedTime() const
Definition: blockindex.h:184
uint32_t nBits
Definition: blockindex.h:93
BlockHash GetBlockHash() const
Definition: blockindex.h:146
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
const Consensus::Params & m_consensusParams
Definition: rtt.h:24
bool operator()(BlockPolicyValidationState &state) override
Definition: rtt.cpp:21
const CBlockIndex & m_blockIndex
Definition: rtt.h:25
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:101
256-bit unsigned big integer.
uint32_t GetCompact(bool fNegative=false) const
static arith_uint256 fromDouble(double d)
std::string ToString() const
Definition: uint256.h:80
double getdouble() const
std::string ToString() const
@ POLICY_VIOLATION
A block policy rule was violated. This block should be parked.
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:87
bool NBitsToTarget(const Consensus::Params &params, uint32_t nBits, arith_uint256 &target)
Convert a header bits difficulty representation to a 256 bits hash target.
Definition: pow.cpp:102
static const double RTT_CONSTANT_FACTOR_5
Definition: rtt.cpp:83
static const std::vector< double > RTT_CONSTANT_FACTOR
Definition: rtt.cpp:92
std::optional< uint32_t > GetNextRTTWorkRequired(const CBlockIndex *pprev, int64_t now, const Consensus::Params &consensusParams)
Compute the real time block hash target given the previous block parameters.
Definition: rtt.cpp:102
static const double RTT_CONSTANT_FACTOR_11
Definition: rtt.cpp:86
bool isRTTEnabled(const Consensus::Params &params, const CBlockIndex *pprev)
Whether the RTT feature is enabled.
Definition: rtt.cpp:150
static const double RTT_CONSTANT_FACTOR_17
Definition: rtt.cpp:89
static const double RTT_CONSTANT_FACTOR_2
Definition: rtt.cpp:80
static constexpr double RTT_K
target(t) = target(prev_block) * RTT_CONSTANT_FACTOR * t^(RTT_K - 1) Where t is the time since the pr...
Definition: rtt.cpp:78
static constexpr bool DEFAULT_ENABLE_RTT
Default for -enablertt.
Definition: rtt.h:20
Parameters that influence chain consensus.
Definition: params.h:34
uint256 powLimit
Proof of work parameters.
Definition: params.h:76
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202