6 #include <chainparams.h> 27 mutable ThresholdConditionCache m_cache;
33 assert(dep.threshold <= dep.period);
35 assert(0 <= dep.min_activation_height);
46 std::vector<std::unique_ptr<CBlockIndex>> m_blocks;
47 const uint32_t m_start_time;
48 const uint32_t m_interval;
49 const int32_t m_signal;
50 const int32_t m_no_signal;
53 Blocks(uint32_t start_time, uint32_t interval, int32_t signal, int32_t no_signal)
54 : m_start_time{start_time}, m_interval{interval}, m_signal{signal}, m_no_signal{no_signal} {}
56 size_t size()
const {
return m_blocks.size(); }
60 return m_blocks.empty() ? nullptr : m_blocks.back().get();
66 header.
nVersion = signal ? m_signal : m_no_signal;
67 header.
nTime = m_start_time + m_blocks.size() * m_interval;
68 header.
nBits = 0x1d00ffff;
70 auto current_block = std::make_unique<CBlockIndex>(header);
71 current_block->pprev = tip();
72 current_block->nHeight = m_blocks.size();
73 current_block->BuildSkip();
75 return m_blocks.emplace_back(std::move(current_block)).get();
79 std::unique_ptr<const CChainParams> g_params;
85 assert(g_params !=
nullptr);
88 constexpr uint32_t MAX_START_TIME = 4102444800;
95 assert(interval < std::numeric_limits<int32_t>::max());
100 const uint32_t period = 32;
101 const size_t max_periods = 16;
102 const size_t max_blocks = 2 * period * max_periods;
106 assert(std::numeric_limits<uint32_t>::max() - MAX_START_TIME > interval * max_blocks);
113 if (ver_nosignal < 0)
return;
116 Blocks blocks(block_start_time, interval, ver_signal, ver_nosignal);
131 if (always_active_test) {
134 }
else if (never_active_test) {
143 dep.
nStartTime = block_start_time + start_block * interval;
144 dep.
nTimeout = block_start_time + end_block * interval;
156 if (!checker.Condition(ver_signal))
return;
157 if (checker.Condition(ver_nosignal))
return;
183 for (uint32_t b = 0; b < period; ++b) {
184 blocks.mine_block(signal);
188 if (blocks.size() + 2 * period > max_blocks)
break;
195 uint32_t blocks_sig = 0;
199 const int exp_since = checker.GetStateSinceHeightFor(prev);
204 last_stats.
period = period;
208 std::vector<bool> last_signals{};
210 int prev_next_height = (prev ==
nullptr ? 0 : prev->
nHeight + 1);
211 assert(exp_since <= prev_next_height);
214 for (uint32_t b = 1; b < period; ++b) {
215 const bool signal = (signalling_mask >> (b % 32)) & 1;
216 if (signal) ++blocks_sig;
218 CBlockIndex* current_block = blocks.mine_block(signal);
225 const int since = checker.GetStateSinceHeightFor(current_block);
226 assert(state == exp_state);
227 assert(since == exp_since);
230 std::vector<bool> signals;
231 const BIP9Stats stats = checker.GetStateStatisticsFor(current_block, &signals);
232 const BIP9Stats stats_no_signals = checker.GetStateStatisticsFor(current_block);
244 assert(signals.size() == last_signals.size() + 1);
245 assert(signals.back() == signal);
246 last_signals.push_back(signal);
247 assert(signals == last_signals);
256 bool signal = (signalling_mask >> (period % 32)) & 1;
257 if (signal) ++blocks_sig;
258 CBlockIndex* current_block = blocks.mine_block(signal);
261 const BIP9Stats stats = checker.GetStateStatisticsFor(current_block);
262 assert(stats.period == period);
264 assert(stats.elapsed == period);
265 assert(stats.count == blocks_sig);
266 assert(stats.possible == (stats.count + period >= stats.elapsed + dep.
threshold));
270 const int since = checker.GetStateSinceHeightFor(current_block);
273 assert(since % period == 0);
275 if (state == exp_state) {
276 assert(since == exp_since);
321 if (blocks.size() >= period * max_periods) {
326 if (always_active_test) {
331 }
else if (never_active_test) {
static constexpr int64_t NO_TIMEOUT
Constant for nTimeout very far in the future.
uint32_t threshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period...
int min_activation_height
If lock in occurs, delay activation until at least this block height.
Opaque type for BIP9 state.
ThresholdState
BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
uint32_t threshold
Number of blocks with the version bit set required to activate the softfork.
static constexpr int64_t ALWAYS_ACTIVE
Special value for nStartTime indicating that the deployment is always active.
const CBlock & GenesisBlock() const
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
uint32_t period
Period of blocks to check signalling in (usually retarget period, ie params.DifficultyAdjustmentInter...
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the state for pindex A based on parent pindexPrev B.
Struct for each individual consensus rule change using BIP9.
Class to implement versionbits logic.
int64_t nStartTime
Start MedianTime for version bits miner confirmation.
int64_t nPowTargetSpacing
static constexpr int64_t NEVER_ACTIVE
Special value for nStartTime indicating that the deployment is never active.
int64_t GetMedianTimePast() const
int32_t nVersion
block header
int64_t nTimeout
Timeout/expiry MedianTime for the deployment attempt.
The block chain is a tree shaped structure starting with the genesis block at the root...
static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION
What block version to use for new blocks (pre versionbits)
FuzzedDataProvider & fuzzed_data_provider
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
bool possible
False if there are not enough blocks left in this period to pass activation threshold.
int GetStateSinceHeightFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the height since when the ThresholdState has started for pindex A based on parent pindexPrev ...
uint32_t period
Length of blocks of the BIP9 signalling period.
int nHeight
height of the entry in the chain. The genesis block has height 0
const Consensus::Params & GetConsensus() const
int bit
Bit position to select the particular bit in nVersion.
T ConsumeIntegralInRange(T min, T max)
uint32_t count
Number of blocks with the version bit set since the beginning of the current period.
static const int32_t VERSIONBITS_NUM_BITS
Total bits available for versionbits.
uint32_t elapsed
Number of blocks elapsed since the beginning of the current period.