Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: bji on June 21, 2011, 06:45:09 AM



Title: Question about how transactions are validated
Post by: bji on June 21, 2011, 06:45:09 AM
I just want to confirm my understanding of how transactions are structured and what miners need to do to verify transactions.  I'd like to do so using an example.

Say that Bitcoin address A is first referenced in block N when it is given 50 coins by the coinbase.

Next it is in block N + 1 where it is the output for a transaction that gives it 40 coins from Bitcoin address B.

Next it is in block N + 2 where it is the output for a transaction that gives it 5 coins from Bitcoin address C.

Next it is in block N + 3 where it is the output for a transaction that gives it 1 coin from Bitcoin address D.

Now a miner sees it in block N + 4 giving 100 coins to bitcoin address E.

Is it true that the miner has to trace back through the history of all blocks (in this case the only relevent blocks turn out to be N through N + 3, but the miner doesn't know that until it traces back all the way to the origin block) in order to verify what the balance is in bitcoin address A and whether or not the total is at least 100 coins (in this case, it isn't, it's only 96 coins, and the transaction would be rejected)?

Of course one would expect that if this were the case, that each miner would keep track, with every block, of the total number of bitcoins now in the bitcoin addresses for which transactions were present in the block, and if it stored this extra information, it could know the balance of each bitcoin address just by looking at the balance in the most recent block in which a transaction involving the address appeared.

Or maybe I'm misunderstanding something?


Title: Re: Question about how transactions are validated
Post by: kjj on June 21, 2011, 06:50:33 AM
Transactions are not address to address.  They are transaction to address.


Title: Re: Question about how transactions are validated
Post by: bji on June 21, 2011, 08:01:43 AM
Transactions are not address to address.  They are transaction to address.

Thanks for the correction.

So replace 'address' with 'transaction' in my original question.  Did I get the right answer?


Title: Re: Question about how transactions are validated
Post by: Gr.Green on June 21, 2011, 08:26:02 AM
He'd need to list all input transactions from blocks N... N+3 as a source of funds. This will allow the network to verify the transaction without having to do a search.

What I don't understand is how do they know which transaction is in which block. Do they build a mega map of all transaction-to-block relationships and look that up? This would grow like mad.

Seems as the number of transactions grows the cost of looking up the validity would become more than the cost to compute the hash. Maybe there would be separate parts of the network responsible for each task and they would split the transaction fees.


Title: Re: Question about how transactions are validated
Post by: Pieter Wuille on June 21, 2011, 08:57:42 AM
No they just keep a map of all unspent transactions.


Title: Re: Question about how transactions are validated
Post by: bji on June 21, 2011, 09:09:45 AM
He'd need to list all input transactions from blocks N... N+3 as a source of funds. This will allow the network to verify the transaction without having to do a search.

Thanks, I think I'm starting to understand a little better.  I was getting confused and thinking that transactions transfer an amount from one bitcoin address to another.

What transactions really seem to do is simply to name outputs from other transactions as inputs to this transaction, provide 'script' which when combined with the output script of those transactions evaluates to true, thus proving the right to 'claim' the entire set of funds declared in the output, and then set up a new set of outputs with script that would then be combined with the next transaction to set up another such sequence.

So there is no 'adding bitcoins to a bitcoin address', there is only 'rewriting a set of old outputs with a new set of outputs' with the inputs used as a mechanism to identify, and provide proof of the claim to, the old outputs.

And so to verify a transaction, a miner just needs to verify that the inputs properly claim outputs of a transactions which themselves were validated, never needing to go further back than the transactions directly referenced by a new transaction.

Quote
What I don't understand is how do they know which transaction is in which block. Do they build a mega map of all transaction-to-block relationships and look that up? This would grow like mad.

Seems like they must, doesn't it?  But I guess they can also forget about individual transaction outputs once they have been claimed and then can forget the whole transaction once all of its outputs have been claimed, since no valid transaction can ever reference those outputs again.

Quote
Seems as the number of transactions grows the cost of looking up the validity would become more than the cost to compute the hash. Maybe there would be separate parts of the network responsible for each task and they would split the transaction fees.

Yeah having to keep track of every unclaimed transaction output for all perpetuity sounds very hard when there are significant numbers of unclaimed transactions.

And having to swallow the feed of transactions (both unverified, and verified within blocks) when there are hundreds of transactions per second sounds even harder.