Skip to content

Commit 7a5916a

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 7a5916a

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

src/chainparams.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@
1010
#include <tinyformat.h>
1111
#include <util.h>
1212
#include <utilstrencodings.h>
13+
#include <crypto/sha256.h>
14+
#include <validation.h>
1315
#include <versionbitsinfo.h>
1416

1517
#include <assert.h>
1618

1719
#include <boost/algorithm/string/classification.hpp>
1820
#include <boost/algorithm/string/split.hpp>
1921

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)
22+
// Safer for users if they load incorrect parameters via arguments.
23+
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID)
24+
{
25+
CSHA256 sha2;
26+
unsigned char commitment[32];
27+
sha2.Write((const unsigned char*)networkID.c_str(), networkID.length());
28+
// sha2.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
29+
// sha2.Write((const unsigned char*)HexStr(params.signblockscript).c_str(), HexStr(params.signblockscript).length());
30+
sha2.Finalize(commitment);
31+
return std::vector<unsigned char>(commitment, commitment + 32);
32+
}
33+
34+
static CBlock CreateGenesisBlock(const CScript& genesisScriptSig, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
2135
{
2236
CMutableTransaction txNew;
2337
txNew.nVersion = 1;
2438
txNew.vin.resize(1);
2539
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));
40+
txNew.vin[0].scriptSig = genesisScriptSig;
2741
txNew.vout[0].nValue = genesisReward;
2842
txNew.vout[0].scriptPubKey = genesisOutputScript;
2943

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

5991
/**
@@ -442,6 +474,7 @@ class CCustomParams : public CRegTestParams {
442474
// All non-zero coinbase outputs must go to this scriptPubKey
443475
std::vector<unsigned char> man_bytes = ParseHex(gArgs.GetArg("-con_mandatorycoinbase", ""));
444476
consensus.mandatory_coinbase_destination = CScript(man_bytes.begin(), man_bytes.end()); // Blank script allows any coinbase destination
477+
initialFreeCoins = gArgs.GetArg("-initialfreecoins", DEFAULT_INITIAL_FREE_COINS);
445478

446479
// Custom chains connect coinbase outputs to db by default
447480
consensus.connect_genesis_outputs = gArgs.GetArg("-con_connect_coinbase", true);
@@ -485,7 +518,11 @@ class CCustomParams : public CRegTestParams {
485518
{
486519
strNetworkID = chain;
487520
UpdateFromArgs(args);
488-
genesis = CreateGenesisBlock(strNetworkID.c_str(), CScript(OP_TRUE), 1296688602, 2, 0x207fffff, 1, 50 * COIN);
521+
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
522+
genesis = CreateGenesisBlock(CScript(commit), CScript(OP_RETURN), 1296688602, 2, 0x207fffff, 1, 0);
523+
if (initialFreeCoins != 0) {
524+
AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), initialFreeCoins, CScript() << OP_TRUE);
525+
}
489526
consensus.hashGenesisBlock = genesis.GetHash();
490527
}
491528
};

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)", DEFAULT_INITIAL_FREE_COINS), true, OptionsCategory::DEBUG_TEST);
539540

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

src/validation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static const int64_t MAX_FEE_ESTIMATION_TIP_AGE = 3 * 60 * 60;
116116
/** Default for -permitbaremultisig */
117117
static const bool DEFAULT_PERMIT_BAREMULTISIG = true;
118118
static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
119+
static const CAmount DEFAULT_INITIAL_FREE_COINS = 0;
119120
static const bool DEFAULT_TXINDEX = false;
120121
static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
121122
/** Default for -persistmempool */

0 commit comments

Comments
 (0)