Skip to content

Commit

Permalink
Add commandline flag to set genesis block script destination
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Nov 22, 2015
1 parent 2843d41 commit da45c44
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
54 changes: 26 additions & 28 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static const Checkpoints::CCheckpointData dataRegtest = {

class CMainParams : public CChainParams {
public:
CMainParams() {
CMainParams(CScript scriptDestination) {
networkID = CBaseChainParams::MAIN;
strNetworkID = "main";
/**
Expand Down Expand Up @@ -127,7 +127,9 @@ class CMainParams : public CChainParams {
genesis.hashMerkleRoot = genesis.BuildMerkleTree();
genesis.nVersion = 1;
genesis.nTime = 1231006505;
CScript scriptDestination(CScript() << OP_5 << ParseHex("027d5d62861df77fc9a37dbe901a579d686d1423be5f56d6fc50bb9de3480871d1") << ParseHex("03b41ea6ba73b94c901fdd43e782aaf70016cc124b72a086e77f6e9f4f942ca9bb") << ParseHex("02be643c3350bade7c96f6f28d1750af2ef507bc1f08dd38f82749214ab90d9037") << ParseHex("021df31471281d4478df85bfce08a10aab82601dca949a79950f8ddf7002bd915a") << ParseHex("0320ea4fcf77b63e89094e681a5bd50355900bf961c10c9c82876cb3238979c0ed") << ParseHex("021c4c92c8380659eb567b497b936b274424662909e1ffebc603672ed8433f4aa1") << ParseHex("027841250cfadc06c603da8bc58f6cd91e62f369826c8718eb6bd114601dd0c5ac") << OP_7 << OP_CHECKMULTISIG);
if (scriptDestination.empty()) {
scriptDestination = CScript() << OP_5 << ParseHex("027d5d62861df77fc9a37dbe901a579d686d1423be5f56d6fc50bb9de3480871d1") << ParseHex("03b41ea6ba73b94c901fdd43e782aaf70016cc124b72a086e77f6e9f4f942ca9bb") << ParseHex("02be643c3350bade7c96f6f28d1750af2ef507bc1f08dd38f82749214ab90d9037") << ParseHex("021df31471281d4478df85bfce08a10aab82601dca949a79950f8ddf7002bd915a") << ParseHex("0320ea4fcf77b63e89094e681a5bd50355900bf961c10c9c82876cb3238979c0ed") << ParseHex("021c4c92c8380659eb567b497b936b274424662909e1ffebc603672ed8433f4aa1") << ParseHex("027841250cfadc06c603da8bc58f6cd91e62f369826c8718eb6bd114601dd0c5ac") << OP_7 << OP_CHECKMULTISIG;
}
genesis.proof = CProof(scriptDestination, CScript()); // genesis block gets a PoW pass

hashGenesisBlock = genesis.GetHash();
Expand Down Expand Up @@ -163,14 +165,13 @@ class CMainParams : public CChainParams {
return data;
}
};
static CMainParams mainParams;

/**
* Testnet (v3)
*/
class CTestNetParams : public CMainParams {
public:
CTestNetParams() {
CTestNetParams(CScript scriptDestination) : CMainParams(scriptDestination) {
networkID = CBaseChainParams::TESTNET;
strNetworkID = "test";
pchMessageStart[0] = 0xee;
Expand Down Expand Up @@ -217,14 +218,13 @@ class CTestNetParams : public CMainParams {
return dataTestnet;
}
};
static CTestNetParams testNetParams;

/**
* Regression test
*/
class CRegTestParams : public CTestNetParams {
public:
CRegTestParams() {
CRegTestParams(CScript scriptDestination) : CTestNetParams(scriptDestination) {
networkID = CBaseChainParams::REGTEST;
strNetworkID = "regtest";
pchMessageStart[0] = 0xfa;
Expand All @@ -249,7 +249,9 @@ class CRegTestParams : public CTestNetParams {
txNew.vout[1].scriptPubKey = CScript() << OP_TRUE;
genesis.vtx[0] = CTransaction(txNew);

CScript scriptDestination(CScript() << OP_TRUE);
if (scriptDestination.empty()) {
scriptDestination = CScript() << OP_TRUE;
}
genesis.proof = CProof(scriptDestination, CScript()); // genesis block gets a PoW pass
genesis.hashMerkleRoot = genesis.BuildMerkleTree();

Expand All @@ -272,14 +274,13 @@ class CRegTestParams : public CTestNetParams {
return dataRegtest;
}
};
static CRegTestParams regTestParams;

/**
* Unit test
*/
class CUnitTestParams : public CMainParams, public CModifiableParams {
public:
CUnitTestParams() {
CUnitTestParams(CScript scriptDestination) : CMainParams(scriptDestination) {
networkID = CBaseChainParams::UNITTEST;
strNetworkID = "unittest";
nDefaultPort = 18445;
Expand Down Expand Up @@ -308,50 +309,47 @@ class CUnitTestParams : public CMainParams, public CModifiableParams {
virtual void setAllowMinDifficultyBlocks(bool afAllowMinDifficultyBlocks) { fAllowMinDifficultyBlocks=afAllowMinDifficultyBlocks; }
virtual void setSkipProofOfWorkCheck(bool afSkipProofOfWorkCheck) { fSkipProofOfWorkCheck = afSkipProofOfWorkCheck; }
};
static CUnitTestParams unitTestParams;


static CChainParams *pCurrentParams = 0;

CModifiableParams *ModifiableParams()
{
assert(pCurrentParams);
assert(pCurrentParams==&unitTestParams);
return (CModifiableParams*)&unitTestParams;
}
static CChainParams *pCurrentParams;

const CChainParams &Params() {
assert(pCurrentParams);
return *pCurrentParams;
}

CChainParams &Params(CBaseChainParams::Network network) {

CChainParams* CChainParams::Factory(CBaseChainParams::Network network, CScript scriptDestination) {
switch (network) {
case CBaseChainParams::MAIN:
return mainParams;
return new CMainParams(scriptDestination);
case CBaseChainParams::TESTNET:
return testNetParams;
return new CTestNetParams(scriptDestination);
case CBaseChainParams::REGTEST:
return regTestParams;
return new CRegTestParams(scriptDestination);
case CBaseChainParams::UNITTEST:
return unitTestParams;
return new CUnitTestParams(scriptDestination);
default:
assert(false && "Unimplemented network");
return mainParams;
return NULL;
}
}

void SelectParams(CBaseChainParams::Network network) {
void SelectParams(CBaseChainParams::Network network, CScript scriptDestination) {
SelectBaseParams(network);
pCurrentParams = &Params(network);
pCurrentParams = CChainParams::Factory(network, scriptDestination);
}

void SelectParams(CBaseChainParams::Network network) {
SelectParams(network, CScript());
}

bool SelectParamsFromCommandLine()
{
CBaseChainParams::Network network = NetworkIdFromCommandLine();
CScript scriptDestination = ScriptDestinationFromCommandLine();
if (network == CBaseChainParams::MAX_NETWORK_TYPES)
return false;

SelectParams(network);
SelectParams(network, scriptDestination);
return true;
}
14 changes: 11 additions & 3 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class CChainParams
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
const std::vector<CAddress>& FixedSeeds() const { return vFixedSeeds; }
virtual const Checkpoints::CCheckpointData& Checkpoints() const = 0;
/**
* Creates and returns a CChainParams* of the chosen chain. The caller has to delete the object.
* @returns a CChainParams* of the chosen chain.
* @throws a std::runtime_error if the chain is not supported.
*/
static CChainParams* Factory(CBaseChainParams::Network network, CScript scriptDestination);
protected:
CChainParams() {}

Expand Down Expand Up @@ -139,12 +145,14 @@ const CChainParams &Params();
/** Return parameters for the given network. */
CChainParams &Params(CBaseChainParams::Network network);

/** Get modifiable network parameters (UNITTEST only) */
CModifiableParams *ModifiableParams();

/** Sets the params returned by Params() to those for the given network. */
void SelectParams(CBaseChainParams::Network network);

/**
* Sets the params returned by Params() to those for the given network
* with given blocksigning pubkey */
void SelectParams(CBaseChainParams::Network network, CScript scriptDestination);

/**
* Looks for -regtest or -testnet and then calls SelectParams as appropriate.
* Returns false if an invalid combination is given.
Expand Down
17 changes: 17 additions & 0 deletions src/chainparamsbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

#include "chainparamsbase.h"

#include "script/script.h"
#include "util.h"
#include "utilstrencodings.h"

#include <assert.h>
#include <stdio.h>

#include <boost/assign/list_of.hpp>

Expand Down Expand Up @@ -114,6 +117,20 @@ CBaseChainParams::Network NetworkIdFromCommandLine()
return CBaseChainParams::MAIN;
}

CScript ScriptDestinationFromCommandLine()
{
std::string sd = GetArg("-genesisscriptdestination", "");
if (!sd.empty()) {
if (IsHex(sd)) {
std::vector<unsigned char> sd_raw(ParseHex(sd));
return CScript(sd_raw.begin(), sd_raw.end());
} else {
fprintf(stderr, "Warning: Genesis script destination was not valid hex, ignoring it.\n");
}
}
return CScript();
}

bool SelectBaseParamsFromCommandLine()
{
CBaseChainParams::Network network = NetworkIdFromCommandLine();
Expand Down
8 changes: 8 additions & 0 deletions src/chainparamsbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <string>
#include <vector>

#include "script/script.h"

/**
* CBaseChainParams defines the base parameters (shared between bitcoin-cli and bitcoind)
* of a given instance of the Bitcoin system.
Expand Down Expand Up @@ -50,6 +52,12 @@ void SelectBaseParams(CBaseChainParams::Network network);
*/
CBaseChainParams::Network NetworkIdFromCommandLine();

/**
* Looks for hex-encoded -genesisscriptdistenation and returns a CScript of it.
* Returns an empty script if the flag is missing or badly encoded.
*/
CScript ScriptDestinationFromCommandLine();

/**
* Calls NetworkIdFromCommandLine() and then calls SelectParams as appropriate.
* Returns false if an invalid combination is given.
Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += " -gen " + strprintf(_("Generate coins (default: %u)"), 0) + "\n";
strUsage += " -genproclimit=<n> " + strprintf(_("Set the number of threads for coin generation if enabled (-1 = all cores, default: %d)"), 1) + "\n";
#endif
strUsage += " -genesisscriptdestination " + _("Set the scriptPubKey used when signing blocks. This is intended for testing, "
"as changing it forks the node off of the network") + "\n";
strUsage += " -help-debug " + _("Show all debugging options (usage: --help -help-debug)") + "\n";
strUsage += " -logips " + strprintf(_("Include IP addresses in debug output (default: %u)"), 0) + "\n";
strUsage += " -logtimestamps " + strprintf(_("Prepend debug output with timestamp (default: %u)"), 1) + "\n";
Expand Down

0 comments on commit da45c44

Please sign in to comment.