<blockquote>
<p>Does the wallet database store a table of all the actual output scripts (i.e. witness programs) derived from its keypool?</p>
</blockquote>
<p>Yes, exactly. The descriptors (and for legacy wallet, legacy data) is expanded into a set of scriptPubKeys to look for.</p>
<ul>
<li>A transaction output is ours if its scriptPubKey matches an entry in that set.</li>
<li>A transaction input is ours if it spends an output that is ours.</li>
<li>A transaction is ours if it has at least one input or output that is ours.</li>
</ul>
<p>Note that this does not require examining witness data for inputs. It suffices to look for spends of our outputs (by txid:vout).</p>

CC BY-SA 4.0
2024-01-23T22:02:41.857Z
2024-01-23T22:02:41.857Z
Pieter Wuille
<p>A wallet examines a transaction for relevance by checking if any of the outputs or inputs are related to its own addresses or scripts. To do this, it needs to know the output scripts (or scriptPubKeys) and the corresponding private keys or scripts that can unlock them. Output descriptors1 are a way of encoding this information in a human-readable string that can be imported or exported by wallets.</p>

CC BY-SA 4.0
2024-01-24T10:56:18.573Z
2024-01-24T10:56:18.573Z
diceyt
<blockquote>
<p>Does the wallet database store a table of all the actual output scripts (i.e. witness programs) derived from its keypool?</p>
</blockquote>
<p>Kind of.</p>
<p>For descriptor wallets, the set of all scriptPubKeys (up to the keypool limit for each descriptor) is generated and held in memory. When determining whether a transaction output belongs to the wallet, the wallet checks whether the output's scriptPubKey is in that set. If it is, then the output belongs to the wallet.</p>
<p>One thing to note though is that this scriptPubKey set is not stored in the database. Once the wallet is unloaded, the set is forgotten. It will be recomputed every time the wallet is loaded.</p>
<p>For legacy wallets, it's a lot more complicated. It will determine the type of script and then do lookups for scripts and keys in the wallet depending on the type. For the most part, it does boil down to a finite set of scripts, but this can actually be a very large set that contains a lot of scripts that you might not expect it to.</p>
<blockquote>
<p>I'm especially curious about scanning tx inputs to recognize coins we have spent. Do we only check outpoints stored in the walletdb?</p>
</blockquote>
<p>Yes. For inputs, the wallet will only check to see if the referenced output exists in the wallet <em>and</em> that output belongs to the wallet. This also means that rescanning must be done serially. If the wallet scanned a transaction that contains a spend of an output that belongs to it, but it hadn't scanned the transaction creating that output yet, then it would not detect the spending transaction.</p>
<p>One unexpected aspect of the Bitcoin Core wallet is that it doesn't explicitly mark which outputs actually belong to it. If it needs to know whether an output belongs to the wallet, it will check the scriptPubKey against the in-memory scriptPubKey set. Similarly, it doesn't explicitly permanently mark outputs as being spent. If you were to remove the spending transaction from your wallet using <code>removeprunedfunds</code>, the output would suddenly appear to be unspent and your wallet could make a transaction spending it. Of course, this would also make an invalid transaction.</p>

CC BY-SA 4.0
2024-01-28T03:59:00.577Z
2024-01-28T03:59:00.577Z
Ava Chow
<p>When the wallet examines a transaction for relevance what does it do exactly with that transaction's output scripts and what is that data compared to? Does the wallet database store a table of all the actual output scripts (i.e. witness programs) derived from its keypool?</p>
<p>I'm especially curious about scanning tx inputs to recognize coins we have spent. Do we only check outpoints stored in the walletdb? Or do we go as far as inspecting input scripts and witness stacks for our own public keys?</p>

<bitcoin-core><wallet><database><rescan>
What data does the Bitcoin Core wallet search for during a blockchain rescan?
pinhead
# What data does the Bitcoin Core wallet search for during a blockchain rescan?

(Asked by: username_0 on 2024-01-23)

When the wallet examines a transaction for relevance what does it do exactly with that transaction's output scripts and what is that data compared to? Does the wallet database store a table of all the actual output scripts (i.e. witness programs) derived from its keypool?

I'm especially curious about scanning tx inputs to recognize coins we have spent. Do we only check outpoints stored in the walletdb? Or do we go as far as inspecting input scripts and witness stacks for our own public keys?

**Tags:** bitcoin-core, wallet, database, rescan


---

## 3 answers

### Answer by username_3 on 2024-01-28. Score: 5 | Accepted answer

> Does the wallet database store a table of all the actual output scripts (i.e. witness programs) derived from its keypool?

Kind of.

For descriptor wallets, the set of all scriptPubKeys (up to the keypool limit for each descriptor) is generated and held in memory. When determining whether a transaction output belongs to the wallet, the wallet checks whether the output's scriptPubKey is in that set. If it is, then the output belongs to the wallet.

One thing to note though is that this scriptPubKey set is not stored in the database. Once the wallet is unloaded, the set is forgotten. It will be recomputed every time the wallet is loaded.

For legacy wallets, it's a lot more complicated. It will determine the type of script and then do lookups for scripts and keys in the wallet depending on the type. For the most part, it does boil down to a finite set of scripts, but this can actually be a very large set that contains a lot of scripts that you might not expect it to.

> I'm especially curious about scanning tx inputs to recognize coins we have spent. Do we only check outpoints stored in the walletdb?

Yes. For inputs, the wallet will only check to see if the referenced output exists in the wallet *and* that output belongs to the wallet. This also means that rescanning must be done serially. If the wallet scanned a transaction that contains a spend of an output that belongs to it, but it hadn't scanned the transaction creating that output yet, then it would not detect the spending transaction.

One unexpected aspect of the Bitcoin Core wallet is that it doesn't explicitly mark which outputs actually belong to it. If it needs to know whether an output belongs to the wallet, it will check the scriptPubKey against the in-memory scriptPubKey set. Similarly, it doesn't explicitly permanently mark outputs as being spent. If you were to remove the spending transaction from your wallet using `removeprunedfunds`, the output would suddenly appear to be unspent and your wallet could make a transaction spending it. Of course, this would also make an invalid transaction.


---

### Answer by username_1 on 2024-01-23. Score: 6

> Does the wallet database store a table of all the actual output scripts (i.e. witness programs) derived from its keypool?

Yes, exactly. The descriptors (and for legacy wallet, legacy data) is expanded into a set of scriptPubKeys to look for.

* A transaction output is ours if its scriptPubKey matches an entry in that set.
* A transaction input is ours if it spends an output that is ours.
* A transaction is ours if it has at least one input or output that is ours.

Note that this does not require examining witness data for inputs. It suffices to look for spends of our outputs (by txid:vout).


---

### Answer by username_2 on 2024-01-24. Score: -3

A wallet examines a transaction for relevance by checking if any of the outputs or inputs are related to its own addresses or scripts. To do this, it needs to know the output scripts (or scriptPubKeys) and the corresponding private keys or scripts that can unlock them. Output descriptors1 are a way of encoding this information in a human-readable string that can be imported or exported by wallets.


---

