w.r.t. a bitcoin transaction, I am really struggling to understand how does bitcoin network validate that I 1) have the bitcoin that I want to send, and 2) haven’t already sent it to someone else.
If my account balance was being kept track of then it would have been easy. The nodes would do a simple if check to test if I have sufficient balance in my account before I spend any money. But as I have learnt a bitcoin transaction destroys bitcoins and generates new ones (UTXO). So a transaction looks like:
FromAddress, ToAddress, Id_of_Bitcoin_being_destroyed, Id_of_bitcoin_being_created
The above assumes that I have the exact change I want to pay - since that is sufficient for purposes of my question.
So now before committing this transaction, the nodes need to verify:
isDestroyed(Id_of_Bitcoin_being_destroyed) == false; i.e. coin has not been spent already
Owner(Id_of_Bitcoin_being_destroyed) == me; i.e., coin belongs to me
could someone please explain in detail how is this happening?
Is a node doing a reverse table scan (i.e., scan the blockchain starting from most recent transaction to oldest one) until it locates Id_of_Bitcoin_being_destroyed
? Then it can answer both questions above. However, this algorithm cannot scale at all and is the crux of what I am trying to understand.
To make it concrete lets say Id_of_Bitcoin_being_destroyed = 1234
. A reverse table scan happens and following record is located:
From:Me, To:Brian, 1234, 5678
The node detects I have already spent the coin and transaction fails.
Another example. In this case a reverse table scan finds:
From:Brian, To:Me, 5678, 1234
The node can say with confidence that I own the coin and it has not been spent.
I have sincerely tried very hard to understand this and find the answer online including looking at [1, 2] but was unsuccessful. E.g., 1 just says this:
This transaction can be validated – that is, it can be confirmed that I own the bitcoin that I am transferring to you, and that I haven’t already sent it to someone else – by plugging the signature and my public key (which everyone knows) into the bitcoin program.
But this leaves much unexplained to me.