How does Bitcoin work ?
This story is about a presentation we (Crypto Lyon) made at INSA Lyon on 03/12/18. It’s more or less a translation of the blogpost I wrote in french on crypto-lyon.fr just after the event.
This article is intended to people who already used Bitcoin (or another cryptocurrency) and who wants to know how it actually works. It is preferable to be comfortable with private / public keys notions, to have already heard of blocks, chain, mining : this article aims to explain each of this things and how their combination results on the first decentralized money. I’ll try not to use too much technical terms in order to let this post readable even by those who don’t have technical knowledge. I’ll rather give a set of links for each notion, if you want to dive deeper into it.
If you have any question, you can join the Crypto Lyon Discord server : we want to democratize the use of cryptocurrencies, this includes helping newcomers so don’t be afraid to ask.
What is Bitcoin ?
Before going into further detail, we should answer this question : Bitcoin is a computer program. The Bitcoin network is a set of computer running this software : bitcoin-core. Each or these computers is called a “node”.
Nodes : the network‘s keystone.
Like in any peer-to-peer network, nodes are Bitcoin’s basis. They are (mostly) running bitcoin-core and therefore keeping the network up. A node is doing 3 main things :
- Synchronize the blocks’ chain : a database containing every transaction since the first block (called “genesis block”), mined on 01/03/2009.
- Once synced, share this database to newcomers.
- Validate and propagate new blocks and transactions.
Once a node has the chain, it has the entire history of the network, and can act as a witness to something which involves knowing everything about the network : like validating transactions. Let’s see how it is achieved.
First you need to understand, and it will be explained in the next part, that the database is incorruptible. Which means that the chain (/database) a node have is the truth, nothing happened but what is in the chain. Now, when a transaction is submitted to the network, by a node sending a message to another, the one which receives it checks if the transaction funding this transaction is in the chain, meaning that the “account” sending the transaction has “money to send”. There is no such thing as a “balance” or even an “account” on Bitcoin because the block chain only stores transactions, and nothing exists but what is in the block chain (the database). An account is an abstraction for an address, and an “account’s balance” (read “address’ balance) is the sum of the values of all the transactions sent to that address in the past : in the history. This address can then create a transaction which actually spends one or more transaction(s) received in the past. The verification process should now be more understandable, here is what will do a node when receiving a transaction :
- Checking its validity : is it signed with the good private key (meaning the private key from which the address sending the transaction is derived) ? Does it spend less than the sum of the values of all the transactions sent to that address (minus the fees) ?
- If it is valid, it will keep it in what’s called its “memory pool”. It’s like caching it.
- If it is valid, it will broadcast it (== send it to every other node it knows about), and they would do the exact same thing. This is how fresh transaction are propagated through the network.
If you still don’t really visualize how transactions are formed, here is a link which provide good explanations with drawings.
Why blocks ? Why a chain ? Why proof of work ?
Joining the Bitcoin network is up to you. Anyone can become a node : all you need is an internet connection, more than 200Go of free space (with 500 you should be OK for next 10 years), and a Bitcoin client (like bitcoin-core). It means that even someone who wants to hurt Bitcoin can be part of it, and, to have a working system, this malicious node should not be able to hurt the network though anyone should be able to join. Bitcoin is all about creating trust where there is not.
How is it achieved
Part of this answer is : thanks to the blockchain.
I said in the previous part that it was the Bitcoin’s database, actually it is a specific database. Not more, not less. You must have heard of “Blockchain technology” with all this “Blockchain-not-Bitcoin” nonsense. The blockchain is “just” a bunch of data.
Let’s get back to what we want to know. Remember : we are in a decentralized system and we can’t trust any third source of information. To understand the solution, we should here expose our needs :
- We need some ordering, which mean time : since the bitcoins you spend come from a transaction in the past, and it’s the same for about 99% of Bitcoin users, it could imply that your transaction depends on dozens, hundreds of transactions passed “before”. If an order is reversed, if the “before” becomes “after”, it invalidates 0 to hundreds (even thousands) of transaction. Remember : we cannot trust any source of information, even for the time.
- We need data persistence. Since the only history, the only truth (the block chain) can be accessed by anyone, and can be modified (again, it’s just a bunch of data), we need to find a way that a modification doesn’t impact the system.
To preserve order, we will add to the database new transactions only every 10 minutes. This transactions will be structured in blocks, and each new block contains the hash of the previous one (if you don’t know what a hash is, it’s a digital print of a data. The function is chaotic which means a little change in the entry implies big changes on the output. You can visualize it here.), which means that the modification of a transaction in the past causes a modification of its block’s hash, which impact the next block’s hash, and so on.. This “link” between blocks provides a good way to verify that nothing has been modified in the database, and that’s why Hal Finney once talked about this as a “block chain”. Thus, a node synchronizing the chain could not be sent a modified one, because it verifies each bloc in the same time.
To preserve data persistence, the chain is distributed to each node of the network. A malicious node modifying his chain and broadcasting it to the network could just not see his chain accepted by other nodes, except if he finds a valid hash for his block, or for every block after the one he modified.. Which introduces the next part : proof of work.
The proof of work
So we have now a way to keep an information incorruptible, by chaining all blocks between them. But who can add new blocks to the chain ? Who can add this information which will stay unchanged until there are less than 2 computer running a Bitcoin client ? And who determines when ? Do we ask someone the time to produce a block every 10 minutes ?
A system to keep information incorruptible is useless if we can add non accurate information in this system.
Blockchain is useless without Proof-of-work.
- But who can add new blocks to the chain ? Any node of the network, and by extension, anyone.
- Who determines when ? Nobody.
The process of adding new blocks to the chain (or, to the chain of every node) is to solve a problem that will take approximately 10 minutes. This problem has also something to do with hash. To understand the problem you need to know that the result of the hash function used (sha256) results in a string of hexadecimal characters, which can be interpreted as a number (again, you can try it here). It can then be interpreted as a pseudo-random number. So, the network will generate a number called “target” and calculated from the time it took to produce the last 2016 blocks (~= 20160min ~= 2 weeks) : if they’ve been generated in less than 20160 minutes, reduce the target, else increase it. A node called “miner” will now try to create a block (containing a set of transactions), hash it (apply 2 times the sha256 function on it) and check if the number resulted is below the target. If it is, the node broadcast the block, each node re-calculates the hash of the block and, if it’s valid, adds it to its chain and broadcasts it. If not, it changes a little value (the nonce) in the block header, and hash it again, repeating this process until it finds a valid hash.
The process is not deterministic : the target is adjusted to set the probability of producing a new block every 10 minutes (with the total hash/s calculated by the entire network).
Because this post turns out to be longer than expected, I’ll stop here and maybe write other posts to detail some part of the Bitcoin network.