Money can only be spent once before it changes the account or wallet. This is a fundamental tenet of our banking and financial system. If I transfer amount XY from my account to your bank account, the amount is deducted from my account and credited to yours. If this was not the case, I could spend it a second time. This is what we call “double spending”.
When using cash, this is impossible – if I hand you my 10 EUR bill, you have it, I no longer do. However, when transactions are purely digital, more checks need to be in place. Hence, when settling transactions digitally, the instantaneous and simultaneous debiting of the amount from the sender and crediting of the receiver’s account must be ensured. In the centralized banking system, integrated banking systems verify this. But how does this work in a decentralized blockchain network like Bitcoin?
The double spending problem in blockchain networks
In a decentralized blockchain network, transactions are proposed and broadcast to all participating nodes1 in the network. These nodes then receive and have to confirm the transactions – a process that takes some time. How does the blockchain ensure that the sender cannot spend that same money a second time until the first transaction has been confirmed?
In other words, how does the Bitcoin network ensure the validity and integrity of the first transaction? To answer this, let’s first recover…
The Basics: What the blockchain is
The blockchain is a special form of distributed ledger, basically a database of transactional records that is stored and updated in real-time across multiple nodes that each store the same version of that database. Cryptocurrencies like the Bitcoin blockchain use a decentralized rule-based decision-making process known as “consensus mechanism” where participating nodes of the network collectively decide on the validity and integrity of a requested transaction.
Once transactions have been validated and confirmed, they are irreversible and posted publicly. Transactions are time-stamped, encrypted and subsequently stored in blocks in chronological order which are connected through a hash. In the words of Satoshi Nakamoto, the infamous creator of Bitcoin, this is a “chain of digital signatures”.
To understand how the Bitcoin blockchain prevents double-spending, we need to take a look at the confirmation process for transactions – also known as consensus mechanism.
How the confirmation process eliminates any possible double spending
The Bitcoin blockchain uses a consensus mechanism called “proof of work”. This mechanism foresees that every requested transaction is announced to all nodes in the network. If the majority of nodes agrees on the validity of a transaction, the transaction is confirmed. The double-spending problem is an issue of time – the question is “Which transaction was received first?”
Here again, the majority decides. The majority of nodes decides which transaction was received first, subsequent attempts at double spending then become irrelevant. This is achieved by a timestamping mechanism that Satoshi Nakamoto outlined in his whitepaper.
The timestamp server solves the double-spending problem
Presented in the Bitcoin whitepaper, Satoshi Nakamoto details a timestamp server as the way to solve the double spending problem. The server uses a hash – a cryptographic signature – of a block of transactions and broadcasts it to all participating nodes of the network. The hash itself is timestamped and then proves to all nodes that data inside the block must have been created before the actual hash. Each timestamp includes a hash of the previous timestamps. This results in an unalterable (immutable) record of the order in which transactions were performed. Each subsequent timestamp builds upon and reinforces the previous one.
Let’s say we attempt a double-spend…
Let’s assume we have 0,1 BTC we want to attempt to spend twice. This would mean sending the 0,1 BTC to two different wallet addresses. Both requested transactions actually go into an entire pool of requested transactions that have to be processed by the network of miners.
When the first transaction is confirmed through the consensus mechanism, it will become part of a block of transactions which is then hashed. The second transaction would be recognized as invalid and be rejected (as the sender’s bitcoin wallet has been deducted 0,1 BTC).
In the unlikely event, that both transactions are selected from two miners simultaneously, both transactions are added to different blocks by different miners. In this case, the blockchain would create a second string.
Let’s have a look at the illustration. In red is the Genesis block, followed by the blocks integrated into the blockchain in blue. The grey blocks represent blocks that were created but not included in the actual blockchain because two variants of the blockchain existed at the same time.
In the blockchain, the longest string is always preferred, the shorter strings are dropped. If two strings (transactions) are created at the same time, a fork occurs. This means that two parallel strings exist at that moment. Since both strings are the same length, neither of them can be preferred. The network is therefore briefly divided and work is continued on both strings. After 2-3 blocks, it becomes clear which network is faster. One of the forks is then accepted while the other will become an “orphaned” block an will be dropped.
In decentralized blockchain networks, too, it must be ensured that the same money cannot be spent twice simultaneously. The solution to this is to timestamp each transaction and block of transactions through a cryptographical process known as a “hash function”.
This creates a chronological and immutable order of all transactions which is why an attempt to double spend is rejected based upon it being requested at a later point in time after a first, validated transaction has already occurred.
1 Nodes: Computers that verify and synchronise the blockchain.