by xg15 · 8y ago · 0 comments
Suppose I have an address with 0BTC balance. (i.e. I have the private key of it on my PC. There isn't yet anything about that address in the blockchain.)
Some other person sends 100mBTC to that address. I can see that the transaction has spread through the network but it hasn't been added to a block yet.
Because I'm overly trusting and/or in a hurry, I don't wait until it's confirmed but I immediately send another transaction that sends the 100mBTC to some other account.
If a miner first picked transaction #1, added it to a block, then added transaction #2 to the following block, both transactions would be valid. However if a miner tried to process transaction #2 first, it would be invalid.
So what would happen? Would miners bring the transactions into correct order and validate both, only validate #1 and reject #2 or reject both?
So, if I send you 2 BTC to a single address, the transaction will probably have a single output. If you then try to send somebody 1 BTC your transaction has to use the entire 2 BTC input (which was the output from my transaction to you), but will have two outputs, 1 BTC with an output for whoever you're sending it to and another 1 BTC to an address your wallet controls.
So your transaction in the above example has to refer to the 2 BTC from my transaction by unique hash, it's not simply saying "send any 1 BTC from this address". This is all dealt with by your wallet.
Due to this, if transaction #2 was being processed first it would be using an input that didn't exist on the blockchain and would therefore be invalid.