Skip to content

Commit fa18504

Browse files
author
MarcoFalke
committed
rpc: Add submit option to generateblock
1 parent fab9a08 commit fa18504

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
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: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

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)