A course on blockchain has been developed in Daizi Boudoir: "ETH Principles and Intelligent Contract Development in Simple Terms", which is taught by Teacher Ma Liang. This collection records my study notes.
course ***8 classes. Among them, the first four lessons talk about the ETH principle, and the last four lessons talk about smart contracts.
The fourth lesson is divided into three parts:
This article is the study note of the first part of the fourth lesson: Ethash algorithm.
This lesson introduces the core mining algorithm of Ethereum.
before introducing Ethash algorithm, let's talk about some background knowledge. In fact, blockchain technology is mainly to solve the problem of * * * knowledge, and * * * knowledge is a very rich concept. Here, the scope is narrowed down and only the * * * knowledge in blockchain is discussed.
what is * * * knowledge?
in the blockchain, * * * knowledge refers to which node has the bookkeeping right. There are many nodes in the network, all of which theoretically have the right to keep accounts. The first problem is who will keep accounts. Another problem is that transactions must be in order, that is, who is in front and who is in front. This can solve the problem of double flowers. The * * * knowledge mechanism in the blockchain is to solve these two problems, who keeps accounts and the order of transactions.
what is the workload proof algorithm
in order to decide who will keep accounts among many nodes, there are many schemes. Among them, the workload proof allows the node to calculate a hash value to meet the goal of winning the difficulty value. This process can only be calculated by enumeration, and whoever calculates quickly has a high probability of winning. Revenue is related to the workload of nodes, which is the workload proof algorithm.
why should we introduce the workload proof algorithm?
Hash Cash was published by Adam Back in 1997, and it was first used in bitcoin in Satoshi Nakamoto to solve the problem of * * *.
it was originally used to solve the spam problem.
its main design idea is to find a Block header combination through brute force search (by adjusting nonce) so that the output of one-way hash value of nested SHA256 is less than a specific Target.
This algorithm is computationally intensive. At first, it was mined from CPU, then turned into GPU, then turned into FPGA, and then turned into ASIC, thus making the computing power very concentrated.
Concentration of computing power will bring a problem. If a mine pool has a computing power of 51%, it will be at risk of doing evil. This is the disadvantage of systems that use workload proof algorithms such as Bitcoin. However, Ethereum learned this lesson, made some improvements, and gave birth to the Ethash algorithm.
Ethash algorithm draws lessons from bitcoin, and specially designs a model that does not use calculation. It uses an I/O-intensive model, which is slow in I/O and useless even if the calculation is fast. In this way, it is not so effective for application specific integrated circuits.
this algorithm is GPU-friendly. First, consider that if you only support CPU, you are worried about being attacked by Trojan horses. Second, the current memory is very large.
The algorithm of light client is not suitable for mining and easy to verify; In the fast start
algorithm, it mainly relies on Keccake256.
in addition to the traditional Block header, the data source also introduces a random number array DAG (directed acyclic graph) (proposed by Vitalik)
The seed value is very small. According to the seed value, the cache value is generated. The initial value of the cache layer is 16M, and it is increased by 128K in each generation.
Below the cache layer are the data values used by miners. The initial value of the data layer is 1G, and each generation is increased by 8M. The size of the entire data layer is a prime multiple of 128Bytes.
the framework is mainly divided into two parts, one is the generation of DAG, and the other is to use Hashimoto to calculate the final result.
DAG is divided into three layers: seed layer, cache layer and data layer. The three levels are gradually increasing.
the seed layer is very small and depends on the seed layer of the previous generation.
The first data in the cache layer is generated according to the seed layer, and the following data is generated according to the previous one, which is a serialization process. Its initial size is 16M, and each generation increases by 128K. 64 bytes per element.
The data layer is the data to be used. Its initial size is 1G, and now it is about 2 gigabytes, and each element is 128 bytes. The elements of the data layer depend on the 256 elements of the cache layer.
the whole process is memory intensive.
firstly, the header information is combined with the random number, and a Keccak operation is performed to obtain the initial one-way hash value Mix[] with 128 bytes. Then, through another function, map it to DAG to get a value, and then mix it with Mix[] to get Mix[1], and so on for 64 times to get Mix[64] with 128 bytes.
next, after post-processing, the value of mix final, 32 bytes, is obtained. (This value has appeared in the previous two sections "9:GHOST Protocol" and "1: Building Test Network")
After calculation, the result is obtained. Compare it with the target value, and if it is less than the target value, the mining is successful.
the higher the difficulty value, the smaller the target value, the more difficult it is (the more zeros are needed in front).
this process is also difficult to mine and easy to verify.
in order to prevent the mining machine, the mix function function has also been updated.
see the screenshot of the courseware for the difficulty formula.
calculate the next block according to the difficulty of the previous block.
From the formula, the difficulty consists of three parts, first, the difficulty of the previous block, then the linear part, and finally the nonlinear part.
The nonlinear part is also called the difficulty bomb. After a certain time node, the difficulty increases exponentially. The purpose behind this design is to change the understanding from POW to POW and POS in the project cycle of Ethereum and in the next version after the metropolitan version. The foundation may mean to make mining boring.
The difficulty graph shows that in October 217, the difficulty dropped greatly, and the reward was changed from 5 to 3.
this section mainly introduces the Ethash algorithm. please criticize and correct the shortcomings.