Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2016-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_VERSIONBITS_IMPL_H6#define BITCOIN_VERSIONBITS_IMPL_H8#include <chain.h>9#include <sync.h>10#include <versionbits.h>12/** BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.13* State transitions happen during retarget period if conditions are met14* In case of reorg, transitions can go backward. Without transition, state is15* inherited between periods. All blocks of a period share the same state.16*/17enum class ThresholdState : uint8_t {18DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.19STARTED, // For blocks past the starttime.20LOCKED_IN, // For at least one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion, until min_activation_height is reached.21ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)22FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)23};25/** Get a string with the state name */26std::string StateName(ThresholdState state);28/**29* Abstract class that implements BIP9-style threshold logic, and caches results.30*/31class AbstractThresholdConditionChecker {32protected:33virtual bool Condition(const CBlockIndex* pindex) const =0;34virtual int64_t BeginTime() const =0;35virtual int64_t EndTime() const =0;36virtual int MinActivationHeight() const { return 0; }37virtual int Period() const =0;38virtual int Threshold() const =0;40public:41virtual ~AbstractThresholdConditionChecker() = default;43/** Returns the numerical statistics of an in-progress BIP9 softfork in the period including pindex44* If provided, signalling_blocks is set to true/false based on whether each block in the period signalled45*/46BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks = nullptr) const;47/** Returns the state for pindex A based on parent pindexPrev B. Applies any state transition if conditions are present.48* Caches state from first block of period. */49ThresholdState GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;50/** Returns the height since when the ThresholdState has started for pindex A based on parent pindexPrev B, all blocks of a period share the same */51int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;52};54/**55* Class to implement versionbits logic.56*/57class VersionBitsConditionChecker : public AbstractThresholdConditionChecker {58private:59const Consensus::BIP9Deployment& dep;61protected:62int64_t BeginTime() const override { return dep.nStartTime; }63int64_t EndTime() const override { return dep.nTimeout; }64int MinActivationHeight() const override { return dep.min_activation_height; }65int Period() const override { return dep.period; }66int Threshold() const override { return dep.threshold; }68bool Condition(const CBlockIndex* pindex) const override69{70return Condition(pindex->nVersion);71}73public:74explicit VersionBitsConditionChecker(const Consensus::BIP9Deployment& dep) : dep{dep} {}75explicit VersionBitsConditionChecker(const Consensus::Params& params, Consensus::DeploymentPos id) : VersionBitsConditionChecker{params.vDeployments[id]} {}77uint32_t Mask() const { return (uint32_t{1}) << dep.bit; }79bool Condition(int32_t nVersion) const80{81return (((nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) && (nVersion & Mask()) != 0);82}83};85#endif // BITCOIN_VERSIONBITS_IMPL_H