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 : : * Template containing all coinbase transaction fields that are set by our
55 : : * miner code.
56 : : */
57 : : CoinbaseTx m_coinbase_tx;
58 : : };
59 : :
60 : : /** Generate a new block, without valid proof-of-work */
61 [ + - ]: 161550 : class BlockAssembler
62 : : {
63 : : private:
64 : : // The constructed block template
65 : : std::unique_ptr<CBlockTemplate> pblocktemplate;
66 : :
67 : : // Information on the current status of the block
68 : : uint64_t nBlockWeight;
69 : : uint64_t nBlockTx;
70 : : uint64_t nBlockSigOpsCost;
71 : : CAmount nFees;
72 : :
73 : : // Chain context for the block
74 : : int nHeight;
75 : : int64_t m_lock_time_cutoff;
76 : :
77 : : const CChainParams& chainparams;
78 : : const CTxMemPool* const m_mempool;
79 : : Chainstate& m_chainstate;
80 : :
81 : : public:
82 [ + - + - : 362532 : struct Options : BlockCreateOptions {
+ - ]
[ + - + - ]
83 : : // Configuration parameters for the block size
84 : : size_t nBlockMaxWeight{DEFAULT_BLOCK_MAX_WEIGHT};
85 : : CFeeRate blockMinFeeRate{DEFAULT_BLOCK_MIN_TX_FEE};
86 : : // Whether to call TestBlockValidity() at the end of CreateNewBlock().
87 : : bool test_block_validity{true};
88 : : bool print_modified_fee{DEFAULT_PRINT_MODIFIED_FEE};
89 : : };
90 : :
91 : : explicit BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options);
92 : :
93 : : /** Construct a new block template */
94 : : std::unique_ptr<CBlockTemplate> CreateNewBlock();
95 : :
96 : : /** The number of transactions in the last assembled block (excluding coinbase transaction) */
97 : : inline static std::optional<int64_t> m_last_block_num_txs{};
98 : : /** The weight of the last assembled block (including reserved weight for block header, txs count and coinbase tx) */
99 : : inline static std::optional<int64_t> m_last_block_weight{};
100 : :
101 : : private:
102 : : const Options m_options;
103 : :
104 : : // utility functions
105 : : /** Clear the block's state and prepare for assembling a new block */
106 : : void resetBlock();
107 : : /** Add a tx to the block */
108 : : void AddToBlock(const CTxMemPoolEntry& entry);
109 : :
110 : : // Methods for how to add transactions to a block.
111 : : /** Add transactions based on chunk feerate
112 : : *
113 : : * @pre BlockAssembler::m_mempool must not be nullptr
114 : : */
115 : : void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs);
116 : :
117 : : // helper functions for addChunks()
118 : : /** Test if a new chunk would "fit" in the block */
119 : : bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const;
120 : : /** Perform locktime checks on each transaction in a chunk:
121 : : * This check should always succeed, and is here
122 : : * only as an extra check in case of a bug */
123 : : bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
124 : : };
125 : :
126 : : /**
127 : : * Get the minimum time a miner should use in the next block. This always
128 : : * accounts for the BIP94 timewarp rule, so does not necessarily reflect the
129 : : * consensus limit.
130 : : */
131 : : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, int64_t difficulty_adjustment_interval);
132 : :
133 : : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
134 : :
135 : : /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
136 : : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
137 : :
138 : : /** Apply -blockmintxfee and -blockmaxweight options from ArgsManager to BlockAssembler options. */
139 : : void ApplyArgsManOptions(const ArgsManager& gArgs, BlockAssembler::Options& options);
140 : :
141 : : /* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
142 : : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
143 : :
144 : :
145 : : /* Interrupt the current wait for the next block template. */
146 : : void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait);
147 : : /**
148 : : * Return a new block template when fees rise to a certain threshold or after a
149 : : * new tip; return nullopt if timeout is reached.
150 : : */
151 : : std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
152 : : KernelNotifications& kernel_notifications,
153 : : CTxMemPool* mempool,
154 : : const std::unique_ptr<CBlockTemplate>& block_template,
155 : : const BlockWaitOptions& options,
156 : : const BlockAssembler::Options& assemble_options,
157 : : bool& interrupt_wait);
158 : :
159 : : /* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/
160 : : std::optional<BlockRef> GetTip(ChainstateManager& chainman);
161 : :
162 : : /* 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`).
163 : : * Returns the current tip, or nullopt if the node is shutting down. */
164 : : std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout);
165 : :
166 : : } // namespace node
167 : :
168 : : #endif // BITCOIN_NODE_MINER_H
|