<p>To just address the transaction part (the "balance" part has already been addressed pretty well by the others):</p>

<p>When you make a transaction, you <em>must</em> refer to previous transactions pouring into your address as "input". In your transaction, after specifying these inputs, you need to specify output addresses and how much money goes to each. Any leftovers can be outputted back to your address, however if you wish to leave a transaction fee then you just imbalance the output (make it less than the input).</p>

<p>Of course, this in itself doesn't solve the problem of double spending. However, the concept of mining does.</p>

<p>When Bitcoin miners add your block to the chain, they verify that you're not spending bitcoins from an already-spent transaction. Of course, a miner could neglect to do so, but then the mined block would be rejected by the network and he wouldn't get the mining reward. Most nodes are honest nodes, so a large-scale double spend isn't a easy as it seems at first glance.</p>

CC BY-SA 3.0
2013-09-14T18:39:45.247Z
2013-09-14T18:39:45.247Z
Manishearth
<ol>
<li>The network knows your "balance" because all the transactions that credited or debited it are in the blockchain.</li>
<li>If Bob is using a full node then yes, his client knows and parses the blockchain (and also floating transactions). However, it doesn't need to go over the whole thing every time, there are more efficient data structures for that. Also, some people use lightweight clients which outsource the work of keeping track of the entire blockchain.</li>
<li>Of course the network doesn't trust your client to "do the right thing", that would be stupid. You must reference a valid previous output, known to the network, in the transaction you sign and broadcast.</li>
</ol>

CC BY-SA 3.0
2013-09-02T09:23:28.437Z
Technically, saying "crediting and debiting an address" and "the network" isn't very correct, as it doesn't work in terms of balances but individually refetenced outputs (as you show in 3.). It may be a useful simplification, though.
Pieter Wuille
CC BY-SA 3.0
2013-09-02T09:59:52.603Z
@PieterWuille: I know, of course, and that's the reason for the scare quotes. But I think it's more than just a simplification, it's a higher-level description of what is happening. "Your balance" is how many bitcoins you have; technically this is "the total value of all UTXO spendable by private keys you uniquely control". A tx that spends such a UTXO and outputs to a receiver and a new change address debits your balance, regardless of what it looks like under the hood. The implementation details are not needed to answer the original question.
Meni Rosenfeld
CC BY-SA 3.0
2013-09-02T10:02:49.113Z
@PieterWuille: Likewise for "the network", the network isn't a monolithic entity but if the nodes follow the protocol rules and have reasonable connectivity, then for the purpose of this question, it behaves like one.
Meni Rosenfeld
CC BY-SA 3.0
2013-09-02T13:10:36.100Z
thanks for the explanations! Now I understand better. Unfortunately, I cannot mark 2 posts as correct answers...
TomS
CC BY-SA 3.0
2013-09-02T13:46:35.530Z
@TomS: You can still upvote. Not asking that you do, just saying.
Meni Rosenfeld
CC BY-SA 3.0
2013-09-02T16:39:22.597Z
I knew, but I had too less reputation here. I need 15. Now I have 16 and I upvoted. :-)
TomS
CC BY-SA 3.0
2013-09-02T19:52:57.823Z
@TomS: To be young and in newbie jail... I remember how that feels :)
Meni Rosenfeld
CC BY-SA 3.0
2013-09-02T06:57:16.480Z
2013-09-02T06:57:16.480Z
Meni Rosenfeld
<p>Bitcoin doesn't track account balances like traditional accounting, it only keeps a record of where BTC came from and which address it is sent to. So, your receiving address doesn't technically have a recorded balance, only a calculated balance for all the transactions received on that address. Once you send Bitcoin, the unspent transaction output you reference when sending becomes spent and the calculated balance for that address you originally received on becomes zero.</p>

<p>You may find this video of assistance:<br>
<a href="https://youtu.be/Lx9zgZCMqXE" rel="nofollow noreferrer">How Bitcoin Works Under the Hood</a></p>

CC BY-SA 3.0
2018-01-27T01:18:55.010Z
2018-01-27T01:18:55.010Z
Willtech
<p>Let me explain the balance calculation in terms of implementation.</p>

<p>Balance is nothing but the summation of all outputs amounts, this user has gotten so far(right from the beginning of chain). And to calculate this value, we have to iterate over all the transactions-outputs objects which belong to this user and add them all. This simply means that all amounts that this particular user has gotten so far. </p>

