1 // Copyright (c) 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 #include <chainparams.h>
7 
8 #include <chainparamsseeds.h>
9 #include <consensus/merkle.h>
10 #include <hash.h> // for signet block challenge hash
11 #include <tinyformat.h>
12 #include <util/system.h>
13 #include <util/strencodings.h>
14 #include <versionbitsinfo.h>
15 
16 #include <assert.h>
17 
18 #include <boost/algorithm/string/classification.hpp>
19 #include <boost/algorithm/string/split.hpp>
20 
IsHistoricBug(const uint256 & txid,unsigned nHeight,BugType & type) const21 bool CChainParams::IsHistoricBug(const uint256& txid, unsigned nHeight, BugType& type) const
22 {
23     const std::pair<unsigned, uint256> key(nHeight, txid);
24     std::map<std::pair<unsigned, uint256>, BugType>::const_iterator mi;
25 
26     mi = mapHistoricBugs.find (key);
27     if (mi != mapHistoricBugs.end ())
28     {
29         type = mi->second;
30         return true;
31     }
32 
33     return false;
34 }
35 
CreateGenesisBlock(const CScript & genesisInputScript,const CScript & genesisOutputScript,uint32_t nTime,uint32_t nNonce,uint32_t nBits,int32_t nVersion,const CAmount & genesisReward)36 static CBlock CreateGenesisBlock(const CScript& genesisInputScript, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
37 {
38     CMutableTransaction txNew;
39     txNew.nVersion = 1;
40     txNew.vin.resize(1);
41     txNew.vout.resize(1);
42     txNew.vin[0].scriptSig = genesisInputScript;
43     txNew.vout[0].nValue = genesisReward;
44     txNew.vout[0].scriptPubKey = genesisOutputScript;
45 
46     CBlock genesis;
47     genesis.nTime    = nTime;
48     genesis.nBits    = nBits;
49     genesis.nNonce   = nNonce;
50     genesis.nVersion = nVersion;
51     genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
52     genesis.hashPrevBlock.SetNull();
53     genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
54     return genesis;
55 }
56 
57 /**
58  * Build the genesis block. Note that the output of its generation
59  * transaction cannot be spent since it did not originally exist in the
60  * database.
61  */
CreateGenesisBlock(uint32_t nTime,uint32_t nNonce,uint32_t nBits,int32_t nVersion,const CAmount & genesisReward)62 static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
63 {
64     const char* pszTimestamp = "... choose what comes next.  Lives of your own, or a return to chains. -- V";
65     const CScript genesisInputScript = CScript() << 0x1c007fff << CScriptNum(522) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
66     const CScript genesisOutputScript = CScript() << ParseHex("04b620369050cd899ffbbc4e8ee51e8c4534a855bb463439d63d235d4779685d8b6f4870a238cf365ac94fa13ef9a2a22cd99d0d5ee86dcabcafce36c7acf43ce5") << OP_CHECKSIG;
67     return CreateGenesisBlock(genesisInputScript, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
68 }
69 
70 /**
71  * Build genesis block for testnet.  In Namecoin, it has a changed timestamp
72  * and output script (it uses Bitcoin's).
73  */
CreateTestnetGenesisBlock(uint32_t nTime,uint32_t nNonce,uint32_t nBits,int32_t nVersion,const CAmount & genesisReward)74 static CBlock CreateTestnetGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
75 {
76     const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
77     const CScript genesisInputScript = CScript() << 0x1d00ffff << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
78     const CScript genesisOutputScript = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
79     return CreateGenesisBlock(genesisInputScript, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
80 }
81 
82 /**
83  * Main network
84  */
85 class CMainParams : public CChainParams {
86 public:
CMainParams()87     CMainParams() {
88         strNetworkID = CBaseChainParams::MAIN;
89         consensus.signet_blocks = false;
90         consensus.signet_challenge.clear();
91         consensus.nSubsidyHalvingInterval = 210000;
92         consensus.BIP16Height = 475000;
93         /* Note that these are not the actual activation heights, but blocks
94            after them.  They are too deep in the chain to be ever reorged,
95            and thus this is also fine.  */
96         consensus.BIP34Height = 250000;
97         consensus.BIP65Height = 335000;
98         consensus.BIP66Height = 250000;
99         /* Namecoin activates CSV/Segwit with BIP16.  */
100         consensus.CSVHeight = 475000;
101         consensus.SegwitHeight = 475000;
102         consensus.MinBIP9WarningHeight = 477016; // segwit activation height + miner confirmation window
103         consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
104         consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
105         consensus.nPowTargetSpacing = 10 * 60;
106         consensus.fPowAllowMinDifficultyBlocks = false;
107         consensus.fPowNoRetargeting = false;
108         consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
109         consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
110         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
111         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
112         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
113 
114         // Deployment of Taproot (BIPs 340-342)
115         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
116         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 1199145601; // January 1, 2008
117         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1230767999; // December 31, 2008
118 
119         // The best chain should have at least this much work.
120         // The value is the chain work of the Namecoin mainnet chain at height
121         // 530'000, with best block hash:
122         // 992d2364a40f7ecc57e518655b99254ca98806a950e6227ad022618c95a75e88
123         consensus.nMinimumChainWork = uint256S("0x0000000000000000000000000000000000000000112dfaade470b8d8a00d2ea4");
124 
125         // By default assume that the signatures in ancestors of this block are valid.
126         consensus.defaultAssumeValid = uint256S("0x992d2364a40f7ecc57e518655b99254ca98806a950e6227ad022618c95a75e88"); // 530'000
127 
128         consensus.nAuxpowChainId = 0x0001;
129         consensus.nAuxpowStartHeight = 19200;
130         consensus.fStrictChainId = true;
131         consensus.nLegacyBlocksBefore = 19200;
132 
133         consensus.rules.reset(new Consensus::MainNetConsensus());
134 
135         /**
136          * The message start string is designed to be unlikely to occur in normal data.
137          * The characters are rarely used upper ASCII, not valid as UTF-8, and produce
138          * a large 32-bit integer with any alignment.
139          */
140         pchMessageStart[0] = 0xf9;
141         pchMessageStart[1] = 0xbe;
142         pchMessageStart[2] = 0xb4;
143         pchMessageStart[3] = 0xfe;
144         nDefaultPort = 8334;
145         nPruneAfterHeight = 100000;
146         m_assumed_blockchain_size = 7;
147         m_assumed_chain_state_size = 1;
148 
149         genesis = CreateGenesisBlock(1303000001, 0xa21ea192u, 0x1c007fff, 1, 50 * COIN);
150         consensus.hashGenesisBlock = genesis.GetHash();
151         assert(consensus.hashGenesisBlock == uint256S("0x000000000062b72c5e2ceb45fbc8587e807c155b0da735e6483dfba2f0a9c770"));
152         assert(genesis.hashMerkleRoot == uint256S("0x41c62dbd9068c89a449525e3cd5ac61b20ece28c3c38b3f35b2161f0e6d3cb0d"));
153 
154         // Note that of those which support the service bits prefix, most only support a subset of
155         // possible options.
156         // This is fine at runtime as we'll fall back to using them as an addrfetch if they don't support the
157         // service bits we want, but we should get them updated to support all service bits wanted by any
158         // release ASAP to avoid it where possible.
159         vSeeds.emplace_back("nmc.seed.quisquis.de");
160         vSeeds.emplace_back("seed.nmc.markasoftware.com");
161 
162         base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,52);
163         base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,13);
164         base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,180);
165         /* FIXME: Update these below.  */
166         base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};
167         base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4};
168 
169         bech32_hrp = "nc";
170 
171         vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));
172 
173         fDefaultConsistencyChecks = false;
174         fRequireStandard = true;
175         m_is_test_chain = false;
176         m_is_mockable_chain = false;
177 
178         checkpointData = {
179             {
180                 {  2016, uint256S("0000000000660bad0d9fbde55ba7ee14ddf766ed5f527e3fbca523ac11460b92")},
181                 {  4032, uint256S("0000000000493b5696ad482deb79da835fe2385304b841beef1938655ddbc411")},
182                 {  6048, uint256S("000000000027939a2e1d8bb63f36c47da858e56d570f143e67e85068943470c9")},
183                 {  8064, uint256S("000000000003a01f708da7396e54d081701ea406ed163e519589717d8b7c95a5")},
184                 { 10080, uint256S("00000000000fed3899f818b2228b4f01b9a0a7eeee907abd172852df71c64b06")},
185                 { 12096, uint256S("0000000000006c06988ff361f124314f9f4bb45b6997d90a7ee4cedf434c670f")},
186                 { 14112, uint256S("00000000000045d95e0588c47c17d593c7b5cb4fb1e56213d1b3843c1773df2b")},
187                 { 16128, uint256S("000000000001d9964f9483f9096cf9d6c6c2886ed1e5dec95ad2aeec3ce72fa9")},
188                 { 18940, uint256S("00000000000087f7fc0c8085217503ba86f796fa4984f7e5a08b6c4c12906c05")},
189                 { 30240, uint256S("e1c8c862ff342358384d4c22fa6ea5f669f3e1cdcf34111f8017371c3c0be1da")},
190                 { 57000, uint256S("aa3ec60168a0200799e362e2b572ee01f3c3852030d07d036e0aa884ec61f203")},
191                 {112896, uint256S("73f880e78a04dd6a31efc8abf7ca5db4e262c4ae130d559730d6ccb8808095bf")},
192                 {182000, uint256S("d47b4a8fd282f635d66ce34ebbeb26ffd64c35b41f286646598abfd813cba6d9")},
193                 {193000, uint256S("3b85e70ba7f5433049cfbcf0ae35ed869496dbedcd1c0fafadb0284ec81d7b58")},
194                 {250000, uint256S("514ec75480df318ffa7eb4eff82e1c583c961aa64cce71b5922662f01ed1686a")},
195                 {400000, uint256S("9d90cb7a56827c70b13192f1b2c6d6b2e6188abc13c5112d47cfd2f8efba8cce")},
196                 {474000, uint256S("83a3251ce38bf08481c3b6ab9128e5d0cbeedd0907dae64029d8669f35a64ad2")},
197             }
198         };
199 
200         chainTxData = ChainTxData{
201             // Data from RPC: getchaintxstats 4096 992d2364a40f7ecc57e518655b99254ca98806a950e6227ad022618c95a75e88
202             /* nTime    */ 1603561382,
203             /* nTxCount */ 5454542,
204             /* dTxRate  */ 0.006425929433975592,
205         };
206 
207         /* See also doc/NamecoinBugs.txt for more explanation on the
208            historical bugs added below.  */
209 
210         /* These transactions have name outputs but a non-Namecoin tx version.
211            They contain NAME_NEWs, which are fine, and also NAME_FIRSTUPDATE.
212            The latter are not interpreted by namecoind, thus also ignore
213            them for us here.  */
214         addBug(98423, "bff3ed6873e5698b97bf0c28c29302b59588590b747787c7d1ef32decdabe0d1", BUG_FULLY_IGNORE);
215         addBug(98424, "e9b211007e5cac471769212ca0f47bb066b81966a8e541d44acf0f8a1bd24976", BUG_FULLY_IGNORE);
216         addBug(98425, "8aa2b0fc7d1033de28e0192526765a72e9df0c635f7305bdc57cb451ed01a4ca", BUG_FULLY_IGNORE);
217 
218         /* These are non-Namecoin tx that contain just NAME_NEWs.  Those were
219            handled with a special rule previously, but now they are fully
220            disallowed and we handle the few exceptions here.  It is fine to
221            "ignore" them, as their outputs need no special Namecoin handling
222            before they are reused in a NAME_FIRSTUPDATE.  */
223         addBug(98318, "0ae5e958ff05ad8e273222656d98d076097def6d36f781a627c584b859f4727b", BUG_FULLY_IGNORE);
224         addBug(98321, "aca8ce46da1bbb9bb8e563880efcd9d6dd18342c446d6f0e3d4b964a990d1c27", BUG_FULLY_IGNORE);
225         addBug(98424, "c29b0d9d478411462a8ac29946bf6fdeca358a77b4be15cd921567eb66852180", BUG_FULLY_IGNORE);
226         addBug(98425, "221719b360f0c83fa5b1c26fb6b67c5e74e4e7c6aa3dce55025da6759f5f7060", BUG_FULLY_IGNORE);
227         addBug(193518, "597370b632efb35d5ed554c634c7af44affa6066f2a87a88046532d4057b46f8", BUG_FULLY_IGNORE);
228         addBug(195605, "0bb8c7807a9756aefe62c271770b313b31dee73151f515b1ac2066c50eaeeb91", BUG_FULLY_IGNORE);
229         addBug(195639, "3181930765b970fc43cd31d53fc6fc1da9439a28257d9067c3b5912d23eab01c", BUG_FULLY_IGNORE);
230         addBug(195639, "e815e7d774937d96a4b265ed4866b7e3dc8d9f2acb8563402e216aba6edd1e9e", BUG_FULLY_IGNORE);
231         addBug(195639, "cdfe6eda068e09fe760a70bec201feb041b8c660d0e98cbc05c8aa4106eae6ab", BUG_FULLY_IGNORE);
232         addBug(195641, "1e29e937b2a9e1f18af500371b8714157cf5ac7c95461913e08ce402de64ae75", BUG_FULLY_IGNORE);
233         addBug(195648, "d44ed6c0fac251931465f9123ada8459ec954cc6c7b648a56c9326ff7b13f552", BUG_FULLY_IGNORE);
234         addBug(197711, "dd77aea50a189935d0ef36a04856805cd74600a53193c539eb90c1e1c0f9ecac", BUG_FULLY_IGNORE);
235         addBug(204151, "f31875dfaf94bd3a93cfbed0e22d405d1f2e49b4d0750cb13812adc5e57f1e47", BUG_FULLY_IGNORE);
236 
237         /* This transaction has both a NAME_NEW and a NAME_FIRSTUPDATE as
238            inputs.  This was accepted due to the "argument concatenation" bug.
239            It is fine to accept it as valid and just process the NAME_UPDATE
240            output that builds on the NAME_FIRSTUPDATE input.  (NAME_NEW has no
241            special side-effect in applying anyway.)  */
242         addBug(99381, "774d4c446cecfc40b1c02fdc5a13be6d2007233f9d91daefab6b3c2e70042f05", BUG_FULLY_APPLY);
243 
244         /* These were libcoin's name stealing bugs.  */
245         addBug(139872, "2f034f2499c136a2c5a922ca4be65c1292815c753bbb100a2a26d5ad532c3919", BUG_IN_UTXO);
246         addBug(139936, "c3e76d5384139228221cce60250397d1b87adf7366086bc8d6b5e6eee03c55c7", BUG_FULLY_IGNORE);
247     }
248 
DefaultCheckNameDB() const249     int DefaultCheckNameDB () const override
250     {
251         return -1;
252     }
253 };
254 
255 /**
256  * Testnet (v3)
257  */
258 class CTestNetParams : public CChainParams {
259 public:
CTestNetParams()260     CTestNetParams() {
261         strNetworkID = CBaseChainParams::TESTNET;
262         consensus.signet_blocks = false;
263         consensus.signet_challenge.clear();
264         consensus.nSubsidyHalvingInterval = 210000;
265         consensus.BIP16Height = 232000;
266         /* As before, these are not the actual activation heights but some
267            blocks after them.  */
268         consensus.BIP34Height = 130000;
269         consensus.BIP65Height = 130000;
270         consensus.BIP66Height = 130000;
271         /* Namecoin activates CSV/Segwit with BIP16.  */
272         consensus.CSVHeight = 232000;
273         consensus.SegwitHeight = 232000;
274         consensus.MinBIP9WarningHeight = 234016; // segwit activation height + miner confirmation window
275         consensus.powLimit = uint256S("0000000fffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
276         consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
277         consensus.nPowTargetSpacing = 10 * 60;
278         consensus.fPowAllowMinDifficultyBlocks = true;
279         consensus.nMinDifficultySince = 1394838000; // 15 Mar 2014
280         consensus.fPowNoRetargeting = false;
281         consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains
282         consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
283         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
284         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
285         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
286 
287         // Deployment of Taproot (BIPs 340-342)
288         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
289         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 1199145601; // January 1, 2008
290         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1230767999; // December 31, 2008
291 
292         // The best chain should have at least this much work.
293         // The value is the chain work of the Namecoin testnet chain at height
294         // 233,000, with best block hash:
295         // bc66fc22b8a2988bdc519c4c6aa431bb57201e5102ad8b8272fcde2937b4d2f7
296         consensus.nMinimumChainWork = uint256S("0x000000000000000000000000000000000000000000000000ed17e3004a583c4f");
297 
298         // By default assume that the signatures in ancestors of this block are valid.
299         consensus.defaultAssumeValid = uint256S("0xbc66fc22b8a2988bdc519c4c6aa431bb57201e5102ad8b8272fcde2937b4d2f7"); // 233,100
300 
301         consensus.nAuxpowStartHeight = 0;
302         consensus.nAuxpowChainId = 0x0001;
303         consensus.fStrictChainId = false;
304         consensus.nLegacyBlocksBefore = -1;
305 
306         consensus.rules.reset(new Consensus::TestNetConsensus());
307 
308         pchMessageStart[0] = 0xfa;
309         pchMessageStart[1] = 0xbf;
310         pchMessageStart[2] = 0xb5;
311         pchMessageStart[3] = 0xfe;
312         nDefaultPort = 18334;
313         nPruneAfterHeight = 1000;
314         m_assumed_blockchain_size = 1;
315         m_assumed_chain_state_size = 1;
316 
317         genesis = CreateTestnetGenesisBlock(1296688602, 0x16ec0bff, 0x1d07fff8, 1, 50 * COIN);
318         consensus.hashGenesisBlock = genesis.GetHash();
319         assert(consensus.hashGenesisBlock == uint256S("00000007199508e34a9ff81e6ec0c477a4cccff2a4767a8eee39c11db367b008"));
320         assert(genesis.hashMerkleRoot == uint256S("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
321 
322         vFixedSeeds.clear();
323         vSeeds.clear();
324         // nodes with support for servicebits filtering should be at the top
325         vSeeds.emplace_back("dnsseed.test.namecoin.webbtc.com");
326         vSeeds.emplace_back("ncts.roanapur.info");
327 
328         base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
329         base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
330         base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,239);
331         /* FIXME: Update these below.  */
332         base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
333         base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
334 
335         bech32_hrp = "tn";
336 
337         vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));
338 
339         fDefaultConsistencyChecks = false;
340         fRequireStandard = false;
341         m_is_test_chain = true;
342         m_is_mockable_chain = false;
343 
344         checkpointData = {
345             {
346                 {  2016, uint256S("00000000b9e4132e1a803114bc88df3e49184a3c794c01a6eac334f12f4ccadb")},
347                 {  4032, uint256S("00000003fbc13a48b8de5c8742044c84b800edeabff8b39f7f23ac572c6d80ce")},
348                 {  8064, uint256S("f594a75db40244bc7baa808a695f796ba81cae5bb48fa920e367cdd31dbfb0e3")},
349                 { 10080, uint256S("398d44a1a6e58dce3f7463217f677c2532e42a83696dcc5d4d97329c00a10891")},
350                 { 12096, uint256S("22c9278493cda563565fc2a4250eff48bd68ed40cb5fb30029ca08ea6120ddab")},
351                 { 14112, uint256S("83bade3e3d88845eb52de90311a8017b1cdf725b15d19bc89c47a568f7b4e08c")},
352                 { 16128, uint256S("f456354835623f733bb928ed77d97ae06b96ad6c40aba63f51f94f06e905effc")},
353                 { 18144, uint256S("c0ec570117822ca3c76abd1d10449b283d8ad39c64290d6abafe2bed23917886")},
354                 { 34715, uint256S("0000000580cf4342f869e278d94d7e67d2ac8cae4a411082e0fd518a8091ebf2")},
355                 { 48384, uint256S("00000001d528af69dce584f882e3bdb36127104988607b726591cc5e62287922")},
356                 { 60480, uint256S("d3af823c32e890ca589dd4277aa4d27b8cd290396b7e0eeeee5121481fd43ca5")},
357                 {130000, uint256S("e0a05455d89a54bb7c1b5bb785d6b1b7c5bda42ed4ce8dc19d68652ba8835954")},
358                 {231000, uint256S("4964042a9c9ca5f1e104246f4d70ecb4f7217e02a2656379560e4ee4590f9870")},
359             }
360         };
361 
362         chainTxData = ChainTxData{
363             // Data from RPC: getchaintxstats 4096 bc66fc22b8a2988bdc519c4c6aa431bb57201e5102ad8b8272fcde2937b4d2f7
364             /* nTime    */ 1573859059,
365             /* nTxCount */ 276907,
366             /* dTxRate  */ 0.0002269357829851853,
367         };
368 
369         assert(mapHistoricBugs.empty());
370     }
371 
DefaultCheckNameDB() const372     int DefaultCheckNameDB () const override
373     {
374         return -1;
375     }
376 };
377 
378 /**
379  * Signet
380  */
381 class SigNetParams : public CChainParams {
382 public:
SigNetParams(const ArgsManager & args)383     explicit SigNetParams(const ArgsManager& args) {
384         std::vector<uint8_t> bin;
385         vSeeds.clear();
386 
387         if (!args.IsArgSet("-signetchallenge")) {
388             /* FIXME: Adjust the default signet challenge to something else if
389                we want to use signet for Namecoin.  */
390             bin = ParseHex("512103ad5e0edad18cb1f0fc0d28a3d4f1f3e445640337489abb10404f2d1e086be430210359ef5021964fe22d6f8e05b2463c9540ce96883fe3b278760f048f5189f2e6c452ae");
391             //vSeeds.emplace_back("178.128.221.177");
392 
393             consensus.nMinimumChainWork = uint256S("0x00000000000000000000000000000000000000000000000000000019fd16269a");
394             consensus.defaultAssumeValid = uint256S("0x0000002a1de0f46379358c1fd09906f7ac59adf3712323ed90eb59e4c183c020"); // 9434
395             m_assumed_blockchain_size = 1;
396             m_assumed_chain_state_size = 0;
397             chainTxData = ChainTxData{
398                 // Data from RPC: getchaintxstats 4096 0000002a1de0f46379358c1fd09906f7ac59adf3712323ed90eb59e4c183c020
399                 /* nTime    */ 1603986000,
400                 /* nTxCount */ 9582,
401                 /* dTxRate  */ 0.00159272030651341,
402             };
403         } else {
404             const auto signet_challenge = args.GetArgs("-signetchallenge");
405             if (signet_challenge.size() != 1) {
406                 throw std::runtime_error(strprintf("%s: -signetchallenge cannot be multiple values.", __func__));
407             }
408             bin = ParseHex(signet_challenge[0]);
409 
410             consensus.nMinimumChainWork = uint256{};
411             consensus.defaultAssumeValid = uint256{};
412             m_assumed_blockchain_size = 0;
413             m_assumed_chain_state_size = 0;
414             chainTxData = ChainTxData{
415                 0,
416                 0,
417                 0,
418             };
419             LogPrintf("Signet with challenge %s\n", signet_challenge[0]);
420         }
421 
422         if (args.IsArgSet("-signetseednode")) {
423             vSeeds = args.GetArgs("-signetseednode");
424         }
425 
426         strNetworkID = CBaseChainParams::SIGNET;
427         consensus.signet_blocks = true;
428         consensus.signet_challenge.assign(bin.begin(), bin.end());
429         consensus.nSubsidyHalvingInterval = 210000;
430         consensus.BIP16Height = 1;
431         consensus.BIP34Height = 1;
432         consensus.BIP34Hash = uint256{};
433         consensus.BIP65Height = 1;
434         consensus.BIP66Height = 1;
435         consensus.CSVHeight = 1;
436         consensus.SegwitHeight = 1;
437         consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
438         consensus.nPowTargetSpacing = 10 * 60;
439         consensus.fPowAllowMinDifficultyBlocks = false;
440         consensus.fPowNoRetargeting = false;
441         consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
442         consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
443         consensus.MinBIP9WarningHeight = 0;
444         consensus.powLimit = uint256S("00000377ae000000000000000000000000000000000000000000000000000000");
445         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
446         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
447         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
448 
449         // Activation of Taproot (BIPs 340-342)
450         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
451         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
452         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
453 
454         consensus.nAuxpowStartHeight = 0;
455         consensus.nAuxpowChainId = 0x0001;
456         consensus.fStrictChainId = true;
457         consensus.nLegacyBlocksBefore = 0;
458 
459         consensus.rules.reset(new Consensus::TestNetConsensus());
460 
461         // message start is defined as the first 4 bytes of the sha256d of the block script
462         CHashWriter h(SER_DISK, 0);
463         h << consensus.signet_challenge;
464         uint256 hash = h.GetHash();
465         memcpy(pchMessageStart, hash.begin(), 4);
466 
467         nDefaultPort = 38334;
468         nPruneAfterHeight = 1000;
469 
470         genesis = CreateTestnetGenesisBlock(1598918400, 52613770, 0x1e0377ae, 1, 50 * COIN);
471         consensus.hashGenesisBlock = genesis.GetHash();
472         assert(consensus.hashGenesisBlock == uint256S("0x00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"));
473         assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
474 
475         vFixedSeeds.clear();
476 
477         base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
478         base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
479         base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,239);
480         base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
481         base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
482 
483         bech32_hrp = "tb";
484 
485         fDefaultConsistencyChecks = false;
486         fRequireStandard = true;
487         m_is_test_chain = true;
488         m_is_mockable_chain = false;
489     }
490 
DefaultCheckNameDB() const491     int DefaultCheckNameDB () const override
492     {
493         return -1;
494     }
495 };
496 
497 /**
498  * Regression test
499  */
500 class CRegTestParams : public CChainParams {
501 public:
CRegTestParams(const ArgsManager & args)502     explicit CRegTestParams(const ArgsManager& args) {
503         strNetworkID =  CBaseChainParams::REGTEST;
504         consensus.signet_blocks = false;
505         consensus.signet_challenge.clear();
506         consensus.nSubsidyHalvingInterval = 150;
507         consensus.BIP16Height = 0;
508         consensus.BIP34Height = 500; // BIP34 activated on regtest (Used in functional tests)
509         consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in functional tests)
510         consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in functional tests)
511         consensus.CSVHeight = 432; // CSV activated on regtest (Used in rpc activation tests)
512         consensus.SegwitHeight = 0; // SEGWIT is always activated on regtest unless overridden
513         consensus.MinBIP9WarningHeight = 0;
514         consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
515         consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
516         consensus.nPowTargetSpacing = 10 * 60;
517         consensus.fPowAllowMinDifficultyBlocks = true;
518         consensus.nMinDifficultySince = 0;
519         consensus.fPowNoRetargeting = true;
520         consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
521         consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016)
522         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
523         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 0;
524         consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
525         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
526         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;
527         consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;
528 
529         consensus.nMinimumChainWork = uint256{};
530         consensus.defaultAssumeValid = uint256{};
531 
532         consensus.nAuxpowStartHeight = 0;
533         consensus.nAuxpowChainId = 0x0001;
534         consensus.fStrictChainId = true;
535         consensus.nLegacyBlocksBefore = 0;
536 
537         consensus.rules.reset(new Consensus::RegTestConsensus());
538 
539         pchMessageStart[0] = 0xfa;
540         pchMessageStart[1] = 0xbf;
541         pchMessageStart[2] = 0xb5;
542         pchMessageStart[3] = 0xda;
543         nDefaultPort = 18445;
544         nPruneAfterHeight = 1000;
545         m_assumed_blockchain_size = 0;
546         m_assumed_chain_state_size = 0;
547 
548         UpdateActivationParametersFromArgs(args);
549 
550         genesis = CreateTestnetGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
551         consensus.hashGenesisBlock = genesis.GetHash();
552         assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
553         assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
554 
555         vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
556         vSeeds.clear();      //!< Regtest mode doesn't have any DNS seeds.
557 
558         fDefaultConsistencyChecks = true;
559         fRequireStandard = true;
560         m_is_test_chain = true;
561         m_is_mockable_chain = true;
562 
563         checkpointData = {
564             {
565                 {0, uint256S("5287b3809b71433729402429b7d909a853cfac5ed40f09117b242c275e6b2d63")},
566             }
567         };
568 
569         chainTxData = ChainTxData{
570             0,
571             0,
572             0
573         };
574 
575         base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
576         base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
577         base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,239);
578         base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
579         base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
580 
581         bech32_hrp = "ncrt";
582 
583         assert(mapHistoricBugs.empty());
584     }
585 
DefaultCheckNameDB() const586     int DefaultCheckNameDB () const override
587     {
588         return 0;
589     }
590 
591     /**
592      * Allows modifying the Version Bits regtest parameters.
593      */
UpdateVersionBitsParameters(Consensus::DeploymentPos d,int64_t nStartTime,int64_t nTimeout)594     void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
595     {
596         consensus.vDeployments[d].nStartTime = nStartTime;
597         consensus.vDeployments[d].nTimeout = nTimeout;
598     }
599     void UpdateActivationParametersFromArgs(const ArgsManager& args);
600 };
601 
UpdateActivationParametersFromArgs(const ArgsManager & args)602 void CRegTestParams::UpdateActivationParametersFromArgs(const ArgsManager& args)
603 {
604     if (args.IsArgSet("-bip16height")) {
605         int64_t height = args.GetArg("-bip16height", consensus.BIP16Height);
606         if (height < -1 || height >= std::numeric_limits<int>::max()) {
607             throw std::runtime_error(strprintf("Activation height %ld for BIP16 is out of valid range. Use -1 to disable BIP16.", height));
608         } else if (height == -1) {
609             LogPrintf("BIP16 disabled for testing\n");
610             height = std::numeric_limits<int>::max();
611         }
612         consensus.BIP16Height = static_cast<int>(height);
613     }
614     if (args.IsArgSet("-segwitheight")) {
615         int64_t height = args.GetArg("-segwitheight", consensus.SegwitHeight);
616         if (height < -1 || height >= std::numeric_limits<int>::max()) {
617             throw std::runtime_error(strprintf("Activation height %ld for segwit is out of valid range. Use -1 to disable segwit.", height));
618         } else if (height == -1) {
619             LogPrintf("Segwit disabled for testing\n");
620             height = std::numeric_limits<int>::max();
621         }
622         consensus.SegwitHeight = static_cast<int>(height);
623     }
624 
625     if (!args.IsArgSet("-vbparams")) return;
626 
627     for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
628         std::vector<std::string> vDeploymentParams;
629         boost::split(vDeploymentParams, strDeployment, boost::is_any_of(":"));
630         if (vDeploymentParams.size() != 3) {
631             throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end");
632         }
633         int64_t nStartTime, nTimeout;
634         if (!ParseInt64(vDeploymentParams[1], &nStartTime)) {
635             throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
636         }
637         if (!ParseInt64(vDeploymentParams[2], &nTimeout)) {
638             throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
639         }
640         bool found = false;
641         for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
642             if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
643                 UpdateVersionBitsParameters(Consensus::DeploymentPos(j), nStartTime, nTimeout);
644                 found = true;
645                 LogPrintf("Setting version bits activation parameters for %s to start=%ld, timeout=%ld\n", vDeploymentParams[0], nStartTime, nTimeout);
646                 break;
647             }
648         }
649         if (!found) {
650             throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
651         }
652     }
653 }
654 
655 static std::unique_ptr<const CChainParams> globalChainParams;
656 
Params()657 const CChainParams &Params() {
658     assert(globalChainParams);
659     return *globalChainParams;
660 }
661 
CreateChainParams(const ArgsManager & args,const std::string & chain)662 std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain)
663 {
664     if (chain == CBaseChainParams::MAIN) {
665         return std::unique_ptr<CChainParams>(new CMainParams());
666     } else if (chain == CBaseChainParams::TESTNET) {
667         return std::unique_ptr<CChainParams>(new CTestNetParams());
668     } else if (chain == CBaseChainParams::SIGNET) {
669         return std::unique_ptr<CChainParams>(new SigNetParams(args));
670     } else if (chain == CBaseChainParams::REGTEST) {
671         return std::unique_ptr<CChainParams>(new CRegTestParams(args));
672     }
673     throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
674 }
675 
SelectParams(const std::string & network)676 void SelectParams(const std::string& network)
677 {
678     SelectBaseParams(network);
679     globalChainParams = CreateChainParams(gArgs, network);
680 }
681