Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #ifndef BITCOIN_NODE_MINER_H
7 : : #define BITCOIN_NODE_MINER_H
8 : :
9 : : #include <interfaces/types.h>
10 : : #include <node/types.h>
11 : : #include <policy/policy.h>
12 : : #include <primitives/block.h>
13 : : #include <txmempool.h>
14 : : #include <util/feefrac.h>
15 : :
16 : : #include <cstdint>
17 : : #include <memory>
18 : : #include <optional>
19 : :
20 : : #include <boost/multi_index/identity.hpp>
21 : : #include <boost/multi_index/indexed_by.hpp>
22 : : #include <boost/multi_index/ordered_index.hpp>
23 : : #include <boost/multi_index/tag.hpp>
24 : : #include <boost/multi_index_container.hpp>
25 : :
26 : : class ArgsManager;
27 : : class CBlockIndex;
28 : : class CChainParams;
29 : : class CScript;
30 : : class Chainstate;
31 : : class ChainstateManager;
32 : :
33 : : namespace Consensus { struct Params; };
34 : :
35 : : using interfaces::BlockRef;
36 : :
37 : : namespace node {
38 : : class KernelNotifications;
39 : :
40 : : static const bool DEFAULT_PRINT_MODIFIED_FEE = false;
41 : :
42 : : struct CBlockTemplate
43 : : {
44 : : CBlock block;
45 : : // Fees per transaction, not including coinbase transaction (unlike CBlock::vtx).
46 : : std::vector<CAmount> vTxFees;
47 : : // Sigops per transaction, not including coinbase transaction (unlike CBlock::vtx).
48 : : std::vector<int64_t> vTxSigOpsCost;
49 : : std::vector<unsigned char> vchCoinbaseCommitment;
50 : : /* A vector of package fee rates, ordered by the sequence in which
51 : : * packages are selected for inclusion in the block template.*/
52 : : std::vector<FeePerVSize> m_package_feerates;
53 : : };
54 : :
55 : : /** Generate a new block, without valid proof-of-work */
56 [ + + ]: 46927 : class BlockAssembler
[ + - + - ]
57 : : {
58 : : private:
59 : : // The constructed block template
60 : : std::unique_ptr<CBlockTemplate> pblocktemplate;
61 : :
62 : : // Information on the current status of the block
63 : : uint64_t nBlockWeight;
64 : : uint64_t nBlockTx;
65 : : uint64_t nBlockSigOpsCost;
66 : : CAmount nFees;
67 : :
68 : : // Chain context for the block
69 : : int nHeight;
70 : : int64_t m_lock_time_cutoff;
71 : :
72 : : const CChainParams& chainparams;
73 : : const CTxMemPool* const m_mempool;
74 : : Chainstate& m_chainstate;
75 : :
76 : : public:
77 [ + - + - : 148019 : struct Options : BlockCreateOptions {
+ - ]
[ + - + - ]
[ + - + -
+ - + - ]
78 : : // Configuration parameters for the block size
79 : : size_t nBlockMaxWeight{DEFAULT_BLOCK_MAX_WEIGHT};
80 : : CFeeRate blockMinFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
81 : : // Whether to call TestBlockValidity() at the end of CreateNewBlock().
82 : : bool test_block_validity{true};
83 : : bool print_modified_fee{DEFAULT_PRINT_MODIFIED_FEE};
84 : : };
85 : :
86 : : explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
87 : :
88 : : /** Construct a new block template */
89 : : std::unique_ptr<CBlockTemplate> CreateNewBlock();
90 : :
91 : : /** The number of transactions in the last assembled block (excluding coinbase transaction) */
92 : : inline static std::optional<int64_t> m_last_block_num_txs{};
93 : : /** The weight of the last assembled block (including reserved weight for block header, txs count and coinbase tx) */
94 : : inline static std::optional<int64_t> m_last_block_weight{};
95 : :
96 : : private:
97 : : const Options m_options;
98 : :
99 : : // utility functions
100 : : /** Clear the block's state and prepare for assembling a new block */
101 : : void resetBlock();
102 : : /** Add a tx to the block */
103 : : void AddToBlock(const CTxMemPoolEntry& entry);
104 : :
105 : : // Methods for how to add transactions to a block.
106 : : /** Add transactions based on chunk feerate
107 : : *
108 : : * @pre BlockAssembler::m_mempool must not be nullptr
109 : : */
110 : : void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs);
111 : :
112 : : // helper functions for addChunks()
113 : : /** Test if a new package would "fit" in the block */
114 : : bool TestPackage(FeePerWeight package_feerate, int64_t packageSigOpsCost) const;
115 : : /** Perform checks on each transaction in a package:
116 : : * locktime, premature-witness, serialized size (if necessary)
117 : : * These checks should always succeed, and they're here
118 : : * only as an extra check in case of suboptimal node configuration */
119 : : bool TestPackageTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
120 : : };
121 : :
122 : : /**
123 : : * Get the minimum time a miner should use in the next block. This always
124 : : * accounts for the BIP94 timewarp rule, so does not necessarily reflect the
125 : : * consensus limit.
126 : : */
127 : : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval);
128 : :
129 : : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
130 : :
131 : : /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
132 : : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
133 : :
134 : : /** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */
135 : : void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options);
136 : :
137 : : /* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
138 : : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
139 : :
140 : :
141 : : /* Interrupt the current wait for the next block template. */
142 : : void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait);
143 : : /**
144 : : * Return a new block template when fees rise to a certain threshold or after a
145 : : * new tip; return nullopt if timeout is reached.
146 : : */
147 : : std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
148 : : KernelNotifications& kernel_notifications,
149 : : CTxMemPool* mempool,
150 : : const std::unique_ptr<CBlockTemplate>& block_template,
151 : : const BlockWaitOptions& options,
152 : : const BlockAssembler::Options& assemble_options,
153 : : bool& interrupt_wait);
154 : :
155 : : /* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/
156 : : std::optional<BlockRef> GetTip(ChainstateManager& chainman);
157 : :
158 : : /* Waits for the connected tip to change until timeout has elapsed. During node initialization, this will wait until the tip is connected (regardless of `timeout`).
159 : : * Returns the current tip, or nullopt if the node is shutting down. */
160 : : std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout);
161 : : } // namespace node
162 : :
163 : : #endif // BITCOIN_NODE_MINER_H
|