14

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 dititally signed mesaage 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 checkes the balance and when is it checked? Does my Bitcon 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?

10

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.

  • I thought the unspent transactions are stored in a separate database. Why not query this (smaller) database for finding the balance? – sandyp Nov 29 '17 at 23:40
  • 2
    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 Jan 26 '18 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. – Stephen Gornick Jan 27 '18 at 1:17
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.
  • 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. – Pieter Wuille Sep 2 '13 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. – Meni Rosenfeld Sep 2 '13 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. – Meni Rosenfeld Sep 2 '13 at 10:02
  • thanks for the explanations! Now I understand better. Unfortunately, I cannot mark 2 posts as correct answers... – TomS Sep 2 '13 at 13:10
  • @TomS: You can still upvote. Not asking that you do, just saying. – Meni Rosenfeld Sep 2 '13 at 13:46
2

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.

0

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
    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 Sep 25 '18 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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