Skip to content

Commit 8acfb1f

Browse files
committed
Merge bitcoin/bitcoin#18933: rpc: Add submit option to generateblock
fa18504 rpc: Add submit option to generateblock (MarcoFalke) fab9a08 refactor: Replace block_hash with block_out (MarcoFalke) Pull request description: When submit is turned off, a block can be generated and returned as hex, to be used for further tests. For example, it can be submitted on a different node, on a different interface (like p2p), or just never submitted and be used for other testing purposes. ACKs for top commit: instagibbs: ACK fa18504 TheCharlatan: tACK fa18504 Tree-SHA512: 1b2ab6b71bb7e155c6482d75f5373f4e77de6446cb16bc2dfd19e7a4075b3a6ad87d7ad7a049a9eed934cb71574acfd27202f54c8bb3b03fac869f2e95db7ee5
2 parents f380bb9 + fa18504 commit 8acfb1f

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
3737
{ "generatetodescriptor", 0, "num_blocks" },
3838
{ "generatetodescriptor", 2, "maxtries" },
3939
{ "generateblock", 1, "transactions" },
40+
{ "generateblock", 2, "submit" },
4041
{ "getnetworkhashps", 0, "nblocks" },
4142
{ "getnetworkhashps", 1, "height" },
4243
{ "sendtoaddress", 1, "amount" },

src/rpc/mining.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

test/functional/rpc_generate.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ def test_generateblock(self):
2929
node = self.nodes[0]
3030
miniwallet = MiniWallet(node)
3131

32-
self.log.info('Generate an empty block to address')
32+
self.log.info('Mine an empty block to address and return the hex')
3333
address = miniwallet.get_address()
34+
generated_block = self.generateblock(node, output=address, transactions=[], submit=False)
35+
node.submitblock(hexdata=generated_block['hex'])
36+
assert_equal(generated_block['hash'], node.getbestblockhash())
37+
38+
self.log.info('Generate an empty block to address')
3439
hash = self.generateblock(node, output=address, transactions=[])['hash']
3540
block = node.getblock(blockhash=hash, verbose=2)
3641
assert_equal(len(block['tx']), 1)

0 commit comments

Comments
 (0)