Bitcoin Core  31.0.0
P2P Digital Currency
miner.cpp
Go to the documentation of this file.
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 #include <node/miner.h>
7 
8 #include <chain.h>
9 #include <chainparams.h>
10 #include <coins.h>
11 #include <common/args.h>
12 #include <consensus/amount.h>
13 #include <consensus/consensus.h>
14 #include <consensus/merkle.h>
15 #include <consensus/tx_verify.h>
16 #include <consensus/validation.h>
17 #include <deploymentstatus.h>
18 #include <logging.h>
19 #include <node/context.h>
21 #include <policy/feerate.h>
22 #include <policy/policy.h>
23 #include <pow.h>
24 #include <primitives/transaction.h>
25 #include <util/moneystr.h>
26 #include <util/signalinterrupt.h>
27 #include <util/time.h>
28 #include <validation.h>
29 
30 #include <algorithm>
31 #include <utility>
32 #include <numeric>
33 
34 namespace node {
35 
36 int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
37 {
38  int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
39  // Height of block to be mined.
40  const int height{pindexPrev->nHeight + 1};
41  // Account for BIP94 timewarp rule on all networks. This makes future
42  // activation safer.
43  if (height % difficulty_adjustment_interval == 0) {
44  min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
45  }
46  return min_time;
47 }
48 
49 int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
50 {
51  int64_t nOldTime = pblock->nTime;
52  int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
53  TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
54 
55  if (nOldTime < nNewTime) {
56  pblock->nTime = nNewTime;
57  }
58 
59  // Updating time can change work required on testnet:
60  if (consensusParams.fPowAllowMinDifficultyBlocks) {
61  pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);
62  }
63 
64  return nNewTime - nOldTime;
65 }
66 
68 {
69  CMutableTransaction tx{*block.vtx.at(0)};
70  tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block));
71  block.vtx.at(0) = MakeTransactionRef(tx);
72 
73  const CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock));
74  chainman.GenerateCoinbaseCommitment(block, prev_block);
75 
76  block.hashMerkleRoot = BlockMerkleRoot(block);
77 }
78 
80 {
81  // Apply DEFAULT_BLOCK_RESERVED_WEIGHT when the caller left it unset.
84  // Limit weight to between block_reserved_weight and MAX_BLOCK_WEIGHT for sanity:
85  // block_reserved_weight can safely exceed -blockmaxweight, but the rest of the block template will be empty.
86  options.nBlockMaxWeight = std::clamp<size_t>(options.nBlockMaxWeight, *options.block_reserved_weight, MAX_BLOCK_WEIGHT);
87  return options;
88 }
89 
90 BlockAssembler::BlockAssembler(Chainstate& chainstate, const CTxMemPool* mempool, const Options& options)
91  : chainparams{chainstate.m_chainman.GetParams()},
92  m_mempool{options.use_mempool ? mempool : nullptr},
93  m_chainstate{chainstate},
94  m_options{ClampOptions(options)}
95 {
96 }
97 
99 {
100  // Block resource limits
101  options.nBlockMaxWeight = args.GetIntArg("-blockmaxweight", options.nBlockMaxWeight);
102  if (const auto blockmintxfee{args.GetArg("-blockmintxfee")}) {
103  if (const auto parsed{ParseMoney(*blockmintxfee)}) options.blockMinFeeRate = CFeeRate{*parsed};
104  }
105  options.print_modified_fee = args.GetBoolArg("-printpriority", options.print_modified_fee);
106  if (!options.block_reserved_weight) {
107  options.block_reserved_weight = args.GetIntArg("-blockreservedweight");
108  }
109 }
110 
111 void BlockAssembler::resetBlock()
112 {
113  // Reserve space for fixed-size block header, txs count, and coinbase tx.
114  nBlockWeight = *Assert(m_options.block_reserved_weight);
115  nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
116 
117  // These counters do not include coinbase tx
118  nBlockTx = 0;
119  nFees = 0;
120 }
121 
122 std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
123 {
124  const auto time_start{SteadyClock::now()};
125 
126  resetBlock();
127 
128  pblocktemplate.reset(new CBlockTemplate());
129  CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
130 
131  // Add dummy coinbase tx as first transaction. It is skipped by the
132  // getblocktemplate RPC and mining interface consumers must not use it.
133  pblock->vtx.emplace_back();
134 
135  LOCK(::cs_main);
136  CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
137  assert(pindexPrev != nullptr);
138  nHeight = pindexPrev->nHeight + 1;
139 
140  pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
141  // -regtest only: allow overriding block.nVersion with
142  // -blockversion=N to test forking scenarios
143  if (chainparams.MineBlocksOnDemand()) {
144  pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
145  }
146 
147  pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
148  m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
149 
150  if (m_mempool) {
151  LOCK(m_mempool->cs);
152  m_mempool->StartBlockBuilding();
153  addChunks();
154  m_mempool->StopBlockBuilding();
155  }
156 
157  const auto time_1{SteadyClock::now()};
158 
159  m_last_block_num_txs = nBlockTx;
160  m_last_block_weight = nBlockWeight;
161 
162  // Create coinbase transaction.
163  CMutableTransaction coinbaseTx;
164 
165  // Construct coinbase transaction struct in parallel
166  CoinbaseTx& coinbase_tx{pblocktemplate->m_coinbase_tx};
167  coinbase_tx.version = coinbaseTx.version;
168 
169  coinbaseTx.vin.resize(1);
170  coinbaseTx.vin[0].prevout.SetNull();
171  coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
172  coinbase_tx.sequence = coinbaseTx.vin[0].nSequence;
173 
174  // Add an output that spends the full coinbase reward.
175  coinbaseTx.vout.resize(1);
176  coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
177  // Block subsidy + fees
178  const CAmount block_reward{nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus())};
179  coinbaseTx.vout[0].nValue = block_reward;
180  coinbase_tx.block_reward_remaining = block_reward;
181 
182  // Start the coinbase scriptSig with the block height as required by BIP34.
183  // Mining clients are expected to append extra data to this prefix, so
184  // increasing its length would reduce the space they can use and may break
185  // existing clients.
186  coinbaseTx.vin[0].scriptSig = CScript() << nHeight;
187  if (m_options.include_dummy_extranonce) {
188  // For blocks at heights <= 16, the BIP34-encoded height alone is only
189  // one byte. Consensus requires coinbase scriptSigs to be at least two
190  // bytes long (bad-cb-length), so tests and regtest include a dummy
191  // extraNonce (OP_0)
192  coinbaseTx.vin[0].scriptSig << OP_0;
193  }
194  coinbase_tx.script_sig_prefix = coinbaseTx.vin[0].scriptSig;
195  Assert(nHeight > 0);
196  coinbaseTx.nLockTime = static_cast<uint32_t>(nHeight - 1);
197  coinbase_tx.lock_time = coinbaseTx.nLockTime;
198 
199  pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
200  m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock, pindexPrev);
201 
202  const CTransactionRef& final_coinbase{pblock->vtx[0]};
203  if (final_coinbase->HasWitness()) {
204  const auto& witness_stack{final_coinbase->vin[0].scriptWitness.stack};
205  // Consensus requires the coinbase witness stack to have exactly one
206  // element of 32 bytes.
207  Assert(witness_stack.size() == 1 && witness_stack[0].size() == 32);
208  coinbase_tx.witness = uint256(witness_stack[0]);
209  }
210  if (const int witness_index = GetWitnessCommitmentIndex(*pblock); witness_index != NO_WITNESS_COMMITMENT) {
211  Assert(witness_index >= 0 && static_cast<size_t>(witness_index) < final_coinbase->vout.size());
212  coinbase_tx.required_outputs.push_back(final_coinbase->vout[witness_index]);
213  }
214 
215  LogInfo("CreateNewBlock(): block weight: %u txs: %u fees: %ld sigops %d\n", GetBlockWeight(*pblock), nBlockTx, nFees, nBlockSigOpsCost);
216 
217  // Fill in header
218  pblock->hashPrevBlock = pindexPrev->GetBlockHash();
219  UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
220  pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
221  pblock->nNonce = 0;
222 
223  if (m_options.test_block_validity) {
224  // if nHeight <= 16, and include_dummy_extranonce=false this will fail due to bad-cb-length.
225  if (BlockValidationState state{TestBlockValidity(m_chainstate, *pblock, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) {
226  throw std::runtime_error(strprintf("TestBlockValidity failed: %s", state.ToString()));
227  }
228  }
229  const auto time_2{SteadyClock::now()};
230 
231  LogDebug(BCLog::BENCH, "CreateNewBlock() chunks: %.2fms, validity: %.2fms (total %.2fms)\n",
232  Ticks<MillisecondsDouble>(time_1 - time_start),
233  Ticks<MillisecondsDouble>(time_2 - time_1),
234  Ticks<MillisecondsDouble>(time_2 - time_start));
235 
236  return std::move(pblocktemplate);
237 }
238 
239 bool BlockAssembler::TestChunkBlockLimits(FeePerWeight chunk_feerate, int64_t chunk_sigops_cost) const
240 {
241  if (nBlockWeight + chunk_feerate.size >= m_options.nBlockMaxWeight) {
242  return false;
243  }
244  if (nBlockSigOpsCost + chunk_sigops_cost >= MAX_BLOCK_SIGOPS_COST) {
245  return false;
246  }
247  return true;
248 }
249 
250 // Perform transaction-level checks before adding to block:
251 // - transaction finality (locktime)
252 bool BlockAssembler::TestChunkTransactions(const std::vector<CTxMemPoolEntryRef>& txs) const
253 {
254  for (const auto tx : txs) {
255  if (!IsFinalTx(tx.get().GetTx(), nHeight, m_lock_time_cutoff)) {
256  return false;
257  }
258  }
259  return true;
260 }
261 
262 void BlockAssembler::AddToBlock(const CTxMemPoolEntry& entry)
263 {
264  pblocktemplate->block.vtx.emplace_back(entry.GetSharedTx());
265  pblocktemplate->vTxFees.push_back(entry.GetFee());
266  pblocktemplate->vTxSigOpsCost.push_back(entry.GetSigOpCost());
267  nBlockWeight += entry.GetTxWeight();
268  ++nBlockTx;
269  nBlockSigOpsCost += entry.GetSigOpCost();
270  nFees += entry.GetFee();
271 
272  if (m_options.print_modified_fee) {
273  LogInfo("fee rate %s txid %s\n",
274  CFeeRate(entry.GetModifiedFee(), entry.GetTxSize()).ToString(),
275  entry.GetTx().GetHash().ToString());
276  }
277 }
278 
279 void BlockAssembler::addChunks()
280 {
281  // Limit the number of attempts to add transactions to the block when it is
282  // close to full; this is just a simple heuristic to finish quickly if the
283  // mempool has a lot of entries.
284  const int64_t MAX_CONSECUTIVE_FAILURES = 1000;
285  constexpr int32_t BLOCK_FULL_ENOUGH_WEIGHT_DELTA = 4000;
286  int64_t nConsecutiveFailed = 0;
287 
288  std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> selected_transactions;
289  selected_transactions.reserve(MAX_CLUSTER_COUNT_LIMIT);
290  FeePerWeight chunk_feerate;
291 
292  // This fills selected_transactions
293  chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
294  FeePerVSize chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
295 
296  while (selected_transactions.size() > 0) {
297  // Check to see if min fee rate is still respected.
298  if (chunk_feerate_vsize << m_options.blockMinFeeRate.GetFeePerVSize()) {
299  // Everything else we might consider has a lower feerate
300  return;
301  }
302 
303  int64_t chunk_sig_ops = 0;
304  for (const auto& tx : selected_transactions) {
305  chunk_sig_ops += tx.get().GetSigOpCost();
306  }
307 
308  // Check to see if this chunk will fit.
309  if (!TestChunkBlockLimits(chunk_feerate, chunk_sig_ops) || !TestChunkTransactions(selected_transactions)) {
310  // This chunk won't fit, so we skip it and will try the next best one.
311  m_mempool->SkipBuilderChunk();
312  ++nConsecutiveFailed;
313 
314  if (nConsecutiveFailed > MAX_CONSECUTIVE_FAILURES && nBlockWeight +
315  BLOCK_FULL_ENOUGH_WEIGHT_DELTA > m_options.nBlockMaxWeight) {
316  // Give up if we're close to full and haven't succeeded in a while
317  return;
318  }
319  } else {
320  m_mempool->IncludeBuilderChunk();
321 
322  // This chunk will fit, so add it to the block.
323  nConsecutiveFailed = 0;
324  for (const auto& tx : selected_transactions) {
325  AddToBlock(tx);
326  }
327  pblocktemplate->m_package_feerates.emplace_back(chunk_feerate_vsize);
328  }
329 
330  selected_transactions.clear();
331  chunk_feerate = m_mempool->GetBlockBuilderChunk(selected_transactions);
332  chunk_feerate_vsize = ToFeePerVSize(chunk_feerate);
333  }
334 }
335 
336 void AddMerkleRootAndCoinbase(CBlock& block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
337 {
338  if (block.vtx.size() == 0) {
339  block.vtx.emplace_back(coinbase);
340  } else {
341  block.vtx[0] = coinbase;
342  }
343  block.nVersion = version;
344  block.nTime = timestamp;
345  block.nNonce = nonce;
346  block.hashMerkleRoot = BlockMerkleRoot(block);
347 
348  // Reset cached checks
349  block.m_checked_witness_commitment = false;
350  block.m_checked_merkle_root = false;
351  block.fChecked = false;
352 }
353 
354 void InterruptWait(KernelNotifications& kernel_notifications, bool& interrupt_wait)
355 {
356  LOCK(kernel_notifications.m_tip_block_mutex);
357  interrupt_wait = true;
358  kernel_notifications.m_tip_block_cv.notify_all();
359 }
360 
361 std::unique_ptr<CBlockTemplate> WaitAndCreateNewBlock(ChainstateManager& chainman,
362  KernelNotifications& kernel_notifications,
363  CTxMemPool* mempool,
364  const std::unique_ptr<CBlockTemplate>& block_template,
365  const BlockWaitOptions& options,
366  const BlockAssembler::Options& assemble_options,
367  bool& interrupt_wait)
368 {
369  // Delay calculating the current template fees, just in case a new block
370  // comes in before the next tick.
371  CAmount current_fees = -1;
372 
373  // Alternate waiting for a new tip and checking if fees have risen.
374  // The latter check is expensive so we only run it once per second.
375  auto now{NodeClock::now()};
376  const auto deadline = now + options.timeout;
377  const MillisecondsDouble tick{1000};
378  const bool allow_min_difficulty{chainman.GetParams().GetConsensus().fPowAllowMinDifficultyBlocks};
379 
380  do {
381  bool tip_changed{false};
382  {
383  WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
384  // Note that wait_until() checks the predicate before waiting
385  kernel_notifications.m_tip_block_cv.wait_until(lock, std::min(now + tick, deadline), [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
386  AssertLockHeld(kernel_notifications.m_tip_block_mutex);
387  const auto tip_block{kernel_notifications.TipBlock()};
388  // We assume tip_block is set, because this is an instance
389  // method on BlockTemplate and no template could have been
390  // generated before a tip exists.
391  tip_changed = Assume(tip_block) && tip_block != block_template->block.hashPrevBlock;
392  return tip_changed || chainman.m_interrupt || interrupt_wait;
393  });
394  if (interrupt_wait) {
395  interrupt_wait = false;
396  return nullptr;
397  }
398  }
399 
400  if (chainman.m_interrupt) return nullptr;
401  // At this point the tip changed, a full tick went by or we reached
402  // the deadline.
403 
404  // Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
405  LOCK(::cs_main);
406 
407  // On test networks return a minimum difficulty block after 20 minutes
408  if (!tip_changed && allow_min_difficulty) {
409  const NodeClock::time_point tip_time{std::chrono::seconds{chainman.ActiveChain().Tip()->GetBlockTime()}};
410  if (now > tip_time + 20min) {
411  tip_changed = true;
412  }
413  }
414 
423  if (options.fee_threshold < MAX_MONEY || tip_changed) {
424  auto new_tmpl{BlockAssembler{
425  chainman.ActiveChainstate(),
426  mempool,
427  assemble_options}
428  .CreateNewBlock()};
429 
430  // If the tip changed, return the new template regardless of its fees.
431  if (tip_changed) return new_tmpl;
432 
433  // Calculate the original template total fees if we haven't already
434  if (current_fees == -1) {
435  current_fees = std::accumulate(block_template->vTxFees.begin(), block_template->vTxFees.end(), CAmount{0});
436  }
437 
438  // Check if fees increased enough to return the new template
439  const CAmount new_fees = std::accumulate(new_tmpl->vTxFees.begin(), new_tmpl->vTxFees.end(), CAmount{0});
440  Assume(options.fee_threshold != MAX_MONEY);
441  if (new_fees >= current_fees + options.fee_threshold) return new_tmpl;
442  }
443 
444  now = NodeClock::now();
445  } while (now < deadline);
446 
447  return nullptr;
448 }
449 
450 std::optional<BlockRef> GetTip(ChainstateManager& chainman)
451 {
452  LOCK(::cs_main);
453  CBlockIndex* tip{chainman.ActiveChain().Tip()};
454  if (!tip) return {};
455  return BlockRef{tip->GetBlockHash(), tip->nHeight};
456 }
457 
458 bool CooldownIfHeadersAhead(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const BlockRef& last_tip, bool& interrupt_mining)
459 {
460  uint256 last_tip_hash{last_tip.hash};
461 
462  while (const std::optional<int> remaining = chainman.BlocksAheadOfTip()) {
463  const int cooldown_seconds = std::clamp(*remaining, 3, 20);
464  const auto cooldown_deadline{MockableSteadyClock::now() + std::chrono::seconds{cooldown_seconds}};
465 
466  {
467  WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
468  kernel_notifications.m_tip_block_cv.wait_until(lock, cooldown_deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
469  const auto tip_block = kernel_notifications.TipBlock();
470  return chainman.m_interrupt || interrupt_mining || (tip_block && *tip_block != last_tip_hash);
471  });
472  if (chainman.m_interrupt || interrupt_mining) {
473  interrupt_mining = false;
474  return false;
475  }
476 
477  // If the tip changed during the wait, extend the deadline
478  const auto tip_block = kernel_notifications.TipBlock();
479  if (tip_block && *tip_block != last_tip_hash) {
480  last_tip_hash = *tip_block;
481  continue;
482  }
483  }
484 
485  // No tip change and the cooldown window has expired.
486  if (MockableSteadyClock::now() >= cooldown_deadline) break;
487  }
488 
489  return true;
490 }
491 
492 std::optional<BlockRef> WaitTipChanged(ChainstateManager& chainman, KernelNotifications& kernel_notifications, const uint256& current_tip, MillisecondsDouble& timeout, bool& interrupt)
493 {
494  Assume(timeout >= 0ms); // No internal callers should use a negative timeout
495  if (timeout < 0ms) timeout = 0ms;
496  if (timeout > std::chrono::years{100}) timeout = std::chrono::years{100}; // Upper bound to avoid UB in std::chrono
497  auto deadline{std::chrono::steady_clock::now() + timeout};
498  {
499  WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
500  // For callers convenience, wait longer than the provided timeout
501  // during startup for the tip to be non-null. That way this function
502  // always returns valid tip information when possible and only
503  // returns null when shutting down, not when timing out.
504  kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
505  return kernel_notifications.TipBlock() || chainman.m_interrupt || interrupt;
506  });
507  if (chainman.m_interrupt || interrupt) {
508  interrupt = false;
509  return {};
510  }
511  // At this point TipBlock is set, so continue to wait until it is
512  // different then `current_tip` provided by caller.
513  kernel_notifications.m_tip_block_cv.wait_until(lock, deadline, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
514  return Assume(kernel_notifications.TipBlock()) != current_tip || chainman.m_interrupt || interrupt;
515  });
516  if (chainman.m_interrupt || interrupt) {
517  interrupt = false;
518  return {};
519  }
520  }
521 
522  // Must release m_tip_block_mutex before getTip() locks cs_main, to
523  // avoid deadlocks.
524  return GetTip(chainman);
525 }
526 
527 } // namespace node
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
uint32_t nNonce
Definition: block.h:35
int32_t GetTxWeight() const
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1038
static BlockAssembler::Options ClampOptions(BlockAssembler::Options options)
Definition: miner.cpp:79
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:57
static constexpr unsigned MAX_CLUSTER_COUNT_LIMIT
Definition: txgraph.h:18
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
std::optional< uint256 > TipBlock() EXCLUSIVE_LOCKS_REQUIRED(m_tip_block_mutex)
The block for which the last blockTip notification was received.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
int64_t GetBlockTime() const
Definition: chain.h:221
assert(!tx.IsCoinBase())
Generate a new block, without valid proof-of-work.
Definition: miner.h:60
unsigned int nonce
Definition: miner_tests.cpp:82
int32_t GetTxSize() const
Definition: block.h:73
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
Definition: validation.h:939
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
static constexpr unsigned int DEFAULT_BLOCK_RESERVED_WEIGHT
Default for -blockreservedweight.
Definition: policy.h:26
std::vector< CTxIn > vin
Definition: transaction.h:359
CChain & ActiveChain() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1165
std::optional< BlockRef > GetTip(ChainstateManager &chainman)
Definition: miner.cpp:450
void GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev) const
Produce the necessary coinbase commitment for a block (modifies the hash, don&#39;t call for mined blocks...
Hash/height pair to help track and identify blocks.
Definition: types.h:13
bool fPowAllowMinDifficultyBlocks
Definition: params.h:113
std::optional< BlockRef > WaitTipChanged(ChainstateManager &chainman, KernelNotifications &kernel_notifications, const uint256 &current_tip, MillisecondsDouble &timeout, bool &interrupt)
Definition: miner.cpp:492
size_t coinbase_output_max_additional_sigops
The maximum additional sigops which the pool will add in coinbase transaction outputs.
Definition: types.h:58
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:515
Definition: script.h:76
util::Result< void > ApplyArgsManOptions(const ArgsManager &args, BlockManager::Options &opts)
static constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction. ...
Definition: validation.h:15
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:238
static const int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule) ...
Definition: consensus.h:17
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
Definition: miner.cpp:49
CAmount GetModifiedFee() const
uint32_t nTime
Definition: block.h:33
const util::SignalInterrupt & m_interrupt
Definition: validation.h:1034
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:65
static const unsigned int MAX_BLOCK_WEIGHT
The maximum allowed weight for a block, see BIP 141 (network rule)
Definition: consensus.h:15
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
uint256 GetBlockHash() const
Definition: chain.h:198
CAmount fee_threshold
The wait method will not return a new template unless it has fees at least fee_threshold sats higher ...
Definition: types.h:99
bool IsValid() const
Definition: validation.h:105
ChainstateManager & m_chainman
The chainstate manager that owns this chainstate.
Definition: validation.h:583
ArgsManager & args
Definition: bitcoind.cpp:277
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:550
uint256 hashMerkleRoot
Definition: block.h:32
#define LOCK(cs)
Definition: sync.h:258
void AddMerkleRootAndCoinbase(CBlock &block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
Definition: miner.cpp:336
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:14
#define LogInfo(...)
Definition: log.h:95
const CAmount & GetFee() const
CTransactionRef GetSharedTx() const
Template containing all coinbase transaction fields that are set by our miner code.
Definition: types.h:119
uint256 hashPrevBlock
Definition: block.h:31
std::optional< CAmount > ParseMoney(const std::string &money_string)
Parse an amount denoted in full coins.
Definition: moneystr.cpp:45
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:66
std::string ToString() const
void InterruptWait(KernelNotifications &kernel_notifications, bool &interrupt_wait)
Definition: miner.cpp:354
#define WAIT_LOCK(cs, name)
Definition: sync.h:264
Parameters that influence chain consensus.
Definition: params.h:84
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::vector< CTxOut > vout
Definition: transaction.h:360
unsigned int nHeight
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:289
#define Assume(val)
Assume is the identity function.
Definition: check.h:125
int64_t GetMedianTimePast() const
Definition: chain.h:233
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
static FeePerVSize ToFeePerVSize(FeePerWeight feerate)
Definition: policy.h:196
int64_t DifficultyAdjustmentInterval() const
Definition: params.h:126
Definition: messages.h:21
ArgsManager gArgs
Definition: args.cpp:40
int64_t GetMinimumTime(const CBlockIndex *pindexPrev, const int64_t difficulty_adjustment_interval)
Get the minimum time a miner should use in the next block.
Definition: miner.cpp:36
const CChainParams & GetParams() const
Definition: validation.h:1007
#define LogDebug(category,...)
Definition: log.h:115
256-bit opaque blob.
Definition: uint256.h:195
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:30
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::vector< CTransactionRef > vtx
Definition: block.h:77
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:186
int32_t size
Definition: feefrac.h:108
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
const CTransaction & GetTx() const
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool, const Options &options)
Definition: miner.cpp:90
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Definition: args.h:306
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:461
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:396
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac...
Definition: feerate.h:31
bool m_checked_witness_commitment
Definition: block.h:81
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
int64_t GetSigOpCost() const
bool CooldownIfHeadersAhead(ChainstateManager &chainman, KernelNotifications &kernel_notifications, const BlockRef &last_tip, bool &interrupt_mining)
Wait while the best known header extends the current chain tip AND at least one block is being added ...
Definition: miner.cpp:458
A mutable version of CTransaction.
Definition: transaction.h:357
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time...
Definition: tx_verify.cpp:17
static const uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65)...
Definition: transaction.h:82
uint32_t version
Definition: types.h:121
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:106
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:89
BlockValidationState TestBlockValidity(Chainstate &chainstate, const CBlock &block, const bool check_pow, const bool check_merkle_root)
Verify a block, including transactions.
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
bool fChecked
Definition: block.h:80
uint256 hash
Definition: types.h:14
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block&#39;s coinbase transaction the witness commitment occurs, or -1 if not found.
Definition: validation.h:147
std::unique_ptr< CBlockTemplate > WaitAndCreateNewBlock(ChainstateManager &chainman, KernelNotifications &kernel_notifications, CTxMemPool *mempool, const std::unique_ptr< CBlockTemplate > &block_template, const BlockWaitOptions &options, const BlockAssembler::Options &assemble_options, bool &interrupt_wait)
Return a new block template when fees rise to a certain threshold or after a new tip; return nullopt ...
Definition: miner.cpp:361
static int64_t GetBlockWeight(const CBlock &block)
Definition: validation.h:136
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
int32_t nVersion
Definition: block.h:30
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed...
Definition: miner.cpp:67
static constexpr unsigned int MINIMUM_BLOCK_RESERVED_WEIGHT
This accounts for the block header, var_int encoding of the transaction count and a minimally viable ...
Definition: policy.h:33
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:94
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:26
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:246
#define Assert(val)
Identity function.
Definition: check.h:113
MillisecondsDouble timeout
How long to wait before returning nullptr instead of a new template.
Definition: types.h:86
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:328
bool m_checked_merkle_root
Definition: block.h:82
std::optional< size_t > block_reserved_weight
The default reserved weight for the fixed-size block header, transaction count and coinbase transacti...
Definition: types.h:53
uint32_t nBits
Definition: block.h:34
static constexpr int64_t MAX_TIMEWARP
Maximum number of seconds that the timestamp of the first block of a difficulty adjustment period is ...
Definition: consensus.h:35