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_H6#define BITCOIN_VERSIONBITS_H8#include <chain.h>9#include <sync.h>11#include <array>12#include <map>13#include <optional>14#include <vector>16class CChainParams;18/** What block version to use for new blocks (pre versionbits) */19inline constexpr int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4;20/** What bits to set in version for versionbits blocks */21inline constexpr int32_t VERSIONBITS_TOP_BITS = 0x20000000UL;22/** What bitmask determines whether versionbits is in use */23inline constexpr int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;24/** Total bits available for versionbits (BIP 323) */25inline constexpr int32_t VERSIONBITS_NUM_BITS = 5;27/** Opaque type for BIP9 state. See versionbits_impl.h for details. */28enum class ThresholdState : uint8_t;30// A map that gives the state for blocks whose height is a multiple of Period().31// The map is indexed by the block's parent, however, so all keys in the map32// will either be nullptr or a block with (height + 1) % Period() == 0.33typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;35/** Display status of an in-progress BIP9 softfork */36struct BIP9Stats {37/** Length of blocks of the BIP9 signalling period */38uint32_t period{0};39/** Number of blocks with the version bit set required to activate the softfork */40uint32_t threshold{0};41/** Number of blocks elapsed since the beginning of the current period */42uint32_t elapsed{0};43/** Number of blocks with the version bit set since the beginning of the current period */44uint32_t count{0};45/** False if there are not enough blocks left in this period to pass activation threshold */46bool possible{false};47};49/** Detailed status of an enabled BIP9 deployment */50struct BIP9Info {51/** Height at which current_state started */52int since{0};53/** String representing the current state */54std::string current_state{};55/** String representing the next block's state */56std::string next_state{};57/** For states where signalling is applicable, information about the signalling */58std::optional<BIP9Stats> stats;59/** Which blocks signalled; empty if signalling is not applicable */60std::vector<bool> signalling_blocks;61/** Height at which the deployment is active, if known. May be in the future. */62std::optional<int> active_since;63};65struct BIP9GBTStatus {66struct Info {67int bit;68uint32_t mask;69bool gbt_optional_rule;70};71std::map<std::string, const Info, std::less<>> signalling, locked_in, active;72};74/** BIP 9 allows multiple softforks to be deployed in parallel. We cache75* per-period state for every one we implement and warning state for each76* BIP 323 allowed bit. */77class VersionBitsCache78{79private:80Mutex m_mutex;81std::array<ThresholdConditionCache,VERSIONBITS_NUM_BITS> m_warning_caches GUARDED_BY(m_mutex);82std::array<ThresholdConditionCache,Consensus::MAX_VERSION_BITS_DEPLOYMENTS> m_caches GUARDED_BY(m_mutex);84public:85BIP9Info Info(const CBlockIndex& block_index, const Consensus::Params& params, Consensus::DeploymentPos id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);87BIP9GBTStatus GBTStatus(const CBlockIndex& block_index, const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);89/** Get the BIP9 state for a given deployment for the block after pindexPrev. */90bool IsActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);92/** Determine what nVersion a new block should use */93int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);95/** Check for unknown activations96* Returns a vector containing the bit number used for signalling and a bool97* indicating the deployment is likely to be ACTIVE, rather than merely LOCKED_IN. */98std::vector<std::pair<int,bool>> CheckUnknownActivations(const CBlockIndex* pindex, const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);100void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);101};103#endif // BITCOIN_VERSIONBITS_H