Branch data Line data Source code
1 : : // Copyright (c) 2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <bitcoin-build-config.h> // IWYU pragma: keep
7 : :
8 : : #include <rpc/register.h> // IWYU pragma: associated
9 : :
10 : : #include <addresstype.h>
11 : : #include <arith_uint256.h>
12 : : #include <chain.h>
13 : : #include <chainparams.h>
14 : : #include <chainparamsbase.h>
15 : : #include <consensus/amount.h>
16 : : #include <consensus/consensus.h>
17 : : #include <consensus/merkle.h>
18 : : #include <consensus/params.h>
19 : : #include <consensus/validation.h>
20 : : #include <core_io.h>
21 : : #include <crypto/hex_base.h>
22 : : #include <interfaces/types.h>
23 : : #include <key_io.h>
24 : : #include <net.h>
25 : : #include <netbase.h>
26 : : #include <node/block_template_manager.h>
27 : : #include <node/blockstorage.h>
28 : : #include <node/context.h>
29 : : #include <node/miner.h>
30 : : #include <node/mining_args.h>
31 : : #include <node/mining_types.h>
32 : : #include <node/warnings.h>
33 : : #include <policy/feerate.h>
34 : : #include <policy/policy.h>
35 : : #include <pow.h>
36 : : #include <primitives/block.h>
37 : : #include <primitives/transaction.h>
38 : : #include <rpc/blockchain.h>
39 : : #include <rpc/mining.h>
40 : : #include <rpc/protocol.h>
41 : : #include <rpc/request.h>
42 : : #include <rpc/server.h>
43 : : #include <rpc/server_util.h>
44 : : #include <rpc/util.h>
45 : : #include <script/descriptor.h>
46 : : #include <script/script.h>
47 : : #include <script/signingprovider.h>
48 : : #include <serialize.h>
49 : : #include <streams.h>
50 : : #include <sync.h>
51 : : #include <tinyformat.h>
52 : : #include <txmempool.h>
53 : : #include <uint256.h>
54 : : #include <univalue.h>
55 : : #include <util/chaintype.h>
56 : : #include <util/check.h>
57 : : #include <util/signalinterrupt.h>
58 : : #include <util/strencodings.h>
59 : : #include <util/string.h>
60 : : #include <util/time.h>
61 : : #include <validation.h>
62 : : #include <validationinterface.h>
63 : : #include <versionbits.h>
64 : :
65 : : #include <algorithm>
66 : : #include <cstddef>
67 : : #include <cstdint>
68 : : #include <functional>
69 : : #include <limits>
70 : : #include <map>
71 : : #include <memory>
72 : : #include <optional>
73 : : #include <set>
74 : : #include <span>
75 : : #include <string>
76 : : #include <string_view>
77 : : #include <utility>
78 : : #include <vector>
79 : :
80 : : using interfaces::BlockRef;
81 : : using node::BlockAssembler;
82 : : using node::GetMinimumTime;
83 : : using node::NodeContext;
84 : : using node::RegenerateCommitments;
85 : : using node::UpdateTime;
86 : : using util::ToString;
87 : :
88 : : /**
89 : : * Return average network hashes per second based on the last 'lookup' blocks,
90 : : * or from the last difficulty change if 'lookup' is -1.
91 : : * If 'height' is -1, compute the estimate from current chain tip.
92 : : * If 'height' is a valid block height, compute the estimate at the time when a given block was found.
93 : : */
94 : 43 : static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_chain) {
95 [ + + ]: 43 : if (lookup < -1 || lookup == 0) {
96 [ + - + - ]: 8 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid nblocks. Must be a positive number or -1.");
97 : : }
98 : :
99 [ + + + + ]: 76 : if (height < -1 || height > active_chain.Height()) {
100 [ + - + - ]: 8 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Block does not exist at specified height");
101 : : }
102 : :
103 [ + - ]: 35 : const CBlockIndex* pb = active_chain.Tip();
104 : :
105 [ + + ]: 35 : if (height >= 0) {
106 : 2 : pb = active_chain[height];
107 : : }
108 : :
109 [ + - + + ]: 35 : if (pb == nullptr || !pb->nHeight)
110 : 5 : return 0;
111 : :
112 : : // If lookup is -1, then use blocks since last difficulty change.
113 [ + + ]: 30 : if (lookup == -1)
114 : 2 : lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
115 : :
116 : : // If lookup is larger than chain, then set it to chain length.
117 [ + + ]: 30 : if (lookup > pb->nHeight)
118 : 2 : lookup = pb->nHeight;
119 : :
120 : 30 : const CBlockIndex* pb0 = pb;
121 : 30 : int64_t minTime = pb0->GetBlockTime();
122 : 30 : int64_t maxTime = minTime;
123 [ + + ]: 3538 : for (int i = 0; i < lookup; i++) {
124 : 3508 : pb0 = pb0->pprev;
125 [ + + ]: 3508 : int64_t time = pb0->GetBlockTime();
126 [ + + ]: 3508 : minTime = std::min(time, minTime);
127 [ + + ]: 3549 : maxTime = std::max(time, maxTime);
128 : : }
129 : :
130 : : // In case there's a situation where minTime == maxTime, we don't want a divide by zero exception.
131 [ - + ]: 30 : if (minTime == maxTime)
132 : 0 : return 0;
133 : :
134 : 30 : arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork;
135 : 30 : int64_t timeDiff = maxTime - minTime;
136 : :
137 : 30 : return workDiff.getdouble() / timeDiff;
138 : : }
139 : :
140 : 2627 : static RPCMethod getnetworkhashps()
141 : : {
142 : 2627 : return RPCMethod{
143 : 2627 : "getnetworkhashps",
144 [ + - ]: 5254 : "Returns the estimated network hashes per second based on the last n blocks.\n"
145 : : "Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change.\n"
146 : : "Pass in [height] to estimate the network speed at the time when a certain block was found.\n",
147 : : {
148 [ + - + - : 7881 : {"nblocks", RPCArg::Type::NUM, RPCArg::Default{120}, "The number of previous blocks to calculate estimate from, or -1 for blocks since last difficulty change."},
+ - ]
149 [ + - + - : 7881 : {"height", RPCArg::Type::NUM, RPCArg::Default{-1}, "To estimate at the time of the given height."},
+ - ]
150 : : },
151 [ + - ]: 5254 : RPCResult{
152 [ + - + - : 5254 : RPCResult::Type::NUM, "", "Hashes per second estimated"},
+ - ]
153 : 2627 : RPCExamples{
154 [ + - + - : 5254 : HelpExampleCli("getnetworkhashps", "")
+ - ]
155 [ + - + - : 10508 : + HelpExampleRpc("getnetworkhashps", "")
+ - + - ]
156 [ + - ]: 2627 : },
157 : 2627 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
158 : : {
159 : 43 : ChainstateManager& chainman = EnsureAnyChainman(request.context);
160 : 43 : LOCK(cs_main);
161 [ + - + - : 43 : return GetNetworkHashPS(self.Arg<int>("nblocks"), self.Arg<int>("height"), chainman.ActiveChain());
+ - + + ]
162 : 35 : },
163 [ + - + - : 15762 : };
+ + - - ]
164 [ + - + - : 10508 : }
- - ]
165 : :
166 : 37732 : static bool GenerateBlock(ChainstateManager& chainman, CBlock&& block, uint64_t& max_tries, std::shared_ptr<const CBlock>& block_out, bool process_new_block)
167 : : {
168 : 37732 : block_out.reset();
169 : 37732 : block.hashMerkleRoot = BlockMerkleRoot(block);
170 : :
171 [ + + + - : 1113707 : while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainman.GetConsensus()) && !chainman.m_interrupt) {
+ + + - ]
172 : 1038243 : ++block.nNonce;
173 : 1038243 : --max_tries;
174 : : }
175 [ + + + - ]: 37732 : if (max_tries == 0 || chainman.m_interrupt) {
176 : 1 : return false;
177 : : }
178 [ + - ]: 37731 : if (block.nNonce == std::numeric_limits<uint32_t>::max()) {
179 : : return true;
180 : : }
181 : :
182 [ - + ]: 37731 : block_out = std::make_shared<const CBlock>(std::move(block));
183 : :
184 [ + + ]: 37731 : if (!process_new_block) return true;
185 : :
186 [ - + ]: 37726 : if (!chainman.ProcessNewBlock(block_out, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr)) {
187 [ # # # # ]: 0 : throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
188 : : }
189 : :
190 : : return true;
191 : : }
192 : :
193 : 3088 : static UniValue generateBlocks(ChainstateManager& chainman, node::BlockTemplateManager& block_template_manager, const CScript& coinbase_output_script, int nGenerate, uint64_t nMaxTries)
194 : : {
195 : 3088 : UniValue blockHashes(UniValue::VARR);
196 [ + + + - : 40455 : while (nGenerate > 0 && !chainman.m_interrupt) {
+ - ]
197 : 37367 : std::unique_ptr<node::CBlockTemplate> block_template{block_template_manager.CreateNewTemplate({.coinbase_output_script = coinbase_output_script})};
198 [ + - ]: 37368 : CHECK_NONFATAL(block_template);
199 : :
200 : 37368 : std::shared_ptr<const CBlock> block_out;
201 [ + - + - : 37368 : if (!GenerateBlock(chainman, CBlock{block_template->block}, nMaxTries, block_out, /*process_new_block=*/true)) {
+ + ]
202 : : break;
203 : : }
204 : :
205 [ + - ]: 37367 : if (block_out) {
206 : 37367 : --nGenerate;
207 [ + - + - : 37367 : blockHashes.push_back(block_out->GetHash().GetHex());
+ - + - ]
208 : : }
209 : 37368 : }
210 : 3088 : return blockHashes;
211 [ + - ]: 37368 : }
212 : :
213 : 1202 : static bool getScriptFromDescriptor(std::string_view descriptor, CScript& script, std::string& error)
214 : : {
215 : 1202 : FlatSigningProvider key_provider;
216 [ + - ]: 1202 : const auto descs = Parse(descriptor, key_provider, error, /* require_checksum = */ false);
217 [ + + ]: 1202 : if (descs.empty()) return false;
218 [ - + - + ]: 869 : if (descs.size() > 1) {
219 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Multipath descriptor not accepted");
220 : : }
221 [ + - ]: 869 : const auto& desc = descs.at(0);
222 [ + - + + ]: 869 : if (desc->IsRange()) {
223 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptor not accepted. Maybe pass through deriveaddresses first?");
224 : : }
225 : :
226 : 868 : FlatSigningProvider provider;
227 : 868 : std::vector<CScript> scripts;
228 [ + - + + ]: 868 : if (!desc->Expand(0, key_provider, scripts, provider)) {
229 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Cannot derive script without private keys");
230 : : }
231 : :
232 : : // Combo descriptors can have 2 or 4 scripts, so we can't just check scripts.size() == 1
233 [ - + + - : 867 : CHECK_NONFATAL(scripts.size() > 0 && scripts.size() <= 4);
- + + - ]
234 : :
235 [ - + + + ]: 867 : if (scripts.size() == 1) {
236 [ + - ]: 865 : script = scripts.at(0);
237 [ + + ]: 2 : } else if (scripts.size() == 4) {
238 : : // For uncompressed keys, take the 3rd script, since it is p2wpkh
239 [ + - ]: 1 : script = scripts.at(2);
240 : : } else {
241 : : // Else take the 2nd script, since it is p2pkh
242 [ + - ]: 1 : script = scripts.at(1);
243 : : }
244 : :
245 : 867 : return true;
246 : 1206 : }
247 : :
248 : 3402 : static RPCMethod generatetodescriptor()
249 : : {
250 : 3402 : return RPCMethod{
251 : 3402 : "generatetodescriptor",
252 [ + - ]: 6804 : "Mine to a specified descriptor and return the block hashes.",
253 : : {
254 [ + - + - ]: 6804 : {"num_blocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated."},
255 [ + - + - ]: 6804 : {"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor to send the newly generated bitcoin to."},
256 [ + - + - : 10206 : {"maxtries", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_MAX_TRIES}, "How many iterations to try."},
+ - ]
257 : : },
258 [ + - ]: 6804 : RPCResult{
259 [ + - + - ]: 6804 : RPCResult::Type::ARR, "", "hashes of blocks generated",
260 : : {
261 [ + - + - ]: 6804 : {RPCResult::Type::STR_HEX, "", "blockhash"},
262 : : }
263 [ + - + - : 13608 : },
+ + - - ]
264 : 3402 : RPCExamples{
265 [ + - + - : 10206 : "\nGenerate 11 blocks to mydesc\n" + HelpExampleCli("generatetodescriptor", "11 \"mydesc\"")},
+ - + - +
- ]
266 : 3402 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
267 : : {
268 : 831 : const auto num_blocks{self.Arg<int>("num_blocks")};
269 : 831 : const auto max_tries{self.Arg<uint64_t>("maxtries")};
270 : :
271 : 831 : CScript coinbase_output_script;
272 [ + - ]: 831 : std::string error;
273 [ + - + - : 831 : if (!getScriptFromDescriptor(self.Arg<std::string_view>("descriptor"), coinbase_output_script, error)) {
- + ]
274 [ # # ]: 0 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
275 : : }
276 : :
277 [ + - ]: 831 : NodeContext& node = EnsureAnyNodeContext(request.context);
278 [ + - ]: 831 : ChainstateManager& chainman = EnsureChainman(node);
279 [ + - ]: 831 : node::BlockTemplateManager& block_template_manager = EnsureBlockTemplateManager(node);
280 : :
281 [ + - ]: 831 : return generateBlocks(chainman, block_template_manager, coinbase_output_script, num_blocks, max_tries);
282 : 831 : },
283 [ + - + - : 23814 : };
+ + - - ]
284 [ + - + - : 27216 : }
+ - + - -
- ]
285 : :
286 : 2573 : static RPCMethod generate()
287 : : {
288 [ + - + - ]: 10292 : return RPCMethod{"generate", "has been replaced by the -generate cli option. Refer to -help for more information.", {}, {}, RPCExamples{""}, [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue {
289 [ + - + - ]: 2 : throw JSONRPCError(RPC_METHOD_NOT_FOUND, self.ToString());
290 [ + - + - : 12865 : }};
+ - ]
291 : : }
292 : :
293 : 4831 : static RPCMethod generatetoaddress()
294 : : {
295 : 4831 : return RPCMethod{"generatetoaddress",
296 [ + - ]: 9662 : "Mine to a specified address and return the block hashes.",
297 : : {
298 [ + - + - ]: 9662 : {"nblocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated."},
299 [ + - + - ]: 9662 : {"address", RPCArg::Type::STR, RPCArg::Optional::NO, "The address to send the newly generated bitcoin to."},
300 [ + - + - : 14493 : {"maxtries", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_MAX_TRIES}, "How many iterations to try."},
+ - ]
301 : : },
302 [ + - ]: 9662 : RPCResult{
303 [ + - + - ]: 9662 : RPCResult::Type::ARR, "", "hashes of blocks generated",
304 : : {
305 [ + - + - ]: 9662 : {RPCResult::Type::STR_HEX, "", "blockhash"},
306 [ + - + - : 19324 : }},
+ + - - ]
307 : 4831 : RPCExamples{
308 : : "\nGenerate 11 blocks to myaddress\n"
309 [ + - + - : 9662 : + HelpExampleCli("generatetoaddress", "11 \"myaddress\"")
+ - + - ]
310 : 4831 : + "If you are using the " CLIENT_NAME " wallet, you can get a new address to send the newly generated bitcoin to with:\n"
311 [ + - + - : 19324 : + HelpExampleCli("getnewaddress", "")
+ - + - ]
312 [ + - ]: 4831 : },
313 : 4831 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
314 : : {
315 : 2259 : const int num_blocks{request.params[0].getInt<int>()};
316 [ + + ]: 2259 : const uint64_t max_tries{request.params[2].isNull() ? DEFAULT_MAX_TRIES : request.params[2].getInt<int>()};
317 : :
318 : 2259 : CTxDestination destination = DecodeDestination(request.params[1].get_str());
319 [ + - + + ]: 2259 : if (!IsValidDestination(destination)) {
320 [ + - + - ]: 4 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address");
321 : : }
322 : :
323 [ + - ]: 2257 : NodeContext& node = EnsureAnyNodeContext(request.context);
324 [ + - ]: 2257 : ChainstateManager& chainman = EnsureChainman(node);
325 [ + - ]: 2257 : node::BlockTemplateManager& block_template_manager = EnsureBlockTemplateManager(node);
326 : :
327 [ + - ]: 2257 : CScript coinbase_output_script = GetScriptForDestination(destination);
328 : :
329 [ + - ]: 2257 : return generateBlocks(chainman, block_template_manager, coinbase_output_script, num_blocks, max_tries);
330 : 2257 : },
331 [ + - + - : 33817 : };
+ + - - ]
332 [ + - + - : 38648 : }
+ - + - -
- ]
333 : :
334 : 2942 : static RPCMethod generateblock()
335 : : {
336 : 2942 : return RPCMethod{"generateblock",
337 [ + - ]: 5884 : "Mine a set of ordered transactions to a specified address or descriptor and return the block hash.\n"
338 : : "Transaction fees are not collected in the block reward.",
339 : : {
340 [ + - + - ]: 5884 : {"output", RPCArg::Type::STR, RPCArg::Optional::NO, "The address or descriptor to send the newly generated bitcoin to."},
341 [ + - + - ]: 5884 : {"transactions", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of hex strings which are either txids or raw transactions.\n"
342 : : "Txids must reference transactions currently in the mempool.\n"
343 : : "All transactions must be valid and in valid order, otherwise the block will be rejected.",
344 : : {
345 [ + - + - ]: 5884 : {"rawtx/txid", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
346 : : },
347 : : },
348 [ + - + - : 8826 : {"submit", RPCArg::Type::BOOL, RPCArg::Default{true}, "Whether to submit the block before the RPC call returns or to return it as hex."},
+ - ]
349 : : },
350 [ + - ]: 5884 : RPCResult{
351 [ + - + - ]: 5884 : RPCResult::Type::OBJ, "", "",
352 : : {
353 [ + - + - ]: 5884 : {RPCResult::Type::STR_HEX, "hash", "hash of generated block"},
354 [ + - + - ]: 5884 : {RPCResult::Type::STR_HEX, "hex", /*optional=*/true, "hex of generated block, only present when submit=false"},
355 : : }
356 [ + - + - : 17652 : },
+ + - - ]
357 : 2942 : RPCExamples{
358 : : "\nGenerate a block to myaddress, with txs rawtx and mempool_txid\n"
359 [ + - + - : 5884 : + HelpExampleCli("generateblock", R"("myaddress" '["rawtx", "mempool_txid"]')")
+ - + - ]
360 [ + - ]: 2942 : },
361 : 2942 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
362 : : {
363 [ - + ]: 371 : const auto address_or_descriptor = request.params[0].get_str();
364 : 371 : CScript coinbase_output_script;
365 [ - + ]: 371 : std::string error;
366 : :
367 [ - + + + : 371 : if (!getScriptFromDescriptor(address_or_descriptor, coinbase_output_script, error)) {
+ + ]
368 [ + - ]: 333 : const auto destination = DecodeDestination(address_or_descriptor);
369 [ + - + + ]: 333 : if (!IsValidDestination(destination)) {
370 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address or descriptor");
371 : : }
372 : :
373 [ + - ]: 664 : coinbase_output_script = GetScriptForDestination(destination);
374 : 333 : }
375 : :
376 [ + - ]: 368 : NodeContext& node = EnsureAnyNodeContext(request.context);
377 [ + - ]: 368 : node::BlockTemplateManager& block_template_manager = EnsureBlockTemplateManager(node);
378 [ + - ]: 368 : const CTxMemPool& mempool = EnsureMemPool(node);
379 : :
380 : 368 : std::vector<CTransactionRef> txs;
381 [ + - + - : 368 : const auto raw_txs_or_txids = request.params[1].get_array();
+ - ]
382 [ - + + + ]: 522 : for (size_t i = 0; i < raw_txs_or_txids.size(); i++) {
383 [ + - + - ]: 156 : const auto& str{raw_txs_or_txids[i].get_str()};
384 : :
385 [ + - ]: 156 : CMutableTransaction mtx;
386 [ - + + - : 156 : if (auto txid{Txid::FromHex(str)}) {
+ + ]
387 [ + - ]: 23 : const auto tx{mempool.get(*txid)};
388 [ + + ]: 23 : if (!tx) {
389 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Transaction %s not in mempool.", str));
390 : : }
391 : :
392 [ + - ]: 22 : txs.emplace_back(tx);
393 : :
394 [ + - + + ]: 156 : } else if (DecodeHexTx(mtx, str)) {
395 [ + - + - : 264 : txs.push_back(MakeTransactionRef(std::move(mtx)));
- + ]
396 : :
397 : : } else {
398 [ + - + - ]: 2 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, strprintf("Transaction decode failed for %s. Make sure the tx has at least one input.", str));
399 : : }
400 : 156 : }
401 : :
402 [ + - + + : 366 : const bool process_new_block{request.params[2].isNull() ? true : request.params[2].get_bool()};
+ - + - ]
403 : 366 : CBlock block;
404 : :
405 [ + - ]: 366 : ChainstateManager& chainman = EnsureChainman(node);
406 : 366 : {
407 [ + - ]: 366 : LOCK(chainman.GetMutex());
408 : 366 : {
409 : 0 : std::unique_ptr<node::CBlockTemplate> block_template{block_template_manager.CreateNewTemplate({.use_mempool = false, .coinbase_output_script = coinbase_output_script})};
410 [ + - ]: 366 : CHECK_NONFATAL(block_template);
411 : :
412 [ + - ]: 366 : block = block_template->block;
413 : 366 : }
414 : :
415 [ - + + - ]: 366 : CHECK_NONFATAL(block.vtx.size() == 1);
416 : :
417 : : // Add transactions
418 [ + - ]: 366 : block.vtx.insert(block.vtx.end(), txs.begin(), txs.end());
419 [ + - ]: 366 : RegenerateCommitments(block, chainman);
420 : :
421 [ + - + - : 366 : if (BlockValidationState state{TestBlockValidity(chainman.ActiveChainstate(), block, /*check_pow=*/false, /*check_merkle_root=*/false)}; !state.IsValid()) {
+ + ]
422 [ + - + - : 4 : throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
+ - ]
423 [ + - ]: 366 : }
424 : 2 : }
425 : :
426 : 364 : std::shared_ptr<const CBlock> block_out;
427 : 364 : uint64_t max_tries{DEFAULT_MAX_TRIES};
428 : :
429 [ + - + - : 364 : if (!GenerateBlock(chainman, std::move(block), max_tries, block_out, process_new_block) || !block_out) {
- + ]
430 [ # # # # ]: 0 : throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
431 : : }
432 : :
433 : 364 : UniValue obj(UniValue::VOBJ);
434 [ + - + - : 728 : obj.pushKV("hash", block_out->GetHash().GetHex());
+ - + - +
- ]
435 [ + + ]: 364 : if (!process_new_block) {
436 : 5 : DataStream block_ser;
437 [ + - ]: 5 : block_ser << TX_WITH_WITNESS(*block_out);
438 [ - + + - : 10 : obj.pushKV("hex", HexStr(block_ser));
+ - + - +
- ]
439 : 5 : }
440 [ + - ]: 364 : return obj;
441 [ + - ]: 752 : },
442 [ + - + - : 29420 : };
+ - + + +
+ - - -
- ]
443 [ + - + - : 35304 : }
+ - + - +
- + - - -
- - ]
444 : :
445 : 2607 : static RPCMethod getmininginfo()
446 : : {
447 : 2607 : return RPCMethod{
448 : 2607 : "getmininginfo",
449 [ + - ]: 5214 : "Returns a json object containing mining-related information.",
450 : : {},
451 [ + - ]: 5214 : RPCResult{
452 [ + - ]: 5214 : RPCResult::Type::OBJ, "", "",
453 : : {
454 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "blocks", "The current block"},
455 [ + - + - ]: 5214 : {RPCResult::Type::STR_HEX, "bestblockhash", "The hash of the current best block"},
456 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "currentblockweight", /*optional=*/true, "The block weight (including reserved weight for block header, txs count and coinbase tx) of the last assembled block (only present if a block was ever assembled)"},
457 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "currentblocktx", /*optional=*/true, "The number of block transactions (excluding coinbase) of the last assembled block (only present if a block was ever assembled)"},
458 [ + - + - ]: 5214 : {RPCResult::Type::STR_HEX, "bits", "The current nBits, compact representation of the block difficulty target"},
459 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "difficulty", "The current difficulty"},
460 [ + - + - ]: 5214 : {RPCResult::Type::STR_HEX, "target", "The current target"},
461 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "networkhashps", "The network hashes per second"},
462 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "pooledtx", "The size of the mempool"},
463 [ + - + - ]: 5214 : {RPCResult::Type::STR_AMOUNT, "blockmintxfee", "Minimum feerate of packages selected for block inclusion in " + CURRENCY_UNIT + "/kvB"},
464 [ + - + - ]: 5214 : {RPCResult::Type::STR, "chain", "current network name (" LIST_CHAIN_NAMES ")"},
465 [ + - + - ]: 5214 : {RPCResult::Type::STR_HEX, "signet_challenge", /*optional=*/true, "The block challenge (aka. block script), in hexadecimal (only present if the current network is a signet)"},
466 [ + - + - ]: 5214 : {RPCResult::Type::OBJ, "next", "The next block",
467 : : {
468 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "height", "The next height"},
469 [ + - + - ]: 5214 : {RPCResult::Type::STR_HEX, "bits", "The next target nBits"},
470 [ + - + - ]: 5214 : {RPCResult::Type::NUM, "difficulty", "The next difficulty"},
471 [ + - + - ]: 5214 : {RPCResult::Type::STR_HEX, "target", "The next target"}
472 : : }},
473 [ + - + - : 5214 : (IsDeprecatedRPCEnabled("warnings") ?
- + ]
474 [ - - - - : 2607 : RPCResult{RPCResult::Type::STR, "warnings", "any network and blockchain warnings (DEPRECATED)"} :
- + - + -
+ - - - -
- - ]
475 [ + - + - : 10428 : RPCResult{RPCResult::Type::ARR, "warnings", "any network and blockchain warnings (run with `-deprecatedrpc=warnings` to return the latest warning as a single string)",
+ - + - -
- - - ]
476 : : {
477 [ + - + - : 10428 : {RPCResult::Type::STR, "", "warning"},
+ - + - -
- - - ]
478 : : }
479 [ + - + - : 13035 : }
+ - + + +
- - - - -
- - - - ]
480 : : ),
481 [ + - + - : 99066 : }},
+ - + + +
+ - - -
- ]
482 : 2607 : RPCExamples{
483 [ + - + - : 5214 : HelpExampleCli("getmininginfo", "")
+ - ]
484 [ + - + - : 10428 : + HelpExampleRpc("getmininginfo", "")
+ - + - ]
485 [ + - ]: 2607 : },
486 : 2607 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
487 : : {
488 : 25 : NodeContext& node = EnsureAnyNodeContext(request.context);
489 : 25 : const CTxMemPool& mempool = EnsureMemPool(node);
490 : 25 : ChainstateManager& chainman = EnsureChainman(node);
491 : 25 : LOCK(cs_main);
492 [ + - ]: 25 : const CChain& active_chain = chainman.ActiveChain();
493 [ - + + - ]: 50 : CBlockIndex& tip{*CHECK_NONFATAL(active_chain.Tip())};
494 : :
495 : 25 : UniValue obj(UniValue::VOBJ);
496 [ - + + - : 50 : obj.pushKV("blocks", active_chain.Height());
+ - + - ]
497 [ + - + - : 50 : obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
+ - + - ]
498 [ + + + - : 29 : if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
+ - + - ]
499 [ + + + - : 29 : if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
+ - + - ]
500 [ + - + - : 50 : obj.pushKV("bits", strprintf("%08x", tip.nBits));
+ - + - ]
501 [ + - + - : 50 : obj.pushKV("difficulty", GetDifficulty(tip));
+ - + - ]
502 [ + - + - : 50 : obj.pushKV("target", GetTarget(tip, chainman.GetConsensus().powLimit).GetHex());
+ - + - +
- ]
503 [ + - + - : 50 : obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
+ - + - ]
504 [ + - + - : 50 : obj.pushKV("pooledtx", mempool.size());
+ - + - ]
505 [ + - + - ]: 25 : const auto mining_options{node::FlattenMiningOptions(EnsureBlockTemplateManager(node).BlockCreateArgs())};
506 [ + - + - : 50 : obj.pushKV("blockmintxfee", ValueFromAmount(CHECK_NONFATAL(mining_options.block_min_fee_rate)->GetFeePerK()));
+ - + - ]
507 [ + - + - : 50 : obj.pushKV("chain", chainman.GetParams().GetChainTypeString());
+ - + - ]
508 : :
509 : 25 : UniValue next(UniValue::VOBJ);
510 : 25 : CBlockIndex next_index;
511 [ + - ]: 25 : NextEmptyBlockIndex(tip, chainman.GetConsensus(), next_index);
512 : :
513 [ + - + - : 50 : next.pushKV("height", next_index.nHeight);
+ - ]
514 [ + - + - : 50 : next.pushKV("bits", strprintf("%08x", next_index.nBits));
+ - + - ]
515 [ + - + - : 50 : next.pushKV("difficulty", GetDifficulty(next_index));
+ - + - ]
516 [ + - + - : 50 : next.pushKV("target", GetTarget(next_index, chainman.GetConsensus().powLimit).GetHex());
+ - + - +
- ]
517 [ + - + - : 50 : obj.pushKV("next", next);
+ - ]
518 : :
519 [ + + ]: 25 : if (chainman.GetParams().GetChainType() == ChainType::SIGNET) {
520 [ - + ]: 3 : const std::vector<uint8_t>& signet_challenge =
521 : 3 : chainman.GetConsensus().signet_challenge;
522 [ - + + - : 6 : obj.pushKV("signet_challenge", HexStr(signet_challenge));
+ - + - +
- ]
523 : : }
524 [ + - + - : 50 : obj.pushKV("warnings", node::GetWarningsForRpc(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings")));
+ - + - +
- + - ]
525 : 50 : return obj;
526 [ + - ]: 50 : },
527 [ + - + - ]: 10428 : };
528 [ + - + - : 106887 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- - + - +
- + - + -
- + - - -
- - - - -
- - ]
529 : :
530 : :
531 : : // NOTE: Unlike wallet RPC (which use BTC values), mining RPCs follow GBT (BIP 22) in using satoshi amounts
532 : 3311 : static RPCMethod prioritisetransaction()
533 : : {
534 : 3311 : return RPCMethod{"prioritisetransaction",
535 [ + - ]: 6622 : "Accepts the transaction into mined blocks at a higher (or lower) priority\n",
536 : : {
537 [ + - + - ]: 6622 : {"txid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction id."},
538 [ + - + - ]: 6622 : {"dummy", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "API-Compatibility for previous API. Must be zero or null.\n"
539 : : " DEPRECATED. For forward compatibility use named arguments and omit this parameter.",
540 [ + - ]: 6622 : RPCArgOptions{.placeholder = true}},
541 [ + - + - ]: 6622 : {"fee_delta", RPCArg::Type::NUM, RPCArg::Optional::NO, "The fee value (in satoshis) to add (or subtract, if negative).\n"
542 : : " Note, that this value is not a fee rate. It is a value to modify absolute fee of the TX.\n"
543 : : " The fee is not actually paid, only the algorithm for selecting transactions into a block\n"
544 : : " considers the transaction as it would have paid a higher (or lower) fee."},
545 : : },
546 [ + - ]: 6622 : RPCResult{
547 [ + - + - : 6622 : RPCResult::Type::BOOL, "", "Returns true"},
+ - ]
548 : 3311 : RPCExamples{
549 [ + - + - : 6622 : HelpExampleCli("prioritisetransaction", "\"txid\" 0.0 10000")
+ - ]
550 [ + - + - : 13244 : + HelpExampleRpc("prioritisetransaction", "\"txid\", 0.0, 10000")
+ - + - ]
551 [ + - ]: 3311 : },
552 : 3311 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
553 : : {
554 : 723 : LOCK(cs_main);
555 : :
556 [ + - + + ]: 723 : auto txid{Txid::FromUint256(ParseHashV(request.params[0], "txid"))};
557 [ + - ]: 721 : const auto dummy{self.MaybeArg<double>("dummy")};
558 [ + - + - ]: 721 : CAmount nAmount = request.params[2].getInt<int64_t>();
559 : :
560 [ + + + + ]: 721 : if (dummy && *dummy != 0) {
561 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.");
562 : : }
563 : :
564 [ + - ]: 720 : CTxMemPool& mempool = EnsureAnyMemPool(request.context);
565 : :
566 : : // Non-0 fee dust transactions are not allowed for entry, and modification not allowed afterwards
567 [ + - ]: 720 : const auto& tx = mempool.get(txid);
568 [ + - + + : 968 : if (mempool.m_opts.require_standard && tx && !GetDust(*tx, mempool.m_opts.dust_relay_feerate).empty()) {
+ - + + +
+ ]
569 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Priority is not supported for transactions with dust outputs.");
570 : : }
571 : :
572 [ + - ]: 719 : mempool.PrioritiseTransaction(txid, nAmount);
573 [ + - ]: 719 : return true;
574 [ + - ]: 1439 : },
575 [ + - + - : 23177 : };
+ + - - ]
576 [ + - + - : 16555 : }
+ - - - ]
577 : :
578 : 2614 : static RPCMethod getprioritisedtransactions()
579 : : {
580 : 2614 : return RPCMethod{"getprioritisedtransactions",
581 [ + - ]: 5228 : "Returns a map of all user-created (see prioritisetransaction) fee deltas by txid, and whether the tx is present in mempool.",
582 : : {},
583 [ + - ]: 5228 : RPCResult{
584 [ + - ]: 5228 : RPCResult::Type::OBJ_DYN, "", "prioritisation keyed by txid",
585 : : {
586 [ + - + - ]: 5228 : {RPCResult::Type::OBJ, "<transactionid>", "", {
587 [ + - + - ]: 5228 : {RPCResult::Type::NUM, "fee_delta", "transaction fee delta in satoshis"},
588 [ + - + - ]: 5228 : {RPCResult::Type::BOOL, "in_mempool", "whether this transaction is currently in mempool"},
589 [ + - + - ]: 5228 : {RPCResult::Type::NUM, "modified_fee", /*optional=*/true, "modified fee in satoshis. Only returned if in_mempool=true"},
590 : : }}
591 : : },
592 [ + - + - : 28754 : },
+ - + + +
+ - - -
- ]
593 : 2614 : RPCExamples{
594 [ + - + - : 5228 : HelpExampleCli("getprioritisedtransactions", "")
+ - ]
595 [ + - + - : 10456 : + HelpExampleRpc("getprioritisedtransactions", "")
+ - + - ]
596 [ + - ]: 2614 : },
597 : 2614 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
598 : : {
599 : 31 : NodeContext& node = EnsureAnyNodeContext(request.context);
600 : 31 : CTxMemPool& mempool = EnsureMemPool(node);
601 : 31 : UniValue rpc_result{UniValue::VOBJ};
602 [ + - + + ]: 61 : for (const auto& delta_info : mempool.GetPrioritisedTransactions()) {
603 : 30 : UniValue result_inner{UniValue::VOBJ};
604 [ + - + - : 60 : result_inner.pushKV("fee_delta", delta_info.delta);
+ - ]
605 [ + - + - : 60 : result_inner.pushKV("in_mempool", delta_info.in_mempool);
+ - ]
606 [ + + ]: 30 : if (delta_info.in_mempool) {
607 [ + - + - : 38 : result_inner.pushKV("modified_fee", *delta_info.modified_fee);
+ - ]
608 : : }
609 [ + - + - ]: 60 : rpc_result.pushKVEnd(delta_info.txid.GetHex(), std::move(result_inner));
610 : 30 : }
611 : 31 : return rpc_result;
612 : 0 : },
613 [ + - + - ]: 10456 : };
614 [ + - + - : 20912 : }
+ - + - -
- ]
615 : :
616 : :
617 : : // NOTE: Assumes a conclusive result; if result is inconclusive, it must be handled by caller
618 : 23177 : static UniValue BIP22ValidationResult(const BlockValidationState& state)
619 : : {
620 [ + + ]: 23177 : if (state.IsValid())
621 : 20261 : return UniValue::VNULL;
622 : :
623 [ - + ]: 2916 : if (state.IsError())
624 [ # # # # ]: 0 : throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
625 [ + - ]: 2916 : if (state.IsInvalid())
626 : : {
627 [ - + ]: 2916 : std::string strRejectReason = state.GetRejectReason();
628 [ - + ]: 2916 : if (strRejectReason.empty())
629 [ # # ]: 0 : return "rejected";
630 [ + - ]: 2916 : return strRejectReason;
631 : 2916 : }
632 : : // Should be impossible
633 : 0 : return "valid?";
634 : : }
635 : :
636 : : // Prefix rule name with ! if not optional, see BIP9
637 : 77 : static std::string gbt_rule_value(const std::string& name, bool gbt_optional_rule)
638 : : {
639 [ - + ]: 77 : std::string s{name};
640 [ - + ]: 77 : if (!gbt_optional_rule) {
641 [ # # ]: 0 : s.insert(s.begin(), '!');
642 : : }
643 : 77 : return s;
644 : 0 : }
645 : :
646 : 2995 : static RPCMethod getblocktemplate()
647 : : {
648 : 2995 : return RPCMethod{
649 : 2995 : "getblocktemplate",
650 [ + - ]: 5990 : "If the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'.\n"
651 : : "It returns data needed to construct a block to work on.\n"
652 : : "For full specification, see BIPs 22, 23, 9, and 145:\n"
653 : : " https://github.com/bitcoin/bips/blob/master/bip-0022.mediawiki\n"
654 : : " https://github.com/bitcoin/bips/blob/master/bip-0023.mediawiki\n"
655 : : " https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki#getblocktemplate_changes\n"
656 : : " https://github.com/bitcoin/bips/blob/master/bip-0145.mediawiki\n",
657 : : {
658 [ + - + - ]: 5990 : {"template_request", RPCArg::Type::OBJ, RPCArg::Optional::NO, "Format of the template",
659 : : {
660 [ + - + - ]: 5990 : {"mode", RPCArg::Type::STR, /* treat as named arg */ RPCArg::Optional::OMITTED, "This must be set to \"template\", \"proposal\" (see BIP 23), or omitted"},
661 [ + - + - ]: 5990 : {"capabilities", RPCArg::Type::ARR, /* treat as named arg */ RPCArg::Optional::OMITTED, "A list of strings",
662 : : {
663 [ + - + - ]: 5990 : {"str", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "client side supported feature, 'longpoll', 'coinbasevalue', 'proposal', 'serverlist', 'workid'"},
664 : : }},
665 [ + - + - ]: 5990 : {"rules", RPCArg::Type::ARR, RPCArg::Optional::NO, "A list of strings",
666 : : {
667 [ + - + - ]: 5990 : {"segwit", RPCArg::Type::STR, RPCArg::Optional::NO, "(literal) indicates client side segwit support"},
668 [ + - + - ]: 5990 : {"str", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "other client side supported softfork deployment"},
669 : : }},
670 [ + - + - ]: 5990 : {"longpollid", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "delay processing request until the result would vary significantly from the \"longpollid\" of a prior template"},
671 [ + - + - ]: 5990 : {"data", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "proposed block data to check, encoded in hexadecimal; valid only for mode=\"proposal\""},
672 : : },
673 : : },
674 : : },
675 : : {
676 [ + - + - : 5990 : RPCResult{"If the proposal was accepted with mode=='proposal'", RPCResult::Type::NONE, "", ""},
+ - ]
677 [ + - + - : 5990 : RPCResult{"If the proposal was not accepted with mode=='proposal'", RPCResult::Type::STR, "", "According to BIP22"},
+ - ]
678 [ + - + - : 5990 : RPCResult{"Otherwise", RPCResult::Type::OBJ, "", "",
+ - ]
679 : : {
680 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "version", "The preferred block version"},
681 [ + - + - ]: 5990 : {RPCResult::Type::ARR, "rules", "specific block rules that are to be enforced",
682 : : {
683 [ + - + - ]: 5990 : {RPCResult::Type::STR, "", "name of a rule the client must understand to some extent; see BIP 9 for format"},
684 : : }},
685 [ + - + - ]: 5990 : {RPCResult::Type::OBJ_DYN, "vbavailable", "set of pending, supported versionbit (BIP 9) softfork deployments",
686 : : {
687 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "rulename", "identifies the bit number as indicating acceptance and readiness for the named softfork rule"},
688 : : }},
689 [ + - + - ]: 5990 : {RPCResult::Type::ARR, "capabilities", "",
690 : : {
691 [ + - + - ]: 5990 : {RPCResult::Type::STR, "value", "A supported feature, for example 'proposal'"},
692 : : }},
693 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "vbrequired", "bit mask of versionbits the server requires set in submissions"},
694 [ + - + - ]: 5990 : {RPCResult::Type::STR, "previousblockhash", "The hash of current highest block"},
695 [ + - + - ]: 5990 : {RPCResult::Type::ARR, "transactions", "contents of non-coinbase transactions that should be included in the next block",
696 : : {
697 [ + - + - ]: 5990 : {RPCResult::Type::OBJ, "", "",
698 : : {
699 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "data", "transaction data encoded in hexadecimal (byte-for-byte)"},
700 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "txid", "transaction hash excluding witness data, shown in byte-reversed hex"},
701 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "hash", "transaction hash including witness data, shown in byte-reversed hex"},
702 [ + - + - ]: 5990 : {RPCResult::Type::ARR, "depends", "array of numbers",
703 : : {
704 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "", "transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is"},
705 : : }},
706 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "fee", "difference in value between transaction inputs and outputs (in satoshis); for coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy); if key is not present, fee is unknown and clients MUST NOT assume there isn't one"},
707 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "sigops", "total SigOps cost, as counted for purposes of block limits; if key is not present, sigop cost is unknown and clients MUST NOT assume it is zero"},
708 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "weight", "total transaction weight, as counted for purposes of block limits"},
709 : : }},
710 : : }},
711 [ + - + - ]: 5990 : {RPCResult::Type::OBJ_DYN, "coinbaseaux", "data that should be included in the coinbase's scriptSig content",
712 : : {
713 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "key", "values must be in the coinbase (keys may be ignored)"},
714 : : }},
715 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "coinbasevalue", "maximum allowable input to coinbase transaction, including the generation award and transaction fees (in satoshis)"},
716 [ + - + - ]: 5990 : {RPCResult::Type::STR, "longpollid", "an id to include with a request to longpoll on an update to this template"},
717 [ + - + - ]: 5990 : {RPCResult::Type::STR, "target", "The hash target"},
718 [ + - + - ]: 5990 : {RPCResult::Type::NUM_TIME, "mintime", "The minimum timestamp appropriate for the next block time, expressed in " + UNIX_EPOCH_TIME + ". Adjusted for the proposed BIP94 timewarp rule."},
719 [ + - + - ]: 5990 : {RPCResult::Type::ARR, "mutable", "list of ways the block template may be changed",
720 : : {
721 [ + - + - ]: 5990 : {RPCResult::Type::STR, "value", "A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock'"},
722 : : }},
723 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "noncerange", "A range of valid nonces"},
724 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "sigoplimit", "limit of sigops in blocks"},
725 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "sizelimit", "limit of block size"},
726 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "weightlimit", /*optional=*/true, "limit of block weight"},
727 [ + - + - ]: 5990 : {RPCResult::Type::NUM_TIME, "curtime", "current timestamp in " + UNIX_EPOCH_TIME + ". Adjusted for the proposed BIP94 timewarp rule."},
728 [ + - + - ]: 5990 : {RPCResult::Type::STR, "bits", "compressed target of next block"},
729 [ + - + - ]: 5990 : {RPCResult::Type::NUM, "height", "The height of the next block"},
730 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "signet_challenge", /*optional=*/true, "Only on signet"},
731 [ + - + - ]: 5990 : {RPCResult::Type::STR_HEX, "default_witness_commitment", /*optional=*/true, "a valid witness commitment for the unmodified block template"},
732 [ + - + - : 245590 : }},
+ - + - +
- + - + -
+ - + - +
+ + + + +
+ + + + +
+ + + + +
+ + - - -
- - - - -
- - - - -
- - - -
- ]
733 : : },
734 : 2995 : RPCExamples{
735 [ + - + - : 5990 : HelpExampleCli("getblocktemplate", "'{\"rules\": [\"segwit\"]}'")
+ - ]
736 [ + - + - : 11980 : + HelpExampleRpc("getblocktemplate", "{\"rules\": [\"segwit\"]}")
+ - ]
737 [ + - ]: 2995 : },
738 : 2995 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
739 : : {
740 : 413 : NodeContext& node = EnsureAnyNodeContext(request.context);
741 : 413 : ChainstateManager& chainman = EnsureChainman(node);
742 : 413 : node::BlockTemplateManager& block_template_manager = EnsureBlockTemplateManager(node);
743 : :
744 : 413 : std::string strMode = "template";
745 [ + - ]: 413 : UniValue lpval = NullUniValue;
746 [ + - ]: 413 : std::set<std::string> setClientRules;
747 [ + - + - ]: 413 : if (!request.params[0].isNull())
748 : : {
749 [ + - + - ]: 413 : const UniValue& oparam = request.params[0].get_obj();
750 [ + - ]: 413 : const UniValue& modeval = oparam.find_value("mode");
751 [ + + ]: 413 : if (modeval.isStr())
752 [ + - + - ]: 321 : strMode = modeval.get_str();
753 [ - + ]: 92 : else if (modeval.isNull())
754 : : {
755 : : /* Do nothing */
756 : : }
757 : : else
758 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
759 [ + - + - ]: 413 : lpval = oparam.find_value("longpollid");
760 : :
761 [ + + ]: 413 : if (strMode == "proposal")
762 : : {
763 [ + - ]: 321 : const UniValue& dataval = oparam.find_value("data");
764 [ - + ]: 321 : if (!dataval.isStr())
765 [ # # # # ]: 0 : throw JSONRPCError(RPC_TYPE_ERROR, "Missing data String key for proposal");
766 : :
767 : 321 : CBlock block;
768 [ + - + - : 321 : if (!DecodeHexBlk(block, dataval.get_str()))
+ + ]
769 [ + - + - ]: 4 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
770 : :
771 [ + - ]: 319 : uint256 hash = block.GetHash();
772 [ + - ]: 319 : LOCK(cs_main);
773 [ + - ]: 319 : const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
774 [ - + ]: 319 : if (pindex) {
775 [ # # # # ]: 0 : if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
776 [ # # ]: 0 : return "duplicate";
777 [ # # ]: 0 : if (pindex->nStatus & BLOCK_FAILED_VALID)
778 [ # # ]: 0 : return "duplicate-invalid";
779 [ # # ]: 0 : return "duplicate-inconclusive";
780 : : }
781 : :
782 [ + - + - : 638 : return BIP22ValidationResult(TestBlockValidity(chainman.ActiveChainstate(), block, /*check_pow=*/false, /*check_merkle_root=*/true));
+ - ]
783 : 321 : }
784 : :
785 [ + - ]: 92 : const UniValue& aClientRules = oparam.find_value("rules");
786 [ + + ]: 92 : if (aClientRules.isArray()) {
787 [ - + + + ]: 188 : for (unsigned int i = 0; i < aClientRules.size(); ++i) {
788 [ + - ]: 97 : const UniValue& v = aClientRules[i];
789 [ + - + - ]: 97 : setClientRules.insert(v.get_str());
790 : : }
791 : : }
792 : : }
793 : :
794 [ - + ]: 92 : if (strMode != "template")
795 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
796 : :
797 [ - + ]: 92 : if (!chainman.GetParams().IsTestChain()) {
798 [ # # ]: 0 : const CConnman& connman = EnsureConnman(node);
799 [ # # # # ]: 0 : if (connman.GetNodeCount(ConnectionDirection::Both) == 0) {
800 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, CLIENT_NAME " is not connected!");
801 : : }
802 : :
803 [ # # ]: 0 : if (chainman.IsInitialBlockDownload()) {
804 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, CLIENT_NAME " is in initial sync and waiting for blocks...");
805 : : }
806 : : }
807 : :
808 : 92 : static unsigned int nTransactionsUpdatedLast;
809 [ + - ]: 92 : const CTxMemPool& mempool = EnsureMemPool(node);
810 : :
811 [ + - ]: 92 : WAIT_LOCK(cs_main, cs_main_lock);
812 [ + - + - : 92 : uint256 tip{CHECK_NONFATAL(block_template_manager.GetTip()).value().hash};
+ - ]
813 : :
814 : : // Long Polling (BIP22)
815 [ + + ]: 92 : if (!lpval.isNull()) {
816 : : /**
817 : : * Wait to respond until either the best block changes, OR there are more
818 : : * transactions.
819 : : *
820 : : * The check for new transactions first happens after 1 minute and
821 : : * subsequently every 10 seconds. BIP22 does not require this particular interval.
822 : : * On mainnet the mempool changes frequently enough that in practice this RPC
823 : : * returns after 60 seconds, or sooner if the best block changes.
824 : : *
825 : : * getblocktemplate is unlikely to be called by bitcoin-cli, so
826 : : * -rpcclienttimeout is not a concern. BIP22 recommends a long request timeout.
827 : : *
828 : : * The longpollid is assumed to be a tip hash if it has the right format.
829 : : */
830 : 3 : uint256 hashWatchedChain;
831 : 3 : unsigned int nTransactionsUpdatedLastLP;
832 : :
833 [ + - ]: 3 : if (lpval.isStr())
834 : : {
835 : : // Format: <hashBestChain><nTransactionsUpdatedLast>
836 [ + - ]: 3 : const std::string& lpstr = lpval.get_str();
837 : :
838 : : // Assume the longpollid is a block hash. If it's not then we return
839 : : // early below.
840 [ + - + - : 3 : hashWatchedChain = ParseHashV(lpstr.substr(0, 64), "longpollid");
+ - ]
841 [ + - + - ]: 6 : nTransactionsUpdatedLastLP = LocaleIndependentAtoi<int64_t>(lpstr.substr(64));
842 : : }
843 : : else
844 : : {
845 : : // NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
846 : 0 : hashWatchedChain = tip;
847 : 0 : nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
848 : : }
849 : :
850 : : // Release lock while waiting
851 : 3 : {
852 [ + - ]: 3 : REVERSE_LOCK(cs_main_lock, cs_main);
853 : 3 : MillisecondsDouble checktxtime{std::chrono::minutes(1)};
854 [ + - + - ]: 3 : while (IsRPCRunning()) {
855 : : // If hashWatchedChain is not a real block hash, this will
856 : : // return immediately.
857 [ + - ]: 3 : std::optional<BlockRef> maybe_tip{block_template_manager.WaitTipChanged(hashWatchedChain, checktxtime)};
858 : : // Node is shutting down
859 [ + - ]: 3 : if (!maybe_tip) break;
860 [ + + ]: 3 : tip = maybe_tip->hash;
861 [ + + ]: 3 : if (tip != hashWatchedChain) break;
862 : :
863 : : // Check transactions for update without holding the mempool
864 : : // lock to avoid deadlocks.
865 [ + - - + ]: 1 : if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP) {
866 : : break;
867 : : }
868 : 0 : checktxtime = std::chrono::seconds(10);
869 : : }
870 : 3 : }
871 [ + - + - : 3 : tip = CHECK_NONFATAL(block_template_manager.GetTip()).value().hash;
+ - ]
872 : :
873 [ + - - + ]: 3 : if (!IsRPCRunning())
874 [ # # # # ]: 0 : throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
875 : : // TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
876 : : }
877 : :
878 [ + + ]: 92 : const Consensus::Params& consensusParams = chainman.GetParams().GetConsensus();
879 : :
880 : : // GBT must be called with 'signet' set in the rules for signet chains
881 [ + + + - : 98 : if (consensusParams.signet_blocks && !setClientRules.contains("signet")) {
- + - + ]
882 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, "getblocktemplate must be called with the signet rule set (call with {\"rules\": [\"segwit\", \"signet\"]})");
883 : : }
884 : :
885 : : // GBT must be called with 'segwit' set in the rules
886 [ + - + + ]: 92 : if (!setClientRules.contains("segwit")) {
887 [ + - + - ]: 2 : throw JSONRPCError(RPC_INVALID_PARAMETER, "getblocktemplate must be called with the segwit rule set (call with {\"rules\": [\"segwit\"]})");
888 : : }
889 : :
890 : : // Update block
891 : 91 : static CBlockIndex* pindexPrev;
892 : 91 : static int64_t time_start;
893 [ + + + - ]: 1375 : static std::unique_ptr<node::CBlockTemplate> block_template;
894 [ + + + + ]: 91 : if (!pindexPrev || pindexPrev->GetBlockHash() != tip ||
895 [ + - + + : 18 : (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5))
+ - + + ]
896 : : {
897 : : // Clear pindexPrev so future calls make a new block, despite any failures from here on
898 : 75 : pindexPrev = nullptr;
899 : :
900 : : // Store the pindexBest used before createNewBlock, to avoid races
901 [ + - ]: 75 : nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
902 [ + - ]: 75 : CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(tip);
903 [ + - ]: 75 : time_start = GetTime();
904 : :
905 : : // Create new block. Opt-out of cooldown mechanism, because it would add
906 : : // a delay to each getblocktemplate call. This differs from typical
907 : : // long-lived IPC usage, where the overhead is paid only when creating
908 : : // the initial template.
909 : 150 : block_template = block_template_manager.CreateNewTemplate({});
910 [ + - ]: 75 : CHECK_NONFATAL(block_template);
911 : :
912 : :
913 : : // Need to update only after we know createNewBlock succeeded
914 : 75 : pindexPrev = pindexPrevNew;
915 : : }
916 [ + - ]: 91 : CHECK_NONFATAL(pindexPrev);
917 [ + - ]: 91 : CBlockHeader block_header{block_template->block};
918 : :
919 : : // Update nTime
920 [ + - ]: 91 : UpdateTime(&block_header, consensusParams, pindexPrev);
921 : 91 : block_header.nNonce = 0;
922 : :
923 : : // NOTE: If at some point we support pre-segwit miners post-segwit-activation, this needs to take segwit support into consideration
924 : 91 : const bool fPreSegWit = !DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_SEGWIT);
925 : :
926 [ + - + - ]: 91 : UniValue aCaps(UniValue::VARR); aCaps.push_back("proposal");
927 : :
928 : 91 : UniValue transactions(UniValue::VARR);
929 : 91 : std::map<Txid, int64_t> setTxIndex;
930 : 91 : const std::vector<CAmount>& tx_fees{block_template->vTxFees};
931 : 91 : const std::vector<int64_t>& tx_sigops{block_template->vTxSigOpsCost};
932 : :
933 : 91 : int i = 0;
934 [ + + ]: 291 : for (const auto& it : block_template->block.vtx) {
935 [ + - ]: 200 : const CTransaction& tx = *it;
936 : 200 : Txid txHash = tx.GetHash();
937 [ + - ]: 200 : setTxIndex[txHash] = i++;
938 : :
939 [ + + ]: 200 : if (tx.IsCoinBase())
940 : 91 : continue;
941 : :
942 : 109 : UniValue entry(UniValue::VOBJ);
943 : :
944 [ + - + - : 218 : entry.pushKV("data", EncodeHexTx(tx));
+ - + - ]
945 [ + - + - : 218 : entry.pushKV("txid", txHash.GetHex());
+ - + - ]
946 [ + - + - : 218 : entry.pushKV("hash", tx.GetWitnessHash().GetHex());
+ - + - ]
947 : :
948 : 109 : UniValue deps(UniValue::VARR);
949 [ + + ]: 218 : for (const CTxIn &in : tx.vin)
950 : : {
951 [ + + ]: 109 : if (setTxIndex.contains(in.prevout.hash))
952 [ + - + - : 8 : deps.push_back(setTxIndex[in.prevout.hash]);
+ - ]
953 : : }
954 [ + - + - ]: 218 : entry.pushKV("depends", std::move(deps));
955 : :
956 : 109 : int index_in_template = i - 2;
957 [ + - + - : 218 : entry.pushKV("fee", tx_fees.at(index_in_template));
+ - + - ]
958 [ + - ]: 109 : int64_t nTxSigOps{tx_sigops.at(index_in_template)};
959 [ + + ]: 109 : if (fPreSegWit) {
960 [ + - ]: 5 : CHECK_NONFATAL(nTxSigOps % WITNESS_SCALE_FACTOR == 0);
961 : 5 : nTxSigOps /= WITNESS_SCALE_FACTOR;
962 : : }
963 [ + - + - : 218 : entry.pushKV("sigops", nTxSigOps);
+ - ]
964 [ + - + - : 218 : entry.pushKV("weight", GetTransactionWeight(tx));
+ - ]
965 : :
966 [ + - ]: 109 : transactions.push_back(std::move(entry));
967 : 109 : }
968 : :
969 : 91 : UniValue aux(UniValue::VOBJ);
970 : :
971 [ + - ]: 91 : arith_uint256 hashTarget = arith_uint256().SetCompact(block_header.nBits);
972 : :
973 : 91 : UniValue aMutable(UniValue::VARR);
974 [ + - + - ]: 91 : aMutable.push_back("time");
975 [ + - + - ]: 91 : aMutable.push_back("transactions");
976 [ + - + - ]: 91 : aMutable.push_back("prevblock");
977 : :
978 : 91 : UniValue result(UniValue::VOBJ);
979 [ + - + - ]: 182 : result.pushKV("capabilities", std::move(aCaps));
980 : :
981 : 91 : UniValue aRules(UniValue::VARR);
982 : : // See getblocktemplate changes in BIP 9:
983 : : // ! indicates a more subtle change to the block structure or generation transaction
984 : : // Otherwise clients may assume the rule will not impact usage of the template as-is.
985 [ + - + - ]: 91 : aRules.push_back("csv");
986 [ + + ]: 91 : if (!fPreSegWit) {
987 [ + - + - ]: 87 : aRules.push_back("!segwit");
988 [ + - + - ]: 87 : aRules.push_back("taproot");
989 : : }
990 [ + + ]: 91 : if (consensusParams.signet_blocks) {
991 : : // indicate to miner that they must understand signet rules
992 : : // when attempting to mine with this template
993 [ + - + - ]: 6 : aRules.push_back("!signet");
994 : : }
995 : :
996 : 91 : UniValue vbavailable(UniValue::VOBJ);
997 [ + - ]: 91 : const auto gbtstatus = chainman.m_versionbitscache.GBTStatus(*pindexPrev, consensusParams);
998 : :
999 [ + - + + ]: 141 : for (const auto& [name, info] : gbtstatus.signalling) {
1000 [ + - + - : 100 : vbavailable.pushKV(gbt_rule_value(name, info.gbt_optional_rule), info.bit);
+ - ]
1001 [ - + - - ]: 50 : if (!info.gbt_optional_rule && !setClientRules.contains(name)) {
1002 : : // If the client doesn't support this, don't indicate it in the [default] version
1003 : 0 : block_header.nVersion &= ~info.mask;
1004 : : }
1005 : : }
1006 : :
1007 [ + - + + ]: 116 : for (const auto& [name, info] : gbtstatus.locked_in) {
1008 : 25 : block_header.nVersion |= info.mask;
1009 [ + - + - : 50 : vbavailable.pushKV(gbt_rule_value(name, info.gbt_optional_rule), info.bit);
+ - ]
1010 [ - + - - ]: 25 : if (!info.gbt_optional_rule && !setClientRules.contains(name)) {
1011 : : // If the client doesn't support this, don't indicate it in the [default] version
1012 : 0 : block_header.nVersion &= ~info.mask;
1013 : : }
1014 : : }
1015 : :
1016 [ + - + + ]: 93 : for (const auto& [name, info] : gbtstatus.active) {
1017 [ + - + - : 2 : aRules.push_back(gbt_rule_value(name, info.gbt_optional_rule));
+ - ]
1018 [ - + - - ]: 2 : if (!info.gbt_optional_rule && !setClientRules.contains(name)) {
1019 : : // Not supported by the client; make sure it's safe to proceed
1020 [ # # # # ]: 0 : throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Support for '%s' rule requires explicit client support", name));
1021 : : }
1022 : : }
1023 : :
1024 [ + - + - : 182 : result.pushKV("version", block_header.nVersion);
+ - ]
1025 [ + - + - ]: 182 : result.pushKV("rules", std::move(aRules));
1026 [ + - + - ]: 182 : result.pushKV("vbavailable", std::move(vbavailable));
1027 [ + - + - : 182 : result.pushKV("vbrequired", 0);
+ - ]
1028 : :
1029 [ + - + - : 182 : result.pushKV("previousblockhash", block_header.hashPrevBlock.GetHex());
+ - + - ]
1030 [ + - + - ]: 182 : result.pushKV("transactions", std::move(transactions));
1031 [ + - + - ]: 182 : result.pushKV("coinbaseaux", std::move(aux));
1032 [ + - + - : 182 : result.pushKV("coinbasevalue", block_template->block.vtx[0]->vout[0].nValue);
+ - ]
1033 [ + - + - : 182 : result.pushKV("longpollid", tip.GetHex() + ToString(nTransactionsUpdatedLast));
+ - + - +
- + - ]
1034 [ + - + - : 182 : result.pushKV("target", hashTarget.GetHex());
+ - + - ]
1035 [ + - + - : 182 : result.pushKV("mintime", GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()));
+ - + - ]
1036 [ + - + - ]: 182 : result.pushKV("mutable", std::move(aMutable));
1037 [ + - + - : 182 : result.pushKV("noncerange", "00000000ffffffff");
+ - ]
1038 : 91 : int64_t nSigOpLimit = MAX_BLOCK_SIGOPS_COST;
1039 : 91 : int64_t nSizeLimit = MAX_BLOCK_SERIALIZED_SIZE;
1040 [ + + ]: 91 : if (fPreSegWit) {
1041 [ + - ]: 4 : CHECK_NONFATAL(nSigOpLimit % WITNESS_SCALE_FACTOR == 0);
1042 : 4 : nSigOpLimit /= WITNESS_SCALE_FACTOR;
1043 [ + - ]: 4 : CHECK_NONFATAL(nSizeLimit % WITNESS_SCALE_FACTOR == 0);
1044 : 4 : nSizeLimit /= WITNESS_SCALE_FACTOR;
1045 : : }
1046 [ + - + - : 182 : result.pushKV("sigoplimit", nSigOpLimit);
+ - ]
1047 [ + - + - : 182 : result.pushKV("sizelimit", nSizeLimit);
+ - ]
1048 [ + + ]: 91 : if (!fPreSegWit) {
1049 [ + - + - : 174 : result.pushKV("weightlimit", MAX_BLOCK_WEIGHT);
+ - ]
1050 : : }
1051 [ + - + - : 182 : result.pushKV("curtime", block_header.GetBlockTime());
+ - ]
1052 [ + - + - : 182 : result.pushKV("bits", strprintf("%08x", block_header.nBits));
+ - + - ]
1053 [ + - + - : 182 : result.pushKV("height", pindexPrev->nHeight + 1);
+ - ]
1054 : :
1055 [ + + ]: 91 : if (consensusParams.signet_blocks) {
1056 [ - + + - : 12 : result.pushKV("signet_challenge", HexStr(consensusParams.signet_challenge));
+ - + - +
- ]
1057 : : }
1058 : :
1059 [ - + + - ]: 91 : if (const auto& coinbase{block_template->m_coinbase_tx}; coinbase.required_outputs.size() > 0) {
1060 [ + - ]: 91 : CHECK_NONFATAL(coinbase.required_outputs.size() == 1); // Only one output is currently expected
1061 [ - + + - : 273 : result.pushKV("default_witness_commitment", HexStr(coinbase.required_outputs[0].scriptPubKey));
+ - + - +
- ]
1062 : : }
1063 : :
1064 : 91 : return result;
1065 [ + - + - : 746 : },
+ - ]
1066 [ + - + - : 74875 : };
+ - + - +
- + - + -
+ + + + +
+ + + + +
- - - - -
- - - -
- ]
1067 [ + - + - : 287520 : }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - ]
1068 : :
1069 : : class submitblock_StateCatcher final : public CValidationInterface
1070 : : {
1071 : : public:
1072 : : uint256 hash;
1073 : : bool found{false};
1074 : : BlockValidationState state;
1075 : :
1076 : 28005 : explicit submitblock_StateCatcher(const uint256 &hashIn) : hash(hashIn), state() {}
1077 : :
1078 : : protected:
1079 : 23020 : void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& stateIn) override
1080 : : {
1081 [ + + ]: 23020 : if (block->GetHash() != hash) return;
1082 : 22858 : found = true;
1083 : 22858 : state = stateIn;
1084 : : }
1085 : : };
1086 : :
1087 : 30589 : static RPCMethod submitblock()
1088 : : {
1089 : : // We allow 2 arguments for compliance with BIP22. Argument 2 is ignored.
1090 : 30589 : return RPCMethod{
1091 : 30589 : "submitblock",
1092 [ + - ]: 61178 : "Attempts to submit new block to network.\n"
1093 : : "See https://en.bitcoin.it/wiki/BIP_0022 for full specification.\n",
1094 : : {
1095 [ + - + - ]: 61178 : {"hexdata", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded block data to submit"},
1096 [ + - + - : 91767 : {"dummy", RPCArg::Type::STR, RPCArg::DefaultHint{"ignored"}, "dummy value, for compatibility with BIP22. This value is ignored.",
+ - ]
1097 [ + - ]: 61178 : RPCArgOptions{.placeholder = true}},
1098 : : },
1099 : : {
1100 [ + - + - : 61178 : RPCResult{"If the block was accepted", RPCResult::Type::NONE, "", ""},
+ - ]
1101 [ + - + - : 61178 : RPCResult{"Otherwise", RPCResult::Type::STR, "", "According to BIP22"},
+ - ]
1102 : : },
1103 : 30589 : RPCExamples{
1104 [ + - + - : 61178 : HelpExampleCli("submitblock", "\"mydata\"")
+ - ]
1105 [ + - + - : 122356 : + HelpExampleRpc("submitblock", "\"mydata\"")
+ - ]
1106 [ + - ]: 30589 : },
1107 : 30589 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1108 : : {
1109 : 28007 : std::shared_ptr<CBlock> blockptr = std::make_shared<CBlock>();
1110 [ + - ]: 28007 : CBlock& block = *blockptr;
1111 [ + - + - : 28007 : if (!DecodeHexBlk(block, request.params[0].get_str())) {
+ - + + ]
1112 [ + - + - ]: 4 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
1113 : : }
1114 : :
1115 [ + - ]: 28005 : ChainstateManager& chainman = EnsureAnyChainman(request.context);
1116 : 28005 : {
1117 [ + - ]: 28005 : LOCK(cs_main);
1118 [ + - ]: 28005 : const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
1119 [ + + ]: 28005 : if (pindex) {
1120 [ + - ]: 28001 : chainman.UpdateUncommittedBlockStructures(block, pindex);
1121 : : }
1122 : 0 : }
1123 : :
1124 : 28005 : bool new_block;
1125 [ + - ]: 28005 : auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
1126 [ + - + - : 56010 : CHECK_NONFATAL(chainman.m_options.signals)->RegisterSharedValidationInterface(sc);
+ - ]
1127 [ + - + - : 56010 : bool accepted = chainman.ProcessNewBlock(blockptr, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
+ - ]
1128 [ + - + - : 56010 : CHECK_NONFATAL(chainman.m_options.signals)->UnregisterSharedValidationInterface(sc);
+ - ]
1129 [ + + + + ]: 28005 : if (!new_block && accepted) {
1130 [ + - ]: 4958 : return "duplicate";
1131 : : }
1132 [ + + ]: 23047 : if (!sc->found) {
1133 [ + - ]: 189 : return "inconclusive";
1134 : : }
1135 [ + - ]: 22858 : return BIP22ValidationResult(sc->state);
1136 [ + - ]: 56010 : },
1137 [ + - + - : 336479 : };
+ - + - +
+ + + - -
- - ]
1138 [ + - + - : 214123 : }
+ - + - -
- - - ]
1139 : :
1140 : 4524 : static RPCMethod submitheader()
1141 : : {
1142 : 4524 : return RPCMethod{
1143 : 4524 : "submitheader",
1144 [ + - ]: 9048 : "Decode the given hexdata as a header and submit it as a candidate chain tip if valid."
1145 : : "\nThrows when the header is invalid.\n",
1146 : : {
1147 [ + - + - ]: 9048 : {"hexdata", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hex-encoded block header data"},
1148 : : },
1149 [ + - ]: 9048 : RPCResult{
1150 [ + - + - : 9048 : RPCResult::Type::NONE, "", "None"},
+ - ]
1151 : 4524 : RPCExamples{
1152 [ + - + - : 9048 : HelpExampleCli("submitheader", "\"aabbcc\"") +
+ - ]
1153 [ + - + - : 13572 : HelpExampleRpc("submitheader", "\"aabbcc\"")
+ - + - ]
1154 [ + - ]: 4524 : },
1155 : 4524 : [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
1156 : : {
1157 : 1942 : CBlockHeader h;
1158 [ + + ]: 1942 : if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
1159 [ + - + - ]: 4 : throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed");
1160 : : }
1161 : 1940 : ChainstateManager& chainman = EnsureAnyChainman(request.context);
1162 : 1940 : {
1163 : 1940 : LOCK(cs_main);
1164 [ + - + + ]: 1940 : if (!chainman.m_blockman.LookupBlockIndex(h.hashPrevBlock)) {
1165 [ + - + - : 3 : throw JSONRPCError(RPC_VERIFY_ERROR, "Must submit previous header (" + h.hashPrevBlock.GetHex() + ") first");
+ - ]
1166 : : }
1167 : 1 : }
1168 : :
1169 [ + - ]: 1939 : BlockValidationState state;
1170 [ + - ]: 1939 : chainman.ProcessNewBlockHeaders({{h}}, /*min_pow_checked=*/true, state);
1171 [ + + ]: 1939 : if (state.IsValid()) return UniValue::VNULL;
1172 [ - + ]: 6 : if (state.IsError()) {
1173 [ # # # # ]: 0 : throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
1174 : : }
1175 [ - + + - ]: 18 : throw JSONRPCError(RPC_VERIFY_ERROR, state.GetRejectReason());
1176 : 1933 : },
1177 [ + - + - : 22620 : };
+ + - - ]
1178 [ + - ]: 9048 : }
1179 : :
1180 : 1430 : void RegisterMiningRPCCommands(CRPCTable& t)
1181 : : {
1182 : 1430 : static const CRPCCommand commands[]{
1183 [ + - ]: 2568 : {"mining", &getnetworkhashps},
1184 [ + - ]: 2568 : {"mining", &getmininginfo},
1185 [ + - ]: 2568 : {"mining", &prioritisetransaction},
1186 [ + - ]: 2568 : {"mining", &getprioritisedtransactions},
1187 [ + - ]: 2568 : {"mining", &getblocktemplate},
1188 [ + - ]: 2568 : {"mining", &submitblock},
1189 [ + - ]: 2568 : {"mining", &submitheader},
1190 : :
1191 [ + - ]: 2568 : {"hidden", &generatetoaddress},
1192 [ + - ]: 2568 : {"hidden", &generatetodescriptor},
1193 [ + - ]: 2568 : {"hidden", &generateblock},
1194 [ + - ]: 2568 : {"hidden", &generate},
1195 [ + + + - : 16838 : };
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - - ]
1196 [ + + ]: 17160 : for (const auto& c : commands) {
1197 : 15730 : t.appendCommand(c.name, &c);
1198 : : }
1199 : 1430 : }
|