Skip to content

Commit 38c5c67

Browse files
committed
Refactor: Easier support for chains with multiple genesis assets
1 parent c4eb0e0 commit 38c5c67

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/chainparams.cpp

+36-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
#include "chainparamsseeds.h"
2121

22+
static const uint32_t MAX_GENESIS_OUTPUTS = 500;
23+
2224
// Safer for users if they load incorrect parameters via arguments.
2325
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
2426
{
@@ -43,20 +45,38 @@ static CScript StrHexToScriptWithDefault(std::string strScript, const CScript de
4345
return returnScript;
4446
}
4547

46-
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, const CScript& genesisOutputScript, uint32_t nTime, int32_t nVersion, const CAmount& genesisReward, const uint32_t rewardShards, const CAsset& genesisAsset)
48+
struct GenesisReward
49+
{
50+
const CAmount nTotalAmount;
51+
const CScript outputScript;
52+
const CAsset asset;
53+
const uint32_t nShards;
54+
55+
GenesisReward(const CAmount& nTotalAmountIn, const CScript& outputScriptIn, const CAsset& assetIn, const uint32_t nShardsIn=1) :
56+
nTotalAmount(nTotalAmountIn), outputScript(outputScriptIn), asset(assetIn), nShards(nShardsIn) {};
57+
};
58+
59+
static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, int32_t nVersion, const std::vector<GenesisReward>& genesisRewards)
4760
{
48-
// Shards must be evenly divisible
49-
assert(MAX_MONEY % rewardShards == 0);
5061
CMutableTransaction txNew;
5162
txNew.nVersion = 1;
5263
txNew.vin.resize(1);
53-
txNew.vout.resize(rewardShards);
5464
// Any consensus-related values that are command-line set can be added here for anti-footgun
5565
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID));
56-
for (unsigned int i = 0; i < rewardShards; i++) {
57-
txNew.vout[i].nValue = genesisReward/rewardShards;
58-
txNew.vout[i].nAsset = genesisAsset;
59-
txNew.vout[i].scriptPubKey = genesisOutputScript;
66+
67+
unsigned int totalOutputs = 0;
68+
for (GenesisReward gReward : genesisRewards) totalOutputs += gReward.nShards;
69+
assert(totalOutputs <= MAX_GENESIS_OUTPUTS);
70+
txNew.vout.resize(totalOutputs);
71+
72+
for (GenesisReward gReward : genesisRewards) {
73+
for (unsigned int i = 0; i < gReward.nShards; i++) {
74+
// Shards must be evenly divisible
75+
assert(gReward.nTotalAmount % gReward.nShards == 0);
76+
txNew.vout[i].nValue = gReward.nTotalAmount / gReward.nShards;
77+
txNew.vout[i].nAsset = gReward.asset;
78+
txNew.vout[i].scriptPubKey = gReward.outputScript;
79+
}
6080
}
6181

6282
CBlock genesis;
@@ -151,7 +171,10 @@ class CElementsParams : public CChainParams {
151171
CalculateAsset(consensus.pegged_asset, entropy);
152172

153173
CScript scriptDestination(CScript() << std::vector<unsigned char>(parentGenesisBlockHash.begin(), parentGenesisBlockHash.end()) << OP_WITHDRAWPROOFVERIFY);
154-
genesis = CreateGenesisBlock(consensus, strNetworkID, scriptDestination, 1231006505, 1, MAX_MONEY, 100, consensus.pegged_asset);
174+
const std::vector<GenesisReward> genesisRewards = {
175+
GenesisReward(MAX_MONEY, scriptDestination, consensus.pegged_asset, 100),
176+
};
177+
genesis = CreateGenesisBlock(consensus, strNetworkID, 1231006505, 1, genesisRewards);
155178
consensus.hashGenesisBlock = genesis.GetHash();
156179

157180
scriptCoinbaseDestination = CScript() << ParseHex("0229536c4c83789f59c30b93eb40d4abbd99b8dcc99ba8bd748f29e33c1d279e3c") << OP_CHECKSIG;
@@ -260,7 +283,10 @@ class CRegTestParams : public CChainParams {
260283
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
261284
CalculateAsset(consensus.pegged_asset, entropy);
262285

263-
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, 1, MAX_MONEY, 100, consensus.pegged_asset);
286+
const std::vector<GenesisReward> genesisRewards = {
287+
GenesisReward(MAX_MONEY, defaultRegtestScript, consensus.pegged_asset, 100),
288+
};
289+
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, 1, genesisRewards);
264290
consensus.hashGenesisBlock = genesis.GetHash();
265291

266292

0 commit comments

Comments
 (0)