How Bitcoin Works: A Simple Guide to Blockchain Technology

Blockchain from the Inside: How Bitcoin Works

Today, when half my friends are trading on crypto exchanges and the other half are preparing for ICOs, only a few of them actually understand how it all works under the hood. This post aims to fix that. In this long read, I want to explain why blockchain was invented, how cryptocurrencies work, and why it’s the most elegant system in recent years from a logic standpoint.

I’m not caught up in the hype around Bitcoin’s price or exchanges; for me, blockchain is a technology. It’s new, strange, and confusing, but it seems to be changing the world—unlike your Instagram stories. Apparently, it’s here to stay.

I wrote this post as if I were explaining blockchain to my parents. I can even send it to my friends in the humanities and be sure they’ll get it. And to help me, there’s Oleg. If you don’t like Oleg, I’ll kick him out.

Basics: Why Do We Need Blockchain?

Blockchain was first described in Satoshi Nakamoto’s article, “Bitcoin: A Peer-to-Peer Electronic Cash System.” In just eight pages, the author outlined the basics of Bitcoin, which is built on the blockchain algorithm.

Blockchain appeared alongside Bitcoin, but it can be used independently and even modified. Anyone can create their own blockchain, even on their laptop.

A List That Can’t Be Changed

Blockchain is a chain of blocks, or in other words, a linked list. In such a list, each entry refers to the previous one, all the way back to the very first. Like train cars, each one pulls the next. There’s a great article by Nikita Likhachev on TJ that explains lists for complete beginners—some analogies here are borrowed from there.

Let’s break it down with an example.

Oleg’s friends are always borrowing money from him. Oleg is kind but very forgetful. After a week, he can’t remember who still owes him, but he’s too shy to ask. So one day, he decides to keep a list of friends he’s lent money to on a board at home.

Now Oleg can always check the board and see that Max paid him back, but Vanya still owes him 700 rubles. One day, Oleg invites Vanya over for drinks. While Oleg is in the bathroom, Vanya erases the entry “Lent Vanya 200 rubles” and writes “Vanya paid back 500 rubles” instead.

Trusting his list, Oleg forgets about the debt and loses 700 rubles. He decides to do something about it.

Last year, Oleg took a programming course where he learned about hashing. He remembers that any string can be turned into a unique set of characters—a hash—and changing even one character completely changes the hash.

Oleg uses the well-known SHA-256 hash and hashes each entry, writing the result at the end. Now Oleg can make sure no one has changed his entries by re-hashing them and comparing the results.

But EVIL RUSSIAN IVAN also knows SHA-256 and can easily change the entry and its hash, especially if the hash is right there on the board.

For more security, Oleg decides to hash not just the entry, but also combine it with the hash of the previous entry. Now all subsequent entries depend on the previous ones. If you change even one line, you have to recalculate the hashes for all the following entries.

That’s how Oleg gets his own personal linked list.

But one night, Ivan sneaks in, changes the entry he wants, and updates the hashes for the whole list. It takes him a few hours, but Oleg sleeps soundly and doesn’t hear a thing. In the morning, Oleg finds a perfectly valid list—all the hashes match. But Ivan still tricked him, even if it cost him a sleepless night. How can Oleg protect himself from Nighttime Ivan?

Oleg decides to make things harder. Now, to add a new entry, Oleg will solve a difficult math problem related to the entry. He’ll add the answer to the final hash.

Oleg is good at math, but even for him, adding an entry now takes ten minutes. Still, it’s worth it, because if Ivan wants to change something again, he’ll have to solve the equations for every line, and there could be dozens. It’ll take a lot of time, since each equation is unique and tied to a specific entry.

But checking the list is still easy: just compare the hashes and check the solutions to the equations. If everything matches, the list hasn’t been changed.

In reality, though, computers are too good at solving equations, and storing so many unique equations is a hassle. So the creators of blockchain came up with a more elegant challenge: you have to find a number (nonce) so that the final hash of the entry starts with 10 zeros. This nonce is hard to find, but the result is easy to check at a glance.

