Skip to content

Commit d526b73

Browse files
committed
Add initialfreecoins option, default of 0
- Add CommitToArgument without fedpegScript or signblockscript - Modify CreateGenesisBlock to take in the genesis scriptSig - Add AppendInitialIssuace without assets support - AppendInitialIssuance to genesis block for Custom chain only - Add -initialfreecoins option
1 parent 8a472b9 commit d526b73

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/chainparams.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@
1010
#include <tinyformat.h>
1111
#include <util.h>
1212
#include <utilstrencodings.h>
13+
#include <crypto/sha256.h>
1314
#include <versionbitsinfo.h>
1415

1516
#include <assert.h>
1617

1718
#include <boost/algorithm/string/classification.hpp>
1819
#include <boost/algorithm/string/split.hpp>
1920

20-
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
21+
// Safer for users if they load incorrect parameters via arguments.
22+
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
23+
{
24+
CSHA256 sha2;
25+
unsigned char commitment[32];
26+
sha2.Write((const unsigned char*)networkID.c_str(), networkID.length());
27+
// sha2.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
28+
// sha2.Write((const unsigned char*)HexStr(params.signblockscript).c_str(), HexStr(params.signblockscript).length());
29+
sha2.Finalize(commitment);
30+
return std::vector<unsigned char>(commitment, commitment + 32);
31+
}
32+
33+
static CBlock CreateGenesisBlock(const CScript& genesisScriptSig, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
2134
{
2235
CMutableTransaction txNew;
2336
txNew.nVersion = 1;
2437
txNew.vin.resize(1);
2538
txNew.vout.resize(1);
26-
txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
39+
txNew.vin[0].scriptSig = genesisScriptSig;
2740
txNew.vout[0].nValue = genesisReward;
2841
txNew.vout[0].scriptPubKey = genesisOutputScript;
2942

@@ -52,8 +65,26 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
5265
static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
5366
{
5467
const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
68+
const CScript genesisScriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
5569
const CScript genesisOutputScript = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
56-
return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
70+
return CreateGenesisBlock(genesisScriptSig, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
71+
}
72+
73+
/** Add an issuance transaction to the genesis block. Typically used to pre-issue
74+
* the policyAsset of a blockchain. The genesis block is not actually validated,
75+
* so this transaction simply has to match issuance structure. */
76+
static void AppendInitialIssuance(CBlock& genesis_block, const COutPoint& prevout, const int64_t asset_values, const CScript& issuance_destination) {
77+
78+
// Note: Genesis block isn't actually validated, outputs are entered into utxo db only
79+
CMutableTransaction txNew;
80+
txNew.nVersion = 1;
81+
txNew.vin.resize(1);
82+
txNew.vin[0].prevout = prevout;
83+
84+
txNew.vout.push_back(CTxOut(asset_values, issuance_destination));
85+
86+
genesis_block.vtx.push_back(MakeTransactionRef(std::move(txNew)));
87+
genesis_block.hashMerkleRoot = BlockMerkleRoot(genesis_block);
5788
}
5889

5990
/**
@@ -442,6 +473,7 @@ class CCustomParams : public CRegTestParams {
442473
// All non-zero coinbase outputs must go to this scriptPubKey
443474
std::vector<unsigned char> man_bytes = ParseHex(gArgs.GetArg("-con_mandatorycoinbase", ""));
444475
consensus.mandatory_coinbase_destination = CScript(man_bytes.begin(), man_bytes.end()); // Blank script allows any coinbase destination
476+
initialFreeCoins = gArgs.GetArg("-initialfreecoins", 0);
445477

446478
// Custom chains connect coinbase outputs to db by default
447479
consensus.connect_genesis_outputs = gArgs.GetArg("-con_connect_coinbase", true);
@@ -485,7 +517,11 @@ class CCustomParams : public CRegTestParams {
485517
{
486518
strNetworkID = chain;
487519
UpdateFromArgs(args);
488-
genesis = CreateGenesisBlock(strNetworkID.c_str(), CScript(OP_TRUE), 1296688602, 2, 0x207fffff, 1, 50 * COIN);
520+
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
521+
genesis = CreateGenesisBlock(CScript(commit), CScript(OP_RETURN), 1296688602, 2, 0x207fffff, 1, 0);
522+
if (initialFreeCoins != 0) {
523+
AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), initialFreeCoins, CScript() << OP_TRUE);
524+
}
489525
consensus.hashGenesisBlock = genesis.GetHash();
490526
}
491527
};

src/chainparams.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class CChainParams
9393
std::string bech32_hrp;
9494
std::string strNetworkID;
9595
CBlock genesis;
96+
CAmount initialFreeCoins;
9697
std::vector<SeedSpec6> vFixedSeeds;
9798
bool fDefaultConsistencyChecks;
9899
bool fRequireStandard;

src/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ void SetupServerArgs()
536536

537537
std::vector<std::string> elements_hidden_args = {"-con_fpowallowmindifficultyblocks", "-con_fpownoretargeting", "-con_nsubsidyhalvinginterval", "-con_bip16exception", "-con_bip34height", "-con_bip65height", "-con_bip66height", "-con_npowtargettimespan", "-con_npowtargetspacing", "-con_nrulechangeactivationthreshold", "-con_nminerconfirmationwindow", "-con_powlimit", "-con_bip34hash", "-con_nminimumchainwork", "-con_defaultassumevalid", "-npruneafterheight", "-fdefaultconsistencychecks", "-fmineblocksondemand", "-fallback_fee_enabled", "-pchmessagestart"};
538538

539+
gArgs.AddArg("-initialfreecoins", strprintf("The amount of OP_TRUE coins created in the genesis block. Primarily for testing. (default: %d)", 0), true, OptionsCategory::DEBUG_TEST);
539540

540541
// Add the hidden options
541542
gArgs.AddHiddenArgs(hidden_args);

0 commit comments

Comments
 (0)