Series written by Maxwell Foley, Software Engineer at CertiK
Now we know how it is possible that only you can spend your own money. But — how is it possible that everyone can agree on what everyone else’s balance is — especially when there’s no central server to coordinate things?
Let’s go back to our example of an anarchic market in which everyone is frantically running around trying to agree on their books — scribbling things down and trying to reach some sort of consensus.
With Blockchain, we can actually outline a system in which it’s possible for a broad group of people to reach consensus and agree on a single valid history of transactions.
Miners: a special type of user
First, we have to give the task of writing in our ledger to a special trusted and verified group of users we call “miners.”
Anyone who wants to can become a miner, but no user is required to — the process is open and is used primarily as a business opportunity. In exchange for the advantages becoming a miner requires up-front purchase of powerful computers and requires you to spend money on a daily basis to maintain your devices.
The hope of a miner is to make money on the endeavor — but this is not guaranteed. As with any business, there are some people who will seize this opportunity in the hopes of making money. But there are many others who will pass it up because it’s too risky or requires too much investment or for any other reason.
Like earlier, the analogy to gold implied in the term “mining” is slightly misleading.
Most people in the Blockchain space are generally aware that miners spend computing power and are rewarded with newly created Bitcoin in exchange. This may lead people to imagine that in order for new Bitcoin to physically exist, it is somehow necessary to crunch a whole bunch of math — or that each Bitcoin somehow “represents” a certain amount of computing work.
This misunderstanding is widespread. There’s a popular tweet that read something like: “Bitcoin explained: imagine that keeping your car idling 24/7 creates solved sudokus that you can trade for heroin.”
Source: Theophite Twitter
This tweet makes the erroneous assumption that one Bitcoin equals one “solved puzzle” which thereby gives it value.
This is a mistake. A Bitcoin is just a number in a (decentralized) ledger. If a transaction in a ledger says I got paid 8 Bitcoin, and I can somehow change this number to 9, then I have 9 Bitcoin. No solved sudoku, puzzle, or artificially large prime number is needed.
So, let me repeat this in bold: the purpose of mining is not to create Bitcoin. Rather, the purpose of mining is to do what the central server would do in a centralized system –process everyone’s transactions and update the database. The newly created Bitcoin is the reward for doing this crucial task.
It’s almost like in order to manage our database in a decentralized way we have simply contracted everything out to a bunch of freelancers we are paying in Bitcoin.
Similarly, the purpose of requiring enormous amounts of computing power is to ensure miners spend a whole bunch of real-world money up-front — burning energy toward no practical end before they get their Bitcoin.
This tax is called a “proof of work” — the miner needs to prove that they have done a certain amount of computing work in order to be allowed to publish.
The miners are responsible for listening for all transactions that happen on the network and reporting them. The miners do not record transactions in the database one at a time, but rather by adding hundreds at a time in batches called “blocks.” Hence “blockchain” — the database is a growing chain of blocks.
The system is designed so that one block is published every ten minutes. Blocks can only be added and not modified. When a miner writes a block, they are allowed to write a transaction crediting themselves a certain amount of newly minted Bitcoin — this is their reward.
Similarly, each miner can collect transaction fees from users for including a transaction in the next block. But don’t worry — your Bitcoin client will tell you the average transaction fee at the moment so you know how much others are paying.
Now: what happens if the miner did not manage to hear about and record your transaction in time? It is simply like it didn’t happen yet! You need to hope it gets included in the new ten-minute round of mining that is about to start. (Try giving the miner a higher transaction fee!)
Let’s revise our little scenario of people trading while carrying books in a lawless marketplace with these new details. No longer does the average person have to worry about updating the book — rather, there are now a group of publishers (representing the miners) walking around earning money by recording every transaction they learn about in their books.
What a service!
Once somebody makes a transaction, they need to tell as many publishers as they can in order to try to get it published. Every ten minutes, a publisher is somehow chosen to put the latest page of his book (the new block) to through a photocopier, and then copies will be distributed to all the members of the marketplace so that everybody involved has a record.
Now everyone’s books are the same — no more anarchy. Decentralized clarity reigns!
By using this system, we can maintain a consistent, regularly updated ledger-book that everyone involved in the system can agree on, as long as we can trust the publishers to all agree on one new valid block to publish at a time. The publishers/miners using this system can easily ensure that no one is spending money that they don’t have, since they are able to look in the ledger-book and see all the transfers a certain person has made — and therefore calculate what that person’s balance should be.
Also, it is impossible for anyone, including the miners/publishers, to forge a transaction from someone else because transactions require our magic unforgeable signature. The users don’t have to blindly trust the miners/publishers either, because they can verify all this themselves.
Now: all we need to figure out how exactly we can get miners — and therefore users — to agree on what the newest block is.
We’ve secured our system against people spending more money than they have, and we’ve prevented people from spending other people’s money, but if we can’t get our strategy for consensus right — in a fair, transparent, unbribable way — there is one subtle way left for people to cheat in our system and defraud us.
This is called a double-spend attack.
Figuring out how to prevent this is a big deal — it’s stopped digital currencies from ever taking root. The reason Bitcoin is the first successful decentralized digital currency is because it was the first digital currency to figure it out.
To explain the solution, we must first explain the problem.
The problem exists as follows: suppose a miner could make a transaction “un-happen”, i.e. publish a transaction, and then take it back. This would enable the miner to do the equivalent of bouncing a check.
Let’s say someone has 200 dollars worth of Bitcoin in their account and has figured out how to reverse transactions. In this situation she could, say, spend two hundred dollars worth of Bitcoin on a TV — then she could instantly take back her transaction, and spend that same amount of Bitcoin on an Xbox. Now she has spent only $200, but has an Xbox and a TV, $400 worth of goods. The guy she bought the TV from is the loser in this situation, because he gave up his TV for nothing in return.
This is why we need consensus. Without this agreement, there can be multiple competing versions of history, and it’s possible for somebody to exploit this by spending twice, across two competing histories.
To see why reaching consensus is tricky, let’s abandon our metaphor about the chaotic marketplace — even that’s too easy.
Imagine this instead: you have just stumbled into a pitch black, misty forest, the only light around you a small lantern you hold and the glow of the moon. You can see nothing and are navigating by touch and sound. A voice in the mist calls out to you “Hey. I’ll give you a warmer jacket for five invisible coins.”
Sure, you want a warmer jacket, and the price sounds fair, but something about this seems strange. “How do I know that you have five invisible coins?” you ask.
“Here, check my ledger,” says the voice, and a wrinkled hand thrusts some scribbled pages into the light of your lantern. You read it, it checks out, and because of the magic signatures you can tell it’s impossible to be forged — but you’re still not sure what you think.
All of a sudden, a chorus of voices call out from the mist. “It’s real!”, say five hundred different voices from all around you. “I was there, I saw him get it! His book looks like mine!”
Though the voices are numerous in count, you have a terrible feeling about them. They might belong to humans, but you can’t be sure. They could be phantoms, illusions conjured up in multitude trying to deceive you. You can’t figure out what’s real.
It feels wrong, but maybe you’re wrong. Maybe this is just how coats are sold in the forest — and, after all, the technical facts do look good.
This is all a metaphor for logging onto a peer-to-peer network for the first time. You have no idea what the history of the ledger is, and you need to get it from someone. But you have no idea who you’re talking to, and since it’s very easy for one person to run the same program across hundreds, even thousands of different computers (for instance through a cloud computing platform), so you don’t know how many people you’re talking to.
Similarly, when a miner publishes a new block, you have no idea if the rest of the network is going to accept it or not. You have no idea if its publisher is trying to trick you into accepting a fake history. You wish you could get a sort of majority rule, a vote on whether or not this block is going to be part of the real history or not, but you can’t do a vote, because you can’t do a head count.
Except you can, and here is how: instead of counting heads, we count electricity.
This is where the expensive, energy-wasting part of mining comes in. We force all miners to play a game where the winner gets the rights to publish the next block. The more electricity you spend on playing this game, the higher your odds of winning become. Your odds of winning are roughly proportional to the percentage of electricity you’re pumping into the system compared to the total amount of electricity coming from all players.
So in order to get favorable odds (51%) that you can publish the next block, you need to be spending more electricity than every single other miner combined. This is very hard to do, since the other miners are legions of unsolicited volunteers drawn from all across the world lured into playing by the simple attraction of profit.
We are voting with electricity, but really we are voting with money–-everyone in the world is united against a single scammer.
So, to go back to the mysterious figure: in order for them to have a real chance of ripping you off, they need to be more powerful than the rest of the world combined.
That’s unlikely. More likely, they’re just a spooky weirdo selling a coat.
If the price is right? Buy the coat. The math says you’re safe.
We’ve talked a lot about this mysterious electricity-wasting game that protects us. Now let’s actually explore how it works.
First, we must understand something called a “hashing function.”
Until next week!