| 118 | } |
| 119 | |
| 120 | std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, std::chrono::seconds min_tx_age, DynaFedParamEntry* proposed_entry, const std::vector<CScript>* commit_scripts) |
| 121 | { |
| 122 | assert(min_tx_age >= std::chrono::seconds(0)); |
| 123 | int64_t nTimeStart = GetTimeMicros(); |
| 124 | |
| 125 | resetBlock(); |
| 126 | |
| 127 | pblocktemplate.reset(new CBlockTemplate()); |
| 128 | |
| 129 | if (!pblocktemplate.get()) { |
| 130 | return nullptr; |
| 131 | } |
| 132 | CBlock* const pblock = &pblocktemplate->block; // pointer for convenience |
| 133 | |
| 134 | |
| 135 | // Add dummy coinbase tx as first transaction |
| 136 | pblock->vtx.emplace_back(); |
| 137 | pblocktemplate->vTxFees.push_back(-1); // updated at end |
| 138 | pblocktemplate->vTxSigOpsCost.push_back(-1); // updated at end |
| 139 | |
| 140 | LOCK2(cs_main, m_mempool.cs); |
| 141 | CBlockIndex* pindexPrev = m_chainstate.m_chain.Tip(); |
| 142 | assert(pindexPrev != nullptr); |
| 143 | nHeight = pindexPrev->nHeight + 1; |
| 144 | |
| 145 | pblock->nVersion = g_versionbitscache.ComputeBlockVersion(pindexPrev, chainparams.GetConsensus()); |
| 146 | // -regtest only: allow overriding block.nVersion with |
| 147 | // -blockversion=N to test forking scenarios |
| 148 | if (chainparams.MineBlocksOnDemand()) { |
| 149 | pblock->nVersion = gArgs.GetIntArg("-blockversion", pblock->nVersion); |
| 150 | } |
| 151 | |
| 152 | pblock->nTime = GetAdjustedTime(); |
| 153 | m_lock_time_cutoff = pindexPrev->GetMedianTimePast(); |
| 154 | |
| 155 | // Decide whether to include witness transactions |
| 156 | // This is only needed in case the witness softfork activation is reverted |
| 157 | // (which would require a very deep reorganization). |
| 158 | // Note that the mempool would accept transactions with witness data before |
| 159 | // the deployment is active, but we would only ever mine blocks after activation |
| 160 | // unless there is a massive block reorganization with the witness softfork |
| 161 | // not activated. |
| 162 | // TODO: replace this with a call to main to assess validity of a mempool |
| 163 | // transaction (which in most cases can be a no-op). |
| 164 | fIncludeWitness = DeploymentActiveAfter(pindexPrev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_SEGWIT); |
| 165 | |
| 166 | if (DeploymentActiveAfter(pindexPrev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_DYNA_FED)) { |
| 167 | const DynaFedParamEntry current_params = ComputeNextBlockCurrentParameters(m_chainstate.m_chain.Tip(), chainparams.GetConsensus()); |
| 168 | const DynaFedParams block_params(current_params, proposed_entry ? *proposed_entry : DynaFedParamEntry()); |
| 169 | pblock->m_dynafed_params = block_params; |
| 170 | nBlockWeight += ::GetSerializeSize(block_params, PROTOCOL_VERSION)*WITNESS_SCALE_FACTOR; |
| 171 | nBlockWeight += current_params.m_signblock_witness_limit; // Note witness discount |
| 172 | assert(pblock->proof.IsNull()); |
| 173 | |
| 174 | } else if (g_signed_blocks) { |
| 175 | // Old style signed blocks |
| 176 | // Pad block weight by block proof fields (including upper-bound of signature) |
| 177 | nBlockWeight += chainparams.GetConsensus().signblockscript.size() * WITNESS_SCALE_FACTOR; |