chainparams.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-2021 The Limenka 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 LIMENKA_KERNEL_CHAINPARAMS_H
   7  #define LIMENKA_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  enum class RDTSConsentFlag {
  28      RUNTIME_CHECK,
  29      IMPLICIT,
  30      RUNTIME_WARN,
  31      UNSUPPORTED_UNSAFE_NO_ENFORCEMENT,
  32  };
  33  
  34  extern RDTSConsentFlag g_rdts_consent;
  35  extern bool g_enable_rdts;
  36  extern bool g_rdts_warning;
  37  
  38  typedef std::map<int, uint256> MapCheckpoints;
  39  
  40  struct CCheckpointData {
  41      MapCheckpoints mapCheckpoints;
  42  
  43      int GetHeight() const {
  44          const auto& final_checkpoint = mapCheckpoints.rbegin();
  45          return final_checkpoint->first /* height */;
  46      }
  47  
  48      bool CheckBlock(int height, const uint256& hash) const {
  49          const auto i = mapCheckpoints.find(height);
  50          if (i == mapCheckpoints.end()) return true;
  51          return hash == i->second;
  52      }
  53  };
  54  
  55  struct AssumeutxoHash : public BaseHash<uint256> {
  56      explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}
  57  };
  58  
  59  /**
  60   * Holds configuration for use during UTXO snapshot load and validation. The contents
  61   * here are security critical, since they dictate which UTXO snapshots are recognized
  62   * as valid.
  63   */
  64  struct AssumeutxoData {
  65      int height;
  66  
  67      //! The expected hash of the deserialized UTXO set.
  68      AssumeutxoHash hash_serialized;
  69  
  70      //! Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex().
  71      //!
  72      //! We need to hardcode the value here because this is computed cumulatively using block data,
  73      //! which we do not necessarily have at the time of snapshot load.
  74      uint64_t m_chain_tx_count;
  75  
  76      //! The hash of the base block for this snapshot. Used to refer to assumeutxo data
  77      //! prior to having a loaded blockindex.
  78      uint256 blockhash;
  79  };
  80  
  81  /**
  82   * Holds various statistics on transactions within a chain. Used to estimate
  83   * verification progress during chain sync.
  84   *
  85   * See also: CChainParams::TxData, GuessVerificationProgress.
  86   */
  87  struct ChainTxData {
  88      int64_t nTime;    //!< UNIX timestamp of last known number of transactions
  89      uint64_t tx_count; //!< total number of transactions between genesis and that timestamp
  90      double dTxRate;   //!< estimated number of transactions per second after that timestamp
  91  };
  92  
  93  /**
  94   * CChainParams defines various tweakable parameters of a given instance of the
  95   * Limenka system.
  96   */
  97  class CChainParams
  98  {
  99  public:
 100      enum Base58Type {
 101          PUBKEY_ADDRESS,
 102          SCRIPT_ADDRESS,
 103          SECRET_KEY,
 104          EXT_PUBLIC_KEY,
 105          EXT_SECRET_KEY,
 106  
 107          MAX_BASE58_TYPES
 108      };
 109  
 110      const Consensus::Params& GetConsensus() const { return consensus; }
 111      const MessageStartChars& MessageStart() const { return pchMessageStart; }
 112      uint16_t GetDefaultPort() const { return nDefaultPort; }
 113      std::vector<int> GetAvailableSnapshotHeights() const;
 114  
 115      const CBlock& GenesisBlock() const { return genesis; }
 116      /** Default value for -checkmempool and -checkblockindex argument */
 117      bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; }
 118      /** If this chain is exclusively used for testing */
 119      bool IsTestChain() const { return m_chain_type != ChainType::MAIN; }
 120      /** If this chain allows time to be mocked */
 121      bool IsMockableChain() const { return m_is_mockable_chain; }
 122      uint64_t PruneAfterHeight() const { return nPruneAfterHeight; }
 123      /** Minimum free space (in GB) needed for data directory */
 124      uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; }
 125      /** Minimum free space (in GB) needed for data directory when pruned; Does not include prune target*/
 126      uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; }
 127      /** Whether it is possible to mine blocks on demand (no retargeting) */
 128      bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }
 129      /** Return the chain type string */
 130      std::string GetChainTypeString() const { return ChainTypeToString(m_chain_type); }
 131      /** Return the chain type */
 132      ChainType GetChainType() const { return m_chain_type; }
 133      /** Return the list of hostnames to look up for DNS seeds */
 134      const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
 135      const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
 136      const std::string& Bech32HRP() const { return bech32_hrp; }
 137      const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
 138      const CCheckpointData& Checkpoints() const { return checkpointData; }
 139      /** Drop all checkpoints (standalone fork chains mined from their own
 140       *  genesis must not inherit the parent chain's checkpoint list).
 141       *  Const-friendly: chainparams arg handling holds const params. */
 142      void ClearCheckpoints() const { checkpointData.mapCheckpoints.clear(); }
 143  
 144      std::optional<AssumeutxoData> AssumeutxoForHeight(int height) const
 145      {
 146          return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.height == height; });
 147      }
 148      std::optional<AssumeutxoData> AssumeutxoForBlockhash(const uint256& blockhash) const
 149      {
 150          return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.blockhash == blockhash; });
 151      }
 152  
 153      const ChainTxData& TxData() const { return chainTxData; }
 154  
 155      /**
 156       * SigNetOptions holds configurations for creating a signet CChainParams.
 157       */
 158      struct SigNetOptions {
 159          std::optional<std::vector<uint8_t>> challenge{};
 160          std::optional<std::vector<std::string>> seeds{};
 161          int64_t pow_target_spacing{10 * 60};
 162      };
 163  
 164      /**
 165       * VersionBitsParameters holds activation parameters
 166       */
 167      struct VersionBitsParameters {
 168          int64_t start_time;
 169          int64_t timeout;
 170          int min_activation_height;
 171          int max_activation_height{std::numeric_limits<int>::max()};
 172          int active_duration{std::numeric_limits<int>::max()};
 173          int threshold{0};  // 0 means use global nRuleChangeActivationThreshold
 174      };
 175  
 176      /**
 177       * RegTestOptions holds configurations for creating a regtest CChainParams.
 178       */
 179      struct RegTestOptions {
 180          std::unordered_map<Consensus::DeploymentPos, VersionBitsParameters> version_bits_parameters{};
 181          std::unordered_map<Consensus::BuriedDeployment, int> activation_heights{};
 182          bool fastprune{false};
 183          bool enforce_bip94{false};
 184      };
 185  
 186      static std::unique_ptr<const CChainParams> RegTest(const RegTestOptions& options);
 187      static std::unique_ptr<const CChainParams> SigNet(const SigNetOptions& options);
 188      static std::unique_ptr<const CChainParams> Main();
 189      static std::unique_ptr<const CChainParams> TestNet();
 190      static std::unique_ptr<const CChainParams> TestNet4();
 191      static std::unique_ptr<const CChainParams> Fork();
 192  
 193  protected:
 194      CChainParams() = default;
 195  
 196      Consensus::Params consensus;
 197      MessageStartChars pchMessageStart;
 198      uint16_t nDefaultPort;
 199      uint64_t nPruneAfterHeight;
 200      uint64_t m_assumed_blockchain_size;
 201      uint64_t m_assumed_chain_state_size;
 202      std::vector<std::string> vSeeds;
 203      std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
 204      std::string bech32_hrp;
 205      ChainType m_chain_type;
 206      CBlock genesis;
 207      std::vector<uint8_t> vFixedSeeds;
 208      bool fDefaultConsistencyChecks;
 209      bool m_is_mockable_chain;
 210      mutable CCheckpointData checkpointData;
 211      std::vector<AssumeutxoData> m_assumeutxo_data;
 212      ChainTxData chainTxData;
 213  };
 214  
 215  std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart);
 216  
 217  #endif // LIMENKA_KERNEL_CHAINPARAMS_H
 218