Coverage Report

Created: 2024-10-29 12:15

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