The bitcoin transaction system involves sending and receiving whole batches of bitcoins, called outputs.
Fair enough. But to be honest the only way to really understand how this works is to look at a few example transactions.
Transaction 1 - A simple transaction.
Let’s begin this story of transactions with the birth of a fresh batch of bitcoins…
You are mining bitcoins on your own. By some miracle, you have managed to mine a block of transactions and earn yourself a fresh batch of 25 bitcoins.
So this is the current state of your bitcoin address:
Naturally, your first instinct is to celebrate. So let’s use 1 of these bitcoins to buy some beer.
Now, your other first instinct would be to take 1 of these bitcoins (from the batch of 25) to pay for this beer. This would make sense, but it’s not how transactions work.
Instead, we have to send the entire batch of 25 bitcoins in the transaction.
But to make sure we don’t spend all 25 bitcoins in a “1 bitcoin” payment, we split the batch up and send it to two destinations:
- The beer shop. (the payment)
- Back to our own address. (our change)
It’s a bit of an around-the-houses way of doing it, but it achieves the same end-result.
The reason why transactions work this way is because it’s an easier and more secure way of doing it from a programming perspective.
Anyway, this is what the bitcoin addresses look like after the transaction:
So it’s just like we took 1 bitcoin from our address and sent it to another address… but now we know what really happened.
Transaction 2 - Using outputs as inputs.
Okay, from now on we’re going to use the word “output” instead of “batch”.
Anyway, a few days have passed since the beer shop sold us that beer. And judging by the current state of their bitcoin address, the beer business is booming:
But as we all know, beer doesn’t grow on trees. So the beer shop is on the lookout for a brand new beer machine.
Oh look, a lovely beer machine for the low low price of 4.2 bitcoins.
Let’s buy it…
Alright, I realise I’ve just cranked the diagram up a few notches on this one, but it’s not hard to explain:
The beer shop doesn’t have a single output (batch) at their address to cover the cost of the beer machine (4.2). So instead, we gather a handful of outputs together to get a total greater than 4.2.
When we construct a transaction, the outputs we are gathering up to be spent are referred to as the transaction “inputs”.
When you’re using an output in a transaction, it’s temporarily called an “input”.
- Using the total input of 4.5, the beer shop creates two new outputs of 4.2 and 0.3.
And here’s the state of the beer shop’s bitcoin address after the transaction:
Once again, the outputs that were used as inputs have been “spent”, and can’t be used again.
The “unspent” outputs however are still good for spending, so we call these the unspent transaction outputs (UTXOs).
The total number of bitcoins at an address is the sum of the address’s UTXOs.
Transaction 3 - Transaction fees.
Ah yes, we’ve not included a transaction fee in either of the last two transactions. Oops-a-diddly.
Without a transaction fee, those two transactions will probably take a while to get included in to a block. This is because a transaction fee gives your transaction priority.
The transaction fees are picked up by miners when they mine a block, so adding a transaction fee basically acts as an incentive for miners to include your transaction in a block.
Anyhow, pretend we didn’t send that last transaction in to the network, and let’s add a transaction fee to it.
Okay, so where the hell is the output for the transaction fee? Well, there isn’t one. But look at the size of the outputs.
The total of the outputs is less than the total of the inputs, which means that there are some remaining bitcoins that aren’t being used up. This “left over” amount is the transaction fee.
And that’s all transaction fees are - the remainder of a transaction.
The amount that’s left over in a transaction (inputs - outputs) always gets picked up by a miner. So if you manually constructed a transaction and forgot to create a change output for yourself, the miner would pick up the amount you left behind, no matter how much it is.