diff --git a/src/config.h b/src/config.h --- a/src/config.h +++ b/src/config.h @@ -51,6 +51,7 @@ CFeeRate GetMinFeePerKB() const override; private: + uint64_t maxBlockSize; bool useCashAddr; Amount excessUTXOCharge; CFeeRate feePerKB; diff --git a/src/config.cpp b/src/config.cpp --- a/src/config.cpp +++ b/src/config.cpp @@ -7,21 +7,25 @@ #include "consensus/consensus.h" #include "globals.h" -GlobalConfig::GlobalConfig() : useCashAddr(false) {} +GlobalConfig::GlobalConfig() : useCashAddr(false) { + if (!SetMaxBlockSize(DEFAULT_MAX_BLOCK_SIZE)) { + throw "Default max block size is smaller than expected!"; + } +} -bool GlobalConfig::SetMaxBlockSize(uint64_t maxBlockSize) { +bool GlobalConfig::SetMaxBlockSize(uint64_t maxBlockSizeIn) { // Do not allow maxBlockSize to be set below historic 1MB limit // It cannot be equal either because of the "must be big" UAHF rule. - if (maxBlockSize <= LEGACY_MAX_BLOCK_SIZE) { + if (maxBlockSizeIn <= LEGACY_MAX_BLOCK_SIZE) { return false; } - nMaxBlockSize = maxBlockSize; + maxBlockSize = maxBlockSizeIn; return true; } uint64_t GlobalConfig::GetMaxBlockSize() const { - return nMaxBlockSize; + return maxBlockSize; } bool GlobalConfig::SetBlockPriorityPercentage(int64_t blockPriorityPercentage) { diff --git a/src/globals.h b/src/globals.h --- a/src/globals.h +++ b/src/globals.h @@ -7,8 +7,6 @@ #include -/** The largest block size this node will accept. */ -extern uint64_t nMaxBlockSize; extern uint64_t nBlockPriorityPercentage; #endif // BITCOIN_GLOBALS_H diff --git a/src/globals.cpp b/src/globals.cpp --- a/src/globals.cpp +++ b/src/globals.cpp @@ -4,8 +4,6 @@ #include "globals.h" -#include "consensus/consensus.h" #include "policy/policy.h" -uint64_t nMaxBlockSize = DEFAULT_MAX_BLOCK_SIZE; uint64_t nBlockPriorityPercentage = DEFAULT_BLOCK_PRIORITY_PERCENTAGE; diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -706,10 +706,7 @@ fCheckpointsEnabled = true; } -void CheckBlockMaxSize(const CChainParams &chainparams, uint64_t size, - uint64_t expected) { - GlobalConfig config; - +void CheckBlockMaxSize(const Config &config, uint64_t size, uint64_t expected) { gArgs.ForceSetArg("-blockmaxsize", std::to_string(size)); BlockAssembler ba(config); @@ -718,34 +715,33 @@ BOOST_AUTO_TEST_CASE(BlockAssembler_construction) { GlobalConfig config; - const CChainParams &chainparams = Params(); // We are working on a fake chain and need to protect ourselves. LOCK(cs_main); // Test around historical 1MB (plus one byte because that's mandatory) config.SetMaxBlockSize(ONE_MEGABYTE + 1); - CheckBlockMaxSize(chainparams, 0, 1000); - CheckBlockMaxSize(chainparams, 1000, 1000); - CheckBlockMaxSize(chainparams, 1001, 1001); - CheckBlockMaxSize(chainparams, 12345, 12345); + CheckBlockMaxSize(config, 0, 1000); + CheckBlockMaxSize(config, 1000, 1000); + CheckBlockMaxSize(config, 1001, 1001); + CheckBlockMaxSize(config, 12345, 12345); - CheckBlockMaxSize(chainparams, ONE_MEGABYTE - 1001, ONE_MEGABYTE - 1001); - CheckBlockMaxSize(chainparams, ONE_MEGABYTE - 1000, ONE_MEGABYTE - 1000); - CheckBlockMaxSize(chainparams, ONE_MEGABYTE - 999, ONE_MEGABYTE - 999); - CheckBlockMaxSize(chainparams, ONE_MEGABYTE, ONE_MEGABYTE - 999); + CheckBlockMaxSize(config, ONE_MEGABYTE - 1001, ONE_MEGABYTE - 1001); + CheckBlockMaxSize(config, ONE_MEGABYTE - 1000, ONE_MEGABYTE - 1000); + CheckBlockMaxSize(config, ONE_MEGABYTE - 999, ONE_MEGABYTE - 999); + CheckBlockMaxSize(config, ONE_MEGABYTE, ONE_MEGABYTE - 999); // Test around default cap config.SetMaxBlockSize(DEFAULT_MAX_BLOCK_SIZE); // Now we can use the default max block size. - CheckBlockMaxSize(chainparams, DEFAULT_MAX_BLOCK_SIZE - 1001, + CheckBlockMaxSize(config, DEFAULT_MAX_BLOCK_SIZE - 1001, DEFAULT_MAX_BLOCK_SIZE - 1001); - CheckBlockMaxSize(chainparams, DEFAULT_MAX_BLOCK_SIZE - 1000, + CheckBlockMaxSize(config, DEFAULT_MAX_BLOCK_SIZE - 1000, DEFAULT_MAX_BLOCK_SIZE - 1000); - CheckBlockMaxSize(chainparams, DEFAULT_MAX_BLOCK_SIZE - 999, + CheckBlockMaxSize(config, DEFAULT_MAX_BLOCK_SIZE - 999, DEFAULT_MAX_BLOCK_SIZE - 1000); - CheckBlockMaxSize(chainparams, DEFAULT_MAX_BLOCK_SIZE, + CheckBlockMaxSize(config, DEFAULT_MAX_BLOCK_SIZE, DEFAULT_MAX_BLOCK_SIZE - 1000); // If the parameter is not specified, we use diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3534,18 +3534,15 @@ "first tx is not coinbase"); } - // Size limits. - auto nMaxBlockSize = config.GetMaxBlockSize(); - // Bail early if there is no way this block is of reasonable size. - if ((block.vtx.size() * MIN_TRANSACTION_SIZE) > nMaxBlockSize) { + if ((block.vtx.size() * MIN_TRANSACTION_SIZE) > config.GetMaxBlockSize()) { return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed"); } auto currentBlockSize = ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION); - if (currentBlockSize > nMaxBlockSize) { + if (currentBlockSize > config.GetMaxBlockSize()) { return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed"); }