diff --git a/src/bench/block_assemble.cpp b/src/bench/block_assemble.cpp index 63acb6c24d3..8dd4117a3ec 100644 --- a/src/bench/block_assemble.cpp +++ b/src/bench/block_assemble.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -45,5 +46,18 @@ static void AssembleBlock(benchmark::Bench& bench) PrepareBlock(test_setup->m_node, P2WSH_OP_TRUE); }); } +static void BlockAssemblerAddPackageTxns(benchmark::Bench& bench) +{ + FastRandomContext det_rand{true}; + auto testing_setup{MakeNoLogFileContext()}; + testing_setup->PopulateMempool(det_rand, /*num_transactions=*/1000, /*submit=*/true); + node::BlockAssembler::Options assembler_options; + assembler_options.test_block_validity = false; + + bench.run([&] { + PrepareBlock(testing_setup->m_node, P2WSH_OP_TRUE, assembler_options); + }); +} BENCHMARK(AssembleBlock, benchmark::PriorityLevel::HIGH); +BENCHMARK(BlockAssemblerAddPackageTxns, benchmark::PriorityLevel::LOW); diff --git a/src/node/miner.cpp b/src/node/miner.cpp index dc6849e0d2f..c2b6fd1dc32 100644 --- a/src/node/miner.cpp +++ b/src/node/miner.cpp @@ -60,10 +60,12 @@ BlockAssembler::Options::Options() { blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE); nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT; + test_block_validity = true; } BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options) - : chainparams{chainstate.m_chainman.GetParams()}, + : test_block_validity{options.test_block_validity}, + chainparams{chainstate.m_chainman.GetParams()}, m_mempool(mempool), m_chainstate(chainstate) { @@ -72,11 +74,10 @@ BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool nBlockMaxWeight = std::max(4000, std::min(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight)); } -static BlockAssembler::Options DefaultOptions() +void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options) { // Block resource limits // If -blockmaxweight is not given, limit to DEFAULT_BLOCK_MAX_WEIGHT - BlockAssembler::Options options; options.nBlockMaxWeight = gArgs.GetIntArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT); if (gArgs.IsArgSet("-blockmintxfee")) { std::optional parsed = ParseMoney(gArgs.GetArg("-blockmintxfee", "")); @@ -84,11 +85,16 @@ static BlockAssembler::Options DefaultOptions() } else { options.blockMinFeeRate = CFeeRate{DEFAULT_BLOCK_MIN_TX_FEE}; } +} +static BlockAssembler::Options ConfiguredOptions() +{ + BlockAssembler::Options options; + ApplyArgsManOptions(gArgs, options); return options; } BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool) - : BlockAssembler(chainstate, mempool, DefaultOptions()) {} + : BlockAssembler(chainstate, mempool, ConfiguredOptions()) {} void BlockAssembler::resetBlock() { @@ -170,7 +176,8 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]); BlockValidationState state; - if (!TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, GetAdjustedTime, false, false)) { + if (test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev, + GetAdjustedTime, /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) { throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString())); } const auto time_2{SteadyClock::now()}; diff --git a/src/node/miner.h b/src/node/miner.h index 8cc5e07237d..ea9e470a640 100644 --- a/src/node/miner.h +++ b/src/node/miner.h @@ -16,6 +16,7 @@ #include #include +class ArgsManager; class ChainstateManager; class CBlockIndex; class CChainParams; @@ -135,6 +136,9 @@ private: unsigned int nBlockMaxWeight; CFeeRate blockMinFeeRate; + // Whether to call TestBlockValidity() at the end of CreateNewBlock(). + const bool test_block_validity; + // Information on the current status of the block uint64_t nBlockWeight; uint64_t nBlockTx; @@ -155,6 +159,7 @@ public: Options(); size_t nBlockMaxWeight; CFeeRate blockMinFeeRate; + bool test_block_validity; }; explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool); @@ -197,6 +202,9 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman); + +/** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */ +void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options); } // namespace node #endif // BITCOIN_NODE_MINER_H diff --git a/src/test/util/mining.cpp b/src/test/util/mining.cpp index 55e9d4d63bc..0df1db84c42 100644 --- a/src/test/util/mining.cpp +++ b/src/test/util/mining.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include