<p>Bitcoin uses the so called <strong>UTXO</strong> (Unspent Transaction Output) model.<br />
The first time it is not very intuitive because it is different from the traditional accounting model where you just spend from and send to an account.<br />
Think about full piggy banks that can't be filled more than they are and that you have to break open before being able to spend the content:</p>
<ol>
<li>Bob has 1 piggy bank, he wants to pay Alice with half of the piggy bank's content.</li>
<li>He breaks his piggy bank.</li>
<li>He takes half of it and gives it to Alice, who will put it all in a new piggy bank belonging to her.</li>
<li>You put the remaining half in a new smaller piggy bank belonging to you and which is waiting to be broken open.</li>
</ol>
<p>Each piggy bank/UTXO is linked to an address.
A Bitcoin user can of course have several UTXOs related to his address. The sum of the UTXOs gives the amount of bitcoins owned by the address (which is one of the function of wallets).<br />
Each transaction input is the output of a previous transaction.
Although wallet software gives the feeling that you &quot;transfer&quot; bitcoins or satoshis, you <strong>don't spend from an account</strong> you <strong>spend UTXO</strong>.<br />
You spend from transactions previously made to you.</p>

CC BY-SA 4.0
2018-07-31T13:51:16.963Z
2021-10-22T18:30:09.037Z
2021-10-22T18:30:09.037Z
sripwoud
<p>A somewhat more intuitive way to think about balance calculations is to think of you you use an actual fiat wallet.</p>

<p>If you want to look at how much money you physically have <em>right now</em>, you would open up your wallet, take out all the notes of varying denominations, and sum them up. This gives you your balance.</p>

<p>Bitcoin does something similar, where an address is similar to your physical wallet. A single address may have multiple utxos linked to it. The balance for that address is the sum of all the utxos.</p>

<p>The key thing here is that bitcoin only tracks <em>unspent</em> transaction outputs. Thus, to arrive at the balance for the address, it just needs to iterate through the blockchain, add new outputs when they are created, and remove old ones when they are spent. Moreover, this is a extendable process. If you know the utxo set as of block 100, you do not need to recalculate from block 0 to update with data from block 101. You can simply update your set from block 100.</p>

<p>This again is similar to how we use physical wallets. No one keeps track of all the notes and coins they have had in the past (spent transaction outputs). They only keep track of what they have currently (utxo). When a new transaction occurs (say you buy milk), you only update the current state of your wallet by removing a note, and putting the change back. You don't need to calculate it from the first time you used money.</p>

<p>Bitcoin takes this a step further and has a concept of accounts, which are multiple addresses that act as one wallet. A similar analogy would be if everyone in your house pooled the money from all of their wallets, so that you can spend it as one. Again, the balance of an account is simply the sum of the balance of the addresses in it.</p>

CC BY-SA 4.0
2018-08-01T00:13:03.660Z
2018-08-01T00:13:03.660Z
Raghav Sood
<p><strong>Edit:</strong></p>

<h2>Address Balance Calculation</h2>

<p>An address balance is calculated by adding up all of the values of the unspent transaction outputs (UTXOs) that are spendable by the key pair corresponding to that address.</p>

<h2>Storage Structure</h2>

<p><em>To answer the broader question: "why do you scan every transaction on the entire blockchain to determine your balance?"</em></p>

<p>Bitcoin core does not need to know the balance of an address in order to do validation. It does, however need to be able to scan through all previous transaction outputs in order to validate new ones. To do this, it scans a generated indexed database (by txid) of all transactions for the previous outputs referenced by a new transaction, and verifies that the signature satisfies the previous output (UTXO). Note this only requires scanning unspent outputs not all transactions.</p>

<p>Optimizing this data storage for balance calculation by indexing by address and txid might make balance calculation easier, but it isn't necessary for validation and would be at the expense of transaction verification efficiency. It is better to leave this to a wallet application for the sake of the network.</p>

<p><strong>Previous Answer:</strong> </p>