<p>But while calculating the balance, we also have to keep in mind that, the user have spent some money or not. If he has then, we only have to add up those outputs which he got after his latest spent transaction.</p>

<p>So these are the steps to calculate balance:</p>

<ul>
<li>Fetch all transactions from block, right from the block 0 by iterating over Blockchain.</li>
<li>Get the timestamp and amount at that timestamp of his latest spent transactions.

<ul>
<li>To get latest spent tx, first get all transactions that were created by this wallet i.e txs whose inputs is his public key.</li>
<li>Now get the most latest tx done by him.</li>
<li>Get the timestamp of that latest tx.</li>
<li>Also record the balance at time point of time.</li>
</ul></li>
<li>Get all the outputs to his address after this timestamp and add them all along with the recorded balance to get the whole balance.</li>
</ul>

CC BY-SA 4.0
2018-09-25T09:37:42.540Z
Downvote: Timestamps have nothing to do with implementing a way for a wallet to track it's balance (or at least, not an efficient way of doing so). The wallet simply needs to find the sum of all UTXOs it controls in the current global UTXO set (perhaps including currently unconfirmed transactions as well).
chytrik
CC BY-SA 4.0
2018-09-25T06:17:34.220Z
2018-09-25T06:17:34.220Z
Vishwas Bhushan
<p>The network doesn't know your current balance.</p>

<p>The bitcoin network doesn't use the concept of "balances".  Instead the Bitcoin ledger tracks assignments of value.  So every bitcoin that exists was initially claimed by a miner, and then there have been assignments of value ever since.  </p>

<p>Now a bitcoin client can show a "balance" for an address but that balance is simply the result of looking at all the transactions for an address and determining which payment assignments made to that address remain unspent.</p>

<p>An unspent transaction is referred to as an UTXO (unspent transaction output).</p>

<p>So a balance for an address is simply all the UTXOs for that address.   This can be determined from the blockchain.  This is a huge amount of transaction data but that's why the Bitcoin-Qt/bitcoind client uses LevelDB which is very efficient at retrieving from a dataset like the blockchain.</p>

CC BY-SA 3.0
2017-11-29T23:40:52.837Z
I thought the unspent transactions are stored in a separate database. Why not query this (smaller) database for finding the balance?
sandyp
CC BY-SA 3.0
2018-01-26T19:32:28.890Z
I'm confused about your answer. You say "It doen't", but then contradictorily say "looking at all the transactions for an address and determining which payment assignments made to that address remain unspent" . Note in the question, there's "Does my Bitcon client fully dive into the block chain and calculates the balance?". I would then expect: "Yes, it does".
Rafael Xavier
CC BY-SA 3.0
2018-01-27T01:17:41.880Z
My answer was to the question in the title.  I've edited my reply to be more clear.   The rest of my reply is correct.  The "network" (actually, the block chain) holds the information that can be used to compute a balance (sum of unspent UTXOs for an address), but it does not hold any attribute that would show the balance.  That's up to the client to do.
Stephen Gornick
CC BY-SA 4.0
2021-01-03T12:49:05.413Z
@sandyp Who would you trust to provide you that separate database? If you have someone you trust, then sure, you could do that.
David Schwartz
CC BY-SA 3.0
2013-09-01T21:35:18.103Z
2018-01-27T01:13:22.197Z
2018-01-27T01:13:22.197Z
Stephen Gornick
<p>Maybe I haven't yet fully understood how transactions work. And if so, please forgive me my stupid question.</p>
<p>But I assume it to be &quot;just&quot; a digitally signed message like &quot;send 1 BTC to <em>Bob</em>&quot; where <em>Bob</em> is identified by its public key (of course obeying to some protocol). The transaction is then signed with my private key which prevents later modification and then sent to the network for hashing and block-building...</p>
<p>I'm not sure how Bob is notified of the transaction.
Does his client software really have to parse the block chain to find the transaction where his public key is listed?</p>
<p>And even more interesting is the question how the sender of the money is restricted to only send as much money as they have? Is this all managed by the Bitcoin client?
If so, I could write my own software which just uses the (or: any) amount of (Bitcoin) money together with the recipient's public key and build the transaction ASN specified somewhere in the protocol.
Who checks the balance and when is it checked?
Does my Bitcoin client fully dive into the block chain and calculates the balance?
Is this then trusted by the network?
Or does the network (e.g. when building and hashing the block) also a verification of all the public keys from their first transactions on?</p>

