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 node {
31 static const bool DEFAULT_PRINTPRIORITY = false;
32 
36  int64_t sigChecks;
37 
38  CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigChecks)
39  : tx(_tx), fees(_fees), sigChecks(_sigChecks){};
40 };
41 
44 
45  std::vector<CBlockTemplateEntry> entries;
46 };
47 
50 private:
51  // The constructed block template
52  std::unique_ptr<CBlockTemplate> pblocktemplate;
53 
54  // Configuration parameters for the block size
58 
59  // Information on the current status of the block
60  uint64_t nBlockSize;
61  uint64_t nBlockTx;
62  uint64_t nBlockSigChecks;
64 
65  // Chain context for the block
66  int nHeight;
69 
70  const CTxMemPool *const m_mempool;
72 
73  const bool fPrintPriority;
74 
75 public:
76  struct Options {
77  Options();
81  };
82 
83  BlockAssembler(const Config &config, Chainstate &chainstate,
84  const CTxMemPool *mempool);
85  BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool,
86  const Options &options);
87 
89  std::unique_ptr<CBlockTemplate>
90  CreateNewBlock(const CScript &scriptPubKeyIn);
91 
92  uint64_t GetMaxGeneratedBlockSize() const { return nMaxGeneratedBlockSize; }
93 
94  static std::optional<int64_t> m_last_block_num_txs;
95  static std::optional<int64_t> m_last_block_size;
96 
97 private:
98  // utility functions
100  void resetBlock();
102  void AddToBlock(const CTxMemPoolEntryRef &entry);
103 
104  // Methods for how to add transactions to a block.
108  void addTxs(const CTxMemPool &mempool) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs);
109 
110  // helper functions for addTxs()
112  bool TestTxFits(uint64_t txSize, int64_t txSigChecks) const;
113 
115  bool CheckTx(const CTransaction &tx) const;
116 };
117 
118 int64_t UpdateTime(CBlockHeader *pblock, const CChainParams &chainParams,
119  const CBlockIndex *pindexPrev);
120 } // namespace node
121 
122 #endif // BITCOIN_NODE_MINER_H
const CChainParams & Params()
Return the currently selected parameters.
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:26
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:74
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:629
Definition: config.h:17
Definition: rcu.h:85
Generate a new block, without valid proof-of-work.
Definition: miner.h:49
Chainstate & m_chainstate
Definition: miner.h:71
uint64_t nMaxGeneratedBlockSize
Definition: miner.h:55
void resetBlock()
Clear the block's state and prepare for assembling a new block.
Definition: miner.cpp:109
const CTxMemPool *const m_mempool
Definition: miner.h:70
uint64_t GetMaxGeneratedBlockSize() const
Definition: miner.h:92
BlockAssembler(const Config &config, Chainstate &chainstate, const CTxMemPool *mempool)
Definition: miner.cpp:105
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:123
CFeeRate blockMinFeeRate
Definition: miner.h:57
const bool fPrintPriority
Definition: miner.h:73
uint64_t nBlockTx
Definition: miner.h:61
const CChainParams & chainParams
Definition: miner.h:68
int64_t m_lock_time_cutoff
Definition: miner.h:67
static std::optional< int64_t > m_last_block_size
Definition: miner.h:95
std::unique_ptr< CBlockTemplate > pblocktemplate
Definition: miner.h:52
bool CheckTx(const CTransaction &tx) const
Check the transaction for finality, etc before adding to block.
Definition: miner.cpp:284
static std::optional< int64_t > m_last_block_num_txs
Definition: miner.h:94
void AddToBlock(const CTxMemPoolEntryRef &entry)
Add a tx to the block.
Definition: miner.cpp:268
void addTxs(const CTxMemPool &mempool) EXCLUSIVE_LOCKS_REQUIRED(mempool.cs)
Add transactions from the mempool based on individual tx feerate.
Definition: miner.cpp:295
uint64_t nBlockSigChecks
Definition: miner.h:62
uint64_t nBlockSize
Definition: miner.h:60
bool TestTxFits(uint64_t txSize, int64_t txSigChecks) const
Test if a new Tx would "fit" in the block.
Definition: miner.cpp:256
uint64_t nMaxGeneratedBlockSigChecks
Definition: miner.h:56
Definition: init.h:28
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:31
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:79
uint64_t nExcessiveBlockSize
Definition: miner.h:78
Definition: miner.h:33
CTransactionRef tx
Definition: miner.h:34
int64_t sigChecks
Definition: miner.h:36
CBlockTemplateEntry(CTransactionRef _tx, Amount _fees, int64_t _sigChecks)
Definition: miner.h:38
Amount fees
Definition: miner.h:35
std::vector< CBlockTemplateEntry > entries
Definition: miner.h:45
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56