What blockchain is needs to be defined at the very beginning of the article. When you hear the word blockchain in a conversation, you immediately understand that miners, traders, investors have gathered - people make money on cryptocurrencies. However, this concept has long left the narrow circle of professionals, and now many people operate with it. If you still haven’t figured out what it is, read our article, where we will tell you about blockchain in simple words.
Intermediary companies complicate life, complicate transactions and waste our time. Take, for example, a bank - not only will many transactions require personal information, but you will also spend a lot of time and money - banks live on interest from the finances circulating in the system.
During globalization, the emergence of cryptocurrencies and blockchain is natural and justified. Those who are “in the know” call this system a breakthrough of the century and a new step in the development of society. Those who are not privy to the nuances of the crypto world are wary of blockchain, Bitcoin, mining and cryptocurrencies, expecting the “bubble” to burst any day now.
Blockchain in the simplest words
Let's say you keep a personal accounting diary. You write down in it what you did, how you spent the money, how much you earned. Usually this diary is available in one copy.
With blockchain it’s different - transaction records are encrypted, and the diary itself (its copies) is simultaneously stored by all your friends. The entries in it are constantly updated, the information is relevant, and there is no risk of losing the diary - you can restore it again.
Blockchain (translated from English as “chain of blocks”) is, in simple words, the same diary or database, which is presented in the form of blocks that are stored on a large number of computers. New elements appear constantly as information accumulates. Each block contains all the accumulated information (that is, transaction records).
Security, decentralization, availability of data at any time of the day and anywhere - this is blockchain.
Structure
With the usual movement of the hand, we open the protocol specification and look at the structure of the block.
- version — block version
- prev_block — hash of the previous block (parent block)
- merkle_root - to put it simply, this is a hash of all transactions in the block
- timestamp — date and time of block creation
- bits, nonce - I will talk about these parameters in detail in the chapter Bitcoin in a nutshell - Mining
- txn_count, txns — number of transactions in the block and their list
The first six parameters (all except txn_count and txns) form the block header. It is the header hash that is called the block hash, that is, the transactions themselves do not directly participate in hashing.
Instead, they are entered into a special structure - the Merkle tree, which I will talk about below.
Blockchain: why is it needed?
All information about operations, transactions, payments and other actions is stored somewhere. Most often these are servers of banks, government organizations, and enterprises. This information can be changed, which is used by unscrupulous people. Blockchain technology or a chain of blocks, if translation from English is needed, is much more reliable. The transaction history is not stored in one place - it is distributed across thousands of computers around the world. In this form, it is more reliable to store data - it will not be lost, will not be lost, will not disappear.
This is the meaning of blockchain - to create a data storage system that is transparent and secure. The system has the main features:
- Safety;
- Openness;
- Distribution.
All information in the system is encoded, and choosing a code is unrealistic - it is too complex and not economically feasible.
To steal or change data, you will have to not just decode and change data on one server or computer, but on all of those in the system. Another option is to have control over 51% of Bitcoin's mining power.
Considering that there can be several hundred thousand or several million computers, it is almost impossible to change information on all devices. Even the signature contains two keys - public and private. Only the key allows access to the participant’s data.
Digital data is distributed across chains without the need for copying. This quality has led to blockchain technology becoming the foundation of a new type of Internet. Blockchain was originally developed as the basis for the bitcoin cryptocurrency, but digital technology experts predict a great future for it, finding new prospects for use.
Let's take another example - Wikipedia. It was created not by one user, but by many, and everyone can add new information to it, monitor it and control the entries. Something similar exists in blockchain: chains of blocks are not the product of an individual, but the result of the actions of people participating in the network. This is the principle of distribution - each participant is both a creator and a controller .
Registration and login to your account on Blockchain.info
So, first you will need to create your account in the BlockChain.info service. To do this, click on the “Get A Free Wallet” and fill out the simplest registration form (no more difficult than on any regular service - just take care of storing the password in encrypted form, because this is access to money):
That's all. All you have to do is look in your mailbox and follow the link to confirm the email you specified during registration. What did you think? I told you, it couldn’t be simpler.
Important point! In the letter (as well as in your personal account) you will see a set of numbers and letters indicating that this is “Your wallet ID” . It is very important that you understand that this is not the address of your wallet (and certainly not the private key, i.e. the password for it). This is just your login to enter the BlockChain.info website and nothing more.
To log into your account, you need to go to the service’s official website from the top menu to the “ Wallet ” tab, then enter your login (the same identifier) and password.
The essence of blockchain technology
What is the Bitcoin blockchain? Within the network, a table is created that is repeated many times (millions and thousands) on computers connected to this network. Each computer monitors the information and can control it, but trying to change this table on one computer will immediately reveal itself. This will be immediately noticed by other users who have an unchanged table. The network is protected from fraudsters and falsifications.
Openness of information for network users is the principle of this technology. An important advantage of distribution was the absence of intermediaries. Yes, sometimes you can hear that after a while the National Bank will become a kind of guarantor, performing the function of an intermediary, but in practice there is no place for an intermediary.
How does the Bitcoin blockchain network work?
Despite the abundance of information, understanding blockchain, even if explained in simple words, is quite difficult. What the essence is is easier to see with an example.
- You have a certain amount of money that you want to send to your friend from Russia to the States. You can use a bank, but they will charge you a commission, and most likely a considerable one, and besides, it will take time. If you don’t want to wait a week or overpay interest, use the blockchain.
- You create a wallet and buy bitcoins for the required amount. Your friend will also create a wallet to which you will need to send the amount in bitcoins.
- Miners create a block that contains transactions. He lines up in a chain. To create it, the hash of the previous block, the hash of all transactions and the nonce variable are used. These values are run through a specific algorithm, and the output is a hash of the new block. This is how the mining process happens.
- The transaction is confirmed, and your friend receives bitcoins in the required amount to their wallet. Bitcoins can be converted into fiat money or exchanged for other cryptocurrencies (altcoins). The main point is that blockchain made it possible to quickly transfer money.
The amount of the commission may vary - its amount is set by the creator of the transfer. A transaction with a small commission is slower - after all, all miners are interested in a larger reward, they set up equipment with priority for the highest commission. To ensure that transactions with small commissions are also transferred quickly, the block structure allows processing only 10% of super profitable transfers.
Blockchain for dummies
Blockchain itself is an extremely simple thing. This is most easily illustrated using the ledger that records all transactions on the Bitcoin network. Moreover, such a book is present in every network participant, which means that anyone, if desired, can check whether this or that transaction took place in reality or not.
And if the entire blockchain is a book, then individual blocks can be thought of as pages on which transactions are “recorded.” Each block “references” the previous one and so on until the very first block (genesis block). This is what creates such an interesting feature of the blockchain as immutability. You can't take block #123 and change it without anyone noticing. Because the blockchain is designed in such a way that this will entail changing block #124, then #125, and so on, until the very top.
What you need to know about hashing
We wrote above about hashing, but did not consider this term in detail.
Hashing is a basic element of cryptography, without which the entire operation of the system would not be possible.
Hashing is a check of the integrity of a digital or alphabetic message and is performed using a special algorithm. By running a message through this algorithm, a participant in the system hashes it, resulting in a hash. This hash is a kind of code that consists of 60 characters - letters or numbers.
The hash is sent to the recipient along with the message. The recipient runs it through the same algorithm, that is, decodes the message. The result is a hash identical to the one that was sent to him earlier. This encoding (in other words, hashing) confirms that no changes were made to the message. Hashing is an irreversible process. The message cannot be dehashed. If the message has already been received and acknowledged, it cannot be hashed again.
The founder of Bitcoin, Satoshi Nakamoto (by the way, it is still not clear whether this is one person or a group of people under a pseudonym) decided to connect hashes into a chain, obtaining a chain of blocks.
Transactions in the blockchain and the role of private keys
Above we talked about creating a Bitcoin wallet, but in fact, what is considered a wallet is not one. If we talk about a WebMoney wallet or other virtual currency, then bitcoins are stored on the blockchain according to a different principle. There is no control over the financial balance. But how does the system know whether you have enough funds to send, for example, 100 bitcoins to your friends?
Let's take a closer look at how transactions occur. The amount of bitcoins appears only in your wallet, that is, it is recorded only on your phone, for example, in the form of a hash. Blockchain does not know this information - it does not store it, does not record it, does not check it. If you need to send 100 btc to someone, the blockchain network will look for receipts to the wallet so that the amount of receipts is 100 bitcoins. Let’s say a thousand transactions of 0.1 bitcoin each came to your wallet - the system will add up these transactions, receive the required amount and understand that you have enough funds that can be sent to whomever you want.
Raw block
If you still have some questions about the structure of the block, then I suggest you look at them in a “raw” form. The most obvious way to do this is to run bitcoind-daemon for a couple of hours, and then examine the already downloaded blocks. But firstly, not everyone has the time/desire to synchronize the blockchain. Secondly, in Bitcoin, blocks are stored in a very specific LevelDB database, also in a rather strange way. And since the book is intended not only for experienced developers, I will follow the already proven path and again use the protocol in its original form.
To receive the block, we will send a getdata message in which we indicate type: MSG_BLOCK and hash: 000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506 - this is the hash of block #100.000. You can see the entire code here.
def getdataMessage(): block_hash = '000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506' count = struct.pack("
Development of Blockchain technology
Miners, processing transactions, release new blocks and a new portion of bitcoins, for which they receive a reward. Simplicity and the ability to get cryptocurrency almost out of thin air are the main reasons why Bitcoin and blockchain have become so popular.
Blockchain, or block chain, as the translation from English says, from the very beginning positioned itself as a simple, transparent and fast technology. Its ancestral home is Japan, and only then it came to other countries. Initially, it was conceived as a counteraction to the imperfect banking system, so the developments are mainly used in the field of finance. However, if previously only narrow specialists worked with it, today this technology is entering our lives by leaps and bounds, and it will no longer be possible to brush it aside. And is it worth it? Many modern economists believe that blockchain is the future, and we find this reflected in the news of the world of cryptocurrencies.
Security, speed, openness of transactions and the principles of anonymity have led to the fact that even representatives of states and government agencies can no longer turn a blind eye to blockchain technologies used in cryptocurrencies.
And abroad, virtual money is used not only for transactions, but also for the purchase of almost any goods:
- online stores Amazon and eBay;
- some Subway restaurants;
- you can buy a ticket for airBaltic planes with bitcoins;
- in Cyprus you can pay for tuition with crypto;
- Dell was one of the first to accept payments for equipment using cryptocurrency.
Merkle tree
Technical side
A Merkle tree is a data structure also known as a binary hash tree. In the case of Bitcoin, it is constructed as follows:
- First, the hashes of all transactions in the block are calculated hash_A = SHA256(SHA256(A))
- Then hashes are calculated from the sum of transaction hashes hash_AB = SHA256(SHA256(hash_A + hash_B))
- In the same way, we calculate hashes from the sum of the resulting hashes hash_ABCD = SHA256(SHA256(hash_AB + hash_CD)) and then recursively. Lyrical digression - since the tree is binary, there must be an even number of elements at each step. Therefore, if, for example, we have only three transactions, then the last transaction is simply duplicated:
- The process continues until one single hash is obtained - it is called merkle_root (the third field in the header of the block)
Below is an implementation of a Merkle tree, you can check it on some block.
import hashlib # Hash pairs of items recursively until a single value is obtained def merkle(hashList): if len(hashList) == 1: return hashList[0] newHashList = [] # Process pairs.
For odd length, the last is skipped for i in range(0, len(hashList)-1, 2): newHashList.append(hash2(hashList , hashList[i+1])) if len(hashList) % 2 == 1: # odd, hash last item twice newHashList.append(hash2(hashList[-1], hashList[-1])) return merkle(newHashList) def hash2(a, b): # Reverse inputs before and after hashing # due to big-endian / little-endian nonsense a1 = a.decode('hex')[::-1] b1 = b.decode('hex')[::-1] h = hashlib.sha256(hashlib.sha256 (a1 + b1).digest()).digest() return h[::-1].encode('hex')
Immutability
Now about why this is needed in Bitcoin. I think you understand that if you change at least one transaction, then merkle_root will also change. Therefore, this data structure makes it possible to ensure the “unforgeable” transactions in the block. That is, the following situation cannot happen:
One of the miners found a new block and began spreading it across the network. At this time, the attacker intercepts the block and, for example, removes some transaction from the block, after which he distributes the already changed block.
To check, just calculate merkle_root yourself and compare it with what is written in the header of the block.
SPV
But here one can reasonably argue that, firstly, such difficulties are completely unnecessary. It is enough to simply calculate the hash of the sum of all transactions in the block txns_hash = SHA256(SHA256(sum(txns))) - it will also change after any manipulations with transactions. And, secondly, what prevents an attacker from replacing merkle_root in a block? I’ll answer the second question right away: in fact, nothing can be changed in the block at all, because the block will immediately become invalid (you will understand this after reading the next chapter Bitcoin in a nutshell - Mining).
And the Merkle tree is actually needed in order to be able to create SPV nodes (Simplified Payment Verification). Such nodes synchronize only block headers, without the transactions themselves. As a result, the blockchain takes up an order of magnitude less space (for beauty, let’s take a height of 500,000 blocks, the header size is fixed - 80 bytes):
500.000 * 80 / 1024 / 1024 ≈ 40 MB
Such a blockchain can already be easily placed on a phone, tablet or some kind of IoT. Which in the future should lead to greater decentralization, network security, and so on.
The essence of simplified payment verification is as follows: let you have an SPV node. I have the entire blockchain and I need to convince you that some kind of transaction really took place (in the picture it is transaction K). In this case, I just need to provide you with a few hashes: H_L, H_IJ, H_MNOP, H_ABCDEFGH, they are also called authentication path.
After which you first calculate H_K = SHA256(SHA256(K)), then H_KL = SHA256(SHA256(H_K + H_L)) and so on until the very top. If you eventually find a block with the same merkle_root, then the existence of the transaction is considered confirmed.
BTW Ralph Merkle even patented his data structure, as evidenced by patent US4309569 A.
The difference between Ethereum and Bitcoin
Bitcoin and Ethereum are the main cryptocurrencies that probably every advanced Internet user has heard of. Some consider them competitive systems, others – analogues. Ethereum and Bitcoin are constantly compared in terms of benefits, capitalization and popularity. Today, Bitcoin is winning by a margin, but the situation may change. The Bitcoin and Ethereum blockchain uses the PoW (Proof of Work) method of achieving distributed consensus; it is the miners who, thanks to PoW, ensure the security of the network.
What is the Ethereum blockchain? With Ethereum, everything is more complicated; it was originally created as a platform that was supposed to surpass Bitcoin. Ethereum is a kind of ecosystem for integrating new decentralized applications, a kind of service complex and the basis for creating a software product with the ability to mine ether coins.
New technologies have room to develop, and the natural rise in cryptocurrency rates confirms the fact that blockchain is needed today, and over time it will only gain momentum. There is confidence that we will soon see it in many areas of our lives, not only financial. Widespread distribution is only a matter of time. Very soon, every schoolchild will know about blockchain technology.
Great article 7