Skip to content

Commit abca2d2

Browse files
committed
Chainparams: Introduce consensus.genesis_style for compatibility
1 parent de842cb commit abca2d2

File tree

4 files changed

+164
-118
lines changed

4 files changed

+164
-118
lines changed

src/chainparams.cpp

+44-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <chainparams.h>
7-
#include <consensus/merkle.h>
87

98
#include <chainparamsseeds.h>
9+
#include <consensus/merkle.h>
10+
#include <hash.h>
1011
#include <tinyformat.h>
1112
#include <util.h>
1213
#include <utilstrencodings.h>
@@ -17,13 +18,13 @@
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+
static CBlock CreateGenesisBlock(const CScript& coinbase_sig, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
2122
{
2223
CMutableTransaction txNew;
2324
txNew.nVersion = 1;
2425
txNew.vin.resize(1);
2526
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));
27+
txNew.vin[0].scriptSig = coinbase_sig;
2728
txNew.vout[0].nValue = genesisReward;
2829
txNew.vout[0].scriptPubKey = genesisOutputScript;
2930

@@ -38,6 +39,12 @@ static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesi
3839
return genesis;
3940
}
4041

42+
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
43+
{
44+
CScript coinbase_sig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
45+
return CreateGenesisBlock(coinbase_sig, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
46+
}
47+
4148
/**
4249
* Build the genesis block. Note that the output of its generation
4350
* transaction cannot be spent since it did not originally exist in the
@@ -403,6 +410,7 @@ class CCustomParams : public CRegTestParams {
403410
{
404411
UpdateVersionBitsParametersFromArgs(args);
405412

413+
consensus.genesis_style = args.GetArg("-con_genesis_style", "signet_new");
406414
consensus.nSubsidyHalvingInterval = args.GetArg("-con_nsubsidyhalvinginterval", consensus.nSubsidyHalvingInterval);
407415
consensus.BIP16Exception = uint256S(args.GetArg("-con_bip16exception", "0x0"));
408416
consensus.BIP34Height = args.GetArg("-con_bip34height", consensus.BIP34Height);
@@ -452,12 +460,44 @@ class CCustomParams : public CRegTestParams {
452460
}
453461
}
454462

463+
void SetGenesisBlock()
464+
{
465+
if (consensus.genesis_style == "genesis_style1") {
466+
// Same style as in https://github.com/bitcoin/bitcoin/pull/8994
467+
assert(consensus.blockscript.empty() && "consensus.blockscript is for signets");
468+
genesis = CreateGenesisBlock(strNetworkID.c_str(), CScript(OP_TRUE), 1296688602, 2, 0x207fffff, 1, 50 * COIN);
469+
470+
} else if (consensus.genesis_style == "signet_new") {
471+
// Same style as in https://github.com/ElementsProject/elements/pull/433
472+
CHashWriter h(SER_DISK, 0);
473+
h << strNetworkID;
474+
if (!consensus.blockscript.empty()) {
475+
h << consensus.blockscript << consensus.siglen;
476+
}
477+
uint256 hash = h.GetHash();
478+
CScript coinbase_sig = CScript() << std::vector<uint8_t>(hash.begin(), hash.end());
479+
genesis = CreateGenesisBlock(coinbase_sig, CScript(OP_TRUE), 1296688602, 2, 0x207fffff, 1, 50 * COIN);
480+
481+
} else if (consensus.genesis_style == "signet_old") {
482+
// Same style as in https://github.com/kallewoof/bitcoin/pull/5
483+
assert(!consensus.blockscript.empty() && "Signets require consensus.blockscript");
484+
CHashWriter h(SER_DISK, 0);
485+
h << consensus.blockscript << consensus.siglen;
486+
uint256 hash = h.GetHash();
487+
CScript coinbase_sig = CScript() << std::vector<uint8_t>(hash.begin(), hash.end());
488+
genesis = CreateGenesisBlock(coinbase_sig, CScript(OP_TRUE), 1534313275, 0, 0x1d00ffff, 1, 50 * COIN);
489+
490+
} else {
491+
throw std::runtime_error(strprintf("%s: Unknown consensus.genesis_style %s.", __func__, consensus.genesis_style));
492+
}
493+
}
494+
455495
public:
456496
CCustomParams(const std::string& chain, ArgsManager& args) : CRegTestParams(args)
457497
{
458498
strNetworkID = chain;
459499
UpdateFromArgs(args);
460-
genesis = CreateGenesisBlock(strNetworkID.c_str(), CScript(OP_TRUE), 1296688602, 2, 0x207fffff, 1, 50 * COIN);
500+
SetGenesisBlock();
461501
consensus.hashGenesisBlock = genesis.GetHash();
462502
}
463503
};

src/chainparamsbase.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void SetupChainParamsBaseOptions()
2121
gArgs.AddArg("-regtest", "Enter regression test mode, which uses a special chain in which blocks can be solved instantly. "
2222
"This is intended for regression testing tools and app development.", true, OptionsCategory::CHAINPARAMS);
2323
gArgs.AddArg("-testnet", "Use the test chain", false, OptionsCategory::CHAINPARAMS);
24+
gArgs.AddArg("-con_genesis_style=<style>", "Use genesis style <style> (default: signet_new). Allowed values: genesis_style1, signet_new, signet_old", true, OptionsCategory::CHAINPARAMS);
2425
gArgs.AddArg("-vbparams=deployment:start:end", "Use given start/end times for specified version bits deployment (regtest or custom only)", true, OptionsCategory::CHAINPARAMS);
2526
gArgs.AddArg("-seednode=<ip>", "Use specified node as seed node. This option can be specified multiple times to connect to multiple nodes. (custom only)", true, OptionsCategory::CHAINPARAMS);
2627
}

src/consensus/params.h

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct BIP9Deployment {
4747
* Parameters that influence chain consensus.
4848
*/
4949
struct Params {
50+
std::string genesis_style;
5051
uint256 hashGenesisBlock;
5152
int nSubsidyHalvingInterval;
5253
/* Block hash that is excepted from BIP16 enforcement */
@@ -75,7 +76,11 @@ struct Params {
7576
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
7677
uint256 nMinimumChainWork;
7778
uint256 defaultAssumeValid;
79+
80+
std::vector<uint8_t> blockscript;
81+
uint32_t siglen;
7882
};
83+
7984
} // namespace Consensus
8085

8186
#endif // BITCOIN_CONSENSUS_PARAMS_H

0 commit comments

Comments
 (0)