Skip to content

Commit

Permalink
Refactor: Easier support for chains with multiple genesis assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimon committed Sep 21, 2017
1 parent c4eb0e0 commit 38c5c67
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "chainparamsseeds.h"

static const uint32_t MAX_GENESIS_OUTPUTS = 500;

// Safer for users if they load incorrect parameters via arguments.
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
{
Expand All @@ -43,20 +45,38 @@ static CScript StrHexToScriptWithDefault(std::string strScript, const CScript de
return returnScript;
}

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)
struct GenesisReward
{
const CAmount nTotalAmount;
const CScript outputScript;
const CAsset asset;
const uint32_t nShards;

GenesisReward(const CAmount& nTotalAmountIn, const CScript& outputScriptIn, const CAsset& assetIn, const uint32_t nShardsIn=1) :
nTotalAmount(nTotalAmountIn), outputScript(outputScriptIn), asset(assetIn), nShards(nShardsIn) {};
};

static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::string& networkID, uint32_t nTime, int32_t nVersion, const std::vector<GenesisReward>& genesisRewards)
{
// Shards must be evenly divisible
assert(MAX_MONEY % rewardShards == 0);
CMutableTransaction txNew;
txNew.nVersion = 1;
txNew.vin.resize(1);
txNew.vout.resize(rewardShards);
// Any consensus-related values that are command-line set can be added here for anti-footgun
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID));
for (unsigned int i = 0; i < rewardShards; i++) {
txNew.vout[i].nValue = genesisReward/rewardShards;
txNew.vout[i].nAsset = genesisAsset;
txNew.vout[i].scriptPubKey = genesisOutputScript;

unsigned int totalOutputs = 0;
for (GenesisReward gReward : genesisRewards) totalOutputs += gReward.nShards;
assert(totalOutputs <= MAX_GENESIS_OUTPUTS);
txNew.vout.resize(totalOutputs);

for (GenesisReward gReward : genesisRewards) {
for (unsigned int i = 0; i < gReward.nShards; i++) {
// Shards must be evenly divisible
assert(gReward.nTotalAmount % gReward.nShards == 0);
txNew.vout[i].nValue = gReward.nTotalAmount / gReward.nShards;
txNew.vout[i].nAsset = gReward.asset;
txNew.vout[i].scriptPubKey = gReward.outputScript;
}
}

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

CScript scriptDestination(CScript() << std::vector<unsigned char>(parentGenesisBlockHash.begin(), parentGenesisBlockHash.end()) << OP_WITHDRAWPROOFVERIFY);
genesis = CreateGenesisBlock(consensus, strNetworkID, scriptDestination, 1231006505, 1, MAX_MONEY, 100, consensus.pegged_asset);
const std::vector<GenesisReward> genesisRewards = {
GenesisReward(MAX_MONEY, scriptDestination, consensus.pegged_asset, 100),
};
genesis = CreateGenesisBlock(consensus, strNetworkID, 1231006505, 1, genesisRewards);
consensus.hashGenesisBlock = genesis.GetHash();

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

genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, 1, MAX_MONEY, 100, consensus.pegged_asset);
const std::vector<GenesisReward> genesisRewards = {
GenesisReward(MAX_MONEY, defaultRegtestScript, consensus.pegged_asset, 100),
};
genesis = CreateGenesisBlock(consensus, strNetworkID, 1296688602, 1, genesisRewards);
consensus.hashGenesisBlock = genesis.GetHash();


Expand Down

0 comments on commit 38c5c67

Please sign in to comment.