Bitcoin ABC 0.32.4
P2P Digital Currency
blockfitter.cpp
Go to the documentation of this file.
1// Copyright (c) 2025 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <node/blockfitter.h>
6
7#include <common/args.h>
8#include <config.h>
10#include <policy/policy.h>
11#include <util/moneystr.h>
12
13#include <algorithm>
14
15namespace node {
16
18 : nExcessiveBlockSize(DEFAULT_MAX_BLOCK_SIZE),
19 nMaxGeneratedBlockSize(DEFAULT_MAX_GENERATED_BLOCK_SIZE),
20 blockMinFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB) {}
21
24 // Limit size to between COINBASE_RESERVED_SIZE and
25 // options.nExcessiveBlockSize - COINBASE_RESERVED_SIZE for sanity:
29 // Calculate the max consensus sigchecks for this block.
30 // Allow the full amount of signature check operations in lieu of a separate
31 // config option. (We are mining relayed transactions with validity cached
32 // by everyone else, and so the block will propagate quickly, regardless of
33 // how many sigchecks it contains.)
36
37 resetBlock();
38}
39
40void ApplyArgsManOptions(const ArgsManager &args, const Config &config,
41 BlockFitter::Options &options) {
42 // Block resource limits
43 // If -blockmaxsize is not given, limit to DEFAULT_MAX_GENERATED_BLOCK_SIZE
44 // If only one is given, only restrict the specified resource.
45 // If both are given, restrict both.
46 options.nExcessiveBlockSize = config.GetMaxBlockSize();
47
48 if (gArgs.IsArgSet("-blockmaxsize")) {
51 }
52
53 Amount n = Amount::zero();
54 if (gArgs.IsArgSet("-blockmintxfee") &&
55 ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
56 options.blockMinFeeRate = CFeeRate(n);
57 }
58}
59
62 ApplyArgsManOptions(gArgs, config, options);
63 return options;
64}
65
67 : BlockFitter(ConfiguredOptions(config)) {}
68
70 // Reserve space for coinbase tx.
73
74 // These counters do not include coinbase tx.
75 nBlockTx = 0;
77}
78
79void BlockFitter::addTx(size_t txSize, int64_t txSigChecks, Amount txFee) {
80 nBlockSize += txSize;
81 ++nBlockTx;
82 nBlockSigChecks += txSigChecks;
83 nFees += txFee;
84}
85
86void BlockFitter::removeTxUnchecked(size_t txSize, int64_t txSigChecks,
87 Amount txFee) {
88 nBlockSize -= txSize;
89 nBlockSigChecks -= txSigChecks;
90 nFees -= txFee;
91 --nBlockTx;
92}
93
94bool BlockFitter::testTxFits(uint64_t txSize, int64_t txSigChecks) const {
95 if (nBlockSize + txSize >= nMaxGeneratedBlockSize) {
96 return false;
97 }
98
99 if (nBlockSigChecks + txSigChecks >= nMaxGeneratedBlockSigChecks) {
100 return false;
101 }
102
103 return true;
104}
105
106bool BlockFitter::isBelowBlockMinFeeRate(const CFeeRate &txFeeRate) const {
107 return txFeeRate < blockMinFeeRate;
108}
109} // namespace node
ArgsManager gArgs
Definition: args.cpp:40
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:372
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:495
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:463
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Definition: config.h:19
virtual uint64_t GetMaxBlockSize() const =0
Check for block limits when adding transactions.
Definition: blockfitter.h:18
bool isBelowBlockMinFeeRate(const CFeeRate &txFeeRate) const
static constexpr uint64_t COINBASE_RESERVED_SIGCHECKS
Definition: blockfitter.h:26
uint64_t nMaxGeneratedBlockSigChecks
Definition: blockfitter.h:21
bool testTxFits(uint64_t txSize, int64_t txSigChecks) const
Test if a new Tx would "fit" in the block.
Definition: blockfitter.cpp:94
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: blockfitter.cpp:69
uint64_t nBlockSize
Definition: blockfitter.h:29
BlockFitter(const Options &options)
Definition: blockfitter.cpp:22
uint64_t nMaxGeneratedBlockSize
Definition: blockfitter.h:20
uint64_t nBlockTx
Definition: blockfitter.h:30
uint64_t nBlockSigChecks
Definition: blockfitter.h:31
void removeTxUnchecked(size_t txSize, int64_t txSigChecks, Amount txFee)
Remove accounting for this tx.
Definition: blockfitter.cpp:86
void addTx(size_t txSize, int64_t txSigChecks, Amount txFee)
Account for this tx.
Definition: blockfitter.cpp:79
CFeeRate blockMinFeeRate
Definition: blockfitter.h:22
static constexpr uint64_t COINBASE_RESERVED_SIZE
Definition: blockfitter.h:25
static const uint64_t DEFAULT_MAX_BLOCK_SIZE
Default setting for maximum allowed size for a block, in bytes.
Definition: consensus.h:20
uint64_t GetMaxBlockSigChecksCount(uint64_t maxBlockSize)
Compute the maximum number of sigchecks that can be contained in a block given the MAXIMUM block size...
Definition: consensus.h:47
bool ParseMoney(const std::string &money_string, Amount &nRet)
Parse an amount denoted in full coins.
Definition: moneystr.cpp:37
Definition: init.h:31
static BlockFitter::Options ConfiguredOptions(const Config &config)
Definition: blockfitter.cpp:60
void ApplyArgsManOptions(const ArgsManager &args, const Config &config, BlockFitter::Options &options)
Apply options from ArgsManager to BlockFitter options.
Definition: blockfitter.cpp:40
static constexpr uint64_t DEFAULT_MAX_GENERATED_BLOCK_SIZE
Default for -blockmaxsize, which controls the maximum size of block the mining code will create.
Definition: policy.h:25
static constexpr Amount DEFAULT_BLOCK_MIN_TX_FEE_PER_KB(1000 *SATOSHI)
Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by min...
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32