<p>The first time you run bitcoin it will download and process every single block and will generate an index of all unspent outputs like</p>
<pre><code>{ ‘outputABC’ : 1 btc
  ‘outputDEF’ : 3 btc
} 
</code></pre>
<p>etc. When this table is constructed you use it to lookup all the values of the outputs owned by each address in your wallet.</p>

CC BY-SA 4.0
2022-01-29T15:45:47.697Z
Thanks, that makes a lot of sense!
Ryan
CC BY-SA 4.0
2022-01-29T13:13:51.897Z
2022-01-29T13:13:51.897Z
Mike D
<p>Your intuition is correct: in order to compute a wallet's balance <code>bitcoind</code> will scan through the entire blockchain, looking for a set of addresses that correspond to its own private keys. As you point out this is a rather time-consuming task. However we should note here that <code>bitcoind</code> needs to iterate through the blockchain and verify all transactions for validity anyway when bootstrapping, so checking if an address belongs to its wallet is rather trivial, compared to verifying scripts and signatures, so it's not adding much to that bootstrap time.</p>
<p>Once you are bootstrapped, you know that anything up to the point you verified is not going to change, so you can just incrementally scan new blocks as they come it, which often takes less than a second with todays code.</p>
<p>It is possible to build indices on which address was used in which transaction, which can help locating transactions that involve a certain transaction quickly, and there are wallets that make use of this (electrum, and all hardware wallets), however this is usefull mostly when you don't know all addresses at verification time, and you want to serve this information to users so they don't have to sync it themselves. To build the index however they themselves had to verify the entire chain. This is true for all blockchains by the way, either you are verifying the chain from its genesis, or you are trusting a third-party not to lie by omission (they can not report addresses that are yours), this is for example the case for metamask as well.</p>
<p>One final detail is that some seed schemas have a birthday date / height, which allows a wallet to start scanning from that height, since the seed basically says &quot;don't bother scanning below, I had no addresses before this time&quot;. This saving is less efficient the older the seed is, since the skipped part will become smaller compared to the part that needs to be processed.</p>

CC BY-SA 4.0
2022-01-29T13:13:34.073Z
2022-01-29T13:13:34.073Z
cdecker
<p>If I open Metamask or Coinbase, for example, I see a balance. This balance is a flat number, similar to what we'd see in a bank to more intuitively check my networth or balance of a particular wallet.</p>
<p>I'm wondering how this is done, from a programmatic perspective. I think I kind of understand this idea of UTXO where if I have coins like (1 coin, 3 coin, 5 coin) and Alice charges 7 coin for some service, I would send 5 coin and 3 coin as input. The output would be 7 coin -&gt; Alice wallet and 1 coin back to my wallet. (Let's ignore fees).</p>
<p>But how do I derive the balance of my wallet?</p>
<p>Intuitively, I get I could start at the genesis block and iterate linearly through the blockchain, however, each block has N transactions, so this seems like a simple derivation would entail scanning N blocks * M transactions/block.</p>
<p>This seems inefficient. How can I understand this better?</p>

<blockchain><wallet>
Is value of a wallet derived by iterating over every block in the chain, scanning each transaction?
Ryan
# Is value of a wallet derived by iterating over every block in the chain, scanning each transaction?

(Asked by: username_0 on 2022-01-29)

If I open Metamask or Coinbase, for example, I see a balance. This balance is a flat number, similar to what we'd see in a bank to more intuitively check my networth or balance of a particular wallet.

I'm wondering how this is done, from a programmatic perspective. I think I kind of understand this idea of UTXO where if I have coins like (1 coin, 3 coin, 5 coin) and Alice charges 7 coin for some service, I would send 5 coin and 3 coin as input. The output would be 7 coin -> Alice wallet and 1 coin back to my wallet. (Let's ignore fees).

But how do I derive the balance of my wallet?

Intuitively, I get I could start at the genesis block and iterate linearly through the blockchain, however, each block has N transactions, so this seems like a simple derivation would entail scanning N blocks \* M transactions/block.

This seems inefficient. How can I understand this better?

**Tags:** blockchain, wallet


---

## 2 answers

### Answer by username_2 on 2022-01-29. Score: 6 | Accepted answer

Your intuition is correct: in order to compute a wallet's balance `bitcoind` will scan through the entire blockchain, looking for a set of addresses that correspond to its own private keys. As you point out this is a rather time-consuming task. However we should note here that `bitcoind` needs to iterate through the blockchain and verify all transactions for validity anyway when bootstrapping, so checking if an address belongs to its wallet is rather trivial, compared to verifying scripts and signatures, so it's not adding much to that bootstrap time.

Once you are bootstrapped, you know that anything up to the point you verified is not going to change, so you can just incrementally scan new blocks as they come it, which often takes less than a second with todays code.

It is possible to build indices on which address was used in which transaction, which can help locating transactions that involve a certain transaction quickly, and there are wallets that make use of this (electrum, and all hardware wallets), however this is usefull mostly when you don't know all addresses at verification time, and you want to serve this information to users so they don't have to sync it themselves. To build the index however they themselves had to verify the entire chain. This is true for all blockchains by the way, either you are verifying the chain from its genesis, or you are trusting a third-party not to lie by omission (they can not report addresses that are yours), this is for example the case for metamask as well.

One final detail is that some seed schemas have a birthday date / height, which allows a wallet to start scanning from that height, since the seed basically says "don't bother scanning below, I had no addresses before this time". This saving is less efficient the older the seed is, since the skipped part will become smaller compared to the part that needs to be processed.


---

### Answer by username_1 on 2022-01-29. Score: 2

The first time you run bitcoin it will download and process every single block and will generate an index of all unspent outputs like

```
{ ‘outputABC’ : 1 btc
  ‘outputDEF’ : 3 btc
} 

```

etc. When this table is constructed you use it to lookup all the values of the outputs owned by each address in your wallet.

> **Comments:**
> **username_0**: Thanks, that makes a lot of sense!  

---

