<p>At the protocol level, Bitcoin has no concept of &quot;balances&quot; or &quot;addresses&quot;, and their common interpretation isn't relevant during verification.</p>
<p>Every transaction spends &quot;coins&quot; and reforges them into new &quot;coins&quot; (called UTXOs). Every UTXO has a value (in number of satoshis) and an locking script. UTXOs are always spent in their entirety. If you only have a 1 BTC UTXO and want to send 0.1 BTC to someone, your wallet will under the hood create a transaction that has one input (your 1 BTC UTXO) and two outputs (one 0.1 BTC UTXO with an locking script created from the receiver's address, and one 0.9 BTC UTXO with an locking script you can unlock yourself). From a protocol perspective, there is nothing special about this UTXO that goes back to yourself (called a &quot;change output&quot;) - it's just another UTXO.</p>
<p>Transaction explicitly identify which previous coins they're spending, based on the txid that created them, not based on the locking script. So it doesn't matter what the sum of the values of UTXOs with a particular locking script is; coins are spent when they're mentioned as an input in another transaction. That means the only thing that matters is:</p>
<ul>
<li>Do the input UTXOs exist?</li>
<li>Is the sum of the input UTXO values (whether they have the same locking script or not) not less than the sum of the values of the newly created UTXOs.</li>
</ul>
<p>Now, sites like blockexplorers do show balances. They're a higher-level - and misleading - abstraction. They do this by simply maintaining an index per address of all transactions that have the script corresponding to that address as locking script, and all transactions that spends such outputs.</p>
<p>Wallets for privacy reasons construct new addresses all the time (typically one per incoming transaction), as there is no cost to creating more addresses. Looking at it from a per-address perspective is therefore misleading, and has caused people to lose money (when they think &quot;X is my address, I only need its private key&quot; - you need the keys to all addresses, even change addresses, your wallet uses).</p>

CC BY-SA 4.0
2020-11-20T01:55:40.417Z
Thanks, very clear explanation! The only thing I still don't get is how do miners check (quick) if an input UTXO exists?
Stijn de Vries
CC BY-SA 4.0
2020-11-20T02:00:34.090Z
All full nodes maintain a database with all unspent outputs.
Pieter Wuille
CC BY-SA 4.0
2020-11-20T14:06:30.767Z
Stumbled a bit on the "not less". Maybe "equal or more" would read a bit smoother.
Murch
CC BY-SA 4.0
2022-04-19T16:19:18.377Z
@PieterWuille - does a full node maintains database of all unspent outputs of all addresses in the block or just my node's wallet address?
Sripaul
CC BY-SA 4.0
2022-04-19T16:33:26.577Z
@Sripaul A full node validates all transactions fully; that is not possible without actually having access to the outputs being spent by those transactions. As a result, it needs some way of getting that information. Common full node implementations due that with a database of UTXOs, or a database of all transactions ever. Just your own wallet data isn't sufficient to validate transactions.
Pieter Wuille
CC BY-SA 4.0
2020-11-20T01:24:07.750Z
2020-11-20T01:24:07.750Z
Pieter Wuille
<p>For what I understand, the balance of a Bitcoin address is the total sum of UTXOs of that address in the entire blockchain.
The current size of the blockchain is around 350GB. How is the total sum of UTXOs for a specific address calculated quickly? I imagine looping over every transaction in the entire blockchain and comparing the UTXO with the given address takes a lot of time. Does the calculation of the balance of a Bitcoin address work like that? And do miners repeat this process for every transaction to check if the available balance of that address enough?</p>

<bitcoin-core><transactions><utxo><balance>
How is the balance of a Bitcoin address quickly calculated?
Stijn de Vries
# How is the balance of a Bitcoin address quickly calculated?

(Asked by: username_0 on 2020-11-20)

For what I understand, the balance of a Bitcoin address is the total sum of UTXOs of that address in the entire blockchain.
The current size of the blockchain is around 350GB. How is the total sum of UTXOs for a specific address calculated quickly? I imagine looping over every transaction in the entire blockchain and comparing the UTXO with the given address takes a lot of time. Does the calculation of the balance of a Bitcoin address work like that? And do miners repeat this process for every transaction to check if the available balance of that address enough?

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


---

## 1 answers

### Answer by username_1 on 2020-11-20. Score: 8 | Accepted answer

At the protocol level, Bitcoin has no concept of "balances" or "addresses", and their common interpretation isn't relevant during verification.

Every transaction spends "coins" and reforges them into new "coins" (called UTXOs). Every UTXO has a value (in number of satoshis) and an locking script. UTXOs are always spent in their entirety. If you only have a 1 BTC UTXO and want to send 0.1 BTC to someone, your wallet will under the hood create a transaction that has one input (your 1 BTC UTXO) and two outputs (one 0.1 BTC UTXO with an locking script created from the receiver's address, and one 0.9 BTC UTXO with an locking script you can unlock yourself). From a protocol perspective, there is nothing special about this UTXO that goes back to yourself (called a "change output") - it's just another UTXO.

Transaction explicitly identify which previous coins they're spending, based on the txid that created them, not based on the locking script. So it doesn't matter what the sum of the values of UTXOs with a particular locking script is; coins are spent when they're mentioned as an input in another transaction. That means the only thing that matters is:

* Do the input UTXOs exist?
* Is the sum of the input UTXO values (whether they have the same locking script or not) not less than the sum of the values of the newly created UTXOs.

Now, sites like blockexplorers do show balances. They're a higher-level - and misleading - abstraction. They do this by simply maintaining an index per address of all transactions that have the script corresponding to that address as locking script, and all transactions that spends such outputs.

Wallets for privacy reasons construct new addresses all the time (typically one per incoming transaction), as there is no cost to creating more addresses. Looking at it from a per-address perspective is therefore misleading, and has caused people to lose money (when they think "X is my address, I only need its private key" - you need the keys to all addresses, even change addresses, your wallet uses).

> **Comments:**
> **username_0**: Thanks, very clear explanation! The only thing I still don't get is how do miners check (quick) if an input UTXO exists?  
> **username_1**: All full nodes maintain a database with all unspent outputs.  
> **username_2**: Stumbled a bit on the "not less". Maybe "equal or more" would read a bit smoother.  
> **username_3**: @username_1 - does a full node maintains database of all unspent outputs of all addresses in the block or just my node's wallet address?  
> **username_1**: @username_3 A full node validates all transactions fully; that is not possible without actually having access to the outputs being spent by those transactions. As a result, it needs some way of getting that information. Common full node implementations due that with a database of UTXOs, or a database of all transactions ever. Just your own wallet data isn't sufficient to validate transactions.  

---

