I teach you to build a blockchain

This is a long read, but at the end of it, you’ll know enough to not be lost.

Market update: now everybody’s wondering if the bull run is over. Bitcoin only doubled its price from last ATH, and the whole thing only took 3 months, so I didn’t think it was over already. Nevertheless, the charts don’t seem to be optimistic. Oh well! The word on the street is “get back to work you lazy programmers you haven’t made it yet”

Accounts

We start with asymmetric cryptography, an old (but still mindblowing!) invention from the 1970s. Two large numbers, related to each other somehow (exactly how? Don’t ask me but there are many ways for that). One of these numbers can be calculated from the other, but the other way around is very difficult,impossible. Let’s call one the private key (because you should keep it secret), and the other the public key (because you should tell the world that this is your public key).

Useful properties:

The private key can be used to calculate the public key, but not the other way round.

The public key can be used to encrypt information, and only the private key can decrypt it.

The private key can be used to sign information, and everyone with the public key, signature, and the original information can be sure that it was you (or whoever had the private key) who signed it.

An account on a blockchain is simply the private-public keypair. Your address, however, which is used to let people send coins to you, is derived from the public key.

(the keys are actually numbers, but they’re so large we have to use the alphabet to represent them. The alphabet has, what, 26 characters? 0-9 is only 10)

Transactions

Transactions. Sign a message with your private key saying that you want to send 10 BTC to someone else, and given your public key, anyone can verify that it was really you. A transaction needs to refer to some previous state, that is, Alice can only send 10 coins to Bob if she has more than 10 coins in the first place.

But what is the ‘true’ state? Transactions are coming in all the time, what if some contradict each other? You have to process them in batches: blocks!

Blocks

Blocks are just batches of transactions, and they refer to previous blocks. That’s why it’s a blockchain!

To prevent people from spamming the network with too many transactions, let’s introduce a fee: it costs some (very small amount of) Bitcoin to send Bitcoin around.

Why would someone want to spam the Bitcoin network? People who want to see it fail, perhaps, or who only want their transaction to be processed.

Decentralization

Speaking of people who hate Bitcoin: countries with their own currencies hate competing currencies. Think about it – a community living in your country, using their own coin. Economically they don’t really care about the larger organism (the country), they have their own blood circulation. That’s exactly like a cancer tumor! That’s why the Wörgl Experiment was shut down quickly in Austria, even though it was necessary for the community to start working again.

If you’ve never heard of a community currency before, it’s useful when the national currency is useless. Imagine A is a master house builder, but he’s just sitting around (because he’s jobless). B wants A to build a house. But they’re both poor, they have no Euros, and Euros are needed to buy food and everything. So nothing gets done, there is no economy.

The solution to this comes in two forms:

a. B barters with A: he will give A something he wants, and A will build him a house. More likely B can’t give A everything he wants.

b. They (and the farmers in their town) agree to use a new coin, not the Euro. Because everybody has some of this new coin, they can start working for each other again.

Suppose your hand is prickling – no fresh blood is coming in from the rest of the body, so everything stops working. Now imagine if your hand said ‘fuck it, I’m going to circulate my own blood, oh and I need to grow some lungs’. it’s a bit like that…

To make sure the system is resilient and can’t be brought down by adversaries, we need to make it decentralized, or distributed. Everybody around the world should run the Bitcoin program on their computers. Their computers would gossip to each other about the latest valid transactions and latest blocks, and hopefully agree on which latest block is ‘the truth’. Information takes time to travel through the network, so not every computer can have heard of every transaction, but that’s just how gossip works in real life.

Easy Things Aren’t Worth Anything / Incentivization

OK, we can’t ‘hope’ for a bunch of computers to agree on anything. Any computer can make a block out of valid transactions. Whose do we choose? We have to make it difficult. But how can we make an easy task difficult?

Proof of Work: make an arbitrary puzzle, involving the block, that requires lots of computing power to solve. The solution that must have (statistically) taken more computing power to solve is the winner, and is the new ‘truth’. This process starts all over again every time a new ‘true’ block comes out.

Proof of Stake: to propose a block, you must first put up lots of coins at stake, so that if you misbehave, the system can take those coins away (or just say they no longer exist). Then your computer will be randomly chosen to propose a block, and other computers will say “that looks good to me”. If you propose a bad block or if others vote for a bad block, they will lose some, or all, of their staked coins.

(Ethereum 2.0 version)

So we’ve made a simple task (proposing a new block of transactions) difficult. Why would anybody be motivated to do this though? Why would anybody run the Bitcoin software? Let’s make a rule saying that whoever finds a new ‘true’ block can create 50 new Bitcoin for himself, out of nothing.

(Bitcoin twist) Bitcoin is supposed to be like gold – that is, something with a limited supply. That is, whereas governments can suddenly create as many dollars as they need (which they did, to fund COVID relief, and the US to pay for World War 2), nobody can do that for gold. There is only so much gold on Earth, and gold mining output is relatively constant. So there’s an additional rule that every 4 years, the new block reward is halved from 50 Bitcoin to 25, 12.5 , 6.25 and so on. Only 21 million Bitcoin will be minted.

Theoretically, Proof of Work means anybody can participate in making a new block as long as they have a working computer. In practice, people with money buy up lots of computing power and find cheap electricity to power it. Nevertheless, it was chosen because it was simple and Bitcoin was made by libertarians who didn’t want another system where the rich get richer. Also, they wanted everybody to have a chance to get some Bitcoin, because then they would use it.

Proof of Stake came about because someone thought it’s silly to spend so much electricity solving an otherwise useless puzzle. The problem is you still need to make it difficult to make a block, and you still need to punish bad actors. So we’re back to the rich (or early adopters) get richer. Remember that every time you hear the “Bitcoin uses so much electricity” argument.

Nothing is free!

By now you should have noticed that if you have enough money, you can buy a lot of computing power and control a Proof of Work blockchain, because now you can write history (create most of the new blocks). Same with Proof of Stake, except that you buy lots of coins, which is arguably even easier! This is called a 51% attack.

But if you actually did execute a 51% attack, nobody would believe in the coin anymore and its price would go down. The community would make another coin and start using that instead.

After all, the point of a blockchain is that it’s ‘neutral’, that control over who gets to decide ‘the truth’ is decentralized. If it weren’t decentralized, we might as well go back to using a database on someone’s server.

Congratulations

You now have a system that is distributed and not controlled by evil, corruptible humans, and we have set incentives up so that it is more profitable to follow the rules than to attack the system.

What if you could run a government on this?

Leave a Reply

Your email address will not be published. Required fields are marked *