The Big Picture
Even if you’re very new to bitcoin and cryptocurrency, you’ve probably heard the term “mining” bandied about – but without truly understanding it. And if you’re a crusty old gold prospector, you probably say “tarnation” a lot. Whichever category you fall into, this article is for you!
Most understand the analogy with mineral extraction from the Earth, and know that mining creates new bitcoins through some mysterious process involving lots of big computers solving complex math problems.
Most people have only a vague understanding of what mining machines actually do, and imagine computers solving arbitrary puzzles of some kind. Or, knowing that graphics cards are involved, think it requires performing some sort of image processing. In fact, the “math problems” are not at all arbitrary – and the design is pure genius.
The key insight is this: the inputs to the “math problems” are bitcoin transactions. Mining is how transactions are processed – and validated and secured – in a decentralized manner over the Internet.
These transactions are collected together into sequential “blocks,” whose order is strictly enforced (more later on how this happens). A new block is generated by the worldwide network about every 10 minutes, and the entire set of blocks (called the “blockchain”) constitutes a permanent, immutable public ledger of every bitcoin ever spent.
As an aside, and contrary to popular opinion, this feature makes bitcoin ill-suited for criminal activity. There is no way to hide transactions or otherwise “cook the books”; all transactions are visible – and auditable – forever.
Anatomy of a Transaction
A bitcoin transaction is just what it sounds like – one person sending bitcoins to another. Coins are associated with addresses; an address consists of a public alphanumeric string and a private key.
The public address is a bit like a bank account number, except that you don’t need to worry about giving it out – in fact, you have to in order to receive bitcoins. (Mine is 1Hrefd8MtVsmnyeRnz2r3mMeHJJSuBYkTs, in case anyone appreciates this article and feels like donating!)
The private key is stored in your “wallet” software (e.g., on your computer or phone), and this key is what allows you to prove ownership and “spend” coins (i.e., move them out of the address where they’re currently stored). This key must be kept very secure, since ownership is defined by possession of the key. It’s like a safe deposit box key or bearer bond; whoever has it can “open” that address and spend the coins.
If someone else gains control of your key, he can steal your coins. And if you lose the key – the coins are gone forever. As if that weren’t scary enough, there is one further risk: your coins can also be stolen if someone simply guesses your key! This is why pass phrases used to generate keys had better be very long and obscure.
Imagine a bank robber making physical keys with random shapes, then trying all of them on every safe deposit box in the world until one fits. Not likely in the physical world of metal keys and bank vaults – but easy with computers operating on a public ledger over the Internet. The person guessing the key does not need access to your computer to do this. Your address is on the blockchain, accessible to everyone forever.
Chain of Ownership vs. the Blockchain
Each transaction has inputs (where the coins are coming from) and outputs (where they are going). Each input to a bitcoin transaction is by definition the output of a previous transaction, all the way back to the original “coinbase” transaction from the miner who created the coins. The chain of transactions therefore determines which addresses own which coins.
Note that bitcoins are not “atomic,” but highly divisible. Very highly. US dollars can be divided into 100 cents; bitcoins can be divided into 100 million “Satoshi.” As the value of bitcoins rises, small transactions will use “millibits” (1/1000 BTC; 100,000 Satoshi), or “microbits” (1/1,000,000 BTC; 100 Satoshi). Even if the value of a whole bitcoin were to rise to $1 million, a Satoshi would still be worth 1 cent. This makes bitcoin suitable for both very small and very large transactions.
Validating a transaction means making sure all the inputs and outputs involved match up and tally precisely – all the way back to the beginning. This is one part of the “math problem” the mining machines need to solve.
When Alice wants to send bitcoins to Bob, her computer “broadcasts” a transaction to the network, which makes its way around the world to thousands of machines running mining software. Of course, some machines are fast; others slow. Some have high speed Internet connections; others are on WiFi or cell networks. And they’re distributed all over the world.
So if Alice sends some coins to Bob, and Bob simultaneously sends some back to Alice (or Alice tries to send the same coins to Joe as well), these transactions will arrive at different mining computers in different order. So which one “wins”?
The answer to this decentralized network problem is the blockchain. The purpose of the blockchain, beyond validating the set of transactions in each block, is to set in stone forever the order of bitcoin transactions. All the transactions within a block are considered “simultaneous” with each other, “after” all those in the previous block, and “before” all those in the next block. The transaction input/outputs define ownership through addresses, and the blockchain defines the ordering of transactions.
This ordering is guaranteed using a cryptographic formula called a “hash function.” The result of “hashing” is a big number generated from some arbitrary block of input (in this case, a set of bitcoin transactions). As an example, running a simple hash function over the previous paragraph yields this result: 418624168216bc60d216f7e6dc63e3db.
There are two bits of magic here. One is that these mathematical functions are “one-way.” You can always and reliably get that “418” number from the preceeding paragraph, but you cannot work backwards and reproduce the paragraph from the number. The other bit of magic is this: if you change anything in the paragraph, the hash will be completely different. For instance, if I remove the comma after “previous block,” the hash becomes bf23371b0517429e17aba486e0ce7b2b: as promised, completely different.
I’m going to describe the last part of the “math problem” miners are solving, hopefully without venturing too far down the rabbit hole. A block is a set of transactions; miners need to “solve” the block, which means both assembling and validating all the component transactions (gathered from the global broadcast “pool”), and generating a valid hash that meets certain strict criteria set by the protocol.
This is what makes the problem “hard” (and it keeps getting harder; more on that later). The only way to find this magic number, roughly analogous to trying to reproduce the paragraph from the hash in the example above, is to try millions of random variations until you find one that works.
You can think of mining as a giant game of Bingo, where broadcasting transactions is analogous to calling out numbers. It is very hard to win, and each time a miner “loses,” he generates a new set of cards, at a rate of millions per second. Every miner in the world is doing this. Eventually, generally within 10 minutes (hence the “or so” part), somebody calls Bingo! and “solves” a block.
This “winning” block is then added to that miner’s blockchain – and broadcast to the network as a candidate for the next “official” block. While it is very difficult to generate a winning hash, it is very easy to verify that a particular hash is correct.
When the block is broadcast, all the other miners examine and validate it, exactly like a Bingo player holding up his cards and letting everyone verify his winning game. If they all heard the same transactions (i.e., a malicious miner didn’t just make them up), and the transactions are all valid and traceable back to the beginning of time according to the protocol – and the hash value is valid – the block is accepted and added to that miner’s blockchain.
Immutability of the Blockchain
Listen closely now: that winning hash number is part of the new block – and that number was generated using, in part, the hash number from the previous block.
Recall that changing even one character completely changes the hash value. This is what enforces the immutability of the blockchain, and why you cannot make yourself a millionaire by inserting transactions into earlier blocks and sending bitcoins to yourself. Any change at all to an existing block would alter its hash value – and therefore the hashes of all future blocks – so the protocol would reject it as invalid.
I’m going to gloss over the details of how miners all agree on the next “official” block, since it’s possible for miners around the world to find different valid blocks simultaneously. There is indeed some temporary uncertainty at the “end” of the chain, but the protocol includes the means to reconcile conflicts and quickly converge on a permanent ordering.
This is what “confirmation” means. If you’ve ever sent bitcoins, you know they don’t “get there” instantly. You have to wait for some number of “confirmations” (typically 3 or 6) before the wallet or exchange or whatever software you’re using will accept the transfer.
For each transaction, the number of confirmations is simply the number of blocks that have been added to the blockchain since the block containing that transaction. Zero confirmations means it’s just been broadcast, and is in the global “pool” waiting for miners to see it and try to include it in a block. 3 confirmations means the transaction is “three blocks deep.”
Since there is some uncertainty at the end of the blockchain, it is possible for a transaction to be “unconfirmed” if the block it was in gets rejected by the protocol in favor of a different block that doesn’t contain it. This is why it’s common for merchants and exchanges to wait for 6 or more confirmations before accepting the transaction as permanent. Unless something goes drastically wrong, the blockchain’s “flailing tail” should never be as long as 6 blocks.
That’s how mining works, but why expend all that energy and pay those huge electrical bills to do it? The answer is arguably the most ingenious aspect of bitcoin – miners receive bitcoin “block rewards” for solving each block. In the beginning, each block was worth 50 bitcoins; now, the reward is 25 bitcoins, and it will continue halving every 4 years or so, even as the difficulty increases.
In other words, miners profit from “winning” blocks – and this universal and absolutely reliable motive also ensures the integrity and security of the network, which in turn encourages adoption and further increases the value of the miner’s coins! Greed can be a beautiful thing.
Why continue mining after the coins are gone?
The bitcoin protocol guarantees that only 21 million bitcoins can be mined. It’s a bit like a spaceship approaching the speed of light. It takes more and more energy to accelerate to higher and higher speeds, and infinite energy to go faster than the speed of light (i.e., it’s impossible). Similarly, bitcoins will be harder and harder to mine as we approach the 21 million mark (well, we won’t see it; current calculations estimate the world’s last bitcoin will be mined around 2140).
Since the rewards get smaller and the task gets harder over time, won’t miners eventually stop? No, they won’t – because in addition to block rewards, miners also earn transaction fees. Each transaction has an optional “fee” attached to it. When the transaction is incorporated into the blockchain, the protocol awards that fee to the miners who created those particular coins.
The fee is optional, and many miners currently ignore it. However, the protocol allows for transactions with fees to be given priority, so that they confirm faster. Eventually, the balance will shift and fees will become more common.
Future miners may simply ignore “free” transactions, which would then never be confirmed, or be confirmed extremely slowly. Eventually, all transactions will have associated fees (presumably still nominal), and these will be enough to make mining continuously profitable.
Bitcoin mining consists of solving difficult mathematical equations whose inputs are bitcoin transactions. The primary purpose of mining is distributed validation and ordering of transactions – and generating a robust, secure network to support these operations. This needs to be done even after all coins have been mined, and will continue in order to secure the network benefits (and ongoing transaction fees).
Here is an excellent video about how bitcoin works “under the hood.”