Repository: bitcoin
Issue: importdescriptors hanging on importing/updating descriptor with large range
Labels: Bug

<!-- Describe the issue -->

When running `importdescriptors` with `range` parameter like `[0, 900000]` if there is a transaction to any of the addresses in high range, e.g. address derived from `84'/1'/0'/0/777777`, bitcoind will hang and no longer respond to some of the RPC requests and syncing stops too.

Furthermore in top I can observe for hours `bitcoind` process consuming 100% of CPU and doing nothing, also wallet isn't being touched.

**Expected behavior**

Bitcoin descriptor wallet should be working correctly and import such descriptor without any issues even if the range specified is huge.

**To reproduce**

1. Install latest `bitcoin v23.0` (built using depends system), sync to testnet, by adding `testnet=1` in your `bitcoin.conf`
2. Create test descriptor wallet: 
```
./bitcoin-cli createwallet test_1
{
  "name": "test_1",
  "warning": ""
}
```
3. Get descriptors for the wallet:
```
$ ./bitcoin-cli -rpcwallet=test_1 listdescriptors true
{
  "wallet_name": "test_1",
  "descriptors": [
    {
      "desc": "tr(tprvXXX/86'/1'/0'/0/*)#fuh4d6pe",
      "timestamp": 1661100252,
      "active": true,
      "internal": false,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "tr(tprvXXX/86'/1'/0'/1/*)#cgj5s03p",
      "timestamp": 1661100254,
      "active": true,
      "internal": true,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "sh(wpkh(tprvXXX/49'/1'/0'/0/*))#5uegq3cr",
      "timestamp": 1661100252,
      "active": true,
      "internal": false,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "sh(wpkh(tprvXXX/49'/1'/0'/1/*))#jl3dmunh",
      "timestamp": 1661100253,
      "active": true,
      "internal": true,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "pkh(tprvXXX/44'/1'/0'/1/*)#t2man3ks",
      "timestamp": 1661100253,
      "active": true,
      "internal": true,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "wpkh(tprvXXX/84'/1'/0'/1/*)#dxzkmnkj",
      "timestamp": 1661100253,
      "active": true,
      "internal": true,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "pkh(tprvXXX/44'/1'/0'/0/*)#677uwyxg",
      "timestamp": 1661100251,
      "active": true,
      "internal": false,
      "range": [
        0,
        999
      ],
      "next": 0
    },
    {
      "desc": "wpkh(tprvXXX/84'/1'/0'/0/*)#uj8hxxx2",
      "timestamp": 1661100252,
      "active": true,
      "internal": false,
      "range": [
        0,
        999
      ],
      "next": 0
    }
  ]
}
```
4. Derive address with huge derivation path `84'/1'/0'/0/777777` for test:
```
./bitcoin-cli deriveaddresses "wpkh(tprvXXX/84'/1'/0'/0/777777)#60yed6sv"
[
  "tb1q5ge5pu77784lzyukur27wz2y2mtc8zzpyfw8nl"
]
```
5. Send some funds to that address:
```
./bitcoin-cli -rpcwallet="" sendtoaddress tb1q5ge5pu77784lzyukur27wz2y2mtc8zzpyfw8nl 0.00001
c7ca6d0a3c94085d393f973af4cf0bfc4e09e2885a5d0ef42af356ffcbeee41d
```

6. Update descriptor on the wallet to reflect the range (up to 900000):
```
./bitcoin-cli -rpcwallet=test_1 importdescriptors '[{ "desc": "wpkh(tprvXXX/84h/1h/0h/0/*)#slgn8chc", "timestamp":"now", "range": [0,900000]}]'
[
  {
    "success": true
  }
]
```
7. Now wait for the transaction you've sent earlier to confirm, check logs:
```
2022-08-21T16:55:15Z [default wallet] AddToWallet c7ca6d0a3c94085d393f973af4cf0bfc4e09e2885a5d0ef42af356ffcbeee41d  update
2022-08-21T16:55:15Z [test_1] MarkUnusedAddresses: Detected a used keypool item at index 777777, mark all keypool items up to this item as used
```

