Skip to content

Commit 08b65df

Browse files
committed
Merge bitcoin/bitcoin#26883: src/node/miner cleanups, follow-ups for #26695
6a5e88e miner: don't re-apply default Options value if argument is unset (stickies-v) ea72c3d refactor: avoid duplicating BlockAssembler::Options members (stickies-v) cba749a refactor: rename local gArgs to args (stickies-v) Pull request description: Two follow-ups for #26695, both refactoring and no observed (*) behaviour change: - Rename `gArgs` to `args` because it's not actually a global - Add `BlockAssembler::Options` as a (private) member to `BlockAssembler` to avoid having to assign all the options individually, essentially duplicating them Reduces LoC and makes the code more readable, in my opinion. --- (*) as [pointed out by ajtowns](bitcoin/bitcoin#26883 (comment)), this PR changes the interface of `ApplyArgsManOptions()`, making this not a pure refactoring PR. In practice, `ApplyArgsManOptions()` is never called in such a way that this leads to observed behaviour change. Regardless, I've carved out the potential behaviour change into a separate commit and would be okay with dropping it, should it turn out to be controversial. ACKs for top commit: glozow: ACK 6a5e88e TheCharlatan: Light code review ACK 6a5e88e Tree-SHA512: 15c30442ff0e070b1a58dc4c9615550d619ce35b4a2596b2c0a9d790259bbf987cab708f7cbb1057a8cf8b4c3226f3ad981282d3499ac442094806492a5f68ce
2 parents 4395b7f + 6a5e88e commit 08b65df

File tree

2 files changed

+24
-34
lines changed

2 files changed

+24
-34
lines changed

src/node/miner.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,27 @@ void RegenerateCommitments(CBlock& block, ChainstateManager& chainman)
5656
block.hashMerkleRoot = BlockMerkleRoot(block);
5757
}
5858

59-
BlockAssembler::Options::Options()
59+
static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
6060
{
61-
blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
62-
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
63-
test_block_validity = true;
61+
// Limit weight to between 4K and DEFAULT_BLOCK_MAX_WEIGHT for sanity:
62+
options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, 4000, DEFAULT_BLOCK_MAX_WEIGHT);
63+
return options;
6464
}
6565

6666
BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
67-
: test_block_validity{options.test_block_validity},
68-
chainparams{chainstate.m_chainman.GetParams()},
69-
m_mempool(mempool),
70-
m_chainstate(chainstate)
67+
: chainparams{chainstate.m_chainman.GetParams()},
68+
m_mempool{mempool},
69+
m_chainstate{chainstate},
70+
m_options{ClampOptions(options)}
7171
{
72-
blockMinFeeRate = options.blockMinFeeRate;
73-
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
74-
nBlockMaxWeight = std::max<size_t>(4000, std::min<size_t>(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight));
7572
}
7673

77-
void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options)
74+
void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
7875
{
7976
// Block resource limits
80-
// If -blockmaxweight is not given, limit to DEFAULT_BLOCK_MAX_WEIGHT
81-
options.nBlockMaxWeight = gArgs.GetIntArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
82-
if (gArgs.IsArgSet("-blockmintxfee")) {
83-
std::optional<CAmount> parsed = ParseMoney(gArgs.GetArg("-blockmintxfee", ""));
84-
options.blockMinFeeRate = CFeeRate{parsed.value_or(DEFAULT_BLOCK_MIN_TX_FEE)};
85-
} else {
86-
options.blockMinFeeRate = CFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
77+
options.nBlockMaxWeight = args.GetIntArg("-blockmaxweight", options.nBlockMaxWeight);
78+
if (const auto blockmintxfee{args.GetArg("-blockmintxfee")}) {
79+
if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
8780
}
8881
}
8982
static BlockAssembler::Options ConfiguredOptions()
@@ -176,7 +169,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
176169
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
177170

178171
BlockValidationState state;
179-
if (test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
172+
if (m_options.test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
180173
GetAdjustedTime, /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
181174
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
182175
}
@@ -205,7 +198,7 @@ void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
205198
bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
206199
{
207200
// TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
208-
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight) {
201+
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= m_options.nBlockMaxWeight) {
209202
return false;
210203
}
211204
if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
@@ -377,7 +370,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
377370
packageSigOpsCost = modit->nSigOpCostWithAncestors;
378371
}
379372

380-
if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
373+
if (packageFees < m_options.blockMinFeeRate.GetFee(packageSize)) {
381374
// Everything else we might consider has a lower fee rate
382375
return;
383376
}
@@ -394,7 +387,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
394387
++nConsecutiveFailed;
395388

396389
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
397-
nBlockMaxWeight - 4000) {
390+
m_options.nBlockMaxWeight - 4000) {
398391
// Give up if we're close to full and haven't succeeded in a while
399392
break;
400393
}

src/node/miner.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_NODE_MINER_H
77
#define BITCOIN_NODE_MINER_H
88

9+
#include <policy/policy.h>
910
#include <primitives/block.h>
1011
#include <txmempool.h>
1112

@@ -132,13 +133,6 @@ class BlockAssembler
132133
// The constructed block template
133134
std::unique_ptr<CBlockTemplate> pblocktemplate;
134135

135-
// Configuration parameters for the block size
136-
unsigned int nBlockMaxWeight;
137-
CFeeRate blockMinFeeRate;
138-
139-
// Whether to call TestBlockValidity() at the end of CreateNewBlock().
140-
const bool test_block_validity;
141-
142136
// Information on the current status of the block
143137
uint64_t nBlockWeight;
144138
uint64_t nBlockTx;
@@ -156,10 +150,11 @@ class BlockAssembler
156150

157151
public:
158152
struct Options {
159-
Options();
160-
size_t nBlockMaxWeight;
161-
CFeeRate blockMinFeeRate;
162-
bool test_block_validity;
153+
// Configuration parameters for the block size
154+
size_t nBlockMaxWeight{DEFAULT_BLOCK_MAX_WEIGHT};
155+
CFeeRate blockMinFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
156+
// Whether to call TestBlockValidity() at the end of CreateNewBlock().
157+
bool test_block_validity{true};
163158
};
164159

165160
explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool);
@@ -172,6 +167,8 @@ class BlockAssembler
172167
inline static std::optional<int64_t> m_last_block_weight{};
173168

174169
private:
170+
const Options m_options;
171+
175172
// utility functions
176173
/** Clear the block's state and prepare for assembling a new block */
177174
void resetBlock();

0 commit comments

Comments
 (0)