Now Oleg checks all the hashes and makes sure each one starts with the required number of zeros. Even a clever Ivan with a powerful laptop won’t have time to recalculate all the hashes in one night to meet the condition—it would take too long.

This kind of list is basically a homemade blockchain. Its security is guaranteed by mathematicians who proved that these hashes can’t be calculated any faster than by brute force. This brute-forcing of hashes for each entry is what mining is all about, which we’ll discuss in detail.

Centralizing Trust

The idea of keeping an unforgeable list of “who owes whom” appeals to Oleg’s friends. They don’t want to remember who paid for whom at the bar or who still owes money—it’s all written on the wall. You discuss the idea and decide you need a single list for everyone.

But who should you trust to keep such important records? When it comes to money, trust is key. We wouldn’t trust a stranger with our money. Our ancestors invented banks for this, and over time, we trusted them because they’re backed by licenses, laws, and central bank insurance.

Among friends, you can just pick the most responsible person. But what if it’s about strangers? A whole city, country, or the entire world, like with Bitcoin? There, no one can trust anyone.

Decentralization: No One Trusts Anyone

So an alternative approach was invented: everyone keeps a copy of the list. That way, a bad actor would have to rewrite not just one list, but sneak into every house and rewrite all the lists. And someone might even have several lists at home that no one knows about. That’s decentralization.

The downside is that to add new entries, you’d have to call everyone and tell them about the changes. But if the participants are soulless machines, this isn’t a problem.

In such a system, there’s no single point of trust, and therefore no way to bribe or cheat. All participants follow one rule: no one trusts anyone. Everyone believes only the information they have. This is the main law of any decentralized network.

Transactions

When you buy ramen at the store, you enter your card’s PIN, allowing the store to ask the bank if you have 35 rubles in your account. In other words, you sign a transaction for 35 rubles with your PIN, which the bank approves or rejects.

Our entries like “Lent Vanya 500 rubles” are also transactions. But we don’t have a bank to authorize the transaction’s author. How do we make sure Ivan didn’t secretly add an entry like “Max owes Oleg 100,500 rubles”?

Blockchain uses a system of public and private keys for this, which IT folks have long used for SSH authorization.

In short, here’s how this complex but beautiful math works: you generate a pair of long prime numbers on your computer—a public and a private key. The private key is super-secret because it can decrypt what’s encrypted with the public key. But it also works the other way: if you share your public key with friends, they can encrypt any message so that only you can read it, since you have the private key. Plus, the public key can be used to verify that data was encrypted with your private key, without decrypting the data itself. All these properties are well explained in “The Code Book.”

Your public key is your crypto wallet address. You can create a wallet for any cryptocurrency without even going online.

For regular users who don’t want to deal with generating and storing private keys, there are online wallet services. To make copying long public keys easier, they use handy QR codes. Personally, I use Blockchain Wallet because it has a convenient mobile app and supports the two main cryptocurrencies—BTC and ETH.

No Such Thing as a “Balance”

Like our board, the blockchain is essentially just a history of transactions. It doesn’t store the balance of each wallet; otherwise, we’d need extra protection methods.

Ownership of a wallet is confirmed only by the private key. But how do other network participants know I have enough money to make a purchase?

Since there’s no balance, you have to prove it yourself. That’s why a blockchain transaction includes not just your signature and the amount you want to spend, but also references to previous transactions where you received the necessary funds. So if you want to spend 400 rubles, you go through your entire income and expense history and attach the incomes where you got 100 + 250 + 50 rubles, proving you have those 400 rubles.

Each network participant will double-check that you haven’t attached the same income twice. That the 300 rubles Max gave you last week haven’t already been spent.

These incomes attached to a transaction are called inputs, and all recipients are outputs. The sum of all inputs rarely matches exactly what you want to send, so one of the outputs is usually you. In other words, a blockchain transaction looks like: “I got 3 and 2 BTC, I want to send 4 BTC and get the remaining 1 BTC back.”

The beauty of blockchain is that inputs don’t have to be from the same wallet. Only the key is checked. If you know the private key for all the inputs, you can attach them to your transaction and spend the money. Like paying at the supermarket with several cards whose PINs you know.

