Bitcoin ABC  0.26.3
P2P Digital Currency
miner.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <kernel/mempool_entry.h>
11 #include <primitives/block.h>
12 #include <txmempool.h>
13 
14 #include <boost/multi_index/ordered_index.hpp>
15 #include <boost/multi_index_container.hpp>
16 
17 #include <cstdint>
18 #include <memory>
19 #include <optional>
20 
21 class CBlockIndex;
22 class CChainParams;
23 class Config;
24 class CScript;
25 
26 namespace Consensus {
27 struct Params;
28 }
29 
30 namespace avalanche {
31 class Processor;
32 }
33 
34 namespace node {
35 static const bool DEFAULT_PRINTPRIORITY = false;
36 
40  int64_t sigChecks;
41 
42  CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigChecks)
43  : tx(_tx), fees(_fees), sigChecks(_sigChecks){};
44 };
45 
48 
49  std::vector<CBlockTemplateEntry> entries;
50 };
51 
54 private:
55  // The constructed block template
56  std::unique_ptr<CBlockTemplate> pblocktemplate;
57 
58  // Configuration parameters for the block size
62 
63  // Information on the current status of the block
64  uint64_t nBlockSize;
65  uint64_t nBlockTx;
66  uint64_t nBlockSigChecks;
68 
69  // Chain context for the block
70  int nHeight;
73 
74  const CTxMemPool *const m_mempool;
77 
78  const bool fPrintPriority;
79 
80 public:
81  struct Options {
82  Options();
86  };
87 
88  BlockAssembler(const Config &config, Chainstate &chainstate,
89  const CTxMemPool *mempool,
90  const avalanche::Processor *avalanche = nullptr);
91  BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool,
92  const Options &options,
93  const avalanche::Processor *avalanche = nullptr);
94 
96  std::unique_ptr<CBlockTemplate>
97  CreateNewBlock(const CScript &scriptPubKeyIn);
98 
99  uint64_t GetMaxGeneratedBlockSize() const { return nMaxGeneratedBlockSize; }
100 
101  static std::optional<int64_t> m_last_block_num_txs;
102  static std::optional<int64_t> m_last_block_size;
103 
104 private:
105  // utility functions
107  void resetBlock();
109  void AddToBlock(const CTxMemPoolEntryRef &entry);
110 
111  // Methods for how to add transactions to a block.
115  void addTxs(const CTxMemPool &mempool) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
116 
117  // helper functions for addTxs()
119  bool TestTxFits(uint64_t txSize, int64_t txSigChecks) const;
120 
122  bool CheckTx(const CTransaction &tx) const;
123 };
124 
125 int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams,
126  const CBlockIndex *pindexPrev);
127 } // namespace node
128 
129 #endif // BITCOIN_NODE_MINER_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:19
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:80
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:209
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:695
Definition: config.h:19
Definition: rcu.h:85
Generate a new block, without valid proof-of-work.
Definition: miner.h:53
Chainstate & m_chainstate
Definition: miner.h:75
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:59
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:112
const CTxMemPool *const m_mempool
Definition: miner.h:74
uint64_t GetMaxGeneratedBlockSize() const
Definition: miner.h:99
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:126
CFeeRate blockMinFeeRate
Definition: miner.h:61
const bool fPrintPriority
Definition: miner.h:78
uint64_t nBlockTx
Definition: miner.h:65
const CChainParams & chainParams
Definition: miner.h:72
int64_t m_lock_time_cutoff
Definition: miner.h:71
static std::optional< int64_t > m_last_block_size
Definition: miner.h:102
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:56
bool CheckTx(const CTransaction &tx) const
Check the transaction for finality, etc before adding to block.
Definition: miner.cpp:287
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:101
void AddToBlock(const CTxMemPoolEntryRef &entry)
Add a tx to the block.
Definition: miner.cpp:271
void addTxs(const CTxMemPool &mempool) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
Add transactions from the mempool based on individual tx feerate.
Definition: miner.cpp:298
uint64_t nBlockSigChecks
Definition: miner.h:66
BlockAssembler(const Config &config, Chainstate &chainstate, const CTxMemPool *mempool, const avalanche::Processor *avalanche=nullptr)
Definition: miner.cpp:107
uint64_t nBlockSize
Definition: miner.h:64
bool TestTxFits(uint64_t txSize, int64_t txSigChecks) const
Test if a new Tx would "fit" in the block.
Definition: miner.cpp:259
uint64_t nMaxGeneratedBlockSigChecks
Definition: miner.h:60
const avalanche::Processor *const m_avalanche
Definition: miner.h:76
Definition: init.h:28
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:35
int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:38
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
Definition: amount.h:19
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:84
uint64_t nExcessiveBlockSize
Definition: miner.h:83
Definition: miner.h:37
CTransactionRef tx
Definition: miner.h:38
int64_t sigChecks
Definition: miner.h:40
CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigChecks)
Definition: miner.h:42
Amount fees
Definition: miner.h:39
std::vector< CBlockTemplateEntry > entries
Definition: miner.h:49
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56