Skip to content

Commit ea72c3d

Browse files
committed
refactor: avoid duplicating BlockAssembler::Options members
Add Options as a member to BlockAssembler to avoid having to assign all the options individually. Additionally brings the struct more in line with how we typically define default and ArgManager values, as e.g. with ChainstateManager::Options and and CTxMemPool::Options
1 parent cba749a commit ea72c3d

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/node/miner.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,19 @@ 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

7774
void ApplyArgsManOptions(const ArgsManager& args, BlockAssembler::Options& options)
@@ -176,7 +173,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
176173
pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);
177174

178175
BlockValidationState state;
179-
if (test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
176+
if (m_options.test_block_validity && !TestBlockValidity(state, chainparams, m_chainstate, *pblock, pindexPrev,
180177
GetAdjustedTime, /*fCheckPOW=*/false, /*fCheckMerkleRoot=*/false)) {
181178
throw std::runtime_error(strprintf("%s: TestBlockValidity failed: %s", __func__, state.ToString()));
182179
}
@@ -205,7 +202,7 @@ void BlockAssembler::onlyUnconfirmed(CTxMemPool::setEntries& testSet)
205202
bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost) const
206203
{
207204
// TODO: switch to weight-based accounting for packages instead of vsize-based accounting.
208-
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= nBlockMaxWeight) {
205+
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= m_options.nBlockMaxWeight) {
209206
return false;
210207
}
211208
if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
@@ -377,7 +374,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
377374
packageSigOpsCost = modit->nSigOpCostWithAncestors;
378375
}
379376

380-
if (packageFees < blockMinFeeRate.GetFee(packageSize)) {
377+
if (packageFees < m_options.blockMinFeeRate.GetFee(packageSize)) {
381378
// Everything else we might consider has a lower fee rate
382379
return;
383380
}
@@ -394,7 +391,7 @@ void BlockAssembler::addPackageTxs(const CTxMemPool& mempool, int& nPackagesSele
394391
++nConsecutiveFailed;
395392

396393
if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight >
397-
nBlockMaxWeight - 4000) {
394+
m_options.nBlockMaxWeight - 4000) {
398395
// Give up if we're close to full and haven't succeeded in a while
399396
break;
400397
}

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)