@@ -115,7 +115,7 @@ static RPCHelpMan getnetworkhashps()
115
115
};
116
116
}
117
117
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 )
119
119
{
120
120
block_out.reset ();
121
121
block.hashMerkleRoot = BlockMerkleRoot (block);
@@ -132,6 +132,9 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
132
132
}
133
133
134
134
block_out = std::make_shared<const CBlock>(block);
135
+
136
+ if (!process_new_block) return true ;
137
+
135
138
if (!chainman.ProcessNewBlock (block_out, /* force_processing=*/ true , /* min_pow_checked=*/ true , nullptr )) {
136
139
throw JSONRPCError (RPC_INTERNAL_ERROR, " ProcessNewBlock, block not accepted" );
137
140
}
@@ -148,7 +151,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
148
151
throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
149
152
150
153
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 )) {
152
155
break ;
153
156
}
154
157
@@ -293,11 +296,13 @@ static RPCHelpMan generateblock()
293
296
{" rawtx/txid" , RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, " " },
294
297
},
295
298
},
299
+ {" submit" , RPCArg::Type::BOOL, RPCArg::Default{true }, " Whether to submit the block before the RPC call returns or to return it as hex." },
296
300
},
297
301
RPCResult{
298
302
RPCResult::Type::OBJ, " " , " " ,
299
303
{
300
304
{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" },
301
306
}
302
307
},
303
308
RPCExamples{
@@ -346,6 +351,7 @@ static RPCHelpMan generateblock()
346
351
}
347
352
}
348
353
354
+ const bool process_new_block{request.params [2 ].isNull () ? true : request.params [2 ].get_bool ()};
349
355
CBlock block;
350
356
351
357
ChainstateManager& chainman = EnsureChainman (node);
@@ -377,12 +383,17 @@ static RPCHelpMan generateblock()
377
383
std::shared_ptr<const CBlock> block_out;
378
384
uint64_t max_tries{DEFAULT_MAX_TRIES};
379
385
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) {
381
387
throw JSONRPCError (RPC_MISC_ERROR, " Failed to make block." );
382
388
}
383
389
384
390
UniValue obj (UniValue::VOBJ);
385
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
+ }
386
397
return obj;
387
398
},
388
399
};
0 commit comments