forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathminer.h
More file actions
182 lines (153 loc) · 7.22 KB
/
Copy pathminer.h
File metadata and controls
182 lines (153 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_NODE_MINER_H
#define BITCOIN_NODE_MINER_H
#include <consensus/amount.h>
#include <node/mining_types.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <threadsafety.h>
#include <txmempool.h>
#include <util/feefrac.h>
#include <util/time.h>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>
class CBlockIndex;
class CChainParams;
class Chainstate;
class ChainstateManager;
namespace Consensus {
struct Params;
} // namespace Consensus
class uint256;
namespace interfaces {
struct BlockRef;
} // namespace interfaces
using interfaces::BlockRef;
namespace node {
class KernelNotifications;
struct CBlockTemplate
{
CBlock block;
// Fees per transaction, not including coinbase transaction (unlike CBlock::vtx).
std::vector<CAmount> vTxFees;
// Sigops per transaction, not including coinbase transaction (unlike CBlock::vtx).
std::vector<int64_t> vTxSigOpsCost;
/* A vector of package fee rates, ordered by the sequence in which
* packages are selected for inclusion in the block template.*/
std::vector<FeePerVSize> m_package_feerates;
/*
* Template containing all coinbase transaction fields that are set by our
* miner code.
*/
CoinbaseTx m_coinbase_tx;
};
/** Generate a new block, without valid proof-of-work */
class BlockAssembler
{
private:
// The constructed block template
std::unique_ptr<CBlockTemplate> pblocktemplate;
// Information on the current status of the block
uint64_t nBlockWeight;
uint64_t nBlockTx;
uint64_t nBlockSigOpsCost;
CAmount nFees;
// Chain context for the block
int nHeight;
int64_t m_lock_time_cutoff;
const CChainParams& chainparams;
const CTxMemPool* const m_mempool;
Chainstate& m_chainstate;
public:
explicit BlockAssembler(Chainstate& chainstate,
const CTxMemPool* mempool,
BlockCreateOptions create_options);
/** Construct a new block template */
std::unique_ptr<CBlockTemplate> CreateNewBlock();
/** The number of transactions in the last assembled block (excluding coinbase transaction) */
inline static std::optional<int64_t> m_last_block_num_txs{};
/** The weight of the last assembled block (including reserved weight for block header, txs count and coinbase tx) */
inline static std::optional<int64_t> m_last_block_weight{};
private:
const BlockCreateOptions m_options;
// utility functions
/** Clear the block's state and prepare for assembling a new block */
void resetBlock();
/** Add a tx to the block */
void AddToBlock(const CTxMemPoolEntry& entry);
// Methods for how to add transactions to a block.
/** Add transactions based on chunk feerate
*
* @pre BlockAssembler::m_mempool must not be nullptr
*/
void addChunks() EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs);
// helper functions for addChunks()
/** Test if a new chunk would "fit" in the block */
bool TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const;
/** Perform locktime checks on each transaction in a chunk:
* This check should always succeed, and is here
* only as an extra check in case of a bug */
bool TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const;
};
/**
* Get the minimum time a miner should use in the next block. This always
* accounts for the BIP94 timewarp rule, so does not necessarily reflect the
* consensus limit.
*/
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, int64_t difficulty_adjustment_interval);
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
void RegenerateCommitments(CBlock& block, ChainstateManager& chainman);
/* Compute the block's merkle root, insert or replace the coinbase transaction and the merkle root into the block */
void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce);
//! Submit a block and capture the validation state via the BlockChecked callback.
//! Returns whether ProcessNewBlock accepted the block.
bool SubmitBlock(ChainstateManager& chainman, const std::shared_ptr<const CBlock>& block, bool* new_block, std::string& reason, std::string& debug);
/* Interrupt a blocking call. */
void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait);
/**
* Return a new block template when fees rise to a certain threshold or after a
* new tip; return nullopt if timeout is reached.
*/
std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
KernelNotifications& kernel_notifications,
CTxMemPool* mempool,
const std::unique_ptr<CBlockTemplate>& block_template,
const BlockWaitOptions& wait_options,
const BlockCreateOptions& create_options,
bool& interrupt_wait);
/* Locks cs_main and returns the block hash and block height of the active chain if it exists; otherwise, returns nullopt.*/
std::optional<BlockRef> GetTip(ChainstateManager& chainman);
/* 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`).
* Returns the current tip, or nullopt if the node is shutting down or interrupt()
* is called.
*/
std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt);
/**
* Wait while the best known header extends the current chain tip AND at least
* one block is being added to the tip every 3 seconds. If the tip is
* sufficiently far behind, allow up to 20 seconds for the next tip update.
*
* It’s not safe to keep waiting, because a malicious miner could announce a
* header and delay revealing the block, causing all other miners using this
* software to stall. At the same time, we need to balance between the default
* waiting time being brief, but not ending the cooldown prematurely when a
* random block is slow to download (or process).
*
* The cooldown only applies to createNewBlock(), which is typically called
* once per connected client. Subsequent templates are provided by waitNext().
*
* @param last_tip tip at the start of the cooldown window.
* @param interrupt_mining set to true to interrupt the cooldown.
*
* @returns false if interrupted.
*/
bool CooldownIfHeadersAhead(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const BlockRef& last_tip, bool& interrupt_mining);
} // namespace node
#endif // BITCOIN_NODE_MINER_H