From 1712adbe0b084b6907b82db350a51ab44f8c3117 Mon Sep 17 00:00:00 2001 From: jtimon Date: Fri, 7 Mar 2014 17:59:30 -0800 Subject: [PATCH 01/13] Add MiningRequiresPeers chain parameter --- src/chainparams.cpp | 1 + src/chainparams.h | 2 ++ src/miner.cpp | 5 +++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index f5cf846a0..ff104e7c8 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -246,6 +246,7 @@ public: } virtual bool RequireRPCPassword() const { return false; } + virtual bool MiningRequiresPeers() const { return false; } virtual Network NetworkID() const { return CChainParams::REGTEST; } }; static CRegTestParams regTestParams; diff --git a/src/chainparams.h b/src/chainparams.h index 5600b904c..a73279fe6 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -59,6 +59,8 @@ public: int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; } virtual const CBlock& GenesisBlock() const = 0; virtual bool RequireRPCPassword() const { return true; } + /* Make miner wait to have peers to avoid wasting work */ + virtual bool MiningRequiresPeers() const { return true; } const string& DataDir() const { return strDataDir; } virtual Network NetworkID() const = 0; const vector& DNSSeeds() const { return vSeeds; } diff --git a/src/miner.cpp b/src/miner.cpp index 94fc8e388..3d46a0d83 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -512,7 +512,7 @@ void static BitcoinMiner(CWallet *pwallet) unsigned int nExtraNonce = 0; try { while (true) { - if (Params().NetworkID() != CChainParams::REGTEST) { + if (Params().MiningRequiresPeers()) { // Busy-wait for the network to come online so we don't waste time mining // on an obsolete chain. In regtest mode we expect to fly solo. while (vNodes.empty()) @@ -620,7 +620,8 @@ void static BitcoinMiner(CWallet *pwallet) // Check for stop or if block needs to be rebuilt boost::this_thread::interruption_point(); - if (vNodes.empty() && Params().NetworkID() != CChainParams::REGTEST) + // Regtest mode doesn't require peers + if (vNodes.empty() && Params().MiningRequiresPeers()) break; if (nBlockNonce >= 0xffff0000) break; From bfa9a1a638d69ec3edc2473eef5f2eee43af5a9d Mon Sep 17 00:00:00 2001 From: jtimon Date: Sun, 9 Mar 2014 13:36:55 -0700 Subject: [PATCH 02/13] Add MineBlocksOnDemand chain parameter --- src/chainparams.cpp | 1 + src/chainparams.h | 3 +++ src/miner.cpp | 5 ++--- src/rpcmining.cpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index ff104e7c8..d7205fa5c 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -247,6 +247,7 @@ public: virtual bool RequireRPCPassword() const { return false; } virtual bool MiningRequiresPeers() const { return false; } + virtual bool MineBlocksOnDemand() const { return true; } virtual Network NetworkID() const { return CChainParams::REGTEST; } }; static CRegTestParams regTestParams; diff --git a/src/chainparams.h b/src/chainparams.h index a73279fe6..d76b1a228 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -62,6 +62,9 @@ public: /* Make miner wait to have peers to avoid wasting work */ virtual bool MiningRequiresPeers() const { return true; } const string& DataDir() const { return strDataDir; } + /* Make miner stop after a block is found. In RPC, don't return + * until nGenProcLimit blocks are generated */ + virtual bool MineBlocksOnDemand() const { return false; } virtual Network NetworkID() const = 0; const vector& DNSSeeds() const { return vSeeds; } const std::vector &Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } diff --git a/src/miner.cpp b/src/miner.cpp index 3d46a0d83..e980fdc42 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -580,9 +580,8 @@ void static BitcoinMiner(CWallet *pwallet) CheckWork(pblock, *pwallet, reservekey); SetThreadPriority(THREAD_PRIORITY_LOWEST); - // In regression test mode, stop mining after a block is found. This - // allows developers to controllably generate a block on demand. - if (Params().NetworkID() == CChainParams::REGTEST) + // In regression test mode, stop mining after a block is found. + if (Params().MineBlocksOnDemand()) throw boost::thread_interrupted(); break; diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 23876c603..70adae146 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -174,7 +174,7 @@ Value setgenerate(const Array& params, bool fHelp) } // -regtest mode: don't return until nGenProcLimit blocks are generated - if (fGenerate && Params().NetworkID() == CChainParams::REGTEST) + if (fGenerate && Params().MineBlocksOnDemand()) { int nHeightStart = 0; int nHeightEnd = 0; From 2595b9ac23735649f4601c27924821641611696e Mon Sep 17 00:00:00 2001 From: jtimon Date: Fri, 7 Mar 2014 22:47:56 -0800 Subject: [PATCH 03/13] Add DefaultMinerThreads chain parameter --- src/chainparams.cpp | 2 ++ src/chainparams.h | 3 +++ src/miner.cpp | 5 +++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index d7205fa5c..64723978b 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -112,6 +112,7 @@ public: nRPCPort = 8332; bnProofOfWorkLimit = ~uint256(0) >> 32; nSubsidyHalvingInterval = 210000; + nMinerThreads = 0; // Build the genesis block. Note that the output of the genesis coinbase cannot // be spent as it did not originally exist in the database. @@ -233,6 +234,7 @@ public: pchMessageStart[2] = 0xb5; pchMessageStart[3] = 0xda; nSubsidyHalvingInterval = 150; + nMinerThreads = 1; bnProofOfWorkLimit = ~uint256(0) >> 1; genesis.nTime = 1296688602; genesis.nBits = 0x207fffff; diff --git a/src/chainparams.h b/src/chainparams.h index d76b1a228..f331d956f 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -57,6 +57,8 @@ public: int GetDefaultPort() const { return nDefaultPort; } const uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; } int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; } + /* Used if GenerateBitcoins is called with a negative number of threads */ + int DefaultMinerThreads() const { return nMinerThreads; } virtual const CBlock& GenesisBlock() const = 0; virtual bool RequireRPCPassword() const { return true; } /* Make miner wait to have peers to avoid wasting work */ @@ -82,6 +84,7 @@ protected: uint256 bnProofOfWorkLimit; int nSubsidyHalvingInterval; string strDataDir; + int nMinerThreads; vector vSeeds; std::vector base58Prefixes[MAX_BASE58_TYPES]; }; diff --git a/src/miner.cpp b/src/miner.cpp index e980fdc42..44c2faaa4 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -652,8 +652,9 @@ void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads) static boost::thread_group* minerThreads = NULL; if (nThreads < 0) { - if (Params().NetworkID() == CChainParams::REGTEST) - nThreads = 1; + // In regtest threads defaults to 1 + if (Params().DefaultMinerThreads()) + nThreads = Params().DefaultMinerThreads(); else nThreads = boost::thread::hardware_concurrency(); } From cb9bd83bba86e73e3720110ce47600b6f895573c Mon Sep 17 00:00:00 2001 From: jtimon Date: Sun, 9 Mar 2014 18:35:41 -0700 Subject: [PATCH 04/13] Add DefaultCheckMemPool chain parameter --- src/chainparams.cpp | 1 + src/chainparams.h | 2 ++ src/init.cpp | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 64723978b..75a14c601 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -250,6 +250,7 @@ public: virtual bool RequireRPCPassword() const { return false; } virtual bool MiningRequiresPeers() const { return false; } virtual bool MineBlocksOnDemand() const { return true; } + virtual bool DefaultCheckMemPool() const { return true; } virtual Network NetworkID() const { return CChainParams::REGTEST; } }; static CRegTestParams regTestParams; diff --git a/src/chainparams.h b/src/chainparams.h index f331d956f..bc982ee04 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -63,6 +63,8 @@ public: virtual bool RequireRPCPassword() const { return true; } /* Make miner wait to have peers to avoid wasting work */ virtual bool MiningRequiresPeers() const { return true; } + /* Default value for -checkmempool argument */ + virtual bool DefaultCheckMemPool() const { return false; } const string& DataDir() const { return strDataDir; } /* Make miner stop after a block is found. In RPC, don't return * until nGenProcLimit blocks are generated */ diff --git a/src/init.cpp b/src/init.cpp index 840a07d61..a225c3f96 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -517,7 +517,8 @@ bool AppInit2(boost::thread_group& threadGroup) InitWarning(_("Warning: Deprecated argument -debugnet ignored, use -debug=net")); fBenchmark = GetBoolArg("-benchmark", false); - mempool.setSanityCheck(GetBoolArg("-checkmempool", RegTest())); + // Checkmempool defaults to true in regtest mode + mempool.setSanityCheck(GetBoolArg("-checkmempool", Params().DefaultCheckMemPool())); Checkpoints::fEnabled = GetBoolArg("-checkpoints", true); // -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency From 8d26721498eb608b1ee5fd4d10083739a77b5905 Mon Sep 17 00:00:00 2001 From: jtimon Date: Fri, 7 Mar 2014 23:06:22 -0800 Subject: [PATCH 05/13] Get rid of RegTest() --- src/chainparams.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/chainparams.h b/src/chainparams.h index bc982ee04..f0c90bf50 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -111,8 +111,4 @@ inline bool TestNet() { return Params().NetworkID() == CChainParams::TESTNET; } -inline bool RegTest() { - return Params().NetworkID() == CChainParams::REGTEST; -} - #endif From d754f34e8d470d5d89e2bc31ff1ab60ae5889266 Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 22 Mar 2014 19:52:26 +0100 Subject: [PATCH 06/13] Move majority constants to chainparams --- src/chainparams.cpp | 9 +++++++++ src/chainparams.h | 8 ++++++++ src/main.cpp | 34 ++++++++++++++-------------------- src/main.h | 5 +++-- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 75a14c601..86d80629e 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -112,6 +112,9 @@ public: nRPCPort = 8332; bnProofOfWorkLimit = ~uint256(0) >> 32; nSubsidyHalvingInterval = 210000; + nEnforceBlockUpgradeMajority = 750; + nRejectBlockOutdatedMajority = 950; + nToCheckBlockUpgradeMajority = 1000; nMinerThreads = 0; // Build the genesis block. Note that the output of the genesis coinbase cannot @@ -199,6 +202,9 @@ public: vAlertPubKey = ParseHex("04302390343f91cc401d56d68b123028bf52e5fca1939df127f63c6467cdf9c8e2c14b61104cf817d0b780da337893ecc4aaff1309e536162dabbdb45200ca2b0a"); nDefaultPort = 18333; nRPCPort = 18332; + nEnforceBlockUpgradeMajority = 51; + nRejectBlockOutdatedMajority = 75; + nToCheckBlockUpgradeMajority = 100; strDataDir = "testnet3"; // Modify the testnet genesis block so the timestamp is valid for a later start. @@ -234,6 +240,9 @@ public: pchMessageStart[2] = 0xb5; pchMessageStart[3] = 0xda; nSubsidyHalvingInterval = 150; + nEnforceBlockUpgradeMajority = 750; + nRejectBlockOutdatedMajority = 950; + nToCheckBlockUpgradeMajority = 1000; nMinerThreads = 1; bnProofOfWorkLimit = ~uint256(0) >> 1; genesis.nTime = 1296688602; diff --git a/src/chainparams.h b/src/chainparams.h index f0c90bf50..f400907d7 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -57,6 +57,11 @@ public: int GetDefaultPort() const { return nDefaultPort; } const uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; } int SubsidyHalvingInterval() const { return nSubsidyHalvingInterval; } + /* Used to check majorities for block version upgrade */ + int EnforceBlockUpgradeMajority() const { return nEnforceBlockUpgradeMajority; } + int RejectBlockOutdatedMajority() const { return nRejectBlockOutdatedMajority; } + int ToCheckBlockUpgradeMajority() const { return nToCheckBlockUpgradeMajority; } + /* Used if GenerateBitcoins is called with a negative number of threads */ int DefaultMinerThreads() const { return nMinerThreads; } virtual const CBlock& GenesisBlock() const = 0; @@ -85,6 +90,9 @@ protected: int nRPCPort; uint256 bnProofOfWorkLimit; int nSubsidyHalvingInterval; + int nEnforceBlockUpgradeMajority; + int nRejectBlockOutdatedMajority; + int nToCheckBlockUpgradeMajority; string strDataDir; int nMinerThreads; vector vSeeds; diff --git a/src/main.cpp b/src/main.cpp index 30fa9c6d7..e6519864d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2469,14 +2469,11 @@ bool AcceptBlockHeader(CBlockHeader& block, CValidationState& state, CBlockIndex return state.DoS(100, error("AcceptBlock() : forked chain older than last checkpoint (height %d)", nHeight)); // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded: - if (block.nVersion < 2) + if (block.nVersion < 2 && + CBlockIndex::IsSuperMajority(2, pindexPrev, Params().RejectBlockOutdatedMajority())) { - if ((!TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 950, 1000)) || - (TestNet() && CBlockIndex::IsSuperMajority(2, pindexPrev, 75, 100))) - { - return state.Invalid(error("AcceptBlock() : rejected nVersion=1 block"), - REJECT_OBSOLETE, "bad-version"); - } + return state.Invalid(error("AcceptBlock() : rejected nVersion=1 block"), + REJECT_OBSOLETE, "bad-version"); } } @@ -2517,19 +2514,15 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, } // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height - if (block.nVersion >= 2) + // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet): + if (block.nVersion >= 2 && + CBlockIndex::IsSuperMajority(2, pindex->pprev, Params().EnforceBlockUpgradeMajority())) { - // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet): - if ((!TestNet() && CBlockIndex::IsSuperMajority(2, pindex->pprev, 750, 1000)) || - (TestNet() && CBlockIndex::IsSuperMajority(2, pindex->pprev, 51, 100))) - { - CScript expect = CScript() << nHeight; - if (block.vtx[0].vin[0].scriptSig.size() < expect.size() || - !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) { - pindex->nStatus |= BLOCK_FAILED_VALID; - return state.DoS(100, error("AcceptBlock() : block height mismatch in coinbase"), - REJECT_INVALID, "bad-cb-height"); - } + CScript expect = CScript() << nHeight; + if (block.vtx[0].vin[0].scriptSig.size() < expect.size() || + !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) { + pindex->nStatus |= BLOCK_FAILED_VALID; + return state.DoS(100, error("AcceptBlock() : block height mismatch in coinbase"), REJECT_INVALID, "bad-cb-height"); } } @@ -2563,8 +2556,9 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, return true; } -bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired, unsigned int nToCheck) +bool CBlockIndex::IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned int nRequired) { + unsigned int nToCheck = Params().ToCheckBlockUpgradeMajority(); unsigned int nFound = 0; for (unsigned int i = 0; i < nToCheck && nFound < nRequired && pstart != NULL; i++) { diff --git a/src/main.h b/src/main.h index 42cf4a086..58515b44b 100644 --- a/src/main.h +++ b/src/main.h @@ -848,10 +848,11 @@ public: /** * Returns true if there are nRequired or more blocks of minVersion or above - * in the last nToCheck blocks, starting at pstart and going backwards. + * in the last Params().ToCheckBlockUpgradeMajority() blocks, starting at pstart + * and going backwards. */ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, - unsigned int nRequired, unsigned int nToCheck); + unsigned int nRequired); std::string ToString() const { From 21913a9ac9525547ebe18619748abb18a2ca8cdf Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 22 Mar 2014 20:09:12 +0100 Subject: [PATCH 07/13] Add AllowMinDifficultyBlocks chain parameter --- src/chainparams.cpp | 2 ++ src/chainparams.h | 2 ++ src/main.cpp | 6 +++--- src/miner.cpp | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 86d80629e..c43de2e72 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -224,6 +224,8 @@ public: base58Prefixes[EXT_PUBLIC_KEY] = list_of(0x04)(0x35)(0x87)(0xCF); base58Prefixes[EXT_SECRET_KEY] = list_of(0x04)(0x35)(0x83)(0x94); } + + virtual bool AllowMinDifficultyBlocks() const { return true; } virtual Network NetworkID() const { return CChainParams::TESTNET; } }; static CTestNetParams testNetParams; diff --git a/src/chainparams.h b/src/chainparams.h index f400907d7..e1ce86550 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -70,6 +70,8 @@ public: virtual bool MiningRequiresPeers() const { return true; } /* Default value for -checkmempool argument */ virtual bool DefaultCheckMemPool() const { return false; } + /* Allow mining of a min-difficulty block */ + virtual bool AllowMinDifficultyBlocks() const { return false; } const string& DataDir() const { return strDataDir; } /* Make miner stop after a block is found. In RPC, don't return * until nGenProcLimit blocks are generated */ diff --git a/src/main.cpp b/src/main.cpp index e6519864d..7d0a57657 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1210,7 +1210,7 @@ unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime) const uint256 &bnLimit = Params().ProofOfWorkLimit(); // Testnet has min-difficulty blocks // after nTargetSpacing*2 time between blocks: - if (TestNet() && nTime > nTargetSpacing*2) + if (Params().AllowMinDifficultyBlocks() && nTime > nTargetSpacing*2) return bnLimit.GetCompact(); uint256 bnResult; @@ -1238,7 +1238,7 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead // Only change once per interval if ((pindexLast->nHeight+1) % nInterval != 0) { - if (TestNet()) + if (Params().AllowMinDifficultyBlocks()) { // Special difficulty rule for testnet: // If the new block's timestamp is more than 2* 10 minutes @@ -1468,7 +1468,7 @@ void UpdateTime(CBlockHeader& block, const CBlockIndex* pindexPrev) block.nTime = max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime()); // Updating time can change work required on testnet: - if (TestNet()) + if (Params().AllowMinDifficultyBlocks()) block.nBits = GetNextWorkRequired(pindexPrev, &block); } diff --git a/src/miner.cpp b/src/miner.cpp index 44c2faaa4..708b9248f 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -632,7 +632,7 @@ void static BitcoinMiner(CWallet *pwallet) // Update nTime every few seconds UpdateTime(*pblock, pindexPrev); nBlockTime = ByteReverse(pblock->nTime); - if (TestNet()) + if (Params().AllowMinDifficultyBlocks()) { // Changing pblock->nTime can change work required on testnet: nBlockBits = ByteReverse(pblock->nBits); From cfeb8235fda44aa71fb99ce583c2c2049105ad5a Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 22 Mar 2014 20:19:48 +0100 Subject: [PATCH 08/13] Add RequireStandard chain parameter --- src/chainparams.cpp | 2 ++ src/chainparams.h | 2 ++ src/main.cpp | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index c43de2e72..f10599690 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -226,6 +226,7 @@ public: } virtual bool AllowMinDifficultyBlocks() const { return true; } + virtual bool RequireStandard() const { return false; } virtual Network NetworkID() const { return CChainParams::TESTNET; } }; static CTestNetParams testNetParams; @@ -262,6 +263,7 @@ public: virtual bool MiningRequiresPeers() const { return false; } virtual bool MineBlocksOnDemand() const { return true; } virtual bool DefaultCheckMemPool() const { return true; } + virtual bool RequireStandard() const { return false; } virtual Network NetworkID() const { return CChainParams::REGTEST; } }; static CRegTestParams regTestParams; diff --git a/src/chainparams.h b/src/chainparams.h index e1ce86550..881eb2294 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -72,6 +72,8 @@ public: virtual bool DefaultCheckMemPool() const { return false; } /* Allow mining of a min-difficulty block */ virtual bool AllowMinDifficultyBlocks() const { return false; } + /* Make standard checks */ + virtual bool RequireStandard() const { return true; } const string& DataDir() const { return strDataDir; } /* Make miner stop after a block is found. In RPC, don't return * until nGenProcLimit blocks are generated */ diff --git a/src/main.cpp b/src/main.cpp index 7d0a57657..2f24beecc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -833,7 +833,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa // Rather not work on nonstandard transactions (unless -testnet/-regtest) string reason; - if (Params().NetworkID() == CChainParams::MAIN && !IsStandardTx(tx, reason)) + if (Params().RequireStandard() && !IsStandardTx(tx, reason)) return state.DoS(0, error("AcceptToMemoryPool : nonstandard transaction: %s", reason), REJECT_NONSTANDARD, reason); @@ -894,7 +894,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa } // Check for non-standard pay-to-script-hash in inputs - if (Params().NetworkID() == CChainParams::MAIN && !AreInputsStandard(tx, view)) + if (Params().RequireStandard() && !AreInputsStandard(tx, view)) return error("AcceptToMemoryPool: : nonstandard transaction input"); // Note: if you modify this code to accept non-standard transactions, then From 6fc0fa63d9eb6bdfcdfd2fd9792d23059c763534 Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 22 Mar 2014 20:20:43 +0100 Subject: [PATCH 09/13] Add RPCisTestNet chain parameter --- src/chainparams.cpp | 1 + src/chainparams.h | 2 ++ src/rpcmining.cpp | 2 +- src/rpcmisc.cpp | 2 +- 4 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index f10599690..109ee02b0 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -227,6 +227,7 @@ public: virtual bool AllowMinDifficultyBlocks() const { return true; } virtual bool RequireStandard() const { return false; } + virtual bool RPCisTestNet() const { return true; } virtual Network NetworkID() const { return CChainParams::TESTNET; } }; static CTestNetParams testNetParams; diff --git a/src/chainparams.h b/src/chainparams.h index 881eb2294..3849d33ac 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -74,6 +74,8 @@ public: virtual bool AllowMinDifficultyBlocks() const { return false; } /* Make standard checks */ virtual bool RequireStandard() const { return true; } + /* Make standard checks */ + virtual bool RPCisTestNet() const { return false; } const string& DataDir() const { return strDataDir; } /* Make miner stop after a block is found. In RPC, don't return * until nGenProcLimit blocks are generated */ diff --git a/src/rpcmining.cpp b/src/rpcmining.cpp index 70adae146..edf91023d 100644 --- a/src/rpcmining.cpp +++ b/src/rpcmining.cpp @@ -265,7 +265,7 @@ Value getmininginfo(const Array& params, bool fHelp) obj.push_back(Pair("genproclimit", (int)GetArg("-genproclimit", -1))); obj.push_back(Pair("networkhashps", getnetworkhashps(params, false))); obj.push_back(Pair("pooledtx", (uint64_t)mempool.size())); - obj.push_back(Pair("testnet", TestNet())); + obj.push_back(Pair("testnet", Params().RPCisTestNet())); #ifdef ENABLE_WALLET obj.push_back(Pair("generate", getgenerate(params, false))); obj.push_back(Pair("hashespersec", gethashespersec(params, false))); diff --git a/src/rpcmisc.cpp b/src/rpcmisc.cpp index 27d6d61a3..a1793e2e4 100644 --- a/src/rpcmisc.cpp +++ b/src/rpcmisc.cpp @@ -73,7 +73,7 @@ Value getinfo(const Array& params, bool fHelp) obj.push_back(Pair("connections", (int)vNodes.size())); obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string()))); obj.push_back(Pair("difficulty", (double)GetDifficulty())); - obj.push_back(Pair("testnet", TestNet())); + obj.push_back(Pair("testnet", Params().RPCisTestNet())); #ifdef ENABLE_WALLET if (pwalletMain) { obj.push_back(Pair("keypoololdest", pwalletMain->GetOldestKeyPoolTime())); From a3d946ebdc2eebef9ccdc2c883f8abfebcf0f653 Mon Sep 17 00:00:00 2001 From: jtimon Date: Sat, 22 Mar 2014 20:22:14 +0100 Subject: [PATCH 10/13] Get rid of TestNet() --- src/bitcoin-cli.cpp | 2 +- src/bitcoind.cpp | 2 +- src/chainparams.h | 5 ----- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index ce9e7a402..29efdfa82 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -33,7 +33,7 @@ static bool AppInitRPC(int argc, char* argv[]) fprintf(stderr,"Error reading configuration file: %s\n", e.what()); return false; } - // Check for -testnet or -regtest parameter (TestNet() calls are only valid after this clause) + // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause) if (!SelectParamsFromCommandLine()) { fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n"); return false; diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 9b535c2e6..704332c39 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -77,7 +77,7 @@ bool AppInit(int argc, char* argv[]) fprintf(stderr,"Error reading configuration file: %s\n", e.what()); return false; } - // Check for -testnet or -regtest parameter (TestNet() calls are only valid after this clause) + // Check for -testnet or -regtest parameter (Params() calls are only valid after this clause) if (!SelectParamsFromCommandLine()) { fprintf(stderr, "Error: Invalid combination of -regtest and -testnet.\n"); return false; diff --git a/src/chainparams.h b/src/chainparams.h index 3849d33ac..653ca20f0 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -120,9 +120,4 @@ void SelectParams(CChainParams::Network network); */ bool SelectParamsFromCommandLine(); -inline bool TestNet() { - // Note: it's deliberate that this returns "false" for regression test mode. - return Params().NetworkID() == CChainParams::TESTNET; -} - #endif From c8c52de3a05b617bc399e2dd45901e74237b7fc1 Mon Sep 17 00:00:00 2001 From: jtimon Date: Wed, 4 Jun 2014 12:51:29 +0200 Subject: [PATCH 11/13] Replace virtual methods with static attributes, chainparams.h depends on protocol.h instead of the other way around --- src/alert.cpp | 1 + src/chainparams.cpp | 51 +++++++++++++++++++++++---------------------- src/chainparams.h | 38 ++++++++++++++++++++------------- src/protocol.cpp | 1 + src/protocol.h | 3 ++- 5 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/alert.cpp b/src/alert.cpp index 99164d63e..638f0d7a1 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -5,6 +5,7 @@ #include "alert.h" +#include "chainparams.h" #include "key.h" #include "net.h" #include "ui_interface.h" diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 109ee02b0..3f4d7f706 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -6,8 +6,6 @@ #include "chainparams.h" #include "assert.h" -#include "core.h" -#include "protocol.h" #include "util.h" #include @@ -100,6 +98,7 @@ unsigned int pnSeed[] = class CMainParams : public CChainParams { public: CMainParams() { + networkID = CChainParams::MAIN; // The message start string is designed to be unlikely to occur in normal data. // The characters are rarely used upper ASCII, not valid as UTF-8, and produce // a large 4-byte int at any alignment. @@ -171,27 +170,25 @@ public: addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek; vFixedSeeds.push_back(addr); } - } - virtual const CBlock& GenesisBlock() const { return genesis; } - virtual Network NetworkID() const { return CChainParams::MAIN; } - - virtual const vector& FixedSeeds() const { - return vFixedSeeds; + fRequireRPCPassword = true; + fMiningRequiresPeers = true; + fDefaultCheckMemPool = false; + fAllowMinDifficultyBlocks = false; + fRequireStandard = true; + fRPCisTestNet = false; + fMineBlocksOnDemand = false; } -protected: - CBlock genesis; - vector vFixedSeeds; }; static CMainParams mainParams; - // // Testnet (v3) // class CTestNetParams : public CMainParams { public: CTestNetParams() { + networkID = CChainParams::TESTNET; // The message start string is designed to be unlikely to occur in normal data. // The characters are rarely used upper ASCII, not valid as UTF-8, and produce // a large 4-byte int at any alignment. @@ -223,22 +220,25 @@ public: base58Prefixes[SECRET_KEY] = list_of(239); base58Prefixes[EXT_PUBLIC_KEY] = list_of(0x04)(0x35)(0x87)(0xCF); base58Prefixes[EXT_SECRET_KEY] = list_of(0x04)(0x35)(0x83)(0x94); - } - virtual bool AllowMinDifficultyBlocks() const { return true; } - virtual bool RequireStandard() const { return false; } - virtual bool RPCisTestNet() const { return true; } - virtual Network NetworkID() const { return CChainParams::TESTNET; } + fRequireRPCPassword = true; + fMiningRequiresPeers = true; + fDefaultCheckMemPool = false; + fAllowMinDifficultyBlocks = true; + fRequireStandard = false; + fRPCisTestNet = true; + fMineBlocksOnDemand = false; + } }; static CTestNetParams testNetParams; - // // Regression test // class CRegTestParams : public CTestNetParams { public: CRegTestParams() { + networkID = CChainParams::REGTEST; pchMessageStart[0] = 0xfa; pchMessageStart[1] = 0xbf; pchMessageStart[2] = 0xb5; @@ -258,14 +258,15 @@ public: assert(hashGenesisBlock == uint256("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206")); vSeeds.clear(); // Regtest mode doesn't have any DNS seeds. - } - virtual bool RequireRPCPassword() const { return false; } - virtual bool MiningRequiresPeers() const { return false; } - virtual bool MineBlocksOnDemand() const { return true; } - virtual bool DefaultCheckMemPool() const { return true; } - virtual bool RequireStandard() const { return false; } - virtual Network NetworkID() const { return CChainParams::REGTEST; } + fRequireRPCPassword = false; + fMiningRequiresPeers = false; + fDefaultCheckMemPool = true; + fAllowMinDifficultyBlocks = true; + fRequireStandard = false; + fRPCisTestNet = true; + fMineBlocksOnDemand = true; + } }; static CRegTestParams regTestParams; diff --git a/src/chainparams.h b/src/chainparams.h index 653ca20f0..988e3ac3a 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -7,17 +7,15 @@ #define BITCOIN_CHAIN_PARAMS_H #include "uint256.h" +#include "core.h" +#include "protocol.h" #include using namespace std; -#define MESSAGE_START_SIZE 4 typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; -class CAddress; -class CBlock; - struct CDNSSeedData { string name, host; CDNSSeedData(const string &strName, const string &strHost) : name(strName), host(strHost) {} @@ -64,26 +62,26 @@ public: /* Used if GenerateBitcoins is called with a negative number of threads */ int DefaultMinerThreads() const { return nMinerThreads; } - virtual const CBlock& GenesisBlock() const = 0; - virtual bool RequireRPCPassword() const { return true; } + const CBlock& GenesisBlock() const { return genesis; }; + bool RequireRPCPassword() const { return fRequireRPCPassword; } /* Make miner wait to have peers to avoid wasting work */ - virtual bool MiningRequiresPeers() const { return true; } + bool MiningRequiresPeers() const { return fMiningRequiresPeers; } /* Default value for -checkmempool argument */ - virtual bool DefaultCheckMemPool() const { return false; } + bool DefaultCheckMemPool() const { return fDefaultCheckMemPool; } /* Allow mining of a min-difficulty block */ - virtual bool AllowMinDifficultyBlocks() const { return false; } + bool AllowMinDifficultyBlocks() const { return fAllowMinDifficultyBlocks; } /* Make standard checks */ - virtual bool RequireStandard() const { return true; } + bool RequireStandard() const { return fRequireStandard; } /* Make standard checks */ - virtual bool RPCisTestNet() const { return false; } + bool RPCisTestNet() const { return fRPCisTestNet; } const string& DataDir() const { return strDataDir; } /* Make miner stop after a block is found. In RPC, don't return * until nGenProcLimit blocks are generated */ - virtual bool MineBlocksOnDemand() const { return false; } - virtual Network NetworkID() const = 0; + bool MineBlocksOnDemand() const { return fMineBlocksOnDemand; } + Network NetworkID() const { return networkID; } const vector& DNSSeeds() const { return vSeeds; } - const std::vector &Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } - virtual const vector& FixedSeeds() const = 0; + const std::vector& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } + const vector& FixedSeeds() const { return vFixedSeeds; } int RPCPort() const { return nRPCPort; } protected: CChainParams() {} @@ -103,6 +101,16 @@ protected: int nMinerThreads; vector vSeeds; std::vector base58Prefixes[MAX_BASE58_TYPES]; + Network networkID; + CBlock genesis; + vector vFixedSeeds; + bool fRequireRPCPassword; + bool fMiningRequiresPeers; + bool fDefaultCheckMemPool; + bool fAllowMinDifficultyBlocks; + bool fRequireStandard; + bool fRPCisTestNet; + bool fMineBlocksOnDemand; }; /** diff --git a/src/protocol.cpp b/src/protocol.cpp index c77a92f02..87b2f2387 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -5,6 +5,7 @@ #include "protocol.h" +#include "chainparams.h" #include "util.h" #ifndef WIN32 diff --git a/src/protocol.h b/src/protocol.h index e6f105fe5..6de5d05a7 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -10,7 +10,6 @@ #ifndef __INCLUDED_PROTOCOL_H__ #define __INCLUDED_PROTOCOL_H__ -#include "chainparams.h" #include "netbase.h" #include "serialize.h" #include "uint256.h" @@ -19,6 +18,8 @@ #include #include +#define MESSAGE_START_SIZE 4 + /** Message header. * (4) message start. * (12) command. From 2871889e834b664bcaa3fdb3ecb6c5cdc751afb1 Mon Sep 17 00:00:00 2001 From: jtimon Date: Wed, 4 Jun 2014 12:52:15 +0200 Subject: [PATCH 12/13] net.h was using std namespace through chainparams.h included in protocol.h --- src/net.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net.h b/src/net.h index d3346873b..d50cd92db 100644 --- a/src/net.h +++ b/src/net.h @@ -129,7 +129,7 @@ struct LocalServiceInfo { }; extern CCriticalSection cs_mapLocalHost; -extern map mapLocalHost; +extern std::map mapLocalHost; class CNodeStats { From f0a83fc256023f68cc046bd096de69f16ce9d394 Mon Sep 17 00:00:00 2001 From: jtimon Date: Wed, 4 Jun 2014 15:25:58 +0200 Subject: [PATCH 13/13] Use Params().NetworkID() instead of TestNet() from the payment protocol --- src/qt/paymentserver.cpp | 14 ++++++++++++-- src/qt/paymentserver.h | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/qt/paymentserver.cpp b/src/qt/paymentserver.cpp index 4c4558568..20551591c 100644 --- a/src/qt/paymentserver.cpp +++ b/src/qt/paymentserver.cpp @@ -490,6 +490,17 @@ bool PaymentServer::readPaymentRequest(const QString& filename, PaymentRequestPl return request.parse(data); } +std::string PaymentServer::mapNetworkIdToName(CChainParams::Network networkId) +{ + if (networkId == CChainParams::MAIN) + return "main"; + if (networkId == CChainParams::TESTNET) + return "test"; + if (networkId == CChainParams::REGTEST) + return "regtest"; + return ""; +} + bool PaymentServer::processPaymentRequest(PaymentRequestPlus& request, SendCoinsRecipient& recipient) { if (!optionsModel) @@ -499,8 +510,7 @@ bool PaymentServer::processPaymentRequest(PaymentRequestPlus& request, SendCoins const payments::PaymentDetails& details = request.getDetails(); // Payment request network matches client network? - if ((details.network() == "main" && TestNet()) || - (details.network() == "test" && !TestNet())) + if (details.network() != mapNetworkIdToName(Params().NetworkID())) { emit message(tr("Payment request rejected"), tr("Payment request network doesn't match client network."), CClientUIInterface::MSG_ERROR); diff --git a/src/qt/paymentserver.h b/src/qt/paymentserver.h index d84d09c57..d6949a47c 100644 --- a/src/qt/paymentserver.h +++ b/src/qt/paymentserver.h @@ -118,6 +118,7 @@ protected: private: static bool readPaymentRequest(const QString& filename, PaymentRequestPlus& request); + std::string mapNetworkIdToName(CChainParams::Network networkId); bool processPaymentRequest(PaymentRequestPlus& request, SendCoinsRecipient& recipient); void fetchRequest(const QUrl& url);