6#include <chainparams.h>
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();
79std::unique_ptr<const CChainParams> g_params;
85 assert(g_params !=
nullptr);
88constexpr 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);
108 const int64_t block_start_time = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(params.
GenesisBlock().
nTime, MAX_START_TIME);
111 const int32_t ver_signal = fuzzed_data_provider.ConsumeIntegral<int32_t>();
112 const int32_t ver_nosignal = fuzzed_data_provider.ConsumeIntegral<int32_t>();
113 if (ver_nosignal < 0)
return;
116 Blocks blocks(block_start_time, interval, ver_signal, ver_nosignal);
118 const bool always_active_test = fuzzed_data_provider.ConsumeBool();
119 const bool never_active_test = !always_active_test && fuzzed_data_provider.ConsumeBool();
125 dep.
threshold = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, period);
131 if (always_active_test) {
134 }
else if (never_active_test) {
140 int start_block = fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, period * (max_periods - 3));
141 int end_block = fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, period * (max_periods - 3));
143 dep.
nStartTime = block_start_time + start_block * interval;
144 dep.
nTimeout = block_start_time + end_block * interval;
147 if (fuzzed_data_provider.ConsumeBool()) dep.
nStartTime += interval / 2;
148 if (fuzzed_data_provider.ConsumeBool()) dep.
nTimeout += interval / 2;
150 dep.
min_activation_height = fuzzed_data_provider.ConsumeIntegralInRange<
int>(0, period * max_periods);
156 if (!checker.Condition(ver_signal))
return;
157 if (checker.Condition(ver_nosignal))
return;
177 const uint32_t signalling_mask = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
180 while (fuzzed_data_provider.remaining_bytes() > 0) {
182 bool signal = fuzzed_data_provider.ConsumeBool();
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);
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) {
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the state for pindex A based on parent pindexPrev B.
int GetStateSinceHeightFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the height since when the ThresholdState has started for pindex A based on parent pindexPrev ...
The block chain is a tree shaped structure starting with the genesis block at the root,...
int64_t GetMedianTimePast() const
int32_t nVersion
block header
int nHeight
height of the entry in the chain. The genesis block has height 0
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const CBlock & GenesisBlock() const
const Consensus::Params & GetConsensus() const
Class to implement versionbits logic.
Display status of an in-progress BIP9 softfork.
uint32_t count
Number of blocks with the version bit set since the beginning of the current period.
uint32_t threshold
Number of blocks with the version bit set required to activate the softfork.
uint32_t elapsed
Number of blocks elapsed since the beginning of the current period.
bool possible
False if there are not enough blocks left in this period to pass activation threshold.
uint32_t period
Length of blocks of the BIP9 signalling period.
Struct for each individual consensus rule change using BIP9.
int min_activation_height
If lock in occurs, delay activation until at least this block height.
int bit
Bit position to select the particular bit in nVersion.
static constexpr int64_t ALWAYS_ACTIVE
Special value for nStartTime indicating that the deployment is always active.
static constexpr int64_t NEVER_ACTIVE
Special value for nStartTime indicating that the deployment is never active.
uint32_t period
Period of blocks to check signalling in (usually retarget period, ie params.DifficultyAdjustmentInter...
uint32_t threshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,...
int64_t nTimeout
Timeout/expiry MedianTime for the deployment attempt.
static constexpr int64_t NO_TIMEOUT
Constant for nTimeout very far in the future.
int64_t nStartTime
Start MedianTime for version bits miner confirmation.
int64_t nPowTargetSpacing
std::map< const CBlockIndex *, ThresholdState > ThresholdConditionCache
static const int32_t VERSIONBITS_NUM_BITS
Total bits available for versionbits.
static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION
What block version to use for new blocks (pre versionbits)
ThresholdState
BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.