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