How Transactions Work in Bitcoin: Explained Like You're 5 Years Old

cover
30 Jul 2024
  • What happens behind the scenes while sending or receiving Bitcoin?
  • How do Bitcoin nodes work to approve transactions?
  • Understanding proof-of-work in Bitcoin as a crypto newbie.

Bitcoin is digital money and to understand how transactions work with Bitcoin—how a wallet is debited and credited—you need to have an idea of how transactions work in the traditional system.

To send money to someone's bank account, you'll need:

  • Two accounts - the receiving and sending accounts.

  • A signature of some sort to prove that you're sending the money with your consent- this could be a pin or ID verification.

Behind the scenes, a program is set up to prevent you from sending more than you have or using the same money for two transactions (double spending).

Sending Bitcoin

When sending Bitcoin to someone,

Three major concepts define this process:

  • State represents the balance sheet of all accounts.

  • Transaction is a request to transfer a certain amount of money from one account (A) to another account (B). For example, moving $100 from account A to account B.

  • The state transition function is a code written to process the transaction by reducing the balance in account A by $100 and increasing the balance in account B by $100. If account A doesn’t have $100, the transaction is rejected.

What Happens Behind the Scenes While Sending Bitcoin - UTXO

The amount of bitcoin remaining after a transaction is executed can be used as input in future transactions. Technically, it is called UTXO - Unspent Transaction Outputs, a function programmed to prevent fraudulent transactions on the Bitcoin blockchain.

UTXO has two major components:

  1. Input: This is the Bitcoin you're spending.
  • Reference: This is like the serial number of the coin. It identifies which coin you're spending.

  • Cryptographic Signature: To prove you own the Bitcoin you're spending (the inputs), you need a cryptographic signature. Think of this as a special code that only you know. It's like a secret password that proves you're the rightful owner of the Bitcoin.

  1. Output: This is where the Bitcoin goes after you spend it. It's like paying someone else.
  • New UTXO: This is a new Bitcoin so to say created by the transaction. It has a new owner and a specific amount of Bitcoin. See it like this: Once you’ve spent a coin, it’s taken out of your jar so you can’t spend it again. After you’ve made a purchase, the change you get back (new coins) is added to your jar. These new coins now belong to you and can be spent in future transactions.

Example

  1. Amber has two UTXOs: 0.5 BTC and 0.8 BTC.
  2. Amber wants to send 1 BTC to Jaden.
  3. Amber uses the  0.5 BTC and 0.8 BTC UTXOs as inputs.
  4. The transaction creates two new UTXOs: 1 BTC for Jaden and 0.3 BTC (change) back to Amber.

Let's recap. To send bitcoin:

  • You have to prove you own it with a signature.

  • You then send it to a new owner, creating a new UTXO in the process.

This entire process is recorded on the Bitcoin blockchain, creating a permanent record of the transaction.

Why UTXOs?

The Bitcoin white paper shows that Satoshi aimed to solve the problem of double spending, common in the banking system. Fraudulent activities can be easily perpetrated if one can use only $100 in their account to pay for groceries and for Uber, both costing $100 each. The UTXO framework simply ensures that you can't spend the same coin in more than one transaction or spend more than you own and that's why the coins are marked with serial numbers (given references).

In the Bitcoin network, nodes (the computers that keep the system running) only need to track the current receipts (UTXOs) rather than the entire transaction history. This makes it easier and more efficient to grow and manage the network as more people use it.

Decentralizing Approvals

In a centralized system, implementing the described state transition would be straightforward. A centralized server could keep track of Bitcoin’s record on its hard drive, handle all the transactions, and update accordingly. This approach relies on trust in a central authority to manage the data accurately and securely.

But in times when the server is down, people will not be able to make transactions with Bitcoin. Such a single point of failure is one of the reasons why Bitcoin exists- to share control and also ensure constant operation of the blockchain.

