MCPcopy Create free account
hub / github.com/ElementsProject/elements / generateblock

Function generateblock

src/rpc/mining.cpp:322–431  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

320}
321
322static RPCHelpMan generateblock()
323{
324 return RPCHelpMan{"generateblock",
325 "Mine a set of ordered transactions to a specified address or descriptor and return the block hash.",
326 {
327 {"output", RPCArg::Type::STR, RPCArg::Optional::NO, "The address or descriptor to send the newly generated bitcoin to."},
328 {"transactions", RPCArg::Type::ARR, RPCArg::Optional::NO, "An array of hex strings which are either txids or raw transactions.\n"
329 "Txids must reference transactions currently in the mempool.\n"
330 "All transactions must be valid and in valid order, otherwise the block will be rejected.",
331 {
332 {"rawtx/txid", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, ""},
333 },
334 },
335 },
336 RPCResult{
337 RPCResult::Type::OBJ, "", "",
338 {
339 {RPCResult::Type::STR_HEX, "hash", "hash of generated block"},
340 }
341 },
342 RPCExamples{
343 "\nGenerate a block to myaddress, with txs rawtx and mempool_txid\n"
344 + HelpExampleCli("generateblock", R"("myaddress" '["rawtx", "mempool_txid"]')")
345 },
346 [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
347{
348 const auto address_or_descriptor = request.params[0].get_str();
349 CScript coinbase_script;
350 std::string error;
351
352 if (!getScriptFromDescriptor(address_or_descriptor, coinbase_script, error)) {
353 const auto destination = DecodeDestination(address_or_descriptor);
354 if (!IsValidDestination(destination)) {
355 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Error: Invalid address or descriptor");
356 }
357
358 coinbase_script = GetScriptForDestination(destination);
359 }
360
361 NodeContext& node = EnsureAnyNodeContext(request.context);
362 const CTxMemPool& mempool = EnsureMemPool(node);
363
364 std::vector<CTransactionRef> txs;
365 const auto raw_txs_or_txids = request.params[1].get_array();
366 for (size_t i = 0; i < raw_txs_or_txids.size(); i++) {
367 const auto str(raw_txs_or_txids[i].get_str());
368
369 uint256 hash;
370 CMutableTransaction mtx;
371 if (ParseHashStr(str, hash)) {
372
373 const auto tx = mempool.get(hash);
374 if (!tx) {
375 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Transaction %s not in mempool.", str));
376 }
377
378 txs.emplace_back(tx);
379

Callers

nothing calls this directly

Calls 15

HelpExampleCliFunction · 0.85
getScriptFromDescriptorFunction · 0.85
DecodeDestinationFunction · 0.85
IsValidDestinationFunction · 0.85
JSONRPCErrorFunction · 0.85
GetScriptForDestinationFunction · 0.85
ParseHashStrFunction · 0.85
DecodeHexTxFunction · 0.85
BlockAssemblerClass · 0.85
RegenerateCommitmentsFunction · 0.85
TestBlockValidityFunction · 0.85
GenerateBlockFunction · 0.85

Tested by

no test coverage detected