Skip to content

Commit fa144e6

Browse files
author
MarcoFalke
committed
rpc: Add generatetodescriptor
1 parent ecad0a8 commit fa144e6

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

src/rpc/client.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
3030
{ "utxoupdatepsbt", 1, "descriptors" },
3131
{ "generatetoaddress", 0, "nblocks" },
3232
{ "generatetoaddress", 2, "maxtries" },
33+
{ "generatetodescriptor", 0, "num_blocks" },
34+
{ "generatetodescriptor", 2, "maxtries" },
3335
{ "getnetworkhashps", 0, "nblocks" },
3436
{ "getnetworkhashps", 1, "height" },
3537
{ "sendtoaddress", 1, "amount" },

src/rpc/mining.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include <rpc/blockchain.h>
1919
#include <rpc/server.h>
2020
#include <rpc/util.h>
21+
#include <script/descriptor.h>
2122
#include <script/script.h>
23+
#include <script/signingprovider.h>
2224
#include <shutdown.h>
2325
#include <txmempool.h>
2426
#include <univalue.h>
@@ -140,6 +142,47 @@ static UniValue generateBlocks(const CScript& coinbase_script, int nGenerate, ui
140142
return blockHashes;
141143
}
142144

145+
static UniValue generatetodescriptor(const JSONRPCRequest& request)
146+
{
147+
RPCHelpMan{
148+
"generatetodescriptor",
149+
"\nMine blocks immediately to a specified descriptor (before the RPC call returns)\n",
150+
{
151+
{"num_blocks", RPCArg::Type::NUM, RPCArg::Optional::NO, "How many blocks are generated immediately."},
152+
{"descriptor", RPCArg::Type::STR, RPCArg::Optional::NO, "The descriptor to send the newly generated bitcoin to."},
153+
{"maxtries", RPCArg::Type::NUM, /* default */ "1000000", "How many iterations to try."},
154+
},
155+
RPCResult{
156+
"[ blockhashes ] (array) hashes of blocks generated\n"},
157+
RPCExamples{
158+
"\nGenerate 11 blocks to mydesc\n" + HelpExampleCli("generatetodescriptor", "11 \"mydesc\"")},
159+
}
160+
.Check(request);
161+
162+
const int num_blocks{request.params[0].get_int()};
163+
const int64_t max_tries{request.params[2].isNull() ? 1000000 : request.params[2].get_int()};
164+
165+
FlatSigningProvider key_provider;
166+
std::string error;
167+
const auto desc = Parse(request.params[1].get_str(), key_provider, error, /* require_checksum = */ false);
168+
if (!desc) {
169+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error);
170+
}
171+
if (desc->IsRange()) {
172+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Ranged descriptor not accepted. Maybe pass through deriveaddresses first?");
173+
}
174+
175+
FlatSigningProvider provider;
176+
std::vector<CScript> coinbase_script;
177+
if (!desc->Expand(0, key_provider, coinbase_script, provider)) {
178+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys"));
179+
}
180+
181+
CHECK_NONFATAL(coinbase_script.size() == 1);
182+
183+
return generateBlocks(coinbase_script.at(0), num_blocks, max_tries);
184+
}
185+
143186
static UniValue generatetoaddress(const JSONRPCRequest& request)
144187
{
145188
RPCHelpMan{"generatetoaddress",
@@ -961,6 +1004,7 @@ static const CRPCCommand commands[] =
9611004

9621005

9631006
{ "generating", "generatetoaddress", &generatetoaddress, {"nblocks","address","maxtries"} },
1007+
{ "generating", "generatetodescriptor", &generatetodescriptor, {"num_blocks","descriptor","maxtries"} },
9641008

9651009
{ "util", "estimatesmartfee", &estimatesmartfee, {"conf_target", "estimate_mode"} },
9661010

test/functional/rpc_invalidateblock.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""Test the invalidateblock RPC."""
66

77
from test_framework.test_framework import BitcoinTestFramework
8-
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE
8+
from test_framework.address import ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR
99
from test_framework.util import (
1010
assert_equal,
1111
connect_nodes,
@@ -62,7 +62,7 @@ def run_test(self):
6262
wait_until(lambda: self.nodes[1].getblockcount() == 4, timeout=5)
6363

6464
self.log.info("Verify that we reconsider all ancestors as well")
65-
blocks = self.nodes[1].generatetoaddress(10, ADDRESS_BCRT1_UNSPENDABLE)
65+
blocks = self.nodes[1].generatetodescriptor(10, ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR)
6666
assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])
6767
# Invalidate the two blocks at the tip
6868
self.nodes[1].invalidateblock(blocks[-1])
@@ -74,7 +74,7 @@ def run_test(self):
7474
assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])
7575

7676
self.log.info("Verify that we reconsider all descendants")
77-
blocks = self.nodes[1].generatetoaddress(10, ADDRESS_BCRT1_UNSPENDABLE)
77+
blocks = self.nodes[1].generatetodescriptor(10, ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR)
7878
assert_equal(self.nodes[1].getbestblockhash(), blocks[-1])
7979
# Invalidate the two blocks at the tip
8080
self.nodes[1].invalidateblock(blocks[-2])

test/functional/test_framework/address.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from . import segwit_addr
1313

1414
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
15+
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
1516

1617

1718
class AddressType(enum.Enum):

0 commit comments

Comments
 (0)