Skip to content

Commit cecfe80

Browse files
committed
Merge #467: [0.17] Add initialfreecoins option, default of 0
95432aa Test listunspent sanity when specifying initialfreecoins (Carl Dong) 1ac14fc restore bitcoin regtest gen block using -genesis_style (Gregory Sanders) 8b8c11a Add initialfreecoins option, default of 0 (Carl Dong) Pull request description: This PR adds a `-initialfreecoins` debug option, and a `-con_genesis_style` to go along with it. This allows us to correctly set the amount of coins created in the genesis block, either in the `elements` style or `bitcoin` style. Tests have been modified to test that the correct amount of coins are created. Tree-SHA512: d3c56e7109e8e6496b6e5b819dc09f3ebf82013882854cc76062a39e4399e7eeb5789b567be527133278c1646a2765d02a09bd2d9618bdaeec5a852042e40c2b
2 parents b59eccb + 95432aa commit cecfe80

File tree

9 files changed

+312
-236
lines changed

9 files changed

+312
-236
lines changed

src/chainparams.cpp

Lines changed: 55 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,10 @@ 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);
477+
478+
// Determines type of genesis block
479+
consensus.genesis_style = gArgs.GetArg("-con_genesis_style", "elements");
445480

446481
// Custom chains connect coinbase outputs to db by default
447482
consensus.connect_genesis_outputs = gArgs.GetArg("-con_connect_coinbase", true);
@@ -480,12 +515,28 @@ class CCustomParams : public CRegTestParams {
480515
}
481516
}
482517

518+
void SetGenesisBlock() {
519+
if (consensus.genesis_style == "bitcoin") {
520+
// For compatibility with bitcoin (regtest)
521+
genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
522+
} else if (consensus.genesis_style == "elements") {
523+
// Intended compatibility with Liquid v1 and elements-0.14.1
524+
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID);
525+
genesis = CreateGenesisBlock(CScript(commit), CScript(OP_RETURN), 1296688602, 2, 0x207fffff, 1, 0);
526+
if (initialFreeCoins != 0) {
527+
AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), initialFreeCoins, CScript() << OP_TRUE);
528+
}
529+
} else {
530+
throw std::runtime_error(strprintf("Invalid -genesis_style (%s)", consensus.genesis_style));
531+
}
532+
}
533+
483534
public:
484535
CCustomParams(const std::string& chain, ArgsManager& args) : CRegTestParams(args)
485536
{
486537
strNetworkID = chain;
487538
UpdateFromArgs(args);
488-
genesis = CreateGenesisBlock(strNetworkID.c_str(), CScript(OP_TRUE), 1296688602, 2, 0x207fffff, 1, 50 * COIN);
539+
SetGenesisBlock();
489540
consensus.hashGenesisBlock = genesis.GetHash();
490541
}
491542
};

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/chainparamsbase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void SetupChainParamsBaseOptions()
2727
gArgs.AddArg("-con_blocksubsidy", "Defines the amount of block subsidy to start with, at genesis block.", false, OptionsCategory::ELEMENTS);
2828
gArgs.AddArg("-con_connect_coinbase", "Connect outputs in genesis block to utxo database.", false, OptionsCategory::ELEMENTS);
2929
gArgs.AddArg("-con_blockheightinheader", "Whether the chain includes the block height directly in the header, for easier validation of block height in low-resource environments. (default: true)", false, OptionsCategory::CHAINPARAMS);
30+
gArgs.AddArg("-con_genesis_style=<style>", "Use genesis style <style> (default: elements). Results in genesis block compatibility with various networks. Allowed values: elements, bitcoin", true, OptionsCategory::ELEMENTS);
3031
}
3132

3233
static std::unique_ptr<CBaseChainParams> globalChainBaseParams;

src/consensus/params.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct Params {
8383
CAmount genesis_subsidy;
8484
bool connect_genesis_outputs;
8585
// g_con_blockheightinheader global hack instead of proper arg due to circular dep
86+
std::string genesis_style;
8687
};
8788
} // namespace Consensus
8889

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)