But if you lose your private key, your hard drive dies, or your laptop is stolen, your bitcoins are lost forever. No one can use them as inputs for new transactions. That amount is lost to the world forever—like burning a stack of bills. There’s no central bank to file a claim with and get new ones printed. For that, you’d need to issue new bitcoins “out of thin air.”

The Double-Spending Problem

Earlier, I said that transactions are added to a special “pool of unconfirmed transactions.” Why do we need this intermediate step if we already have signed transactions? Why not write them straight to the blockchain?

Because signals from point A to point B always have delays. Two transactions can take completely different routes. And a transaction that was initiated first might reach the recipient later because it took a longer path. This leads to double spending, where the same money is sent to two recipients, and neither knows it. This isn’t like handing over cash in person.

For a decentralized network where no one can be trusted, this is a big problem. How can you be sure one transaction was definitely before another? Ask the sender to include a timestamp? But remember—no one can be trusted, not even the sender. Clocks on all computers will differ, and there’s no way to guarantee synchronization. Each computer stores its own copy of the blockchain and trusts only it.

So how can you be sure one transaction was before another?

The answer is simple: you can’t. There’s no way to confirm the time of a transaction in a decentralized network. And solving this problem is the third key idea of blockchain, which Satoshi came up with and which, oddly enough, is right in the name—blocks.

Blocks: The Foundation of Blockchain

Every computer in the network picks any transactions it likes from the common pool—usually those with the highest fees. It collects transactions until their total size reaches the agreed limit. In Bitcoin, this block size limit is 1 MB (after SegWit2x, it’s 2 MB), and in Bitcoin Cash, it’s 8 MB.

In networks like Ethereum, it’s a bit more complicated: the number of transactions per block depends on the computational complexity of the included smart contracts. But the idea is the same—there’s a set limit.

The entire blockchain is essentially a list of such blocks, each referring to the previous one. You can trace any transaction in history by unwinding the blockchain all the way back to the first entry. This list now takes up hundreds of gigabytes and must be fully downloaded by any computer that wants to participate in the network (but to just create transactions and send money, this isn’t necessary). It’s downloaded from all nearby computers in the network, like downloading a TV series from torrents, except new episodes come out every 10 minutes.

After collecting transactions from the pool, the computer starts building an unforgeable list, like Oleg’s board at home. But it does it as a tree—hashing entries in pairs, then hashing the results in pairs, and so on until only one hash remains—the root of the tree, which is added to the block. Why a tree? I haven’t found a definitive answer, but I assume it’s just faster. More details can be found on the wiki: Merkle Tree.

Since the current blockchain is already downloaded, our computer knows exactly which block is the latest. It just needs to add a link to it in the block header, hash everything, and tell the rest of the network, “Look, I made a new block, let’s add it to our blockchain.”

The others must check that the block follows all the rules and that no extra transactions were added, then add it to their chains. Now all transactions in it are confirmed, the blockchain grows by one block, and everything is fine, right?

Not quite. Thousands of computers are working in the network at the same time, and as soon as they assemble a new block, they almost simultaneously rush to announce that their block was created first. And as we already know, in a decentralized network, it’s impossible to prove who was actually first.

Therefore, to add a block to the chain, computers must solve a difficult problem that takes a certain amount of time. Like in school, when everyone was solving a tough test, it was rare for even the best students to finish at exactly the same time.

For a person, a hard problem might be planning a vacation to fit the holidays and get cheap tickets. For a computer, it’s adding a number (nonce) to the end of the block so that the SHA-256 hash of the whole block starts, say, with 10 zeros. That’s the problem you have to solve to add a block to the Bitcoin network. Other networks may have different problems.

This brings us to the concept of mining, which everyone’s been obsessed with in recent years.

Mining

Bitcoin mining isn’t some sacred ritual. It’s not about finding new bitcoins hidden deep in the internet. Mining is when thousands of computers around the world hum away in basements, trying millions of numbers per second to find a hash that starts with 10 zeros. They don’t even have to be online to do it.

Video cards, with their hundreds of parallel cores, solve this problem faster than any CPU.