As a crypto newbie, you've probably heard of the consensus mechanism. It is simply a method of agreement used to validate transactions on any cryptocurrency. In this regard, computers (nodes) make the agreement.

  • Computers (nodes) participate in the Bitcoin network by approving transactions called blocks.

  • The Bitcoin network produces one block every 10 minutes.

  • Each block includes a:
    • Timestamp - when the transaction occurred.
    • Nonce - a number used only once in a cryptographic operation.
    • Hash - reference to the previous block.
    • List of transactions that occurred since the last block.

  • The repetition of this process forms the Bitcoin network with a chain of blocks called the blockchain.

How Do Bitcoin Nodes Approve Blocks?

Bitcoin nodes follow this process to check if a transaction should be either approved or rejected:

  • Check to verify if the previous block referenced exists and is valid. For example, if block 216 is referenced as the last block, it should be checked to confirm that block 216 is actually the last and not block 217.

  • Ensure the block's timestamp is greater than the previous block's timestamp and is not more than two hours into the future. This means:
    • If a new block's timestamp reads 3:00 and the previous one reads 5:00, there's a problem.
    • If the current time is 3:00 and the new block's timestamp reads 6:00, there's a problem.

  • Verify if the computational puzzle solved (proof-of-work) is correct. The puzzle is solved by miners - participants on the blockchain who solve mathematical problems to validate transactions and get rewarded in the process.

  • After all checks are done, a new state (UTXO) is created.

State Transition Process in Bitcoin

Each transaction in a Bitcoin block must ensure a valid transition from the current state (before the transaction) to a new state (after the transaction).

  1. A transaction must correctly change the current valid state to a new state. For example, if Amber has $100 and sends $50 to Jaden, Amber should have $50 left, not $100. The states are not stored in any of the blocks to save storage space but are tracked by validating nodes—computers participating in approving transactions.

  2. The state of any block is determined by starting from the initial state and applying every transaction in order up to the current block, ensuring all nodes calculate the same state.

Example

Amber has 10 BTC.

  • In the first transaction, Amber sends 3 BTC to Jaden.
  • In the second transaction, Jaden sends 2 BTC to Henry.

To determine the current state:

  • Start with Amber having 10 BTC.
  • After the first transaction, Amber has 7 BTC, and Jaden has 3 BTC.
  • After the second transaction, Jaden has 1 BTC, and Henry has 2 BTC.

When determining states, it's important to start from the initial state. If nodes start at different points in the middle, they will get varying results. The goal is to achieve the same result across all nodes, ensuring transparency.

  1. The order of transactions in a block matters. If transaction B uses an output from transaction A, then A must come before B in the block.

Proof-of-work in Bitcoin

Creating blocks in Bitcoin is intentionally difficult to prevent attacks where a single node tries to take over the blockchain for personal gain. This is why the proof-of-work system exists. Think of it as a puzzle where the first person to get the correct answer is rewarded for their proof of work (pun intended!).

  1. Each block in the blockchain contains data, including transactions and a reference to the previous block.

  2. The data is run through a 'unique fingerprint' or hash (SHA256) twice.

  3. The result obtained is a very long string of numbers and letters, often represented in hexadecimal format like this: 00000000000000000007d0f084a5e4c1fbc0fdb451d0d7a5b2f2ac1245e8d938

  4. The Bitcoin network sets a dynamically changing number called the target.

  5. For a block to be valid, its double-SHA256 hash must be numerically smaller than the target.

  6. Miners actively work to find the numerical figure that matches by solving really hard puzzles.

Implications of proof-of-work in Bitcoin

  • No single entity can control the network because it requires massive computing resources to consistently find valid blocks.

  • Miners need to make about 590 quintillion (590,000,000,000,000,000,000) attempts to find a valid block!

  • Altering a block would require recalculating all subsequent blocks, which is computationally, quite impossible.

  • To keep the average time to find a new block at approximately ten minutes, the Bitcoin network adjusts the difficulty of the mining puzzle every 2016 blocks, which is roughly every two weeks.