1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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_CHAINPARAMS_H
7 #define BITCOIN_CHAINPARAMS_H
8 
9 #include <chainparamsbase.h>
10 #include <consensus/params.h>
11 #include <netaddress.h>
12 #include <primitives/block.h>
13 #include <protocol.h>
14 #include <util/hash_type.h>
15 
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 typedef std::map<int, uint256> MapCheckpoints;
21 
22 struct CCheckpointData {
23     MapCheckpoints mapCheckpoints;
24 
GetHeightCCheckpointData25     int GetHeight() const {
26         const auto& final_checkpoint = mapCheckpoints.rbegin();
27         return final_checkpoint->first /* height */;
28     }
29 };
30 
31 struct AssumeutxoHash : public BaseHash<uint256> {
AssumeutxoHashAssumeutxoHash32     explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}
33 };
34 
35 /**
36  * Holds configuration for use during UTXO snapshot load and validation. The contents
37  * here are security critical, since they dictate which UTXO snapshots are recognized
38  * as valid.
39  */
40 struct AssumeutxoData {
41     //! The expected hash of the deserialized UTXO set.
42     const AssumeutxoHash hash_serialized;
43 
44     //! Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
45     //!
46     //! We need to hardcode the value here because this is computed cumulatively using block data,
47     //! which we do not necessarily have at the time of snapshot load.
48     const unsigned int nChainTx;
49 };
50 
51 using MapAssumeutxo = std::map<int, const AssumeutxoData>;
52 
53 /**
54  * Holds various statistics on transactions within a chain. Used to estimate
55  * verification progress during chain sync.
56  *
57  * See also: CChainParams::TxData, GuessVerificationProgress.
58  */
59 struct ChainTxData {
60     int64_t nTime;    //!< UNIX timestamp of last known number of transactions
61     int64_t nTxCount; //!< total number of transactions between genesis and that timestamp
62     double dTxRate;   //!< estimated number of transactions per second after that timestamp
63 };
64 
65 /**
66  * CChainParams defines various tweakable parameters of a given instance of the
67  * Bitcoin system.
68  */
69 class CChainParams
70 {
71 public:
72     enum Base58Type {
73         PUBKEY_ADDRESS,
74         SCRIPT_ADDRESS,
75         SECRET_KEY,
76         EXT_PUBLIC_KEY,
77         EXT_SECRET_KEY,
78 
79         MAX_BASE58_TYPES
80     };
81 
GetConsensus()82     const Consensus::Params& GetConsensus() const { return consensus; }
MessageStart()83     const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
GetDefaultPort()84     uint16_t GetDefaultPort() const { return nDefaultPort; }
GetDefaultPort(Network net)85     uint16_t GetDefaultPort(Network net) const
86     {
87         return net == NET_I2P ? I2P_SAM31_PORT : GetDefaultPort();
88     }
GetDefaultPort(const std::string & addr)89     uint16_t GetDefaultPort(const std::string& addr) const
90     {
91         CNetAddr a;
92         return a.SetSpecial(addr) ? GetDefaultPort(a.GetNetwork()) : GetDefaultPort();
93     }
94 
GenesisBlock()95     const CBlock& GenesisBlock() const { return genesis; }
96     /** Default value for -checkmempool and -checkblockindex argument */
DefaultConsistencyChecks()97     bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; }
98     /** Policy: Filter transactions that do not match well-defined patterns */
RequireStandard()99     bool RequireStandard() const { return fRequireStandard; }
100     /** If this chain is exclusively used for testing */
IsTestChain()101     bool IsTestChain() const { return m_is_test_chain; }
102     /** If this chain allows time to be mocked */
IsMockableChain()103     bool IsMockableChain() const { return m_is_mockable_chain; }
PruneAfterHeight()104     uint64_t PruneAfterHeight() const { return nPruneAfterHeight; }
105     /** Minimum free space (in GB) needed for data directory */
AssumedBlockchainSize()106     uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; }
107     /** Minimum free space (in GB) needed for data directory when pruned; Does not include prune target*/
AssumedChainStateSize()108     uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; }
109     /** Whether it is possible to mine blocks on demand (no retargeting) */
MineBlocksOnDemand()110     bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }
111     /** Return the network string */
NetworkIDString()112     std::string NetworkIDString() const { return strNetworkID; }
113     /** Return the list of hostnames to look up for DNS seeds */
DNSSeeds()114     const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
Base58Prefix(Base58Type type)115     const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
Bech32HRP()116     const std::string& Bech32HRP() const { return bech32_hrp; }
FixedSeeds()117     const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
Checkpoints()118     const CCheckpointData& Checkpoints() const { return checkpointData; }
119 
120     //! Get allowed assumeutxo configuration.
121     //! @see ChainstateManager
Assumeutxo()122     const MapAssumeutxo& Assumeutxo() const { return m_assumeutxo_data; }
123 
TxData()124     const ChainTxData& TxData() const { return chainTxData; }
125 protected:
CChainParams()126     CChainParams() {}
127 
128     Consensus::Params consensus;
129     CMessageHeader::MessageStartChars pchMessageStart;
130     uint16_t nDefaultPort;
131     uint64_t nPruneAfterHeight;
132     uint64_t m_assumed_blockchain_size;
133     uint64_t m_assumed_chain_state_size;
134     std::vector<std::string> vSeeds;
135     std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
136     std::string bech32_hrp;
137     std::string strNetworkID;
138     CBlock genesis;
139     std::vector<uint8_t> vFixedSeeds;
140     bool fDefaultConsistencyChecks;
141     bool fRequireStandard;
142     bool m_is_test_chain;
143     bool m_is_mockable_chain;
144     CCheckpointData checkpointData;
145     MapAssumeutxo m_assumeutxo_data;
146     ChainTxData chainTxData;
147 };
148 
149 /**
150  * Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
151  * @returns a CChainParams* of the chosen chain.
152  * @throws a std::runtime_error if the chain is not supported.
153  */
154 std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain);
155 
156 /**
157  * Return the currently selected parameters. This won't change after app
158  * startup, except for unit tests.
159  */
160 const CChainParams &Params();
161 
162 /**
163  * Sets the params returned by Params() to those for the given chain name.
164  * @throws std::runtime_error when the chain is not supported.
165  */
166 void SelectParams(const std::string& chain);
167 
168 #endif // BITCOIN_CHAINPARAMS_H
169