Understanding transactions is key to understanding bitcoin
What is a bitcoin transaction?
A bitcoin transaction is the means by which the ownership of some bitcoin value is transferred. A transaction is basically a record of how much bitcoin got moved from one address to another (or multiple others). In essence, bitcoins exist as records of bitcoin transactions
Structurally, it is a collection of inputs and outputs. Input means an address that contains a non-zero bitcoin value from which the bitcoin will be sent. Similarly, ‘an output’ is an address that receives the bitcoin. Inputs belong to the same owner, whereas outputs may not!
This description makes a transaction look more like a double-entry bookkeeper’s record — with the left side being the current owner’s bitcoin value and the right side being the new owners’.
Implicit in this transaction is the fee that the miner receives as a reward for confirming this transaction.
Bitcoins exist as records of bitcoin transactions
In that sense, it isn’t very different from a normal, day-to-day, fiat transaction. In reality, a bitcoin transaction is much more. In addition to the addresses, a typical transaction contains information like
- Transaction size in bytes
- Block number to which the transaction belongs
- The time it was received by the network
- Number of confirmations
Most of this information is relevant to either the current transaction or the current block. However, what connects all bitcoin transactions happening across the network are the inputs and outputs. The input of one transaction had been the output of an earlier transaction — forming a transaction chain. This chain in essence is the proof of bitcoin’s existence.
What does a transaction chain look like?
Don, an American baker, wants to venture into the world of Bitcoin. He asks his rich neighbor Vlad to help him get some BTC. Vlad asks Don to pay him $5.5K in exchange for 0.1BTC.
Don’s friend Salman recently started allowing customers to purchase gas in exchange for BTC. After having received his own BTC, Don goes to Salman to purchase some gas for his truck. He fills in $1000 worth of gas in exchange for 0.02BTC.
Salman has a party at his house for which he requires a lot of vegetables. So he purchases vegetables worth $500 from his friend Mukesh for 0.005BTC.
The transaction chain for the above dealings looks like this.
Three new aspects of a transaction can be seen in the above chain
- Spent/Unspent Outputs
- Transaction Hash
While transaction hash is out of scope for the current article, it will be covered in a later post. Let’s take a look at the other two
An interesting property of the transaction inputs is that like currency notes they cannot be divided any further. So unless all the inputs are consumed in the transaction some change needs to be tendered.
Notice that in the output section of the second and third transactions there’s an output labeled change. Philosophically, it is the same as offering change when a big note is given for a transaction of a small value. However, there is a subtle difference. The change itself is viewed as ‘an output’ and hence needs an output address — called the change address.
The change address need not be the same as the input address. In fact, for security purposes, it should be a different address. Many wallets make sure that the change goes to a different address.
Issues with change
Anybody carrying out a lot of transactions is bound to end up with a lot of change. That means multiple addresses with very small values of BTC. As we all know handling a lot of change in a physical wallet is painful. Bitcoin wallets solve this problem through a method known as Input Aggregation.
Wallets adopt various strategies to aggregate inputs.
- Strategy 1 — Aggregate many small inputs
- Strategy 2 — Use two large inputs, less than the required output.
- Strategy 3 — One large input and a combination of many small inputs
Wallets are only trying to emulate the optimizations that we humans have been doing for decades with our change.
Another aspect of transaction-chains that we haven’t touched upon yet is the information about whether an output is spent or not.
Spent outputs indicate that there is a valid transaction downstream that has utilized this output.
Spent/Unspent keywords indicate whether there exists a transaction after the current transaction or not. Basically, any transaction that has all of its outputs as unspent is the end of its line. It has no further transactions in the transaction-chain.
Different Forms of Transactions
- Common Transactions
- Aggregating Transaction — When many small inputs are combined to form one output
- Distributing Transaction — When one input is sent to multiple outputs, as in the case of a company paying salaries to all its employees through 1 company account.
Transactions can be viewed using blockchain explorers. These are web applications that serve as a search engine for the bitcoin network. One can easily go to one of these explore a transaction. Let’s take a look at a real world transaction. I used Blockchain.com to view this transaction
- The following image gives the Hash, Inputs, Outputs and the fee of the the transaction
- Input, Output Details
- Transaction Chain
Same transaction on another blockchain explorer — explorer.bitcoin.com
A very fun way to visualize transactions is available on txstreet website. They portray blocks as buses and transactions as passengers.
Having understood what makes a transaction, the next logical step is to understand
- How do we construct a transaction?
- How does it get added to the blockchain?
We will cover these two in the next article. Meanwhile, have fun exploring a transaction on one of the blockchain explorers and try to follow a transaction chain.