/root/bitcoin/src/kernel/chainparams.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_KERNEL_CHAINPARAMS_H |
7 | | #define BITCOIN_KERNEL_CHAINPARAMS_H |
8 | | |
9 | | #include <consensus/params.h> |
10 | | #include <kernel/messagestartchars.h> |
11 | | #include <primitives/block.h> |
12 | | #include <uint256.h> |
13 | | #include <util/chaintype.h> |
14 | | #include <util/hash_type.h> |
15 | | #include <util/vector.h> |
16 | | |
17 | | #include <cstddef> |
18 | | #include <cstdint> |
19 | | #include <memory> |
20 | | #include <optional> |
21 | | #include <string> |
22 | | #include <unordered_map> |
23 | | #include <vector> |
24 | | |
25 | | struct AssumeutxoHash : public BaseHash<uint256> { |
26 | 0 | explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {} |
27 | | }; |
28 | | |
29 | | /** |
30 | | * Holds configuration for use during UTXO snapshot load and validation. The contents |
31 | | * here are security critical, since they dictate which UTXO snapshots are recognized |
32 | | * as valid. |
33 | | */ |
34 | | struct AssumeutxoData { |
35 | | int height; |
36 | | |
37 | | //! The expected hash of the deserialized UTXO set. |
38 | | AssumeutxoHash hash_serialized; |
39 | | |
40 | | //! Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex(). |
41 | | //! |
42 | | //! We need to hardcode the value here because this is computed cumulatively using block data, |
43 | | //! which we do not necessarily have at the time of snapshot load. |
44 | | uint64_t m_chain_tx_count; |
45 | | |
46 | | //! The hash of the base block for this snapshot. Used to refer to assumeutxo data |
47 | | //! prior to having a loaded blockindex. |
48 | | uint256 blockhash; |
49 | | }; |
50 | | |
51 | | /** |
52 | | * Holds various statistics on transactions within a chain. Used to estimate |
53 | | * verification progress during chain sync. |
54 | | * |
55 | | * See also: CChainParams::TxData, GuessVerificationProgress. |
56 | | */ |
57 | | struct ChainTxData { |
58 | | int64_t nTime; //!< UNIX timestamp of last known number of transactions |
59 | | uint64_t tx_count; //!< total number of transactions between genesis and that timestamp |
60 | | double dTxRate; //!< estimated number of transactions per second after that timestamp |
61 | | }; |
62 | | |
63 | | //! Configuration for headers sync memory usage. |
64 | | struct HeadersSyncParams { |
65 | | //! Distance in blocks between header commitments. |
66 | | size_t commitment_period{0}; |
67 | | //! Minimum number of validated headers to accumulate in the redownload |
68 | | //! buffer before feeding them into the permanent block index. |
69 | | size_t redownload_buffer_size{0}; |
70 | | }; |
71 | | |
72 | | /** |
73 | | * CChainParams defines various tweakable parameters of a given instance of the |
74 | | * Bitcoin system. |
75 | | */ |
76 | | class CChainParams |
77 | | { |
78 | | public: |
79 | | enum Base58Type { |
80 | | PUBKEY_ADDRESS, |
81 | | SCRIPT_ADDRESS, |
82 | | SECRET_KEY, |
83 | | EXT_PUBLIC_KEY, |
84 | | EXT_SECRET_KEY, |
85 | | |
86 | | MAX_BASE58_TYPES |
87 | | }; |
88 | | |
89 | 0 | const Consensus::Params& GetConsensus() const { return consensus; } |
90 | 0 | const MessageStartChars& MessageStart() const { return pchMessageStart; } |
91 | 0 | uint16_t GetDefaultPort() const { return nDefaultPort; } |
92 | | std::vector<int> GetAvailableSnapshotHeights() const; |
93 | | |
94 | 0 | const CBlock& GenesisBlock() const { return genesis; } |
95 | | /** Default value for -checkmempool and -checkblockindex argument */ |
96 | 0 | bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; } |
97 | | /** If this chain is exclusively used for testing */ |
98 | 0 | bool IsTestChain() const { return m_chain_type != ChainType::MAIN; } |
99 | | /** If this chain allows time to be mocked */ |
100 | 0 | bool IsMockableChain() const { return m_is_mockable_chain; } |
101 | 0 | uint64_t PruneAfterHeight() const { return nPruneAfterHeight; } |
102 | | /** Minimum free space (in GB) needed for data directory */ |
103 | 0 | uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; } |
104 | | /** Minimum free space (in GB) needed for data directory when pruned; Does not include prune target*/ |
105 | 0 | uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; } |
106 | | /** Whether it is possible to mine blocks on demand (no retargeting) */ |
107 | 0 | bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; } |
108 | | /** Return the chain type string */ |
109 | 0 | std::string GetChainTypeString() const { return ChainTypeToString(m_chain_type); } |
110 | | /** Return the chain type */ |
111 | 0 | ChainType GetChainType() const { return m_chain_type; } |
112 | | /** Return the list of hostnames to look up for DNS seeds */ |
113 | 0 | const std::vector<std::string>& DNSSeeds() const { return vSeeds; } |
114 | 0 | const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } |
115 | 0 | const std::string& Bech32HRP() const { return bech32_hrp; } |
116 | 0 | const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; } |
117 | 0 | const HeadersSyncParams& HeadersSync() const { return m_headers_sync_params; } |
118 | | |
119 | | std::optional<AssumeutxoData> AssumeutxoForHeight(int height) const |
120 | 0 | { |
121 | 0 | return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.height == height; }); |
122 | 0 | } |
123 | | std::optional<AssumeutxoData> AssumeutxoForBlockhash(const uint256& blockhash) const |
124 | 0 | { |
125 | 0 | return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.blockhash == blockhash; }); |
126 | 0 | } |
127 | | |
128 | 0 | const ChainTxData& TxData() const { return chainTxData; } |
129 | | |
130 | | /** |
131 | | * SigNetOptions holds configurations for creating a signet CChainParams. |
132 | | */ |
133 | | struct SigNetOptions { |
134 | | std::optional<std::vector<uint8_t>> challenge{}; |
135 | | std::optional<std::vector<std::string>> seeds{}; |
136 | | }; |
137 | | |
138 | | /** |
139 | | * VersionBitsParameters holds activation parameters |
140 | | */ |
141 | | struct VersionBitsParameters { |
142 | | int64_t start_time; |
143 | | int64_t timeout; |
144 | | int min_activation_height; |
145 | | }; |
146 | | |
147 | | /** |
148 | | * RegTestOptions holds configurations for creating a regtest CChainParams. |
149 | | */ |
150 | | struct RegTestOptions { |
151 | | std::unordered_map<Consensus::DeploymentPos, VersionBitsParameters> version_bits_parameters{}; |
152 | | std::unordered_map<Consensus::BuriedDeployment, int> activation_heights{}; |
153 | | bool fastprune{false}; |
154 | | bool enforce_bip94{false}; |
155 | | }; |
156 | | |
157 | | static std::unique_ptr<const CChainParams> RegTest(const RegTestOptions& options); |
158 | | static std::unique_ptr<const CChainParams> SigNet(const SigNetOptions& options); |
159 | | static std::unique_ptr<const CChainParams> Main(); |
160 | | static std::unique_ptr<const CChainParams> TestNet(); |
161 | | static std::unique_ptr<const CChainParams> TestNet4(); |
162 | | |
163 | | protected: |
164 | 0 | CChainParams() = default; |
165 | | |
166 | | Consensus::Params consensus; |
167 | | MessageStartChars pchMessageStart; |
168 | | uint16_t nDefaultPort; |
169 | | uint64_t nPruneAfterHeight; |
170 | | uint64_t m_assumed_blockchain_size; |
171 | | uint64_t m_assumed_chain_state_size; |
172 | | std::vector<std::string> vSeeds; |
173 | | std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES]; |
174 | | std::string bech32_hrp; |
175 | | ChainType m_chain_type; |
176 | | CBlock genesis; |
177 | | std::vector<uint8_t> vFixedSeeds; |
178 | | bool fDefaultConsistencyChecks; |
179 | | bool m_is_mockable_chain; |
180 | | std::vector<AssumeutxoData> m_assumeutxo_data; |
181 | | ChainTxData chainTxData; |
182 | | HeadersSyncParams m_headers_sync_params; |
183 | | }; |
184 | | |
185 | | std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart); |
186 | | |
187 | | #endif // BITCOIN_KERNEL_CHAINPARAMS_H |