Skip to content

Commit 48faf0b

Browse files
committed
Abstract out BlockAssembler options
1 parent f19afdb commit 48faf0b

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/miner.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,56 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
7272
return nNewTime - nOldTime;
7373
}
7474

75-
BlockAssembler::BlockAssembler(const CChainParams& _chainparams)
76-
: chainparams(_chainparams)
75+
BlockAssembler::Options::Options() {
76+
blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
77+
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
78+
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
79+
}
80+
81+
BlockAssembler::BlockAssembler(const CChainParams& params, const Options& options) : chainparams(params)
82+
{
83+
blockMinFeeRate = options.blockMinFeeRate;
84+
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
85+
nBlockMaxWeight = std::max<size_t>(4000, std::min<size_t>(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight));
86+
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
87+
nBlockMaxSize = std::max<size_t>(1000, std::min<size_t>(MAX_BLOCK_SERIALIZED_SIZE - 1000, options.nBlockMaxSize));
88+
// Whether we need to account for byte usage (in addition to weight usage)
89+
fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE - 1000);
90+
}
91+
92+
static BlockAssembler::Options DefaultOptions(const CChainParams& params)
7793
{
7894
// Block resource limits
7995
// If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_*
8096
// If only one is given, only restrict the specified resource.
8197
// If both are given, restrict both.
82-
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
83-
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
98+
BlockAssembler::Options options;
99+
options.nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
100+
options.nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
84101
bool fWeightSet = false;
85102
if (IsArgSet("-blockmaxweight")) {
86-
nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
87-
nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
103+
options.nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
104+
options.nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
88105
fWeightSet = true;
89106
}
90107
if (IsArgSet("-blockmaxsize")) {
91-
nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
108+
options.nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
92109
if (!fWeightSet) {
93-
nBlockMaxWeight = nBlockMaxSize * WITNESS_SCALE_FACTOR;
110+
options.nBlockMaxWeight = options.nBlockMaxSize * WITNESS_SCALE_FACTOR;
94111
}
95112
}
96113
if (IsArgSet("-blockmintxfee")) {
97114
CAmount n = 0;
98115
ParseMoney(GetArg("-blockmintxfee", ""), n);
99-
blockMinFeeRate = CFeeRate(n);
116+
options.blockMinFeeRate = CFeeRate(n);
100117
} else {
101-
blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
118+
options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
102119
}
103-
104-
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
105-
nBlockMaxWeight = std::max((unsigned int)4000, std::min((unsigned int)(MAX_BLOCK_WEIGHT-4000), nBlockMaxWeight));
106-
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
107-
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SERIALIZED_SIZE-1000), nBlockMaxSize));
108-
// Whether we need to account for byte usage (in addition to weight usage)
109-
fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000);
120+
return options;
110121
}
111122

123+
BlockAssembler::BlockAssembler(const CChainParams& params) : BlockAssembler(params, DefaultOptions(params)) {}
124+
112125
void BlockAssembler::resetBlock()
113126
{
114127
inBlock.clear();

src/miner.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,16 @@ class BlockAssembler
163163
bool blockFinished;
164164

165165
public:
166-
BlockAssembler(const CChainParams& chainparams);
166+
struct Options {
167+
Options();
168+
size_t nBlockMaxWeight;
169+
size_t nBlockMaxSize;
170+
CFeeRate blockMinFeeRate;
171+
};
172+
173+
BlockAssembler(const CChainParams& params);
174+
BlockAssembler(const CChainParams& params, const Options& options);
175+
167176
/** Construct a new block template with coinbase to scriptPubKeyIn */
168177
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
169178

0 commit comments

Comments
 (0)