LCOV - code coverage report
Current view: top level - src/kernel - chainparams.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 76.7 % 30 23
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 2 2
Branches: 37.7 % 244 92

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

Generated by: LCOV version 2.0-1