Skip to content

Commit 0cac6d2

Browse files
theunijtimon
authored andcommitted
consensus: Move consensus constants into Consensus::Params and consensus.h (as functions)
The following are now tied to a chain rather than being defined as global constants. Their values have not changed. nMinTxSize nMaxBlockSize nMaxTxSize nMaxBlockSigops nCoinbaseMaturity Also, for free (diff-wise): Blocksize: Turn MAX_BLOCK_SIZE (nMaxBlockSize) and MAX_BLOCK_SIGOPS (nMaxBlockSigops) into functions ...which take Consensus::Params as parameter This will be convenient to reduce the diff of any proposal that changes the blocksize as a hardfork
1 parent 660e072 commit 0cac6d2

File tree

13 files changed

+78
-36
lines changed

13 files changed

+78
-36
lines changed

src/bitcoin-tx.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)
176176

177177
static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
178178
{
179+
const Consensus::Params& consensusParams = Params().GetConsensus();
179180
// separate TXID:VOUT in string
180181
size_t pos = strInput.find(':');
181182
if ((pos == string::npos) ||
@@ -190,7 +191,7 @@ static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
190191
uint256 txid(uint256S(strTxid));
191192

192193
static const unsigned int minTxOutSz = 9;
193-
static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
194+
const unsigned int maxVout = consensusParams.nMaxTxSize / minTxOutSz;
194195

195196
// extract and validate vout
196197
string strVout = strInput.substr(pos + 1, string::npos);

src/chainparams.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ class CMainParams : public CChainParams {
7676
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
7777
consensus.nPowTargetSpacing = 10 * 60;
7878
consensus.fPowAllowMinDifficultyBlocks = false;
79+
consensus.nMaxBlockSize = 1000000;
7980
consensus.nMaxTxSize = 1000000;
81+
consensus.nMinTxSize = 60;
82+
consensus.nMaxBlockSigops = consensus.nMaxBlockSize / 50;
83+
consensus.nCoinbaseMaturity = 100;
8084

8185
/**
8286
* The message start string is designed to be unlikely to occur in normal data.
@@ -156,7 +160,11 @@ class CTestNetParams : public CChainParams {
156160
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
157161
consensus.nPowTargetSpacing = 10 * 60;
158162
consensus.fPowAllowMinDifficultyBlocks = true;
163+
consensus.nMaxBlockSize = 1000000;
159164
consensus.nMaxTxSize = 1000000;
165+
consensus.nMinTxSize = 60;
166+
consensus.nMaxBlockSigops = consensus.nMaxBlockSize / 50;
167+
consensus.nCoinbaseMaturity = 100;
160168

161169
pchMessageStart[0] = 0x0b;
162170
pchMessageStart[1] = 0x11;
@@ -219,7 +227,11 @@ class CRegTestParams : public CChainParams {
219227
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
220228
consensus.nPowTargetSpacing = 10 * 60;
221229
consensus.fPowAllowMinDifficultyBlocks = true;
230+
consensus.nMaxBlockSize = 1000000;
222231
consensus.nMaxTxSize = 1000000;
232+
consensus.nMinTxSize = 60;
233+
consensus.nMaxBlockSigops = consensus.nMaxBlockSize / 50;
234+
consensus.nCoinbaseMaturity = 100;
223235

224236
pchMessageStart[0] = 0xfa;
225237
pchMessageStart[1] = 0xbf;

src/consensus/consensus.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <stdint.h>
1212

13+
class CCoinsViewCache;
1314
class CTransaction;
1415
class CValidationState;
1516

@@ -26,14 +27,24 @@ namespace Consensus {
2627
* Context-independent CTransaction validity checks
2728
*/
2829
bool CheckTx(const CTransaction& tx, CValidationState& state, const Params& consensusParams);
30+
/**
31+
* Check whether all inputs of this transaction are valid (no double spends and amounts)
32+
* This does not modify the UTXO set. This does not check scripts and sigs.
33+
* Preconditions: tx.IsCoinBase() is false.
34+
*/
35+
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const Params& consensusParams, const CCoinsViewCache& inputs, int nSpendHeight);
2936

3037
} // namespace Consensus
3138

3239
/** The maximum allowed size for a serialized block, in bytes (network rule) */
33-
static const unsigned int MAX_BLOCK_SIZE = 1000000;
40+
inline uint64_t MaxBlockSize(const Consensus::Params& consensusParams)
41+
{
42+
return consensusParams.nMaxBlockSize;
43+
}
3444
/** The maximum allowed number of signature check operations in a block (network rule) */
35-
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
36-
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
37-
static const int COINBASE_MATURITY = 100;
45+
inline uint64_t MaxBlockSigops(const Consensus::Params& consensusParams)
46+
{
47+
return consensusParams.nMaxBlockSigops;
48+
}
3849

3950
#endif // BITCOIN_CONSENSUS_CONSENSUS_H

src/consensus/params.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ namespace Consensus {
1414
*/
1515
struct Params {
1616
uint256 hashGenesisBlock;
17+
uint32_t nMinTxSize;
18+
uint64_t nMaxBlockSize;
1719
uint32_t nMaxTxSize;
20+
uint64_t nMaxBlockSigops;
21+
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
22+
int32_t nCoinbaseMaturity;
1823
int nSubsidyHalvingInterval;
1924
/** Used to check majorities for block version upgrade */
2025
int nMajorityEnforceBlockUpgrade;

src/main.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
868868
// Check that the transaction doesn't have an excessive number of
869869
// sigops, making it impossible to mine. Since the coinbase transaction
870870
// itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
871-
// MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
871+
// MaxBlockSigops(); we still consider this an invalid rather than
872872
// merely non-standard transaction.
873873
unsigned int nSigOps = GetLegacySigOpCount(tx);
874874
nSigOps += GetP2SHSigOpCount(tx, view);
@@ -1293,8 +1293,7 @@ int GetSpendHeight(const CCoinsViewCache& inputs)
12931293
return pindexPrev->nHeight + 1;
12941294
}
12951295

1296-
namespace Consensus {
1297-
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1296+
bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, const Consensus::Params& consensusParams, const CCoinsViewCache& inputs, int nSpendHeight)
12981297
{
12991298
// This doesn't trigger the DoS code on purpose; if it did, it would make it easier
13001299
// for an attacker to attempt to split the network.
@@ -1311,7 +1310,7 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins
13111310

13121311
// If prev is coinbase, check that it's matured
13131312
if (coins->IsCoinBase()) {
1314-
if (nSpendHeight - coins->nHeight < COINBASE_MATURITY)
1313+
if (nSpendHeight - coins->nHeight < consensusParams.nCoinbaseMaturity)
13151314
return state.Invalid(false,
13161315
REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",
13171316
strprintf("tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight));
@@ -1337,13 +1336,13 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins
13371336
return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-outofrange");
13381337
return true;
13391338
}
1340-
}// namespace Consensus
13411339

13421340
bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
13431341
{
1342+
const Consensus::Params& consensusParams = Params().GetConsensus();
13441343
if (!tx.IsCoinBase())
13451344
{
1346-
if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
1345+
if (!Consensus::CheckTxInputs(tx, state, consensusParams, inputs, GetSpendHeight(inputs)))
13471346
return false;
13481347

13491348
if (pvChecks)
@@ -1749,13 +1748,14 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17491748
std::vector<std::pair<uint256, CDiskTxPos> > vPos;
17501749
vPos.reserve(block.vtx.size());
17511750
blockundo.vtxundo.reserve(block.vtx.size() - 1);
1751+
uint64_t nMaxBlockSigops = MaxBlockSigops(chainparams.GetConsensus());
17521752
for (unsigned int i = 0; i < block.vtx.size(); i++)
17531753
{
17541754
const CTransaction &tx = block.vtx[i];
17551755

17561756
nInputs += tx.vin.size();
17571757
nSigOps += GetLegacySigOpCount(tx);
1758-
if (nSigOps > MAX_BLOCK_SIGOPS)
1758+
if (nSigOps > nMaxBlockSigops)
17591759
return state.DoS(100, error("ConnectBlock(): too many sigops"),
17601760
REJECT_INVALID, "bad-blk-sigops");
17611761

@@ -1771,7 +1771,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
17711771
// this is to prevent a "rogue miner" from creating
17721772
// an incredibly-expensive-to-validate block.
17731773
nSigOps += GetP2SHSigOpCount(tx, view);
1774-
if (nSigOps > MAX_BLOCK_SIGOPS)
1774+
if (nSigOps > nMaxBlockSigops)
17751775
return state.DoS(100, error("ConnectBlock(): too many sigops"),
17761776
REJECT_INVALID, "bad-blk-sigops");
17771777
}
@@ -2593,7 +2593,9 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
25932593
// because we receive the wrong transactions for it.
25942594

25952595
// Size limits
2596-
if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
2596+
uint64_t nMaxBlockSize = MaxBlockSize(consensusParams);
2597+
uint64_t nMaxBlockSigops = MaxBlockSigops(consensusParams);
2598+
if (block.vtx.empty() || block.vtx.size() > nMaxBlockSize || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > nMaxBlockSize)
25972599
return state.DoS(100, error("CheckBlock(): size limits failed"),
25982600
REJECT_INVALID, "bad-blk-length");
25992601

@@ -2618,7 +2620,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
26182620
{
26192621
nSigOps += GetLegacySigOpCount(tx);
26202622
}
2621-
if (nSigOps > MAX_BLOCK_SIGOPS)
2623+
if (nSigOps > nMaxBlockSigops)
26222624
return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
26232625
REJECT_INVALID, "bad-blk-sigops", true);
26242626

@@ -3333,11 +3335,12 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
33333335
// Map of disk positions for blocks with unknown parent (only used for reindex)
33343336
static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
33353337
int64_t nStart = GetTimeMillis();
3338+
uint64_t nMaxBlockSize = MaxBlockSize(chainparams.GetConsensus());
33363339

33373340
int nLoaded = 0;
33383341
try {
33393342
// This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
3340-
CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
3343+
CBufferedFile blkdat(fileIn, 2*nMaxBlockSize, nMaxBlockSize+8, SER_DISK, CLIENT_VERSION);
33413344
uint64_t nRewind = blkdat.GetPos();
33423345
while (!blkdat.eof()) {
33433346
boost::this_thread::interruption_point();
@@ -3356,7 +3359,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
33563359
continue;
33573360
// read size
33583361
blkdat >> nSize;
3359-
if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
3362+
if (nSize < 80 || nSize > nMaxBlockSize)
33603363
continue;
33613364
} catch (const std::exception&) {
33623365
// no valid block header found; don't complain

src/miner.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
110110

111111
// -regtest only: allow overriding block.nVersion with
112112
// -blockversion=N to test forking scenarios
113-
if (Params().MineBlocksOnDemand())
113+
if (chainparams.MineBlocksOnDemand())
114114
pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
115115

116116
// Create coinbase tx
@@ -126,18 +126,19 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
126126
pblocktemplate->vTxSigOps.push_back(-1); // updated at end
127127

128128
// Largest block you're willing to create:
129-
unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
130-
// Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
131-
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
129+
uint64_t nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
130+
// Limit to betweeen 1K and nMaxBlockSize-1K for sanity:
131+
nBlockMaxSize = std::max((uint64_t)1000, std::min((MaxBlockSize(chainparams.GetConsensus()) - 1000), nBlockMaxSize));
132+
uint64_t nMaxBlockSigops = MaxBlockSigops(chainparams.GetConsensus());
132133

133134
// How much of the block should be dedicated to high-priority transactions,
134135
// included regardless of the fees they pay
135-
unsigned int nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
136+
uint64_t nBlockPrioritySize = GetArg("-blockprioritysize", DEFAULT_BLOCK_PRIORITY_SIZE);
136137
nBlockPrioritySize = std::min(nBlockMaxSize, nBlockPrioritySize);
137138

138139
// Minimum block size you want to create; block will be filled with free transactions
139140
// until there are no more or the block reaches this size:
140-
unsigned int nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
141+
uint64_t nBlockMinSize = GetArg("-blockminsize", DEFAULT_BLOCK_MIN_SIZE);
141142
nBlockMinSize = std::min(nBlockMaxSize, nBlockMinSize);
142143

143144
// Collect memory pool transactions into the block
@@ -255,7 +256,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
255256

256257
// Legacy limits on sigOps:
257258
unsigned int nTxSigOps = GetLegacySigOpCount(tx);
258-
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
259+
if (nBlockSigOps + nTxSigOps >= nMaxBlockSigops)
259260
continue;
260261

261262
// Skip free transactions if we're past the minimum block size:
@@ -282,7 +283,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
282283
CAmount nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
283284

284285
nTxSigOps += GetP2SHSigOpCount(tx, view);
285-
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
286+
if (nBlockSigOps + nTxSigOps >= nMaxBlockSigops)
286287
continue;
287288

288289
// Note that flags: we don't want to set mempool/IsStandard()
@@ -339,8 +340,8 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
339340

340341
// Fill in header
341342
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
342-
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
343-
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
343+
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
344+
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
344345
pblock->nNonce = 0;
345346
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
346347

src/qt/transactiondesc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionReco
5050
QString strHTML;
5151

5252
LOCK2(cs_main, wallet->cs_wallet);
53+
const Consensus::Params& consensusParams = Params().GetConsensus();
5354
strHTML.reserve(4000);
5455
strHTML += "<html><font face='verdana, arial, helvetica, sans-serif'>";
5556

@@ -262,7 +263,7 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx, TransactionReco
262263

263264
if (wtx.IsCoinBase())
264265
{
265-
quint32 numBlocksToMaturity = COINBASE_MATURITY + 1;
266+
quint32 numBlocksToMaturity = consensusParams.nCoinbaseMaturity + 1;
266267
strHTML += "<br>" + tr("Generated coins must mature %1 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.").arg(QString::number(numBlocksToMaturity)) + "<br>";
267268
}
268269

src/rpcmining.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
382382
);
383383

384384
LOCK(cs_main);
385+
const Consensus::Params& consensusParams = Params().GetConsensus();
385386

386387
std::string strMode = "template";
387388
UniValue lpval = NullUniValue;
@@ -519,7 +520,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
519520
CBlock* pblock = &pblocktemplate->block; // pointer for convenience
520521

521522
// Update nTime
522-
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
523+
UpdateTime(pblock, consensusParams, pindexPrev);
523524
pblock->nNonce = 0;
524525

525526
UniValue aCaps(UniValue::VARR); aCaps.push_back("proposal");
@@ -580,8 +581,8 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
580581
result.push_back(Pair("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1));
581582
result.push_back(Pair("mutable", aMutable));
582583
result.push_back(Pair("noncerange", "00000000ffffffff"));
583-
result.push_back(Pair("sigoplimit", (int64_t)MAX_BLOCK_SIGOPS));
584-
result.push_back(Pair("sizelimit", (int64_t)MAX_BLOCK_SIZE));
584+
result.push_back(Pair("sigoplimit", MaxBlockSigops(consensusParams)));
585+
result.push_back(Pair("sizelimit", MaxBlockSize(consensusParams)));
585586
result.push_back(Pair("curtime", pblock->GetBlockTime()));
586587
result.push_back(Pair("bits", strprintf("%08x", pblock->nBits)));
587588
result.push_back(Pair("height", (int64_t)(pindexPrev->nHeight+1)));

src/rpcrawtransaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ UniValue gettxoutproof(const UniValue& params, bool fHelp)
283283

284284
UniValue verifytxoutproof(const UniValue& params, bool fHelp)
285285
{
286+
const Consensus::Params& consensusParams = Params().GetConsensus();
286287
if (fHelp || params.size() != 1)
287288
throw runtime_error(
288289
"verifytxoutproof \"proof\"\n"
@@ -300,7 +301,7 @@ UniValue verifytxoutproof(const UniValue& params, bool fHelp)
300301

301302
UniValue res(UniValue::VARR);
302303

303-
uint64_t nMaxTransactions = MAX_BLOCK_SIZE / 60;
304+
uint64_t nMaxTransactions = consensusParams.nMaxTxSize / consensusParams.nMinTxSize;
304305
vector<uint256> vMatch;
305306
if (merkleBlock.txn.ExtractMatches(nMaxTransactions, vMatch) != merkleBlock.header.hashMerkleRoot)
306307
return res;

src/test/checkblock_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include "chainparams.h"
56
#include "clientversion.h"
7+
#include "consensus/consensus.h"
68
#include "consensus/validation.h"
79
#include "main.h" // For CheckBlock
810
#include "primitives/block.h"

0 commit comments

Comments
 (0)