Why exactly 10 zeros? Just because—that’s how Satoshi designed it. It’s one of those problems that always has a solution, but it can’t be found any faster than by monotonously trying options.

The difficulty of mining depends directly on the size of the network, i.e., its total power. If you create your own blockchain and run it at home on two laptops, the problem should be easier. For example, the hash only needs to start with one zero, or the sum of even digits equals the sum of odd ones.

For one computer, finding a hash that starts with 10 zeros would take decades. But if you combine thousands of computers into a single network and search in parallel, probability theory says the problem is solved on average in 10 minutes. That’s the time it takes for a new block to appear in the Bitcoin blockchain. Every 8–12 minutes, someone in the world finds such a hash and gets the privilege of announcing their find, thus avoiding the “who was first” problem.

For finding the answer, the computer (as of 2017) gets 12.5 BTC—a reward generated by the Bitcoin system “out of thin air” and halved every four years. Technically, this means every miner always adds one more transaction to their block: “create 12.5 BTC and send them to my wallet.” When you hear “the number of bitcoins is limited to 21 million, and 16 million have already been mined,” these are the rewards generated by the network.

Check out a real live Bitcoin block on one of the special sites. There you’ll see transactions with inputs and outputs, 18 zeros at the start, and all the hashes described above.

Any blockchain exists only as long as its miners do. Miners add new transactions to the blockchain. So if someone tells you they’re “making a blockchain for ***,” the first question they should answer is who will mine it and why. Usually, the answer is “everyone will, because we give out our coins for mining, which will grow in value and make it profitable for miners.” But that doesn’t work for every project. For example, if the Health Ministry creates a closed blockchain for doctors (which they want to), who will mine it? Doctors on weekends?

But what’s in it for miners when the rewards disappear or become tiny?

According to the Creator’s plan, by then people will believe in Bitcoin’s reality, and mining will be profitable from the transaction fees included in each transaction. That’s where things are headed: back in 2012, all fees were zero, and miners mined only for block rewards. Today, a transaction with zero fees can sit in the pool for hours, because there’s competition and people are willing to pay for speed.

So mining is about solving pointless problems. Can’t we use all that computing power for something more useful—like finding a cure for cancer?

The point of mining is to solve any computational problem. The problem must be simple enough that participants have a stable chance of finding the answer—otherwise, transactions would take forever to confirm. Imagine waiting half an hour at the store for the bank to confirm your transaction. No one would use such a bank.

But the problem must also be hard enough that not all users find the answer at once. Otherwise, they’d announce lots of blocks with the same transactions, and there’d be a risk of double spending or, worse, splitting the blockchain into several branches, making it impossible to tell which transaction is confirmed and which isn’t.

If the 12.5 BTC reward is given only once every 10 minutes and only to the one who finds the block, does that mean I have to burn my video cards for years hoping to one day get $40,000 (at the current rate)?

For Bitcoin, yes. But it wasn’t always like this. The network used to be smaller, the difficulty lower, and the chance of finding a new block alone was higher. But Bitcoin was also worth less then.

Now, no one mines bitcoins solo. Participants join special groups—mining pools—where everyone tries to find the right hash together. If anyone in the group finds it, the reward is split among participants based on their contribution. So you mine and get a small payout every week from the group’s total.

But solo mining is still possible in other networks. Until recently, it was easy to mine Ethereum, where blocks are found every 10 seconds. The reward per block is much lower, but the chance of earning a little is higher.

So are we just going to keep burning thousands of video cards for nothing?

Yes, but there are ideas. The mining I described is classic and called Proof-of-Work. Each machine proves it worked for the network by solving pointless problems with a set probability.

But some people are making blockchains with other types of mining. The second most popular concept is Proof-of-Stake. In this type of mining, the more coins a participant has, the higher their chance to add their block to the blockchain—like the loudest guy in the village.

You can come up with other types of mining. As some have suggested, all computers in the network could search for a cure for cancer, but you’d need a way to record each participant’s contribution. After all, I could claim I’m participating but turn off my video card and do nothing. How do you measure each participant’s contribution to the search for a cure? If you figure it out, go ahead and make your own CancerCoin—the media hype is guaranteed.

