1 // Copyright (c) 2016-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 5 #ifndef BITCOIN_VERSIONBITS_H
6 #define BITCOIN_VERSIONBITS_H
7 8 #include <chain.h>
9 #include <sync.h>
10 11 #include <array>
12 #include <map>
13 #include <optional>
14 #include <vector>
15 16 class CChainParams;
17 18 /** What block version to use for new blocks (pre versionbits) */
19 static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4;
20 /** What bits to set in version for versionbits blocks */
21 static const int32_t VERSIONBITS_TOP_BITS = 0x20000000UL;
22 /** What bitmask determines whether versionbits is in use */
23 static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;
24 /** Total bits available for versionbits (BIP 323) */
25 static const int32_t VERSIONBITS_NUM_BITS = 5;
26 27 /** Opaque type for BIP9 state. See versionbits_impl.h for details. */
28 enum class ThresholdState : uint8_t;
29 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 map
32 // will either be nullptr or a block with (height + 1) % Period() == 0.
33 typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;
34 35 /** Display status of an in-progress BIP9 softfork */
36 struct BIP9Stats {
37 /** Length of blocks of the BIP9 signalling period */
38 uint32_t period{0};
39 /** Number of blocks with the version bit set required to activate the softfork */
40 uint32_t threshold{0};
41 /** Number of blocks elapsed since the beginning of the current period */
42 uint32_t elapsed{0};
43 /** Number of blocks with the version bit set since the beginning of the current period */
44 uint32_t count{0};
45 /** False if there are not enough blocks left in this period to pass activation threshold */
46 bool possible{false};
47 };
48 49 /** Detailed status of an enabled BIP9 deployment */
50 struct BIP9Info {
51 /** Height at which current_state started */
52 int since{0};
53 /** String representing the current state */
54 std::string current_state{};
55 /** String representing the next block's state */
56 std::string next_state{};
57 /** For states where signalling is applicable, information about the signalling */
58 std::optional<BIP9Stats> stats;
59 /** Which blocks signalled; empty if signalling is not applicable */
60 std::vector<bool> signalling_blocks;
61 /** Height at which the deployment is active, if known. May be in the future. */
62 std::optional<int> active_since;
63 };
64 65 struct BIP9GBTStatus {
66 struct Info {
67 int bit;
68 uint32_t mask;
69 bool gbt_optional_rule;
70 };
71 std::map<std::string, const Info, std::less<>> signalling, locked_in, active;
72 };
73 74 /** BIP 9 allows multiple softforks to be deployed in parallel. We cache
75 * per-period state for every one we implement and warning state for each
76 * BIP 323 allowed bit. */
77 class VersionBitsCache
78 {
79 private:
80 Mutex m_mutex;
81 std::array<ThresholdConditionCache,VERSIONBITS_NUM_BITS> m_warning_caches GUARDED_BY(m_mutex);
82 std::array<ThresholdConditionCache,Consensus::MAX_VERSION_BITS_DEPLOYMENTS> m_caches GUARDED_BY(m_mutex);
83 84 public:
85 BIP9Info Info(const CBlockIndex& block_index, const Consensus::Params& params, Consensus::DeploymentPos id) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
86 87 BIP9GBTStatus GBTStatus(const CBlockIndex& block_index, const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
88 89 /** Get the BIP9 state for a given deployment for the block after pindexPrev. */
90 bool IsActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
91 92 /** Determine what nVersion a new block should use */
93 int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
94 95 /** Check for unknown activations
96 * Returns a vector containing the bit number used for signalling and a bool
97 * indicating the deployment is likely to be ACTIVE, rather than merely LOCKED_IN. */
98 std::vector<std::pair<int,bool>> CheckUnknownActivations(const CBlockIndex* pindex, const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
99 100 void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
101 };
102 103 #endif // BITCOIN_VERSIONBITS_H
104