<p><em>The previous answer explains how to index transactions in bitcoin core which can help a wallet or block explorer determine an address balance more efficiently.</em></p>

<p>You can index the transactions on the blockchain to make it more easily searchable. Bitcoin-core allows this using the <code>-txindex=1</code> flag.</p>

<p>From the <a href="https://en.bitcoin.it/wiki/Running_Bitcoin" rel="nofollow noreferrer">Bitcoin Wiki</a>:  </p>

<p><code>txindex</code> | Maintain a full transaction index, used by the getrawtransaction rpc call (default: 0)</p>

CC BY-SA 4.0
2018-07-31T19:45:59.507Z
This has nothing to do with address balances.
Pieter Wuille
CC BY-SA 4.0
2018-07-31T19:55:19.827Z
I agree, the title doesn't seem to match the question. The question talks more about the practicality of scanning all transactions and how that could be done.
JBaczuk
CC BY-SA 4.0
2018-07-31T20:01:54.433Z
And the answer has no relation to that, because the txindex isn't used for validation even.
Pieter Wuille
CC BY-SA 4.0
2018-07-31T20:10:17.987Z
Yeah that makes sense, the question is more high level
JBaczuk
CC BY-SA 4.0
2018-07-31T21:09:41.863Z
Just updated the answer to better address the question.
JBaczuk
CC BY-SA 4.0
2018-07-31T23:31:18.530Z
There is no index of txids (or index whatsoever) used at any point in time during validation.
Pieter Wuille
CC BY-SA 4.0
2018-07-31T23:47:03.113Z
Is this answer referring to a different utxo index, then? https://bitcoin.stackexchange.com/a/29782/60443
JBaczuk
CC BY-SA 4.0
2018-07-31T23:51:22.653Z
Bitcoin Core maintains a database of all unspent transaction outputs. It isn't an index, as it doesn't index into anything else. The original transactions or blocks that created the outputs being spent are not accessed. It's just adding new outputs to the database, and removing the ones that are spent.
Pieter Wuille
CC BY-SA 4.0
2018-07-31T13:32:26.500Z
2018-08-01T15:03:21.470Z
2018-08-01T15:03:21.470Z
JBaczuk
<p>It does not seem practical to scan every transaction on a block chain to determine the balance for an account.  As the block chain grows this seems even more difficult and resource consuming. Perhaps all the transactions are grouped into what we used to call a record, specifically a variable length record.
address..trans1, trans2, etc...transN  for the last transaction. </p>

CC BY-SA 4.0
2018-07-31T15:06:56.380Z
Note that to bitcoin, "address" and "account" are separate things - which one are you talking about?
Caius Jard
<transactions><balance><bitcoin-core-accounts>
How address balance is calculated?
Patrick Loria
# How address balance is calculated?

(Asked by: username_0 on 2018-07-31)

It does not seem practical to scan every transaction on a block chain to determine the balance for an account. As the block chain grows this seems even more difficult and resource consuming. Perhaps all the transactions are grouped into what we used to call a record, specifically a variable length record.
address..trans1, trans2, etc...transN for the last transaction.

**Tags:** transactions, balance, bitcoin-core-accounts

> **Comments:**
> **username_1**: Note that to bitcoin, "address" and "account" are separate things - which one are you talking about?  

---

## 3 answers

### Answer by username_2 on 2018-07-31. Score: 3

Bitcoin uses the so called **UTXO** (Unspent Transaction Output) model.  
The first time it is not very intuitive because it is different from the traditional accounting model where you just spend from and send to an account.  
Think about full piggy banks that can't be filled more than they are and that you have to break open before being able to spend the content:

1. Bob has 1 piggy bank, he wants to pay Alice with half of the piggy bank's content.
2. He breaks his piggy bank.
3. He takes half of it and gives it to Alice, who will put it all in a new piggy bank belonging to her.
4. You put the remaining half in a new smaller piggy bank belonging to you and which is waiting to be broken open.

