Abstract out BlockAssembler options

This commit is contained in:
Pieter Wuille 2017-02-26 16:13:17 -08:00
parent f19afdbfb4
commit 48faf0bf63
2 changed files with 40 additions and 18 deletions

View File

@ -72,43 +72,56 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
return nNewTime - nOldTime; return nNewTime - nOldTime;
} }
BlockAssembler::BlockAssembler(const CChainParams& _chainparams) BlockAssembler::Options::Options() {
: chainparams(_chainparams) blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
}
BlockAssembler::BlockAssembler(const CChainParams& params, const Options& options) : chainparams(params)
{
blockMinFeeRate = options.blockMinFeeRate;
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
nBlockMaxWeight = std::max<size_t>(4000, std::min<size_t>(MAX_BLOCK_WEIGHT - 4000, options.nBlockMaxWeight));
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
nBlockMaxSize = std::max<size_t>(1000, std::min<size_t>(MAX_BLOCK_SERIALIZED_SIZE - 1000, options.nBlockMaxSize));
// Whether we need to account for byte usage (in addition to weight usage)
fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE - 1000);
}
static BlockAssembler::Options DefaultOptions(const CChainParams& params)
{ {
// Block resource limits // Block resource limits
// If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_* // If neither -blockmaxsize or -blockmaxweight is given, limit to DEFAULT_BLOCK_MAX_*
// If only one is given, only restrict the specified resource. // If only one is given, only restrict the specified resource.
// If both are given, restrict both. // If both are given, restrict both.
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT; BlockAssembler::Options options;
nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE; options.nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
options.nBlockMaxSize = DEFAULT_BLOCK_MAX_SIZE;
bool fWeightSet = false; bool fWeightSet = false;
if (IsArgSet("-blockmaxweight")) { if (IsArgSet("-blockmaxweight")) {
nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT); options.nBlockMaxWeight = GetArg("-blockmaxweight", DEFAULT_BLOCK_MAX_WEIGHT);
nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE; options.nBlockMaxSize = MAX_BLOCK_SERIALIZED_SIZE;
fWeightSet = true; fWeightSet = true;
} }
if (IsArgSet("-blockmaxsize")) { if (IsArgSet("-blockmaxsize")) {
nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE); options.nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
if (!fWeightSet) { if (!fWeightSet) {
nBlockMaxWeight = nBlockMaxSize * WITNESS_SCALE_FACTOR; options.nBlockMaxWeight = options.nBlockMaxSize * WITNESS_SCALE_FACTOR;
} }
} }
if (IsArgSet("-blockmintxfee")) { if (IsArgSet("-blockmintxfee")) {
CAmount n = 0; CAmount n = 0;
ParseMoney(GetArg("-blockmintxfee", ""), n); ParseMoney(GetArg("-blockmintxfee", ""), n);
blockMinFeeRate = CFeeRate(n); options.blockMinFeeRate = CFeeRate(n);
} else { } else {
blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE); options.blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
} }
return options;
// Limit weight to between 4K and MAX_BLOCK_WEIGHT-4K for sanity:
nBlockMaxWeight = std::max((unsigned int)4000, std::min((unsigned int)(MAX_BLOCK_WEIGHT-4000), nBlockMaxWeight));
// Limit size to between 1K and MAX_BLOCK_SERIALIZED_SIZE-1K for sanity:
nBlockMaxSize = std::max((unsigned int)1000, std::min((unsigned int)(MAX_BLOCK_SERIALIZED_SIZE-1000), nBlockMaxSize));
// Whether we need to account for byte usage (in addition to weight usage)
fNeedSizeAccounting = (nBlockMaxSize < MAX_BLOCK_SERIALIZED_SIZE-1000);
} }
BlockAssembler::BlockAssembler(const CChainParams& params) : BlockAssembler(params, DefaultOptions(params)) {}
void BlockAssembler::resetBlock() void BlockAssembler::resetBlock()
{ {
inBlock.clear(); inBlock.clear();

View File

@ -163,7 +163,16 @@ private:
bool blockFinished; bool blockFinished;
public: public:
BlockAssembler(const CChainParams& chainparams); struct Options {
Options();
size_t nBlockMaxWeight;
size_t nBlockMaxSize;
CFeeRate blockMinFeeRate;
};
BlockAssembler(const CChainParams& params);
BlockAssembler(const CChainParams& params, const Options& options);
/** Construct a new block template with coinbase to scriptPubKeyIn */ /** Construct a new block template with coinbase to scriptPubKeyIn */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn); std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);