4

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?

1 Answer 1

8

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).

5
  • Thanks, very clear explanation! The only thing I still don't get is how do miners check (quick) if an input UTXO exists? Nov 20, 2020 at 1:55
  • 3
    All full nodes maintain a database with all unspent outputs. Nov 20, 2020 at 2:00
  • 1
    Stumbled a bit on the "not less". Maybe "equal or more" would read a bit smoother.
    – Murch
    Nov 20, 2020 at 14:06
  • @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
    Apr 19, 2022 at 16:19
  • @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. Apr 19, 2022 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.