CC BY-SA 3.0
2013-09-01T19:03:23.313Z
The link below explains it a little. The blockchain keeps track of every transaction and the clients either keep a local copy or use a cloud-hosted version.  The blockchain won't accept a transaction it seems invalid.  http://bitcoin.org/en/how-it-works
Neil Neyman
CC BY-SA 3.0
2013-09-01T21:27:47.377Z
possible duplicate of [How does Bitcoin keep track of account balances?](http://bitcoin.stackexchange.com/questions/5997/how-does-bitcoin-keep-track-of-account-balances)
Stephen Gornick
<balance>
How does the network know my current balance?
TomS
# How does the network know my current balance?

(Asked by: username_0 on 2013-09-01)

Maybe I haven't yet fully understood how transactions work. And if so, please forgive me my stupid question.

But I assume it to be "just" a digitally signed message like "send 1 BTC to *Bob*" where *Bob* is identified by its public key (of course obeying to some protocol). The transaction is then signed with my private key which prevents later modification and then sent to the network for hashing and block-building...

I'm not sure how Bob is notified of the transaction.
Does his client software really have to parse the block chain to find the transaction where his public key is listed?

And even more interesting is the question how the sender of the money is restricted to only send as much money as they have? Is this all managed by the Bitcoin client?
If so, I could write my own software which just uses the (or: any) amount of (Bitcoin) money together with the recipient's public key and build the transaction ASN specified somewhere in the protocol.
Who checks the balance and when is it checked?
Does my Bitcoin client fully dive into the block chain and calculates the balance?
Is this then trusted by the network?
Or does the network (e.g. when building and hashing the block) also a verification of all the public keys from their first transactions on?

**Tags:** balance

> **Comments:**
> **username_1**: The link below explains it a little. The blockchain keeps track of every transaction and the clients either keep a local copy or use a cloud-hosted version.  The blockchain won't accept a transaction it seems invalid.  http://bitcoin.org/en/how-it-works  
> **username_2**: possible duplicate of [How does Bitcoin keep track of account balances?](http://bitcoin.stackexchange.com/questions/5997/how-does-bitcoin-keep-track-of-account-balances)  

---

## 5 answers

### Answer by username_2 on 2013-09-01. Score: 19 | Accepted answer

The network doesn't know your current balance.

The bitcoin network doesn't use the concept of "balances". Instead the Bitcoin ledger tracks assignments of value. So every bitcoin that exists was initially claimed by a miner, and then there have been assignments of value ever since.

Now a bitcoin client can show a "balance" for an address but that balance is simply the result of looking at all the transactions for an address and determining which payment assignments made to that address remain unspent.

An unspent transaction is referred to as an UTXO (unspent transaction output).

So a balance for an address is simply all the UTXOs for that address. This can be determined from the blockchain. This is a huge amount of transaction data but that's why the Bitcoin-Qt/bitcoind client uses LevelDB which is very efficient at retrieving from a dataset like the blockchain.

> **Comments:**
> **username_9**: I thought the unspent transactions are stored in a separate database. Why not query this (smaller) database for finding the balance?  
> **username_10**: I'm confused about your answer. You say "It doen't", but then contradictorily say "looking at all the transactions for an address and determining which payment assignments made to that address remain unspent" . Note in the question, there's "Does my Bitcon client fully dive into the block chain and calculates the balance?". I would then expect: "Yes, it does".  
> **username_2**: My answer was to the question in the title.  I've edited my reply to be more clear.   The rest of my reply is correct.  The "network" (actually, the block chain) holds the information that can be used to compute a balance (sum of unspent UTXOs for an address), but it does not hold any attribute that would show the balance.  That's up to the client to do.  
> **username_11**: @username_9 Who would you trust to provide you that separate database? If you have someone you trust, then sure, you could do that.  

---

### Answer by username_3 on 2013-09-14. Score: 3

To just address the transaction part (the "balance" part has already been addressed pretty well by the others):

When you make a transaction, you *must* refer to previous transactions pouring into your address as "input". In your transaction, after specifying these inputs, you need to specify output addresses and how much money goes to each. Any leftovers can be outputted back to your address, however if you wish to leave a transaction fee then you just imbalance the output (make it less than the input).

Of course, this in itself doesn't solve the problem of double spending. However, the concept of mining does.

When Bitcoin miners add your block to the chain, they verify that you're not spending bitcoins from an already-spent transaction. Of course, a miner could neglect to do so, but then the mined block would be rejected by the network and he wouldn't get the mining reward. Most nodes are honest nodes, so a large-scale double spend isn't a easy as it seems at first glance.


---

### Answer by username_4 on 2013-09-02. Score: 2

1. The network knows your "balance" because all the transactions that credited or debited it are in the blockchain.
2. If Bob is using a full node then yes, his client knows and parses the blockchain (and also floating transactions). However, it doesn't need to go over the whole thing every time, there are more efficient data structures for that. Also, some people use lightweight clients which outsource the work of keeping track of the entire blockchain.
3. Of course the network doesn't trust your client to "do the right thing", that would be stupid. You must reference a valid previous output, known to the network, in the transaction you sign and broadcast.

> **Comments:**
> **username_5**: Technically, saying "crediting and debiting an address" and "the network" isn't very correct, as it doesn't work in terms of balances but individually refetenced outputs (as you show in 3.). It may be a useful simplification, though.  
> **username_4**: @username_5: I know, of course, and that's the reason for the scare quotes. But I think it's more than just a simplification, it's a higher-level description of what is happening. "Your balance" is how many bitcoins you have; technically this is "the total value of all UTXO spendable by private keys you uniquely control". A tx that spends such a UTXO and outputs to a receiver and a new change address debits your balance, regardless of what it looks like under the hood. The implementation details are not needed to answer the original question.  
> **username_4**: @username_5: Likewise for "the network", the network isn't a monolithic entity but if the nodes follow the protocol rules and have reasonable connectivity, then for the purpose of this question, it behaves like one.  
> **username_0**: thanks for the explanations! Now I understand better. Unfortunately, I cannot mark 2 posts as correct answers...  
> **username_4**: @username_0: You can still upvote. Not asking that you do, just saying.  
> **username_0**: I knew, but I had too less reputation here. I need 15. Now I have 16 and I upvoted. :-)  
> **username_4**: @username_0: To be young and in newbie jail... I remember how that feels :)  

