4545#include < memory>
4646#include < stdint.h>
4747
48- using node::BlockAssembler;
49- using node::CBlockTemplate;
48+ using interfaces::BlockTemplate;
5049using interfaces::Mining;
50+ using node::BlockAssembler;
5151using node::NodeContext;
5252using node::RegenerateCommitments;
5353using node::UpdateTime;
@@ -130,7 +130,7 @@ static RPCHelpMan getnetworkhashps()
130130 };
131131}
132132
133- static bool GenerateBlock (ChainstateManager& chainman, Mining& miner, CBlock& block, uint64_t & max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
133+ static bool GenerateBlock (ChainstateManager& chainman, Mining& miner, CBlock&& block, uint64_t & max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
134134{
135135 block_out.reset ();
136136 block.hashMerkleRoot = BlockMerkleRoot (block);
@@ -146,7 +146,7 @@ static bool GenerateBlock(ChainstateManager& chainman, Mining& miner, CBlock& bl
146146 return true ;
147147 }
148148
149- block_out = std::make_shared<const CBlock>(block);
149+ block_out = std::make_shared<const CBlock>(std::move ( block) );
150150
151151 if (!process_new_block) return true ;
152152
@@ -161,12 +161,11 @@ static UniValue generateBlocks(ChainstateManager& chainman, Mining& miner, const
161161{
162162 UniValue blockHashes (UniValue::VARR);
163163 while (nGenerate > 0 && !chainman.m_interrupt ) {
164- std::unique_ptr<CBlockTemplate> pblocktemplate (miner.createNewBlock (coinbase_script));
165- if (!pblocktemplate.get ())
166- throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
164+ std::unique_ptr<BlockTemplate> block_template (miner.createNewBlock (coinbase_script));
165+ CHECK_NONFATAL (block_template);
167166
168167 std::shared_ptr<const CBlock> block_out;
169- if (!GenerateBlock (chainman, miner, pblocktemplate-> block , nMaxTries, block_out, /* process_new_block=*/ true )) {
168+ if (!GenerateBlock (chainman, miner, block_template-> getBlock () , nMaxTries, block_out, /* process_new_block=*/ true )) {
170169 break ;
171170 }
172171
@@ -371,11 +370,10 @@ static RPCHelpMan generateblock()
371370
372371 ChainstateManager& chainman = EnsureChainman (node);
373372 {
374- std::unique_ptr<CBlockTemplate> blocktemplate{miner.createNewBlock (coinbase_script, {.use_mempool = false })};
375- if (!blocktemplate) {
376- throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
377- }
378- block = blocktemplate->block ;
373+ std::unique_ptr<BlockTemplate> block_template{miner.createNewBlock (coinbase_script, {.use_mempool = false })};
374+ CHECK_NONFATAL (block_template);
375+
376+ block = block_template->getBlock ();
379377 }
380378
381379 CHECK_NONFATAL (block.vtx .size () == 1 );
@@ -394,7 +392,7 @@ static RPCHelpMan generateblock()
394392 std::shared_ptr<const CBlock> block_out;
395393 uint64_t max_tries{DEFAULT_MAX_TRIES};
396394
397- if (!GenerateBlock (chainman, miner, block, max_tries, block_out, process_new_block) || !block_out) {
395+ if (!GenerateBlock (chainman, miner, std::move ( block) , max_tries, block_out, process_new_block) || !block_out) {
398396 throw JSONRPCError (RPC_MISC_ERROR, " Failed to make block." );
399397 }
400398
@@ -800,7 +798,7 @@ static RPCHelpMan getblocktemplate()
800798 // Update block
801799 static CBlockIndex* pindexPrev;
802800 static int64_t time_start;
803- static std::unique_ptr<CBlockTemplate> pblocktemplate ;
801+ static std::unique_ptr<BlockTemplate> block_template ;
804802 if (!pindexPrev || pindexPrev->GetBlockHash () != tip ||
805803 (miner.getTransactionsUpdated () != nTransactionsUpdatedLast && GetTime () - time_start > 5 ))
806804 {
@@ -814,20 +812,19 @@ static RPCHelpMan getblocktemplate()
814812
815813 // Create new block
816814 CScript scriptDummy = CScript () << OP_TRUE;
817- pblocktemplate = miner.createNewBlock (scriptDummy);
818- if (!pblocktemplate) {
819- throw JSONRPCError (RPC_OUT_OF_MEMORY, " Out of memory" );
820- }
815+ block_template = miner.createNewBlock (scriptDummy);
816+ CHECK_NONFATAL (block_template);
817+
821818
822819 // Need to update only after we know createNewBlock succeeded
823820 pindexPrev = pindexPrevNew;
824821 }
825822 CHECK_NONFATAL (pindexPrev);
826- CBlock* pblock = &pblocktemplate-> block ; // pointer for convenience
823+ CBlock block{block_template-> getBlock ()};
827824
828825 // Update nTime
829- UpdateTime (pblock , consensusParams, pindexPrev);
830- pblock-> nNonce = 0 ;
826+ UpdateTime (&block , consensusParams, pindexPrev);
827+ block. nNonce = 0 ;
831828
832829 // NOTE: If at some point we support pre-segwit miners post-segwit-activation, this needs to take segwit support into consideration
833830 const bool fPreSegWit = !DeploymentActiveAfter (pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT);
@@ -836,8 +833,11 @@ static RPCHelpMan getblocktemplate()
836833
837834 UniValue transactions (UniValue::VARR);
838835 std::map<uint256, int64_t > setTxIndex;
836+ std::vector<CAmount> tx_fees{block_template->getTxFees ()};
837+ std::vector<CAmount> tx_sigops{block_template->getTxSigops ()};
838+
839839 int i = 0 ;
840- for (const auto & it : pblock-> vtx ) {
840+ for (const auto & it : block. vtx ) {
841841 const CTransaction& tx = *it;
842842 uint256 txHash = tx.GetHash ();
843843 setTxIndex[txHash] = i++;
@@ -860,8 +860,8 @@ static RPCHelpMan getblocktemplate()
860860 entry.pushKV (" depends" , std::move (deps));
861861
862862 int index_in_template = i - 1 ;
863- entry.pushKV (" fee" , pblocktemplate-> vTxFees [ index_in_template] );
864- int64_t nTxSigOps = pblocktemplate-> vTxSigOpsCost [ index_in_template] ;
863+ entry.pushKV (" fee" , tx_fees. at ( index_in_template) );
864+ int64_t nTxSigOps{tx_sigops. at ( index_in_template)} ;
865865 if (fPreSegWit ) {
866866 CHECK_NONFATAL (nTxSigOps % WITNESS_SCALE_FACTOR == 0 );
867867 nTxSigOps /= WITNESS_SCALE_FACTOR;
@@ -874,7 +874,7 @@ static RPCHelpMan getblocktemplate()
874874
875875 UniValue aux (UniValue::VOBJ);
876876
877- arith_uint256 hashTarget = arith_uint256 ().SetCompact (pblock-> nBits );
877+ arith_uint256 hashTarget = arith_uint256 ().SetCompact (block. nBits );
878878
879879 UniValue aMutable (UniValue::VARR);
880880 aMutable.push_back (" time" );
@@ -904,7 +904,7 @@ static RPCHelpMan getblocktemplate()
904904 break ;
905905 case ThresholdState::LOCKED_IN:
906906 // Ensure bit is set in block version
907- pblock-> nVersion |= chainman.m_versionbitscache .Mask (consensusParams, pos);
907+ block. nVersion |= chainman.m_versionbitscache .Mask (consensusParams, pos);
908908 [[fallthrough]];
909909 case ThresholdState::STARTED:
910910 {
@@ -913,7 +913,7 @@ static RPCHelpMan getblocktemplate()
913913 if (setClientRules.find (vbinfo.name ) == setClientRules.end ()) {
914914 if (!vbinfo.gbt_force ) {
915915 // If the client doesn't support this, don't indicate it in the [default] version
916- pblock-> nVersion &= ~chainman.m_versionbitscache .Mask (consensusParams, pos);
916+ block. nVersion &= ~chainman.m_versionbitscache .Mask (consensusParams, pos);
917917 }
918918 }
919919 break ;
@@ -933,15 +933,15 @@ static RPCHelpMan getblocktemplate()
933933 }
934934 }
935935 }
936- result.pushKV (" version" , pblock-> nVersion );
936+ result.pushKV (" version" , block. nVersion );
937937 result.pushKV (" rules" , std::move (aRules));
938938 result.pushKV (" vbavailable" , std::move (vbavailable));
939939 result.pushKV (" vbrequired" , int (0 ));
940940
941- result.pushKV (" previousblockhash" , pblock-> hashPrevBlock .GetHex ());
941+ result.pushKV (" previousblockhash" , block. hashPrevBlock .GetHex ());
942942 result.pushKV (" transactions" , std::move (transactions));
943943 result.pushKV (" coinbaseaux" , std::move (aux));
944- result.pushKV (" coinbasevalue" , (int64_t )pblock-> vtx [0 ]->vout [0 ].nValue );
944+ result.pushKV (" coinbasevalue" , (int64_t )block. vtx [0 ]->vout [0 ].nValue );
945945 result.pushKV (" longpollid" , tip.GetHex () + ToString (nTransactionsUpdatedLast));
946946 result.pushKV (" target" , hashTarget.GetHex ());
947947 result.pushKV (" mintime" , (int64_t )pindexPrev->GetMedianTimePast ()+1 );
@@ -960,16 +960,16 @@ static RPCHelpMan getblocktemplate()
960960 if (!fPreSegWit ) {
961961 result.pushKV (" weightlimit" , (int64_t )MAX_BLOCK_WEIGHT);
962962 }
963- result.pushKV (" curtime" , pblock-> GetBlockTime ());
964- result.pushKV (" bits" , strprintf (" %08x" , pblock-> nBits ));
963+ result.pushKV (" curtime" , block. GetBlockTime ());
964+ result.pushKV (" bits" , strprintf (" %08x" , block. nBits ));
965965 result.pushKV (" height" , (int64_t )(pindexPrev->nHeight +1 ));
966966
967967 if (consensusParams.signet_blocks ) {
968968 result.pushKV (" signet_challenge" , HexStr (consensusParams.signet_challenge ));
969969 }
970970
971- if (!pblocktemplate-> vchCoinbaseCommitment .empty ()) {
972- result.pushKV (" default_witness_commitment" , HexStr (pblocktemplate-> vchCoinbaseCommitment ));
971+ if (!block_template-> getCoinbaseCommitment () .empty ()) {
972+ result.pushKV (" default_witness_commitment" , HexStr (block_template-> getCoinbaseCommitment () ));
973973 }
974974
975975 return result;
0 commit comments