-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathchainparams.cpp
286 lines (240 loc) · 13.4 KB
/
chainparams.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "chainparams.h"
#include "consensus/merkle.h"
#include "issuance.h"
#include "tinyformat.h"
#include "util.h"
#include "utilstrencodings.h"
#include "crypto/sha256.h"
#include <assert.h>
#include <boost/assign/list_of.hpp>
// Safer for users if they load incorrect parameters via arguments.
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
{
CSHA256 sha2;
unsigned char commitment[32];
sha2.Write((const unsigned char*)networkID.c_str(), networkID.length());
sha2.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
sha2.Write((const unsigned char*)HexStr(params.signblockscript).c_str(), HexStr(params.signblockscript).length());
sha2.Finalize(commitment);
return std::vector<unsigned char>(commitment, commitment + 32);
}
static CScript StrHexToScriptWithDefault(std::string strScript, const CScript defaultScript)
{
CScript returnScript;
if (!strScript.empty()) {
std::vector<unsigned char> scriptData = ParseHex(strScript);
returnScript = CScript(scriptData.begin(), scriptData.end());
} else {
returnScript = defaultScript;
}
return returnScript;
}
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, int32_t nVersion)
{
CMutableTransaction txNew;
txNew.nVersion = 1;
txNew.vin.resize(1);
// Any consensus-related values that are command-line set can be added here for anti-footgun
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID));
txNew.vout.clear();
txNew.vout.push_back(CTxOut(CAsset(), 0, CScript() << OP_RETURN));
CBlock genesis;
genesis.nTime = nTime;
genesis.proof = CProof(params.signblockscript, CScript());
genesis.nVersion = nVersion;
genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
genesis.hashPrevBlock.SetNull();
genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
return genesis;
}
/** Add an issuance transaction to the genesis block. Typically used to pre-issue
* the policyAsset of a blockchain. The genesis block is not actually validated,
* so this transaction simply has to match issuance structure. */
static void AppendInitialIssuance(CBlock& genesis_block, const COutPoint& prevout, const uint256& contract, const int64_t asset_outputs, const int64_t asset_values, const int64_t reissuance_outputs, const int64_t reissuance_values, const CScript& issuance_destination) {
uint256 entropy;
GenerateAssetEntropy(entropy, prevout, contract);
CAsset asset;
CalculateAsset(asset, entropy);
// Re-issuance of policyAsset is always unblinded
CAsset reissuance;
CalculateReissuanceToken(reissuance, entropy, false);
// Note: Genesis block isn't actually validated, outputs are entered into utxo db only
CMutableTransaction txNew;
txNew.nVersion = 1;
txNew.vin.resize(1);
txNew.vin[0].prevout = prevout;
txNew.vin[0].assetIssuance.assetEntropy = contract;
txNew.vin[0].assetIssuance.nAmount = asset_values*asset_outputs;
txNew.vin[0].assetIssuance.nInflationKeys = reissuance_values*reissuance_outputs;
for (unsigned int i = 0; i < asset_outputs; i++) {
txNew.vout.push_back(CTxOut(asset, asset_values, issuance_destination));
}
for (unsigned int i = 0; i < reissuance_outputs; i++) {
txNew.vout.push_back(CTxOut(reissuance, reissuance_values, issuance_destination));
}
genesis_block.vtx.push_back(MakeTransactionRef(std::move(txNew)));
genesis_block.hashMerkleRoot = BlockMerkleRoot(genesis_block);
}
void CChainParams::UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
{
consensus.vDeployments[d].nStartTime = nStartTime;
consensus.vDeployments[d].nTimeout = nTimeout;
}
/**
* Custom chain params
*/
class CCustomParams : public CChainParams {
protected:
void UpdateFromArgs()
{
consensus.nSubsidyHalvingInterval = GetArg("-con_nsubsidyhalvinginterval", 150);
// BIP34 has not activated on regtest (far in the future so block v1 are not rejected in tests)
consensus.BIP34Height = GetArg("-con_bip34height", 100000000);
consensus.BIP34Hash = uint256S(GetArg("-con_bip34hash", "0x00"));
consensus.BIP65Height = GetArg("-con_bip65height", 1351);
consensus.BIP66Height = GetArg("-con_bip66height", 1251);
consensus.powLimit = uint256S(GetArg("-con_powlimit", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
consensus.parentChainPowLimit = uint256S(GetArg("-con_parentpowlimit", "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
consensus.nPowTargetTimespan = GetArg("-con_npowtargettimespan", 14 * 24 * 60 * 60); // two weeks
consensus.nPowTargetSpacing = GetArg("-con_npowtargetspacing", 10 * 60);
consensus.fPowAllowMinDifficultyBlocks = GetBoolArg("-con_fpowallowmindifficultyblocks", true);
consensus.fPowNoRetargeting = GetBoolArg("-con_fpownoretargeting", true);
consensus.nRuleChangeActivationThreshold = GetArg("-con_nrulechangeactivationthreshold", 108); // 75% for testchains
consensus.nMinerConfirmationWindow = GetArg("-con_nminerconfirmationwindow", 144); // Faster than normal for custom (144 instead of 2016)
// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S(GetArg("-con_nminimumchainwork", "0x00"));
// By default assume that the signatures in ancestors of this block are valid.
consensus.defaultAssumeValid = uint256S(GetArg("-con_defaultassumevalid", "0x00"));
consensus.pegin_min_depth = GetArg("-peginconfirmationdepth", DEFAULT_PEGIN_CONFIRMATION_DEPTH);
consensus.mandatory_coinbase_destination = StrHexToScriptWithDefault(GetArg("-con_mandatorycoinbase", ""), CScript()); // Blank script allows any coinbase destination
consensus.parent_chain_signblockscript = StrHexToScriptWithDefault(GetArg("-con_parent_chain_signblockscript", ""), CScript());
consensus.parent_pegged_asset.SetHex(GetArg("-con_parent_pegged_asset", "0x00"));
// bitcoin regtest is the parent chain by default
parentGenesisBlockHash = uint256S(GetArg("-parentgenesisblockhash", "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
initialFreeCoins = GetArg("-initialfreecoins", 0);
initial_reissuance_tokens = GetArg("-initialreissuancetokens", 0);
consensus.has_parent_chain = GetBoolArg("-con_has_parent_chain", true);
// Either it has a parent chain or not
const bool parent_genesis_is_null = parentGenesisBlockHash == uint256();
assert(consensus.has_parent_chain != parent_genesis_is_null);
const CScript default_script(CScript() << OP_TRUE);
consensus.signblockscript = StrHexToScriptWithDefault(GetArg("-signblockscript", ""), default_script);
consensus.fedpegScript = StrHexToScriptWithDefault(GetArg("-fedpegscript", ""), default_script);
nDefaultPort = GetArg("-ndefaultport", 7042);
nPruneAfterHeight = GetArg("-npruneafterheight", 1000);
fMiningRequiresPeers = GetBoolArg("-fminingrequirespeers", false);
fDefaultConsistencyChecks = GetBoolArg("-fdefaultconsistencychecks", true);
fRequireStandard = GetBoolArg("-frequirestandard", false);
fMineBlocksOnDemand = GetBoolArg("-fmineblocksondemand", true);
anyonecanspend_aremine = GetBoolArg("-anyonecanspendaremine", true);
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1, GetArg("-pubkeyprefix", 235));
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1, GetArg("-scriptprefix", 75));
base58Prefixes[BLINDED_ADDRESS]= std::vector<unsigned char>(1, GetArg("-blindedprefix", 4));
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1, GetArg("-secretprefix", 239));
std::string extpubprefix = GetArg("-extpubkeyprefix", "043587CF");
if (!IsHex(extpubprefix) || extpubprefix.size() != 8) {
assert("-extpubkeyprefix must be hex string of length 8" && false);
}
base58Prefixes[EXT_PUBLIC_KEY] = ParseHex(extpubprefix);
std::string extprvprefix = GetArg("-extprvkeyprefix", "04358394");
if (!IsHex(extprvprefix) || extprvprefix.size() != 8) {
assert("-extprvkeyprefix must be hex string of length 8" && false);
}
base58Prefixes[EXT_SECRET_KEY] = ParseHex(extprvprefix);
base58Prefixes[PARENT_PUBKEY_ADDRESS] = std::vector<unsigned char>(1, GetArg("-parentpubkeyprefix", 111));
base58Prefixes[PARENT_SCRIPT_ADDRESS] = std::vector<unsigned char>(1, GetArg("-parentscriptprefix", 196));
}
public:
CCustomParams(const std::string& chain) : CChainParams(chain)
{
this->UpdateFromArgs();
if (!anyonecanspend_aremine) {
assert("Anyonecanspendismine was marked as false, but they are in the genesis block"
&& initialFreeCoins == 0);
}
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 0;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 999999999999ULL;
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nStartTime = 0;
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nTimeout = 999999999999ULL;
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].bit = 1;
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nStartTime = 0;
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout = 999999999999ULL;
pchMessageStart[0] = 0xfa;
pchMessageStart[1] = 0xbf;
pchMessageStart[2] = 0xb5;
pchMessageStart[3] = 0xda;
// Generate pegged Bitcoin asset
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
uint256 entropy;
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
CalculateAsset(consensus.pegged_asset, entropy);
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, 1);
if (initialFreeCoins != 0 || initial_reissuance_tokens != 0) {
AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), parentGenesisBlockHash, (initialFreeCoins > 0) ? 1 : 0, initialFreeCoins, (initial_reissuance_tokens > 0) ? 1 : 0, initial_reissuance_tokens, CScript() << OP_TRUE);
}
consensus.hashGenesisBlock = genesis.GetHash();
vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
vSeeds.clear(); //!< Regtest mode doesn't have any DNS seeds.
checkpointData = (CCheckpointData){
boost::assign::map_list_of
( 0, consensus.hashGenesisBlock),
};
chainTxData = ChainTxData{
0,
0,
0
};
}
};
/**
* Use base58 and other old configurations for outdated unittests
*/
class CMainParams : public CCustomParams {
public:
CMainParams(const std::string& chain) : CCustomParams(chain)
{
consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,0);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,5);
base58Prefixes[BLINDED_ADDRESS]= std::vector<unsigned char>(1,11);
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,128);
base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x88)(0xB2)(0x1E).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x88)(0xAD)(0xE4).convert_to_container<std::vector<unsigned char> >();
base58Prefixes[PARENT_PUBKEY_ADDRESS] = std::vector<unsigned char>(1,0);
base58Prefixes[PARENT_SCRIPT_ADDRESS] = std::vector<unsigned char>(1,5);
}
};
const std::vector<std::string> CChainParams::supportedChains =
boost::assign::list_of
( CHAINPARAMS_REGTEST )
;
static std::unique_ptr<CChainParams> globalChainParams;
const CChainParams &Params() {
assert(globalChainParams);
return *globalChainParams;
}
std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
{
if (chain == CBaseChainParams::MAIN)
return std::unique_ptr<CChainParams>(new CMainParams(chain));
return std::unique_ptr<CChainParams>(new CCustomParams(chain));
}
void SelectParams(const std::string& network)
{
SelectBaseParams(network);
globalChainParams = CreateChainParams(network);
}
void UpdateBIP9Parameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
{
globalChainParams->UpdateBIP9Parameters(d, nStartTime, nTimeout);
}