---

### Answer by username_6 on 2018-01-27. Score: 1

Bitcoin doesn't track account balances like traditional accounting, it only keeps a record of where BTC came from and which address it is sent to. So, your receiving address doesn't technically have a recorded balance, only a calculated balance for all the transactions received on that address. Once you send Bitcoin, the unspent transaction output you reference when sending becomes spent and the calculated balance for that address you originally received on becomes zero.

You may find this video of assistance:  
[How Bitcoin Works Under the Hood](https://youtu.be/Lx9zgZCMqXE)


---

### Answer by username_7 on 2018-09-25. Score: -1

Let me explain the balance calculation in terms of implementation.

Balance is nothing but the summation of all outputs amounts, this user has gotten so far(right from the beginning of chain). And to calculate this value, we have to iterate over all the transactions-outputs objects which belong to this user and add them all. This simply means that all amounts that this particular user has gotten so far.

But while calculating the balance, we also have to keep in mind that, the user have spent some money or not. If he has then, we only have to add up those outputs which he got after his latest spent transaction.

So these are the steps to calculate balance:

* Fetch all transactions from block, right from the block 0 by iterating over Blockchain.
* Get the timestamp and amount at that timestamp of his latest spent transactions.
  + To get latest spent tx, first get all transactions that were created by this wallet i.e txs whose inputs is his public key.
  + Now get the most latest tx done by him.
  + Get the timestamp of that latest tx.
  + Also record the balance at time point of time.
* Get all the outputs to his address after this timestamp and add them all along with the recorded balance to get the whole balance.

> **Comments:**
> **username_8**: Downvote: Timestamps have nothing to do with implementing a way for a wallet to track it's balance (or at least, not an efficient way of doing so). The wallet simply needs to find the sum of all UTXOs it controls in the current global UTXO set (perhaps including currently unconfirmed transactions as well).  

---

