Skip to content

Commit 4566011

Browse files
committed
Add support for stop/[start,stop] ranges to scantxoutset
1 parent 6b9f45e commit 4566011

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/rpc/blockchain.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
21592159
{"", RPCArg::Type::OBJ, RPCArg::Optional::OMITTED, "An object with output descriptor and metadata",
21602160
{
21612161
{"desc", RPCArg::Type::STR, RPCArg::Optional::NO, "An output descriptor"},
2162-
{"range", RPCArg::Type::NUM, /* default */ "1000", "Up to what child index HD chains should be explored"},
2162+
{"range", RPCArg::Type::RANGE, /* default */ "1000", "The range of HD chain indexes to explore (either end or [begin,end])"},
21632163
},
21642164
},
21652165
},
@@ -2216,7 +2216,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
22162216
// loop through the scan objects
22172217
for (const UniValue& scanobject : request.params[1].get_array().getValues()) {
22182218
std::string desc_str;
2219-
int range = 1000;
2219+
std::pair<int64_t, int64_t> range = {0, 1000};
22202220
if (scanobject.isStr()) {
22212221
desc_str = scanobject.get_str();
22222222
} else if (scanobject.isObject()) {
@@ -2225,8 +2225,8 @@ UniValue scantxoutset(const JSONRPCRequest& request)
22252225
desc_str = desc_uni.get_str();
22262226
UniValue range_uni = find_value(scanobject, "range");
22272227
if (!range_uni.isNull()) {
2228-
range = range_uni.get_int();
2229-
if (range < 0 || range > 1000000) throw JSONRPCError(RPC_INVALID_PARAMETER, "range out of range");
2228+
range = ParseRange(range_uni);
2229+
if (range.first < 0 || (range.second >> 31) != 0 || range.second >= range.first + 1000000) throw JSONRPCError(RPC_INVALID_PARAMETER, "range out of range");
22302230
}
22312231
} else {
22322232
throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan object needs to be either a string or an object");
@@ -2237,8 +2237,11 @@ UniValue scantxoutset(const JSONRPCRequest& request)
22372237
if (!desc) {
22382238
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Invalid descriptor '%s'", desc_str));
22392239
}
2240-
if (!desc->IsRange()) range = 0;
2241-
for (int i = 0; i <= range; ++i) {
2240+
if (!desc->IsRange()) {
2241+
range.first = 0;
2242+
range.second = 0;
2243+
}
2244+
for (int i = range.first; i <= range.second; ++i) {
22422245
std::vector<CScript> scripts;
22432246
if (!desc->Expand(i, provider, scripts, provider)) {
22442247
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys: '%s'", desc_str));

test/functional/rpc_scantxoutset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def run_test(self):
9595
assert_equal(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)", "range": 1500}])['total_amount'], Decimal("28.672"))
9696
assert_equal(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/1/1/*)", "range": 1499}])['total_amount'], Decimal("12.288"))
9797
assert_equal(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/1/1/*)", "range": 1500}])['total_amount'], Decimal("28.672"))
98+
assert_equal(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/1/1/*)", "range": [1500,1500]}])['total_amount'], Decimal("16.384"))
9899

99100
# Test the reported descriptors for a few matches
100101
assert_equal(descriptors(self.nodes[0].scantxoutset("start", [ {"desc": "combo(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/0h/0'/*)", "range": 1499}])), ["pkh([0c5f9a1e/0'/0'/0]026dbd8b2315f296d36e6b6920b1579ca75569464875c7ebe869b536a7d9503c8c)#dzxw429x", "pkh([0c5f9a1e/0'/0'/1]033e6f25d76c00bedb3a8993c7d5739ee806397f0529b1b31dda31ef890f19a60c)#43rvceed"])

0 commit comments

Comments
 (0)