@@ -115,9 +115,9 @@ static RPCHelpMan getnetworkhashps()
115115 };
116116}
117117
118- static bool GenerateBlock (ChainstateManager& chainman, CBlock& block, uint64_t & max_tries, uint256& block_hash )
118+ static bool GenerateBlock (ChainstateManager& chainman, CBlock& block, uint64_t & max_tries, std::shared_ptr< const CBlock>& block_out, bool process_new_block )
119119{
120- block_hash. SetNull ();
120+ block_out. reset ();
121121 block.hashMerkleRoot = BlockMerkleRoot (block);
122122
123123 while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t >::max () && !CheckProofOfWork (block.GetHash (), block.nBits , chainman.GetConsensus ()) && !ShutdownRequested ()) {
@@ -131,12 +131,14 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
131131 return true ;
132132 }
133133
134- std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
135- if (!chainman.ProcessNewBlock (shared_pblock, /* force_processing=*/ true , /* min_pow_checked=*/ true , nullptr )) {
134+ block_out = std::make_shared<const CBlock>(block);
135+
136+ if (!process_new_block) return true ;
137+
138+ if (!chainman.ProcessNewBlock (block_out, /* force_processing=*/ true , /* min_pow_checked=*/ true , nullptr )) {
136139 throw JSONRPCError (RPC_INTERNAL_ERROR, " ProcessNewBlock, block not accepted" );
137140 }
138141
139- block_hash = block.GetHash ();
140142 return true ;
141143}
142144
@@ -147,16 +149,15 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
147149 std::unique_ptr<CBlockTemplate> pblocktemplate (BlockAssembler{chainman.ActiveChainstate (), &mempool}.CreateNewBlock (coinbase_script));
148150 if (!pblocktemplate.get ())
149151 throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
150- CBlock *pblock = &pblocktemplate->block ;
151152
152- uint256 block_hash ;
153- if (!GenerateBlock (chainman, *pblock , nMaxTries, block_hash )) {
153+ std::shared_ptr< const CBlock> block_out ;
154+ if (!GenerateBlock (chainman, pblocktemplate-> block , nMaxTries, block_out, /* process_new_block= */ true )) {
154155 break ;
155156 }
156157
157- if (!block_hash. IsNull () ) {
158+ if (block_out ) {
158159 --nGenerate;
159- blockHashes.push_back (block_hash .GetHex ());
160+ blockHashes.push_back (block_out-> GetHash () .GetHex ());
160161 }
161162 }
162163 return blockHashes;
@@ -295,11 +296,13 @@ static RPCHelpMan generateblock()
295296 {" rawtx/txid" , RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, " " },
296297 },
297298 },
299+ {" submit" , RPCArg::Type::BOOL, RPCArg::Default{true }, " Whether to submit the block before the RPC call returns or to return it as hex." },
298300 },
299301 RPCResult{
300302 RPCResult::Type::OBJ, " " , " " ,
301303 {
302304 {RPCResult::Type::STR_HEX, " hash" , " hash of generated block" },
305+ {RPCResult::Type::STR_HEX, " hex" , /* optional=*/ true , " hex of generated block, only present when submit=false" },
303306 }
304307 },
305308 RPCExamples{
@@ -348,6 +351,7 @@ static RPCHelpMan generateblock()
348351 }
349352 }
350353
354+ const bool process_new_block{request.params [2 ].isNull () ? true : request.params [2 ].get_bool ()};
351355 CBlock block;
352356
353357 ChainstateManager& chainman = EnsureChainman (node);
@@ -376,15 +380,20 @@ static RPCHelpMan generateblock()
376380 }
377381 }
378382
379- uint256 block_hash ;
383+ std::shared_ptr< const CBlock> block_out ;
380384 uint64_t max_tries{DEFAULT_MAX_TRIES};
381385
382- if (!GenerateBlock (chainman, block, max_tries, block_hash ) || block_hash. IsNull () ) {
386+ if (!GenerateBlock (chainman, block, max_tries, block_out, process_new_block ) || !block_out ) {
383387 throw JSONRPCError (RPC_MISC_ERROR, " Failed to make block." );
384388 }
385389
386390 UniValue obj (UniValue::VOBJ);
387- obj.pushKV (" hash" , block_hash.GetHex ());
391+ obj.pushKV (" hash" , block_out->GetHash ().GetHex ());
392+ if (!process_new_block) {
393+ CDataStream block_ser{SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags ()};
394+ block_ser << *block_out;
395+ obj.pushKV (" hex" , HexStr (block_ser));
396+ }
388397 return obj;
389398},
390399 };
0 commit comments