aboutsummaryrefslogtreecommitdiff
path: root/src/rpc
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2023-03-23 13:28:07 +0000
committerfanquake <fanquake@gmail.com>2023-03-23 13:40:30 +0000
commit8acfb1f8e045677b04cf8c4aa790dc6b90d93fa1 (patch)
tree0b6ab01e0d8b0201d41823da86ed10545c0616a8 /src/rpc
parentf380bb93e854b24cec4ef020235340f5186deded (diff)
parentfa18504d5767a40dc9827fb081633219bf251001 (diff)
Merge bitcoin/bitcoin#18933: rpc: Add submit option to generateblock
fa18504d5767a40dc9827fb081633219bf251001 rpc: Add submit option to generateblock (MarcoFalke) fab9a08e145dc5a1d9576bf062473f1095b56a16 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 fa18504d5767a40dc9827fb081633219bf251001 TheCharlatan: tACK fa18504d5767a40dc9827fb081633219bf251001 Tree-SHA512: 1b2ab6b71bb7e155c6482d75f5373f4e77de6446cb16bc2dfd19e7a4075b3a6ad87d7ad7a049a9eed934cb71574acfd27202f54c8bb3b03fac869f2e95db7ee5
Diffstat (limited to 'src/rpc')
-rw-r--r--src/rpc/client.cpp1
-rw-r--r--src/rpc/mining.cpp35
2 files changed, 23 insertions, 13 deletions
diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp
index 9449b9d197..4459dd71aa 100644
--- a/src/rpc/client.cpp
+++ b/src/rpc/client.cpp
@@ -37,6 +37,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "generatetodescriptor", 0, "num_blocks" },
{ "generatetodescriptor", 2, "maxtries" },
{ "generateblock", 1, "transactions" },
+ { "generateblock", 2, "submit" },
{ "getnetworkhashps", 0, "nblocks" },
{ "getnetworkhashps", 1, "height" },
{ "sendtoaddress", 1, "amount" },
diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp
index 8753f845a5..d55e20ba5e 100644
--- a/src/rpc/mining.cpp
+++ b/src/rpc/mining.cpp
@@ -115,9 +115,9 @@ static RPCHelpMan getnetworkhashps()
};
}
-static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, uint256& block_hash)
+static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
{
- block_hash.SetNull();
+ block_out.reset();
block.hashMerkleRoot = BlockMerkleRoot(block);
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&
return true;
}
- std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
- if (!chainman.ProcessNewBlock(shared_pblock, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
+ block_out = std::make_shared<const CBlock>(block);
+
+ if (!process_new_block) return true;
+
+ if (!chainman.ProcessNewBlock(block_out, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
}
- block_hash = block.GetHash();
return true;
}
@@ -147,16 +149,15 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), &mempool}.CreateNewBlock(coinbase_script));
if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
- CBlock *pblock = &pblocktemplate->block;
- uint256 block_hash;
- if (!GenerateBlock(chainman, *pblock, nMaxTries, block_hash)) {
+ std::shared_ptr<const CBlock> block_out;
+ if (!GenerateBlock(chainman, pblocktemplate->block, nMaxTries, block_out, /*process_new_block=*/true)) {
break;
}
- if (!block_hash.IsNull()) {
+ if (block_out) {
--nGenerate;
- blockHashes.push_back(block_hash.GetHex());
+ blockHashes.push_back(block_out->GetHash().GetHex());
}
}
return blockHashes;
@@ -295,11 +296,13 @@ static RPCHelpMan generateblock()
{"rawtx/txid", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
},
},
+ {"submit", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to submit the block before the RPC call returns or to return it as hex."},
},
RPCResult{
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "hash", "hash of generated block"},
+ {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "hex of generated block, only present when submit=false"},
}
},
RPCExamples{
@@ -348,6 +351,7 @@ static RPCHelpMan generateblock()
}
}
+ const bool process_new_block{request.params[2].isNull() ? true : request.params[2].get_bool()};
CBlock block;
ChainstateManager& chainman = EnsureChainman(node);
@@ -376,15 +380,20 @@ static RPCHelpMan generateblock()
}
}
- uint256 block_hash;
+ std::shared_ptr<const CBlock> block_out;
uint64_t max_tries{DEFAULT_MAX_TRIES};
- if (!GenerateBlock(chainman, block, max_tries, block_hash) || block_hash.IsNull()) {
+ if (!GenerateBlock(chainman, block, max_tries, block_out, process_new_block) || !block_out) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
}
UniValue obj(UniValue::VOBJ);
- obj.pushKV("hash", block_hash.GetHex());
+ obj.pushKV("hash", block_out->GetHash().GetHex());
+ if (!process_new_block) {
+ CDataStream block_ser{SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()};
+ block_ser << *block_out;
+ obj.pushKV("hex", HexStr(block_ser));
+ }
return obj;
},
};