Blockchain Tutorial for Beginners
Blockchain Tutorial for Beginners
There is a video explanation at the bottom of the article
I. The nature of blockchain
What is blockchain? In a word, it is a special kind of distributed database.
Firstly, the main function of the blockchain is to store information. Any information that needs to be stored can be written to the blockchain and can be read from it, so it is a database.
Secondly, anyone can set up a server and join the blockchain network as a node. Inside the blockchain world, there is no central node, each node is equal and keeps the entire database. You can write/read data to any node, as all nodes will eventually be synchronised, guaranteeing a consistent blockchain.
II. The best features of blockchain
Distributed databases are not a new invention, they have been on the market for a long time. However, blockchain has one revolutionary feature.
The blockchain has no administrators, it is completely decentralised. All other databases have administrators, but the blockchain does not. If someone wanted to add auditing to the blockchain, it wouldn't be possible because it is designed to prevent the emergence of a centrally located administration.Other databases have administrators, but blockchains do not. If someone wanted to add auditing to the blockchain, it wouldn't be possible because it is designed to prevent the emergence of a centrally located administration.
It is because it is unmanageable that the blockchain can be made uncontrollable. Otherwise once the big companies and conglomerates control the management, they would control the whole platform and all other users would have to do their bidding.
But without an administrator, everyone can write data into it, so how can you be sure it's trustworthy? What if it's changed by the wrong people? Read on, this is where the magic of blockchain comes in.
III. ZONES
A blockchain is made up of blocks. A block is much like a record in a database; each time data is written, a block is created.
Each block contains two parts.
- Block Header: records the characteristic values of the current block
- Block Body: actual data
The block header contains several characteristic values of the current block.
- Generation time
- Generation time
- Hash of the previous block
Here, you need to understand what is meant by Hash (hash), which is necessary to understand the blockchain.hash,This is necessary to understand blockchain.
A "hash" is when a computer can calculate a characteristic value of the same length for any content. The blockchain hash is 256 bits long, which means that whatever the original content is, a 256-bit binary number will be calculated at the end. And it is guaranteed that as long as the original content is different, the corresponding hash must be different.
As an example, the string123
The hash isa8fdc205a9f19cc1c7507a60c4f01b13d11d7fd0
v123
It is possible to get this hash. (It is theoretically possible for other strings to get this hash, but the probability is so low that it can be approximated as unlikely to happen.)
There are thus two important corollaries.
- Corollary 1: The hash of each block is different and the block can be identified by the hash.
- Corollary 2: If the content of a block changes, its hash must change.
IV. The Immodifiability of Hash
The hash of each block is calculated for the "head" of the block. In other words, the features of the block head are concatenated together in order to form a long string, and the hash is calculated for this string.
Hash = SHA256( block header )
Above is the formula for calculating the block hash, theSHA256
is the blockchain's hashing algorithm. Note that this formula contains only the block header and not the block body, i.e., the hash is uniquely determined by the block header that
As mentioned earlier, the block header contains a lot of content, among which are the hash of the current block body and the hash of the previous block. This means that if the contents of the current block body change, or if the hash of the previous block changes, it must cause the hash of the current block to change.
This has significant implications for the blockchain. If someone modifies a block, the hash of that block changes. In order for subsequent blocks to still be connected to it (since the next block contains the hash of the previous one), that person must modify all subsequent blocks in turn, otherwise the changed block is out of the blockchain. For reasons to be mentioned later, hash calculations are time consuming and modifying multiple blocks in a short period of time is almost impossible to happen unless someone has more than 51% of the computing power of the entire network.
It is through this linkage mechanism that the blockchain guarantees itself reliability; once data is written, it cannot be tampered with. It is like history, what happens is what happens, and it can never be changed again.
Each block is connected to the previous one, which is where the name "blockchain" comes from.
V. Mining
Since synchronisation between nodes must be guaranteed, new blocks cannot be added too quickly. Imagine that you have just synchronised a block and are ready to generate the next block based on it, but then another node generates a new block and you have to abandon half of your calculation and go back to synchronisation. Because each block can only be followed by one block, you can only ever generate the next block after the latest block. So, you have no choice but to synchronise as soon as you hear the signal.
So, Satoshi Nakamoto, the inventor of the blockchain (this is a pseudonym, and his real identity is still unknown), deliberately made adding new blocks, difficult. His design was such that, on average, only one new block could be generated across the network every 10 minutes, or six in an hour.
This output speed is not reached by command, but by deliberately setting up a huge amount of calculations. That is, only through extremely large amounts of computation can the valid hash of the current block be obtained so that new blocks can be added to the blockchain. It is not fast enough because of the sheer volume of calculations.
This process is called mining, because the difficulty of calculating a valid hash is like finding a single grain of sand in the world that fits the bill. The machine that calculates the hash is called a miner, and the person who operates it is called a miner.
VI. Difficulty factor
When you read this, you may have a question. People say mining is hard, but isn't mining just using a computer to work out a hash, which is what computers are good at, so how can it become hard and delayed?
It turns out that not just any hash will do; only those that satisfy the condition will be accepted by the block link. This condition is particularly harsh, making the vast majority of hashes fail to meet the requirement and have to be recalculated.
It turns out that the block header contains adifficulty factor(difficulty), this value determines the difficulty of computing the hash. For example, the difficulty factor for the 100000th block is 14484.16236122.
The blockchain protocol states that the target value (target) can be obtained by dividing a constant by a difficulty factor. Obviously, the larger the difficulty factor, the smaller the target value.
The validity of a hash is closely related to the target value, only hashes smaller than the target value are valid, otherwise the hash is invalid and must be recalculated. Since the target value is so small, the chances of a hash being less than that value are extremely slim, and it may be possible to count a billion times before hitting it once. This is the fundamental reason why mining is so slow.
As mentioned earlier, the hash of the current block is uniquely determined by the block header. To calculate the hash repeatedly for the same block would mean that the block header would have to keep changing, otherwise it would be impossible to calculate a different hash. All the eigenvalues inside the block header are fixed, and to make the block header change, Satoshi Nakamoto deliberately added a random term called Nonce.
Nonce is a random value and the miner's role is to guess the value of the nonce so that the hash of the block header can be smaller than the target value and thus be written to the blockchain. nonce is very difficult to guess and currently can only be guessed one by one through exhaustive trial and error. According to the protocol, Nonce is a 32-bit binary value, i.e. it can go up to 2.147 billion. The Nonce value for the 100000th block is274148111
This can be interpreted as the miner starting from 0 and counting 274 million times before getting a valid Nonce value that allows the calculated hash to satisfy the condition.
If you are lucky, you may find the Nonce in a few moments; if you are unlucky, you may not find the Nonce after 2.147 billion calculations, i.e. it is impossible to calculate a hash that satisfies the condition in the current block. At this point, the protocol allows the miner to change the block body and start a new computation.
VII. Dynamic adjustment of the difficulty factor
As mentioned in the previous section, mining is random in nature and there is no guarantee that a block will be produced in exactly ten minutes, sometimes it will come out in a minute and sometimes it may not come out for hours. In general, as hardware equipment improves and the number of miners
In order to keep the output rate constant at ten minutes, Satoshi Nakamoto also devised a dynamic adjustment mechanism for the difficulty factor. He specified that the difficulty factor would be adjusted every fortnight (2016 blocks). If the average block generation rate is 9 minutes in those two weeks, it means that it is 10% faster than the legal rate, so the difficulty factor is adjusted up by 10% for the next two weeks; if the average generation rate is 11 minutes, it means that it is 10% slower than the legal rate, so the difficulty factor is adjusted down by 10% for the next two weeks.
The difficulty factor has been adjusted higher and higher (with smaller and smaller target values), resulting in mining becoming more and more difficult.
VIII. Blockchain forks
Even if the blockchain is reliable, there is now an unresolved problem: if two people write data to the blockchain at the same time, that is, if two blocks are joined at the same time, because they are both connected to the previous block, a fork is formed. Which block should be adopted at this point?
The rule now is that the new node always uses the longest blockchain. If there is a fork in the blockchain, it will depend on which branch is behind the fork point and reaches six new blocks first (called "six confirmations"). Based on a block in 10 minutes, this would be confirmed in one hour.
Since the rate at which new blocks are generated is determined by computing power, this rule says that the branch with the majority of computing power is the authentic blockchain.
IX. Summary
The blockchain has been operating as an unmanaged distributed database for eight years since 2009 without major problems. This proves that it works.
However, in order to ensure the reliability of the data, the blockchain has its own costs. One is efficiency, as data is written to the blockchain with a minimum wait of ten minutes, and more time is needed when all nodes synchronise their data; the second is energy consumption, as the generation of blocks requires miners to perform countless meaningless calculations, which is very energy-intensive.
The scenarios in which blockchain is applicable are, therefore, actually very limited.
- There is no governing authority that all members trust
- The data written is not required to be used in real time
- The proceeds from mining can cover its own costs
If the above conditions cannot be met, then a traditional database is a better solution.
Currently, the biggest application scenario (and probably the only one) for blockchain is cryptocurrency, represented by Bitcoin. In the next article, I'll cover a primer on Bitcoin.
For those who don't understand much of the article, you can watch the video below, which goes into more detail. ?
Please specify source if reproduced区块链入门教程 | Dexnav 区块链导航网