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 <consensus/amount.h>
10 : #include <node/mining_types.h>
11 : #include <primitives/block.h>
12 : #include <primitives/transaction.h>
13 : #include <threadsafety.h>
14 : #include <txmempool.h>
15 : #include <util/feefrac.h>
16 : #include <util/not_null.h>
17 :
18 : #include <cstdint>
19 : #include <memory>
20 : #include <optional>
21 : #include <vector>
22 :
23 : class CBlockIndex;
24 : class CChainParams;
25 : class Chainstate;
26 : class ChainstateManager;
27 :
28 : namespace Consensus {
29 : struct Params;
30 : } // namespace Consensus
31 : namespace node {
32 :
33 : struct CBlockTemplate
34 : {
35 : CBlock block;
36 : // Fees per transaction, not including coinbase transaction (unlike CBlock::vtx).
37 : std::vector<CAmount> vTxFees;
38 : // Sigops per transaction, not including coinbase transaction (unlike CBlock::vtx).
39 : std::vector<int64_t> vTxSigOpsCost;
40 : /* A vector of package fee rates, ordered by the sequence in which
41 : * packages are selected for inclusion in the block template.*/
42 : std::vector<FeePerVSize> m_package_feerates;
43 : /*
44 : * Template containing all coinbase transaction fields that are set by our
45 : * miner code.
46 : */
47 : CoinbaseTx m_coinbase_tx;
48 : };
49 :
50 : /** Generate a new block, without valid proof-of-work */
51 48148 : class BlockAssembler
52 : {
53 : private:
54 : // The constructed block template
55 : std::unique_ptr<CBlockTemplate> pblocktemplate;
56 :
57 : // Information on the current status of the block
58 : uint64_t nBlockWeight;
59 : uint64_t nBlockTx;
60 : uint64_t nBlockSigOpsCost;
61 : CAmount nFees;
62 :
63 : // Chain context for the block
64 : int nHeight;
65 : int64_t m_lock_time_cutoff;
66 :
67 : const CChainParams& chainparams;
68 : const CTxMemPool* const m_mempool;
69 : Chainstate& m_chainstate;
70 :
71 : public:
72 : explicit BlockAssembler(Chainstate& chainstate,
73 : const CTxMemPool* mempool,
74 : BlockCreateOptions create_options);
75 :
76 : /** Construct a new block template */
77 : util::NotNullUniquePtr<CBlockTemplate> CreateNewBlock();
78 :
79 : /** The number of transactions in the last assembled block (excluding coinbase transaction) */
80 : inline static std::optional<int64_t> m_last_block_num_txs{};
81 : /** The weight of the last assembled block (including reserved weight for block header, txs count and coinbase tx) */
82 : inline static std::optional<int64_t> m_last_block_weight{};
83 :
84 : private:
85 : const BlockCreateOptions m_options;
86 :
87 : // utility functions
88 : /** Clear the block's state and prepare for assembling a new block */
89 : void resetBlock();
90 : /** Add a tx to the block */
91 : void AddToBlock(const CTxMemPoolEntry& entry);
92 :
93 : // Methods for how to add transactions to a block.
94 : /** Add transactions based on chunk feerate
95 : *
96 : * @pre BlockAssembler::m_mempool must not be nullptr
97 : */
98 : void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs);
99 :
100 : // helper functions for addChunks()
101 : /** Test if a new chunk would "fit" in the block */
102 : bool TestChunkBlockLimits(int64_t chunk_weight, int64_t chunk_sigops_cost) const;
103 : /** Perform locktime checks on each transaction in a chunk:
104 : * This check should always succeed, and is here
105 : * only as an extra check in case of a bug */
106 : bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
107 : };
108 :
109 : /**
110 : * Get the minimum time a miner should use in the next block. This always
111 : * accounts for the BIP94 timewarp rule, so does not necessarily reflect the
112 : * consensus limit.
113 : */
114 : int64_t GetMinimumTime(const CBlockIndex* pindexPrev, int64_t difficulty_adjustment_interval);
115 :
116 : int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
117 :
118 : /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
119 : void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
120 :
121 : /* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
122 : void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
123 : } // namespace node
124 :
125 : #endif // BITCOIN_NODE_MINER_H
|