Each piggy bank/UTXO is linked to an address.
A Bitcoin user can of course have several UTXOs related to his address. The sum of the UTXOs gives the amount of bitcoins owned by the address (which is one of the function of wallets).  
Each transaction input is the output of a previous transaction.
Although wallet software gives the feeling that you "transfer" bitcoins or satoshis, you **don't spend from an account** you **spend UTXO**.  
You spend from transactions previously made to you.


---

### Answer by username_3 on 2018-08-01. Score: 1

A somewhat more intuitive way to think about balance calculations is to think of you you use an actual fiat wallet.

If you want to look at how much money you physically have *right now*, you would open up your wallet, take out all the notes of varying denominations, and sum them up. This gives you your balance.

Bitcoin does something similar, where an address is similar to your physical wallet. A single address may have multiple utxos linked to it. The balance for that address is the sum of all the utxos.

The key thing here is that bitcoin only tracks *unspent* transaction outputs. Thus, to arrive at the balance for the address, it just needs to iterate through the blockchain, add new outputs when they are created, and remove old ones when they are spent. Moreover, this is a extendable process. If you know the utxo set as of block 100, you do not need to recalculate from block 0 to update with data from block 101. You can simply update your set from block 100.

This again is similar to how we use physical wallets. No one keeps track of all the notes and coins they have had in the past (spent transaction outputs). They only keep track of what they have currently (utxo). When a new transaction occurs (say you buy milk), you only update the current state of your wallet by removing a note, and putting the change back. You don't need to calculate it from the first time you used money.

Bitcoin takes this a step further and has a concept of accounts, which are multiple addresses that act as one wallet. A similar analogy would be if everyone in your house pooled the money from all of their wallets, so that you can spend it as one. Again, the balance of an account is simply the sum of the balance of the addresses in it.


---

### Answer by username_4 on 2018-07-31. Score: -1

**Edit:**

## Address Balance Calculation

An address balance is calculated by adding up all of the values of the unspent transaction outputs (UTXOs) that are spendable by the key pair corresponding to that address.

## Storage Structure

*To answer the broader question: "why do you scan every transaction on the entire blockchain to determine your balance?"*

Bitcoin core does not need to know the balance of an address in order to do validation. It does, however need to be able to scan through all previous transaction outputs in order to validate new ones. To do this, it scans a generated indexed database (by txid) of all transactions for the previous outputs referenced by a new transaction, and verifies that the signature satisfies the previous output (UTXO). Note this only requires scanning unspent outputs not all transactions.

Optimizing this data storage for balance calculation by indexing by address and txid might make balance calculation easier, but it isn't necessary for validation and would be at the expense of transaction verification efficiency. It is better to leave this to a wallet application for the sake of the network.

**Previous Answer:**

*The previous answer explains how to index transactions in bitcoin core which can help a wallet or block explorer determine an address balance more efficiently.*

You can index the transactions on the blockchain to make it more easily searchable. Bitcoin-core allows this using the `-txindex=1` flag.

From the [Bitcoin Wiki](https://en.bitcoin.it/wiki/Running_Bitcoin):

`txindex` | Maintain a full transaction index, used by the getrawtransaction rpc call (default: 0)

> **Comments:**
> **username_5**: This has nothing to do with address balances.  
> **username_4**: I agree, the title doesn't seem to match the question. The question talks more about the practicality of scanning all transactions and how that could be done.  
> **username_5**: And the answer has no relation to that, because the txindex isn't used for validation even.  
> **username_4**: Yeah that makes sense, the question is more high level  
> **username_4**: Just updated the answer to better address the question.  
> **username_5**: There is no index of txids (or index whatsoever) used at any point in time during validation.  
> **username_4**: Is this answer referring to a different utxo index, then? https://bitcoin.stackexchange.com/a/29782/60443  
> **username_5**: Bitcoin Core maintains a database of all unspent transaction outputs. It isn't an index, as it doesn't index into anything else. The original transactions or blocks that created the outputs being spent are not accessed. It's just adding new outputs to the database, and removing the ones that are spent.  

---

