MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / CreateNewBlock

Method CreateNewBlock

src/node/miner.cpp:126–244  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

124}
125
126std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock()
127{
128 const auto time_start{SteadyClock::now()};
129
130 resetBlock();
131
132 pblocktemplate.reset(new CBlockTemplate());
133 CBlock* const pblock = &pblocktemplate->block; // pointer for convenience
134
135 // Add dummy coinbase tx as first transaction. It is skipped by the
136 // getblocktemplate RPC and mining interface consumers must not use it.
137 pblock->vtx.emplace_back();
138
139 LOCK(::cs_main);
140 CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip();
141 assert(pindexPrev != nullptr);
142 nHeight = pindexPrev->nHeight + 1;
143
144 pblock->nVersion = m_chainstate.m_chainman.m_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus());
145 // -regtest only: allow overriding block.nVersion with
146 // -blockversion=N to test forking scenarios
147 if (chainparams.MineBlocksOnDemand()) {
148 pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion);
149 }
150
151 pblock->nTime = TicksSinceEpoch<std::chrono::seconds>(NodeClock::now());
152 m_lock_time_cutoff = pindexPrev->GetMedianTimePast();
153
154 if (m_mempool) {
155 LOCK(m_mempool->cs);
156 m_mempool->StartBlockBuilding();
157 addChunks();
158 m_mempool->StopBlockBuilding();
159 }
160
161 const auto time_1{SteadyClock::now()};
162
163 m_last_block_num_txs = nBlockTx;
164 m_last_block_weight = nBlockWeight;
165
166 // Create coinbase transaction.
167 CMutableTransaction coinbaseTx;
168
169 // Construct coinbase transaction struct in parallel
170 CoinbaseTx& coinbase_tx{pblocktemplate->m_coinbase_tx};
171 coinbase_tx.version = coinbaseTx.version;
172
173 coinbaseTx.vin.resize(1);
174 coinbaseTx.vin[0].prevout.SetNull();
175 coinbaseTx.vin[0].nSequence = CTxIn::MAX_SEQUENCE_NONFINAL; // Make sure timelock is enforced.
176 coinbase_tx.sequence = coinbaseTx.vin[0].nSequence;
177
178 // Add an output that spends the full coinbase reward.
179 coinbaseTx.vout.resize(1);
180 coinbaseTx.vout[0].scriptPubKey = m_options.coinbase_output_script;
181 // Block subsidy + fees
182 const CAmount block_reward{nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus())};
183 coinbaseTx.vout[0].nValue = block_reward;

Callers 4

TestPackageSelectionMethod · 0.80
FinishFunction · 0.80
WaitAndCreateNewBlockFunction · 0.80
createNewBlockMethod · 0.80

Calls 15

GetBlockSubsidyFunction · 0.85
MakeTransactionRefFunction · 0.85
GetBlockWeightFunction · 0.85
UpdateTimeFunction · 0.85
GetNextWorkRequiredFunction · 0.85
TestBlockValidityFunction · 0.85
ComputeBlockVersionMethod · 0.80
MineBlocksOnDemandMethod · 0.80
GetMedianTimePastMethod · 0.80
uint256Class · 0.70

Tested by 2

TestPackageSelectionMethod · 0.64
FinishFunction · 0.64