@@ -115,7 +115,7 @@ static RPCHelpMan getnetworkhashps()
115115 };
116116}
117117
118- static bool GenerateBlock (ChainstateManager& chainman, CBlock& block, uint64_t & max_tries, std::shared_ptr<const CBlock>& block_out)
118+ static bool GenerateBlock (ChainstateManager& chainman, CBlock& block, uint64_t & max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block )
119119{
120120 block_out.reset ();
121121 block.hashMerkleRoot = BlockMerkleRoot (block);
@@ -132,6 +132,9 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
132132 }
133133
134134 block_out = std::make_shared<const CBlock>(block);
135+
136+ if (!process_new_block) return true ;
137+
135138 if (!chainman.ProcessNewBlock (block_out, /* force_processing=*/ true , /* min_pow_checked=*/ true , nullptr )) {
136139 throw JSONRPCError (RPC_INTERNAL_ERROR, " ProcessNewBlock, block not accepted" );
137140 }
@@ -148,7 +151,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
148151 throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
149152
150153 std::shared_ptr<const CBlock> block_out;
151- if (!GenerateBlock (chainman, pblocktemplate->block , nMaxTries, block_out)) {
154+ if (!GenerateBlock (chainman, pblocktemplate->block , nMaxTries, block_out, /* process_new_block= */ true )) {
152155 break ;
153156 }
154157
@@ -293,11 +296,13 @@ static RPCHelpMan generateblock()
293296 {" rawtx/txid" , RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, " " },
294297 },
295298 },
299+ {" submit" , RPCArg::Type::BOOL, RPCArg::Default{true }, " Whether to submit the block before the RPC call returns or to return it as hex." },
296300 },
297301 RPCResult{
298302 RPCResult::Type::OBJ, " " , " " ,
299303 {
300304 {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" },
301306 }
302307 },
303308 RPCExamples{
@@ -346,6 +351,7 @@ static RPCHelpMan generateblock()
346351 }
347352 }
348353
354+ const bool process_new_block{request.params [2 ].isNull () ? true : request.params [2 ].get_bool ()};
349355 CBlock block;
350356
351357 ChainstateManager& chainman = EnsureChainman (node);
@@ -377,12 +383,17 @@ static RPCHelpMan generateblock()
377383 std::shared_ptr<const CBlock> block_out;
378384 uint64_t max_tries{DEFAULT_MAX_TRIES};
379385
380- if (!GenerateBlock (chainman, block, max_tries, block_out) || !block_out) {
386+ if (!GenerateBlock (chainman, block, max_tries, block_out, process_new_block ) || !block_out) {
381387 throw JSONRPCError (RPC_MISC_ERROR, " Failed to make block." );
382388 }
383389
384390 UniValue obj (UniValue::VOBJ);
385391 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+ }
386397 return obj;
387398},
388399 };
0 commit comments