@@ -115,9 +115,9 @@ static RPCHelpMan getnetworkhashps()
115
115
};
116
116
}
117
117
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 )
119
119
{
120
- block_hash. SetNull ();
120
+ block_out. reset ();
121
121
block.hashMerkleRoot = BlockMerkleRoot (block);
122
122
123
123
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&
131
131
return true ;
132
132
}
133
133
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 )) {
136
139
throw JSONRPCError (RPC_INTERNAL_ERROR, " ProcessNewBlock, block not accepted" );
137
140
}
138
141
139
- block_hash = block.GetHash ();
140
142
return true ;
141
143
}
142
144
@@ -147,16 +149,15 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
147
149
std::unique_ptr<CBlockTemplate> pblocktemplate (BlockAssembler{chainman.ActiveChainstate (), &mempool}.CreateNewBlock (coinbase_script));
148
150
if (!pblocktemplate.get ())
149
151
throw JSONRPCError (RPC_INTERNAL_ERROR, " Couldn't create new block" );
150
- CBlock *pblock = &pblocktemplate->block ;
151
152
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 )) {
154
155
break ;
155
156
}
156
157
157
- if (!block_hash. IsNull () ) {
158
+ if (block_out ) {
158
159
--nGenerate;
159
- blockHashes.push_back (block_hash .GetHex ());
160
+ blockHashes.push_back (block_out-> GetHash () .GetHex ());
160
161
}
161
162
}
162
163
return blockHashes;
@@ -295,11 +296,13 @@ static RPCHelpMan generateblock()
295
296
{" rawtx/txid" , RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, " " },
296
297
},
297
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." },
298
300
},
299
301
RPCResult{
300
302
RPCResult::Type::OBJ, " " , " " ,
301
303
{
302
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" },
303
306
}
304
307
},
305
308
RPCExamples{
@@ -348,6 +351,7 @@ static RPCHelpMan generateblock()
348
351
}
349
352
}
350
353
354
+ const bool process_new_block{request.params [2 ].isNull () ? true : request.params [2 ].get_bool ()};
351
355
CBlock block;
352
356
353
357
ChainstateManager& chainman = EnsureChainman (node);
@@ -376,15 +380,20 @@ static RPCHelpMan generateblock()
376
380
}
377
381
}
378
382
379
- uint256 block_hash ;
383
+ std::shared_ptr< const CBlock> block_out ;
380
384
uint64_t max_tries{DEFAULT_MAX_TRIES};
381
385
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 ) {
383
387
throw JSONRPCError (RPC_MISC_ERROR, " Failed to make block." );
384
388
}
385
389
386
390
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
+ }
388
397
return obj;
389
398
},
390
399
};
0 commit comments