8. After this bitcoind and wallet `test_1` will hang and no longer sync for hours doing something..
9. E.g. doing any request to wallet like:
```
./bitcoin-cli -rpcwallet=test_1 getwalletinfo
```

will be hanging forever until the timeout occurs.

**System information**
Ubuntu 18.04.6 LTS
<!-- What version of Bitcoin Core are you using, where did you get it (website, self-compiled, etc)? -->
self-compiled v23.0 using internal depends

<!-- What type of machine are you observing the error on (OS/CPU and disk type)? -->
Wallet is on the NVME, while bitcoind blockchain data is on HDDs, plenty of memory about 16GB and 24 CPUs (AMD Ryzen 9 3900 12-Core Processor)

<!-- GUI-related issue? What is your operating system and its version? If Linux, what is your desktop environment and graphical shell? -->

<!-- Any extra information that might be useful in the debugging process. -->
<!--- This is normally the contents of a `debug.log` or `config.log` file. Raw text or a link to a pastebin type site are preferred. -->

**bitcoin.conf**:

```
rpcuser=bitcoin_testnet_rpc
rpcpassword=password
rpcbind=127.0.0.1
rpcallowip=127.0.0.1

walletrbf=1
txindex=1

settxfee=0.0006
paytxfee=0.0006

dbcache=512
walletnotify=/home/bitcoin/.bitcoin/cryptonotify.sh %s %w
testnet=1

[test]
rpcworkqueue=128
txindex=1
rpcbind=127.0.0.1
rpcallowip=127.0.0.1

# default wallet
wallet=
```


**debug.log:**

```
2022-08-21T16:45:01Z Using SQLite Version 3.32.1
2022-08-21T16:45:01Z Using wallet /home/bitcoin/.bitcoin/testnet3/test_1
2022-08-21T16:45:01Z init message: Loading wallet…
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 0, internal = 0
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 1, internal = 0
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 2, internal = 0
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 3, internal = 0
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 0, internal = 1
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 1, internal = 1
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 2, internal = 1
2022-08-21T16:45:01Z [test_1] Setting spkMan to active: id = xxx, type = 3, internal = 1
2022-08-21T16:45:01Z [test_1] Wallet File Version = 169900
2022-08-21T16:45:01Z [test_1] Keys: 8 plaintext, 0 encrypted, 0 w/ metadata, 8 total. Unknown wallet records: 0
2022-08-21T16:45:01Z [test_1] Wallet completed loading in             397ms
2022-08-21T16:45:01Z [test_1] setKeyPool.size() = 8000
2022-08-21T16:45:01Z [test_1] mapWallet.size() = 0
2022-08-21T16:45:01Z [test_1] m_address_book.size() = 0
2022-08-21T16:49:52Z [test_1] Update existing descriptor: wpkh(tpubXXX/84'/1'/0'/0/*)#qfxctt9d
2022-08-21T16:50:29Z [test_1] Deactivate spkMan: id = xxx, type = 2, internal = 0
2022-08-21T16:50:29Z [test_1] RescanFromTime: Rescanning last 23 blocks
2022-08-21T16:50:29Z [test_1] Rescan started from block 000000000000000cda1c49b7202b4714498675d4017d4b49e00f6d13926f3e7a...
2022-08-21T16:50:29Z [test_1] Rescan completed in               2ms
2022-08-21T16:55:15Z UpdateTip: new best=00000000a24fd66fed58a7f821f74af68541b303bae8e26642499b4f5ff9cc35 height=2343214 version=0x20000000 log2_work=74.879888 tx=63453651 date='2022-08-21T16:55:11Z' progress=1.000000 cache=0.1MiB(612txo)
2022-08-21T16:55:15Z [default wallet] AddToWallet c7ca6d0a3c94085d393f973af4cf0bfc4e09e2885a5d0ef42af356ffcbeee41d  update
2022-08-21T16:55:15Z [test_1] MarkUnusedAddresses: Detected a used keypool item at index 777777, mark all keypool items up to this item as used
```