Coverage Report

Created: 2026-08-05 14:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/consensus/params.h
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#ifndef BITCOIN_CONSENSUS_PARAMS_H
7
#define BITCOIN_CONSENSUS_PARAMS_H
8
9
#include <script/verify_flags.h>
10
#include <uint256.h>
11
12
#include <array>
13
#include <chrono>
14
#include <cstdint>
15
#include <limits>
16
#include <map>
17
#include <vector>
18
19
namespace Consensus {
20
21
/**
22
 * A buried deployment is one where the height of the activation has been hardcoded into
23
 * the client implementation long after the consensus change has activated. See BIP 90.
24
 * Consensus changes for which the new rules are enforced from genesis are not listed here.
25
 */
26
enum BuriedDeployment : int16_t {
27
    // buried deployments get negative values to avoid overlap with DeploymentPos
28
    DEPLOYMENT_HEIGHTINCB = std::numeric_limits<int16_t>::min(),
29
    DEPLOYMENT_CLTV,
30
    DEPLOYMENT_DERSIG,
31
    DEPLOYMENT_CSV,
32
    // SCRIPT_VERIFY_WITNESS is enforced from genesis, but the check for downloading
33
    // missing witness data is not. BIP 147 also relies on hardcoded activation height.
34
    DEPLOYMENT_SEGWIT,
35
};
36
1.93M
constexpr bool ValidDeployment(BuriedDeployment dep) { return dep <= DEPLOYMENT_SEGWIT; }
37
38
enum DeploymentPos : uint16_t {
39
    DEPLOYMENT_TESTDUMMY,
40
    // NOTE: Also add new deployments to VersionBitsDeploymentInfo in deploymentinfo.cpp
41
    // Removing an entry may require bumping MinBIP9WarningHeight.
42
    MAX_VERSION_BITS_DEPLOYMENTS
43
};
44
182
constexpr bool ValidDeployment(DeploymentPos dep) { return dep < MAX_VERSION_BITS_DEPLOYMENTS; }
45
46
/**
47
 * Struct for each individual consensus rule change using BIP9.
48
 */
49
struct BIP9Deployment {
50
    /** Bit position to select the particular bit in nVersion. */
51
    int bit{28};
52
    /** Start MedianTime for version bits miner confirmation. Can be a date in the past */
53
    int64_t nStartTime{NEVER_ACTIVE};
54
    /** Timeout/expiry MedianTime for the deployment attempt. */
55
    int64_t nTimeout{NEVER_ACTIVE};
56
    /** If lock in occurs, delay activation until at least this block
57
     *  height.  Note that activation will only occur on a retarget
58
     *  boundary.
59
     */
60
    int min_activation_height{0};
61
    /** Period of blocks to check signalling in (usually retarget period, ie params.DifficultyAdjustmentInterval()) */
62
    uint32_t period{2016};
63
    /**
64
     * Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
65
     * which is also used for BIP9 deployments.
66
     * Examples: 1916 for 95%, 1512 for testchains.
67
     */
68
    uint32_t threshold{1916};
69
70
    /** Constant for nTimeout very far in the future. */
71
    static constexpr int64_t NO_TIMEOUT = std::numeric_limits<int64_t>::max();
72
73
    /** Special value for nStartTime indicating that the deployment is always active.
74
     *  This is useful for testing, as it means tests don't need to deal with the activation
75
     *  process (which takes at least 3 BIP9 intervals). Only tests that specifically test the
76
     *  behaviour during activation cannot use this. */
77
    static constexpr int64_t ALWAYS_ACTIVE = -1;
78
79
    /** Special value for nStartTime indicating that the deployment is never active.
80
     *  This is useful for integrating the code changes for a new feature
81
     *  prior to deploying it on some or all networks. */
82
    static constexpr int64_t NEVER_ACTIVE = -2;
83
};
84
85
/**
86
 * Parameters that influence chain consensus.
87
 */
88
struct Params {
89
    uint256 hashGenesisBlock;
90
    int nSubsidyHalvingInterval;
91
    /**
92
     * Hashes of blocks that
93
     * - are known to be consensus valid, and
94
     * - buried in the chain, and
95
     * - fail if the default script verify flags are applied.
96
     */
97
    std::map<uint256, script_verify_flags> script_flag_exceptions;
98
    /** Block height and hash at which BIP34 becomes active */
99
    int BIP34Height;
100
    uint256 BIP34Hash;
101
    /** Block height at which BIP65 becomes active */
102
    int BIP65Height;
103
    /** Block height at which BIP66 becomes active */
104
    int BIP66Height;
105
    /** Block height at which CSV (BIP68, BIP112 and BIP113) becomes active */
106
    int CSVHeight;
107
    /** Block height at which Segwit (BIP141, BIP143 and BIP147) becomes active.
108
     * Note that segwit v0 script rules are enforced on all blocks except the
109
     * BIP 16 exception blocks. */
110
    int SegwitHeight;
111
    /** Don't warn about unknown BIP 9 activations below this height.
112
     * This prevents us from warning about the CSV, segwit and taproot activations. */
113
    int MinBIP9WarningHeight;
114
    std::array<BIP9Deployment,MAX_VERSION_BITS_DEPLOYMENTS> vDeployments;
115
    /** Proof of work parameters */
116
    uint256 powLimit;
117
    bool fPowAllowMinDifficultyBlocks;
118
    /**
119
      * Enforce BIP94 timewarp attack mitigation. On testnet4 this also enforces
120
      * the block storm mitigation.
121
      */
122
    bool enforce_BIP94;
123
    bool fPowNoRetargeting;
124
    int64_t nPowTargetSpacing;
125
    int64_t nPowTargetTimespan;
126
    std::chrono::seconds PowTargetSpacing() const
127
65.5k
    {
128
65.5k
        return std::chrono::seconds{nPowTargetSpacing};
129
65.5k
    }
130
18.5M
    int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
131
    /** The best chain should have at least this much work */
132
    uint256 nMinimumChainWork;
133
    /** By default assume that the signatures in ancestors of this block are valid */
134
    uint256 defaultAssumeValid;
135
136
    /**
137
     * If true, witness commitments contain a payload equal to a Bitcoin Script solution
138
     * to the signet challenge. See BIP325.
139
     */
140
    bool signet_blocks{false};
141
    std::vector<uint8_t> signet_challenge;
142
143
    int DeploymentHeight(BuriedDeployment dep) const
144
1.93M
    {
145
1.93M
        switch (dep) {
146
159k
        case DEPLOYMENT_HEIGHTINCB:
147
159k
            return BIP34Height;
148
203k
        case DEPLOYMENT_CLTV:
149
203k
            return BIP65Height;
150
203k
        case DEPLOYMENT_DERSIG:
151
203k
            return BIP66Height;
152
526k
        case DEPLOYMENT_CSV:
153
526k
            return CSVHeight;
154
844k
        case DEPLOYMENT_SEGWIT:
155
844k
            return SegwitHeight;
156
1.93M
        } // no default case, so the compiler can warn about missing cases
157
0
        return std::numeric_limits<int>::max();
158
1.93M
    }
159
};
160
161
} // namespace Consensus
162
163
#endif // BITCOIN_CONSENSUS_PARAMS_H