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