Blockchain in Action

Imagine that, despite all the probability theory, two miners manage to find the right answer at the same time. They start sending two completely valid blocks through the network. These blocks are guaranteed to be different, because even if they miraculously picked the same transactions from the pool, built identical trees, and guessed the same random number (nonce), their hashes would still be different, since each would write their own wallet number for the reward.

Now we have two valid blocks and the problem of which to consider first arises again. How does the network behave?

The blockchain algorithm says that network participants simply accept the first valid answer they receive. Then they continue based on their own view of the world. Both miners get their reward, and everyone else starts mining based on the last block they personally received, ignoring all other equally valid ones. The network now has two versions of the correct blockchain. A paradox, but a normal situation.

Probability theory helps here again. The network functions in this split state until a miner finds the next block for one of these chains. As soon as that block is found and added, the chain becomes longer, and one of the blockchain network’s rules kicks in: under all circumstances, the longest chain is considered the only valid one for the entire network.

The shorter chain, no matter how correct, is rejected by all participants. Its transactions return to the pool (if they weren’t confirmed in the other chain), and processing starts over. The miner loses their reward because their block no longer exists.

As the network grows, such coincidences go from “very unlikely” to “well, it happens sometimes.” Old-timers say there have been cases where a chain of four blocks was discarded at once.

Because of this, three safety rules for the end of the blockchain (end of chain insecurity) were invented:

  1. Mining rewards can only be used after 20 more confirmed blocks. For Bitcoin, that’s about three hours.
  2. If you receive bitcoins, you can only use them as inputs in new transactions after 1–5 blocks.
  3. Rules 1 and 2 are just set in each client’s settings. No one enforces them. But the longest chain rule will still destroy your transactions if you try to cheat by ignoring them.

Trying to Cheat the Blockchain

Now that you know all about mining, how blockchain works, and the longest chain rule, you might wonder: can you somehow outrun the blockchain by building the longest chain yourself, confirming your fake transactions?

Suppose you have the most powerful computer on Earth. Google and Amazon’s data centers are at your disposal, and you try to calculate a chain that becomes the longest in the blockchain network.

You can’t just calculate several blocks at once, since each block depends on the previous one. So you try to calculate each block as fast as possible in your huge data centers, while everyone else keeps growing the main blockchain. Is it possible to overtake them? Probably, yes.

If your computing power is more than 50% of the entire network, you have a 50% chance of building a longer chain faster than everyone else combined. This is a theoretical way to cheat the blockchain by calculating a longer chain of transactions. Then all the real network’s transactions would be considered invalid, you’d collect all the rewards, and start a new era in cryptocurrency history called a “blockchain split.” This actually happened once with Ethereum due to a bug in the code.

But in reality, no data center can match the power of all the world’s computers. One and a half billion Chinese with ASICs, another one and a half billion hungry Indians with mining farms and cheap electricity—that’s a huge amount of computing power. No one in the world can compete with them alone, not even Google.

It’s like going outside and trying to convince everyone in the world that the dollar is now worth one ruble and doing it before the media exposes you. If you manage to convince everyone, you could crash the world economy. Theoretically possible? Yes. But in practice, no one’s ever done it.

This probability is what blockchain relies on. The more miners, the greater the network’s security and trust. That’s why when a big mining farm in China is shut down, the price drops. People fear that somewhere there’s an evil genius who’s already gathered a mining pool with ~49% of the network’s power.

This has actually happened a few times, like in 2014, when one mining pool temporarily became more powerful than the rest of the network. But no manipulation was observed.

Conclusion: Blockchain Is Not Just for Cryptocurrencies

Blockchain isn’t a strictly defined set of algorithms. It’s a way to build an unforgeable network among participants where no one can trust anyone. While reading, you probably thought, “You could tweak it like this and make it even more useful.” That means you understand blockchain—congratulations.

Some people around the world have also understood it and wanted to improve or adapt it for specific tasks. Not just for cryptocurrencies, though there are plenty of those too. Here’s a brief list of some ideas and projects that have gained popularity by rethinking the blockchain concept.

Leave a Reply