Merge #15751: Speed up deriveaddresses for large ranges

41a46cbb31 Speed up deriveaddresses for large ranges (Pieter Wuille)

Pull request description:

  `deriveaddresses` dumps all generated addresses into a single `FlatSigningProvider`, which is also used for looking up information for future derivations. @achow101 points out that the growing data structures may unnecessary increase lookup time for later derivations.

  Fix this by separating the provider used for lookups (`key_provider`) and the one we dump things into.

  This gives a 10x speedup for a range of 7000 elements, and probably a larger speedup for larger ranges.

ACKs for commit 41a46c:
  achow101:
    Regardless, I do think this is a good change, so utACK 41a46cbb31
  fanquake:
    tACK 41a46cb
  meshcollider:
    utACK 41a46cbb31

Tree-SHA512: a1b894ce9d5195d8f9760f44acc6d67a90bb259283fd8c1524c38a222fe53e8c1d35b6653a508b121b7ad91e155c97d26c658f6bdcebf6c360546931e4a26a22
This commit is contained in:
Wladimir J. van der Laan 2019-04-15 13:51:52 +02:00
commit 0c6487c74f
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D

View file

@ -229,8 +229,8 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
range_end = range.second;
}
FlatSigningProvider provider;
auto desc = Parse(desc_str, provider, /* require_checksum = */ true);
FlatSigningProvider key_provider;
auto desc = Parse(desc_str, key_provider, /* require_checksum = */ true);
if (!desc) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Invalid descriptor"));
}
@ -246,8 +246,9 @@ UniValue deriveaddresses(const JSONRPCRequest& request)
UniValue addresses(UniValue::VARR);
for (int i = range_begin; i <= range_end; ++i) {
FlatSigningProvider provider;
std::vector<CScript> scripts;
if (!desc->Expand(i, provider, scripts, provider)) {
if (!desc->Expand(i, key_provider, scripts, provider)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Cannot derive script without private keys"));
}