Bitcoin ABC 0.31.1
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
41 // Block resource limits
42 // If -blockmaxsize is not given, limit to DEFAULT_MAX_GENERATED_BLOCK_SIZE
43 // If only one is given, only restrict the specified resource.
44 // If both are given, restrict both.
46
47 options.nExcessiveBlockSize = config.GetMaxBlockSize();
48
49 if (gArgs.IsArgSet("-blockmaxsize")) {
52 }
53
54 Amount n = Amount::zero();
55 if (gArgs.IsArgSet("-blockmintxfee") &&
56 ParseMoney(gArgs.GetArg("-blockmintxfee", ""), n)) {
57 options.blockMinFeeRate = CFeeRate(n);
58 }
59
60 return options;
61}
62
64 : BlockFitter(DefaultOptions(config)) {}
65
67 // Reserve space for coinbase tx.
70
71 // These counters do not include coinbase tx.
72 nBlockTx = 0;
74}
75
76void BlockFitter::addTx(size_t txSize, int64_t txSigChecks, Amount txFee) {
77 nBlockSize += txSize;
78 ++nBlockTx;
79 nBlockSigChecks += txSigChecks;
80 nFees += txFee;
81}
82
83void BlockFitter::removeTxUnchecked(size_t txSize, int64_t txSigChecks,
84 Amount txFee) {
85 nBlockSize -= txSize;
86 nBlockSigChecks -= txSigChecks;
87 nFees -= txFee;
88 --nBlockTx;
89}
90
91bool BlockFitter::testTxFits(uint64_t txSize, int64_t txSigChecks) const {
92 if (nBlockSize + txSize >= nMaxGeneratedBlockSize) {
93 return false;
94 }
95
96 if (nBlockSigChecks + txSigChecks >= nMaxGeneratedBlockSigChecks) {
97 return false;
98 }
99
100 return true;
101}
102
103bool BlockFitter::isBelowBlockMinFeeRate(const CFeeRate &txFeeRate) const {
104 return txFeeRate < blockMinFeeRate;
105}
106} // namespace node
ArgsManager gArgs
Definition: args.cpp:38
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:381
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:526
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:494
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:17
bool isBelowBlockMinFeeRate(const CFeeRate &txFeeRate) const
static constexpr uint64_t COINBASE_RESERVED_SIGCHECKS
Definition: blockfitter.h:25
uint64_t nMaxGeneratedBlockSigChecks
Definition: blockfitter.h:20
bool testTxFits(uint64_t txSize, int64_t txSigChecks) const
Test if a new Tx would "fit" in the block.
Definition: blockfitter.cpp:91
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: blockfitter.cpp:66
uint64_t nBlockSize
Definition: blockfitter.h:28
BlockFitter(const Options &options)
Definition: blockfitter.cpp:22
uint64_t nMaxGeneratedBlockSize
Definition: blockfitter.h:19
uint64_t nBlockTx
Definition: blockfitter.h:29
uint64_t nBlockSigChecks
Definition: blockfitter.h:30
void removeTxUnchecked(size_t txSize, int64_t txSigChecks, Amount txFee)
Remove accounting for this tx.
Definition: blockfitter.cpp:83
void addTx(size_t txSize, int64_t txSigChecks, Amount txFee)
Account for this tx.
Definition: blockfitter.cpp:76
CFeeRate blockMinFeeRate
Definition: blockfitter.h:21
static constexpr uint64_t COINBASE_RESERVED_SIZE
Definition: blockfitter.h:24
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 DefaultOptions(const Config &config)
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