24

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?

2

5 Answers 5

19

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.

4
  • I thought the unspent transactions are stored in a separate database. Why not query this (smaller) database for finding the balance?
    – sandyp
    Commented Nov 29, 2017 at 23:40
  • 4
    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". Commented Jan 26, 2018 at 19:32
  • 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. Commented Jan 27, 2018 at 1:17
  • @sandyp Who would you trust to provide you that separate database? If you have someone you trust, then sure, you could do that. Commented Jan 3, 2021 at 12:49
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.

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.
7
  • 1
    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. Commented Sep 2, 2013 at 9:23
  • 1
    @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. Commented Sep 2, 2013 at 9:59
  • 1
    @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. Commented Sep 2, 2013 at 10:02
  • thanks for the explanations! Now I understand better. Unfortunately, I cannot mark 2 posts as correct answers...
    – TomS
    Commented Sep 2, 2013 at 13:10
  • @TomS: You can still upvote. Not asking that you do, just saying. Commented Sep 2, 2013 at 13:46
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

-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.
1
  • 1
    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
    Commented Sep 25, 2018 at 9:37

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.