9#include <chainparams.h>
38#include <versionbits.h>
65 min_time = std::max<int64_t>(min_time,
first_block->GetBlockTime());
103 : chainparams{
chainstate.m_chainman.GetParams()},
104 m_mempool{options.use_mempool ? mempool :
nullptr},
115void BlockAssembler::resetBlock()
118 nBlockWeight = *
Assert(m_options.block_reserved_weight);
119 nBlockSigOpsCost = m_options.coinbase_output_max_additional_sigops;
126std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
133 CBlock*
const pblock = &pblocktemplate->block;
137 pblock->
vtx.emplace_back();
144 pblock->
nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(
pindexPrev, chainparams.GetConsensus());
147 if (chainparams.MineBlocksOnDemand()) {
152 m_lock_time_cutoff =
pindexPrev->GetMedianTimePast();
156 m_mempool->StartBlockBuilding();
158 m_mempool->StopBlockBuilding();
161 const auto time_1{SteadyClock::now()};
163 m_last_block_num_txs = nBlockTx;
164 m_last_block_weight = nBlockWeight;
180 coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
208 m_chainstate.m_chainman.GenerateCoinbaseCommitment(*pblock,
pindexPrev);
231 if (m_options.test_block_validity) {
233 throw std::runtime_error(
strprintf(
"TestBlockValidity failed: %s", state.ToString()));
236 const auto time_2{SteadyClock::now()};
243 return std::move(pblocktemplate);
249 Assert(m_options.block_max_weight);
250 if (nBlockWeight +
chunk_weight >= m_options.block_max_weight) {
261bool BlockAssembler::TestChunkTransactions(
const std::vector<CTxMemPoolEntryRef>&
txs)
const
263 for (
const auto tx :
txs) {
273 pblocktemplate->block.vtx.emplace_back(entry.
GetSharedTx());
274 pblocktemplate->vTxFees.push_back(entry.
GetFee());
275 pblocktemplate->vTxSigOpsCost.push_back(entry.
GetSigOpCost());
281 if (*m_options.print_modified_fee) {
282 LogInfo(
"fee rate %s txid %s\n",
288void BlockAssembler::addChunks()
322 m_mempool->SkipBuilderChunk();
326 Assert(m_options.block_max_weight);
333 m_mempool->IncludeBuilderChunk();
351 if (block.
vtx.size() == 0) {
357 block.
nTime = timestamp;
int64_t CAmount
Amount in satoshis (Can be negative)
#define Assert(val)
Identity function.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Wrapper around FeeFrac & derived types, which adds a feerate-based ordering which treats equal-feerat...
bool m_checked_merkle_root
std::vector< CTransactionRef > vtx
bool m_checked_witness_commitment
The block chain is a tree shaped structure starting with the genesis block at the root,...
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
std::string ToString(FeeRateFormat fee_rate_format=FeeRateFormat::BTC_KVB) const
Serialized script, used inside transaction inputs and outputs.
const Txid & GetHash() const LIFETIMEBOUND
static constexpr uint32_t MAX_SEQUENCE_NONFINAL
This is the maximum sequence number that enables both nLockTime and OP_CHECKLOCKTIMEVERIFY (BIP 65).
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
const CTransaction & GetTx() const
int32_t GetTxWeight() const
int64_t GetSigOpCost() const
CTransactionRef GetSharedTx() const
int32_t GetTxSize() const
const CAmount & GetFee() const
CAmount GetModifiedFee() const
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
void GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev) const
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
BlockAssembler(Chainstate &chainstate, const CTxMemPool *mempool, BlockCreateOptions create_options)
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::string ToString() const
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
constexpr int NO_WITNESS_COMMITMENT
Index marker for when no witness commitment is present in a coinbase transaction.
static int64_t GetBlockWeight(const CBlock &block)
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
constexpr int64_t MAX_BLOCK_SIGOPS_COST
The maximum allowed number of signature check operations in a block (network rule)
constexpr int64_t MAX_TIMEWARP
Maximum number of seconds that the timestamp of the first block of a difficulty adjustment period is ...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LogDebug(category,...)
is used externally by mining IPC clients, so it should only declare simple data definitions.
void RegenerateCommitments(CBlock &block, ChainstateManager &chainman)
Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed.
int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params &consensusParams, const CBlockIndex *pindexPrev)
BlockCreateOptions FlattenMiningOptions(BlockCreateOptions options)
Replace null optional values with their hardcoded defaults.
int64_t GetMinimumTime(const CBlockIndex *pindexPrev, const int64_t difficulty_adjustment_interval)
Get the minimum time a miner should use in the next block.
void AddMerkleRootAndCoinbase(CBlock &block, CTransactionRef coinbase, uint32_t version, uint32_t timestamp, uint32_t nonce)
Result< void > CheckMiningOptions(BlockCreateOptions options, bool use_argnames)
Check option values for validity.
bilingual_str ErrorString(const Result< T > &result)
static FeePerVSize ToFeePerVSize(FeePerWeight feerate)
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params ¶ms)
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
A mutable version of CTransaction.
Parameters that influence chain consensus.
Tagged wrapper around FeeFrac to avoid unit confusion.
static time_point now() noexcept
Return current system time or mocked time, if set.
Block template creation options.
Template containing all coinbase transaction fields that are set by our miner code.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
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.
constexpr unsigned MAX_CLUSTER_COUNT_LIMIT
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
BlockValidationState TestBlockValidity(Chainstate &chainstate, const CBlock &block, const bool check_pow, const bool check_merkle_root)
Verify a block, including transactions.