Blockchain from the inside: how Bitcoin works

By Crypter9 | Cryptointerest | 19 Feb 2024


We reveal all the possibilities of the Bitcoin blockchain with examples on most questions in a more detailed understanding.
Basics: Why do we need blockchain?
The blockchain was described in the article “Bitcoin: A Peer-to-Peer Electronic Cash System” by Satoshi Nakamoto. There, in just eight pages, the author described the basics of the Bitcoin cryptocurrency, which was based on the Blockchain algorithm.
Blockchain appeared along with Bitcoin, but can be used independently of it and even modified. Anyone can make their own blockchain even on their laptop.
List that cannot be changed
Blockchain is a chain of blocks or in other words a linked list. In such a list, each subsequent entry refers to one previous one and so on in the chain until the very first. Like train cars, each one drags the next one along.
Let's look at it with an example.
Oleg's friends constantly borrow money from him. Oleg is kind, but extremely forgetful. A week later, he no longer remembers who did not repay him, but is embarrassed to ask everyone about it. Therefore, one day he decided to put an end to this by making a list of friends to whom he lent money on a board at home.
Now Oleg can always go up to the board and make sure that Max has returned everything, but Vanya won’t give back the 700 rubles. One day Oleg invites Vanya to have a drink at his home. While Oleg goes to the toilet, Vanya erases the entry “I borrowed Vanya 200 rubles” and writes in its place “Vanya gave 500 rubles.”
Oleg, who trusted his list, forgets about his debt and loses 700 rubles. He decides to deal with this somehow.
Last year, Oleg attended a programming course, where he was taught about hashing. He remembers that any string can be turned into a unique set of characters - a hash, and changing any character in the string will completely change it.
Adding a dot at the end changed the final hash beyond recognition - you can take advantage of this.
Oleg takes the well-known SHA-256 hash and hashes each entry with it, adding the result at the end. Now Oleg can make sure that no one has changed his records by hashing them again and comparing them with the green one.
But EVIL RYUSKY IVAN also knows how to use SHA-256 and can easily change the record along with its hash. Especially if the hash is written right next to it on the board.
Therefore, for greater security, Oleg decides to hash not only the record itself, but add it together with the hash from the previous record. Now all subsequent entries depend on the previous ones. If you change even one line, you will have to recalculate the hashes of all the others below in the list.
This is how Oleg gets a personal connected list.
But one day Ivan sneaks in at night, changes the entry he needs and updates the hashes for the entire list to the end. It takes him several hours, but Oleg is still fast asleep and cannot hear. In the morning, Oleg discovers an absolutely correct list - all the hashes match. But Ivan deceived him anyway, even though he spent a sleepless night on it. How else can you protect yourself from Night Ivan?
Oleg decides to somehow complicate his life. Now, to add a new entry to the list, Oleg will solve a complex problem associated with it, for example a mathematical equation. He will add the answer to the final hash.
Oleg is strong in mathematics, but even it takes him ten minutes to add an entry. Despite this, the time spent is worth it, because if Ivan wants to change something again, he will have to solve the equations again for each line, and there can be dozens of them. This will take a lot of time, because the equations are unique each time and associated with a specific record.
But checking the list is still just as simple: first you need to compare the hashes as before, and then check the solutions to the equations by simple substitution. If everything matches, the list has not been changed.
In reality, the equations are not so good: computers are too good at solving them, and where to store so many unique equations. Therefore, the authors of the blockchain came up with a more beautiful problem: you need to find a number (nonce) such that the final hash of the entire record begins with 10 zeros. Such a nonce is difficult to find, but the result can always be checked simply with your eyes.
Mini-game with hashes
You can try it. Manually searching for a hash starting with 10 zeros takes too long, so let’s look for two for fun. In the “Nonce” field below, write whatever you want. As soon as the hash of what you entered begins with “00,” the game stops.
Enter any characters until their hash starts with “00”:
Now Oleg checks all the hashes and additionally makes sure that each one begins with the agreed number of zeros. Cunning Ivan, even armed with a powerful laptop, will not have time to recalculate all the hashes overnight so that they satisfy the condition - there is not enough time.
Such a list is essentially a home blockchain on the knee. Its security is guaranteed by mathematicians who have proven that these hashes cannot be calculated any faster than by brute force. This search of hashes for each record is mining, which will be discussed in detail today.
Centralization of trust
Our friends liked the idea of keeping an untampered list of “who held who.” They also don’t want to remember who paid for whom at the bar and how much they still owe - everything is written down on the wall. You discussed the idea and decided that now you need a single list for everyone.
But who can be trusted to handle such important accounting? After all, when it comes to money, trust comes to the fore. We will not trust an unknown person to keep our money. Our ancestors came up with banks for this, which over time they began to trust, because they are backed by a license, laws and insurance from the Central Bank.
In a circle of friends, everyone trusts each other and you can simply choose the most responsible person for this role. But what if the question concerns strangers? An entire city, a country, or the whole world, as is the case with Bitcoin? No one there can trust anyone at all.
Decentralization: no one trusts anyone
So they came up with an alternative approach: keep a copy of the list for everyone. Thus, the attacker will have to not only rewrite one list, but also sneak into each house and rewrite the lists there. And then it turns out that someone kept several lists at home, which no one knew about. This is decentralization.
The disadvantage of this approach is that to make new entries you will have to call all other participants and inform each of them of the latest changes. But if these participants are soulless machines, this ceases to be any problem.
In such a system there is no single point of trust, and therefore no possibility of bribery and fraud. All participants in the system act according to a single rule: no one trusts anyone. Everyone believes only the information that they themselves have. This is the main law of any decentralized network.
Transactions
When buying doshirak in a store, you enter the PIN code for your card, 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 code, which the bank confirms or rejects.
Our entries like “I borrowed Vanya 500 rubles” are also transactions. But we do not have a bank authorizing the author of the transactions. How can we check that Ivan did not quietly add the entry “Max owes Oleg 100,500 rubles”?
In blockchain, the mechanism of public and private keys is used for this; IT specialists have long used them for authorization in SSH. Briefly about how this complex but beautiful mathematics works: you generate a pair of long prime numbers on your computer - a public and private key. The private key is considered super-secret because it can decrypt what is encrypted by the public key. But the other way around also works. If you tell all your friends the public key, they will be able to encrypt any message with it so that only you can read it, since you own the private one. But besides this, the public key has a useful effect - it can be used to verify that the data was encrypted with your private key, without decrypting the data itself.
We are on a decentralized internet where no one can be trusted. The transaction is signed with a private key and, together with the public key, is sent to a special storage - a pool of unconfirmed transactions. This way, any network participant can verify that it was you who initiated it, and not someone else who wants to pay with your money.
This ensures the openness and security of the network. If banks were previously responsible for this, then in blockchain mathematics is responsible for this.
Your public key is the number of your crypto wallet. That is, you can create a wallet for any cryptocurrency without going online at all.
For ordinary users who do not want to figure out how to issue and store private keys, online wallet services will help. To copy long public keys, they make convenient QR codes. Personally, I use Blockchain Wallet because it has a user-friendly mobile app and supports the two major cryptocurrencies - BTC and ETH.
Lack of the concept of “balance”
Like our board, the blockchain essentially consists only of transaction history. It does not store the balance of each wallet, otherwise we would have to invent additional security methods.
Ownership of the wallet is confirmed only by the private key. But how can others in the network make sure I have enough money to make a purchase?
Since we don’t have a balance, you must prove it. Therefore, a blockchain transaction includes not only your signature and how much you want to spend, but also links to previous transactions in which you received the required amount of money. That is, if you want to spend 400 rubles, you run through your entire history of income and expenses, and attach to your transaction those incomes where you were given 100 + 250 + 50 rubles, thereby proving that you have these 400 rubles.
Each network participant will once again check that you have not attached income twice. That the 300 rubles that Max gave last week you really haven’t spent yet.
Such income attached to the transaction in the blockchain is called inputs, and all recipients of money are called outputs. The sum of all inputs is rarely exactly the amount you want to transfer at a time - therefore, one of the outputs will most often be you yourself. In other words, a transaction in the blockchain looks like “I was given 3 and 2 BTC, I want to transfer 4 BTC from them and return the remaining 1 BTC back.”
The beauty of the blockchain is that inputs do not have to come from the same wallet. Only the key is checked. If you know the private key of all inputs, then you can easily attach them to your transaction and pay with this money. As if you were paying at a supermarket with several cards at once, from which you know the PIN code.
However, if you lose your private key, your drive dies, or your laptop is stolen, your bitcoins will be lost forever. No one will be able to use them as inputs for new transactions. This amount will be inaccessible to the whole world forever - as if you burned a wad of banknotes. There is no single bank where you can write an application with a copy of your passport, and it will print it. This requires an additional release of new bitcoins out of thin air.
Double spending problem
Above, I said that transactions are added to a special “pool of unconfirmed transactions.” Why do we need some kind of intermediate entity if we already have essentially ready-made signed transactions? Why not write them directly to the blockchain?
Because signals from point A to point B always go with a delay. Two transactions can take completely different paths. And the transaction that was initiated first may reach the recipient later because it took a longer path. This results in double spending, when the same money was sent to two recipients at once, which they will not even realize. This is not for you to pass banknotes from hand to hand.
For a decentralized network where no one can be trusted, this problem is especially acute. How do you make sure that one transaction definitely happened before the other? Ask the sender to embed the sending time into it, right? But remember, you can’t trust anyone, not even the sender. The time on all computers will necessarily be different and there is no way to guarantee they will be synchronized. A copy of the blockchain is stored on every computer in the network and each participant trusts only it.
How can you be sure that one transaction occurred before another?
The answer is simple: it is impossible. There is no way to confirm the time of a transaction on a decentralized network. And in solving this problem lies the third important idea of the blockchain, which was invented by Satoshi and which, oddly enough, is spelled out right in its name - blocks.
Blocks are the basis of the blockchain
Each running computer on the network selects from the common pool any transactions that it likes. Usually just at the highest commission he can earn on it. So he collects transactions until their total size reaches the agreed limit. In Bitcoin, this block size limit is 1 MB (after SegWit2x it will be 2 MB), and in Bitcoin Cash it is 8 MB.
The entire blockchain is essentially a list of such blocks, where each one refers to the previous one. It can be used to track any transaction throughout history, unwinding the blockchain even back to the very first record. It is this list that now weighs hundreds of gigabytes and must be completely downloaded to all computers that want to participate in the network (but to simply create transactions and transfer money, this is not necessary). It is downloaded in the same way from all nearby computers on the network, as if you were downloading a series from torrents, only new new episodes are released every 10 minutes.
Having collected transactions from the pool, the computer begins to compile from them the same unforgeable list as we did at the beginning of the post on the board at home. He just makes it in the form of a tree - he hashes the records in pairs, then the result again in pairs, and so on until only one hash remains - the root of the tree, which is added to the block. Why a tree? Because it becomes possible to remove unnecessary (spent) transactions from the block. Those. for example, there are two transactions united by a hash, one or both are no longer needed because all this has long been given away by other transactions - so these old ones can be deleted and the hash left, in the end nothing is broken. See paragraph "7. Reclaiming Disk Space" in Stoshi's original article. There is more information on the wiki: Merkle tree.
Since the current blockchain has already been downloaded, our computer knows exactly what the last block is now. All he has to do is add a link to it in the block header, hash it all and tell all the other computers on the network “look, I made a new block, let’s add it to our blockchain.”
The rest should check that the block was built according to all the rules and that we did not add unnecessary transactions there, and then add it to their chains. Now all transactions in it are confirmed, the blockchain is increased by one block and everything is going well, right?
But no. There are thousands of computers working on the network at the same time, and as soon as they collect a new block, they will almost simultaneously rush to tell everyone that their block was created first. And from the previous section we already know that in a decentralized network it is impossible to prove who really was first.
Therefore, to include a block in the chain, computers must solve some complex problem that will take them a certain amount of time.
Just like in school, when everyone was solving a difficult test, it very rarely happened that even excellent students submitted their answers absolutely at the same time.
But if for a person a difficult task is to plan a vacation so that he can get to the May holidays and have inexpensive sea tickets, then for a computer it is to add such a number (nonce) to the end of the block so that as a result the SHA-256 hash for the entire block begins , say, by 10 zeros. This is the problem that must be solved in order to add a block to the Bitcoin network. For other networks, the tasks may differ.
This is how we come to the concept of mining, which everyone has been so obsessed with in recent years.
Bitcoin mining is not some kind of sacred mystery. Mining is not about finding new bitcoins somewhere in the depths of the Internet. Mining is when thousands of computers around the world hum in their basements, crunching millions of numbers per second, trying to find a hash that starts with 10 zeros. They don't even have to be online to do this.
Why exactly 10 zeros? But just like that, it doesn’t make any sense. That's what Satoshi came up with. Because this is one of those problems for which there is always a solution, but it certainly cannot be found faster than by a long monotonous search of options.
The difficulty of mining directly depends on the size of the network, that is, its total power. If you create your own blockchain and run it at home on two laptops, then the task should be simpler. For example, so that the hash starts with only one zero, or so that the sum of even bits is equal to the sum of odd ones.
To find a hash starting with 10 zeros, it will take one computer several decades. But if you combine thousands of computers into a single network and search in parallel, then, according to probability theory, this problem is solved on average in 10 minutes. This is the time a new block appears in the Bitcoin blockchain. Every 8-12 minutes, someone on earth finds such a hash and gets the privilege of announcing their find to everyone, thereby avoiding the problem of who was first.
For finding the answer, the computer (as of 2017) receives 12.5 BTC - this is the amount of reward that is generated by the Bitcoin system “out of thin air” and decreases every four years. Technically, this means that each miner always adds one more transaction to his block - “create 12.5 BTC and send it to my wallet.” When you hear “the number of bitcoins in the world is limited to 21 million, now 16 million have already been mined” - these are the rewards generated by the network.
Go look at a real live Bitcoin block on one of the special sites. There are transactions with inputs and outputs, as many as 18 zeros at the beginning and all the hashes described above.
Any blockchain exists only as long as its miners exist.
It is the miners who add emerging transactions to the blockchain. So if someone tells you that they will “make a blockchain for X”, the first question they should answer is who will mine on it and why. Most often, the correct answer is “everyone will, because for mining we give our coins, which will grow and it is profitable for miners.” But this is not applicable for all projects. For example, tomorrow some Ministry of Health is creating its own private blockchain for doctors (and they want it), who will mine it? Therapists on weekends?
But what benefit will miners have later, when the rewards disappear or become meager?
According to the Creator’s idea, by that time people will have to believe in the reality of Bitcoin and mining will begin to pay off with the amount of commissions included in each transaction. This is where everything is heading: back in 2012, all commissions were zero, miners mined only for rewards from blocks. Today, a transaction with zero commission can hang in the pool for several hours, because competition has appeared and people are willing to pay for speed.
That is, the essence of mining is solving meaningless problems. Can't all this power be used for something more useful - finding a cure for cancer, for example?
The essence of mining is to solve any computing problem. This task must be simple enough that network participants have a stable probability of finding the answer - otherwise transactions will take forever to be confirmed. Imagine that at the checkout counter in a store you have to wait half an hour each time for the bank to confirm your transaction. No one will use such a bank.
But the task must be complex at the same time, so that the answer is not found by all network users at once. Because in this case, they announce many blocks with identical transactions to the network and there will be a possibility of “double waste” that I spoke about. Or even worse - dividing a single blockchain into several branches, in which no one will be able to figure out which transaction is confirmed and which is not.
If a reward of 12.5 BTC is awarded only once every 10 minutes and only to one person who finds a block, does it mean I need to waste my video cards for several years in the hope that one day I will get $40,000 (at the current rate)? This is exactly the case for Bitcoin. But it was not always so. Previously, the network was smaller, the complexity was lower, and therefore the probability of single-handedly finding a hash for a new block was higher. But Bitcoin wasn’t that expensive back then.
Nowadays no one mines Bitcoins alone. Now participants unite in special groups - mining pools, where everyone together tries to find the correct hash. If at least one of the group finds it, then the entire reward is divided among the participants depending on their contribution to the overall work. It turns out that you mine and every week you get a penny from the total share.
But single mining is quite possible on other networks. Until recently, it was easy to mine Ethereum, where blocks are found every 10 seconds. The reward for a block is much lower there, but the likelihood of earning a pretty penny is higher.
So we will continue to burn thousands of video cards in vain and there is no way out?
Yes, but I have ideas. The mining that I described is classic and is called Proof-of-Work. That is, each machine proves that it worked for the benefit of the network by solving meaningless problems with a given probability.
But some guys are starting to make blockchains with other types of mining. Now the second most popular concept is Proof-of-Stake (proof of ownership). In this type of mining, the more “coins” a network participant has in his account, the greater his likelihood of inserting his own block into the blockchain. Like the loudest guy in the village.
You can come up with other types of mining. As has already been suggested, all computers on the network can search for a cure for cancer, you just need to figure out how to record their contribution to the system in this case. After all, I can say that I am also participating, but turn off my video card and not count anything. How can we quantify each participant's contribution to the search for a cure for cancer? If you come up with an idea, go ahead and make your own CancerCoin, you are guaranteed hype in the media.
Blockchain
Let’s imagine a situation in which, despite all our probability theory, two miners still managed to simultaneously find the correct answer. They begin sending two absolutely correct blocks across the network. These blocks are guaranteed to be different, because even if they miraculously selected identical transactions from the pool, compiled absolutely identical trees and guessed the same random number (nonce), their hashes will still be different, since everyone will write their own wallet number for the reward in the block.
Now we have two valid blocks and the problem of who to count first arises again. How will the network behave in this case?
The blockchain algorithm states that network participants simply accept the first correct answer that reaches them. Then they live based on their own picture of the world. Both miners will receive their reward, and everyone else begins to mine, relying on the last block they personally received, discarding all other re-valid ones. Two versions of the correct blockchain appear on the network. Such is the paradox.
This is a normal situation in which probability theory again helps. The network operates in this bifurcated state until one of the miners finds the next block to one of these chains. As soon as such a block is found and inserted into the chain, it becomes longer and one of the conventions of the blockchain network is included: under any conditions, the longest chain of blocks is accepted as the only correct one for the entire network.
The short chain, despite all its correctness, is rejected by all network participants. Transactions from it are returned to the pool (if they were not confirmed in another), and their processing begins anew. The miner loses his reward because his block no longer exists.
As the network grows, such coincidences go from “very unlikely” to “well, sometimes it happens.” Old-timers say that there were cases when a chain of four blocks was discarded at once.
Because of this, three rules for end of chain insecurity were invented:
Mining rewards can only be used after another 20 confirmed blocks after receipt. For Bitcoin, this is about three hours.
If you were sent bitcoins, you can use them as inputs in new transactions only after 1-5 blocks.
Rules 1 and 2 are just written in the settings of each client. Nobody monitors their compliance. But the longest chain law will still destroy all your transactions if you try to cheat the system by not honoring them.
We are trying to deceive the blockchain
Now that you know everything about mining, the structure of the blockchain and the rule of the longest chain, you might have a question: is it possible to somehow specifically overtake the blockchain by creating the longest chain yourself, thereby confirming your fake transactions.
Let's say you have the most powerful computer on Earth. Google and Amazon data centers combined are at your disposal and you are trying to calculate a chain that will become the longest in the blockchain network.
You cannot take and immediately calculate several blocks of a chain, because each subsequent block depends on the previous one. Then you decide to count each block as quickly as possible on your huge data centers in parallel with how everyone else continues to increase the main blockchain. Is it possible to overtake them? Probably yes.
If your computing power is more than 50% of the power of all network participants, then with a 50% probability you will be able to build a longer chain faster than everyone else combined. This is a theoretically possible way to trick the blockchain by calculating a longer chain of transactions. Then all transactions of the real network will be considered incorrect, and you will collect all the rewards and begin a new milestone in the history of cryptocurrency, which is called “blockchain split”. Once, due to a bug in the code, this happened with Ethereum.
But in reality, no data center can match the power of all the computers in the world. One and a half billion Chinese with computers, another one and a half billion hungry Indians with mining farms and cheap electricity - this is enormous computing power. No one in the world can yet compete with them alone, not even Google.
It’s like going out on the street and trying to convince every person in the world that a dollar is now worth 1 ruble and doing it before the media exposes you. And if you manage to convince everyone, you can collapse the world economy. In theory, is this possible? But in practice, for some reason, no one succeeded.
Blockchain also rests on this probability. The more participating miners, the greater the security and trust of the network. Therefore, when another large mining farm is closed in China, the exchange rate collapses. Everyone is afraid that somewhere in the world there was an evil genius who had already assembled a pool of miners at ~49% capacity.
Blockchain is not only for cryptocurrencies
Blockchain is not a strictly defined set of algorithms. This is a structure for building an unforgeable network between participants, where no one can trust anyone. While reading, the thought probably crossed your mind more than once that “we can do it this way and it will be even more useful.” This means you understand blockchain, congratulations.
Some guys in the world also understood it and wanted to improve it or adapt it to some specific tasks. Not just cryptocurrencies, although there are quite a few of them too. Here is a short list of some ideas and projects that have gained some popularity due to rethinking the idea of ​​blockchain.
Ethereum
“Ether” is the second most popular word you hear in news about cryptohype, after Bitcoin. For ordinary people, this is another cryptocurrency and a way to do the most fashionable thing called ICO. The developers on the site describe Ethereum as “a blockchain builder for your needs.” This is also possible, yes.
But if you dig even deeper, ether is not just a network with coins. This is a huge global computing machine where users execute the code of other people's programs (smart contracts), receiving a reward for each completed line. And all this is decentralized, indestructible and with all the guarantees of the blockchain.
You can talk about Ethereum and smart contracts for so long that it would be enough for one more post. Therefore, let’s do it in the style of top bloggers: if this post is actively reposted and retweeted, and by Friday it gets at least 1,500 unique views, I will write a continuation about Ethereum and smart contracts.

How do you rate this article?

6


Crypter9
Crypter9

I have a passion for cryptocurrency technology, a little bit of programming, often spend time reading books, write short posts...https://www.publish0x.com/cryptointerest


Cryptointerest
Cryptointerest

Consistent information material about events, new technologies, the development of gaming platforms - platforms based on the cryptocurrency core, both for commercial purposes and for crypto enthusiasts in general. Modern blocks of information in every post of my Cryptointerest blog

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.