Detailed explanation of blockchain data structure

Why should a front-end developer study blockchain? Because it is hot, the technology is new, the field is subdivided enough, and the track has just been opened. And Ethereum has a dedicated team responsible for developing JavaScript API libraries, such as Web3.js and Ethers.js.

The essence of the blockchain is a distributed database. Its data structure is similar to a one-way linked list. Each block includes two parts, the block header and the block body . This is a bit like an http request, with a request header and a request body

The main body of the block is mainly used to store data; the content stored
in the block header can be divided into 4 parts, which are the root hash of the previous block (the hash value stored in the root node of the Merkle tree of the previous block), the root hash of the Merkle tree Column (hash value stored in the root node of the Merkle tree of the current block), timestamp , others ;

blockchain

There is nothing to say about the body of the block. Let me introduce the parts of the block header in detail.

1. What does root hash mean?

Hash is Hash (hash), the root refers to the root node of the Merkle tree, and the root hash refers to the hash value stored in the root node of the Merkle tree

2. What does hashing mean?

After any data is hashed, a fixed-length string will be obtained. This string is the hash value of the data , which can be used as the key of the data and is unique. The most commonly used hash algorithm is MD5 encryption. The hash table looks like this
hash table

3. What is a Merkle tree?

A Merkle tree is also called a hash tree. It is a complete tree that stores hash values. Most of the time it is a complete binary tree, and it may be multi-forked. In fact, a hash table is a multi-fork Merkle tree with a row height of 2.
Merkle Tree

(1) How the Merkle tree is generated

Each node of the Merkle tree stores a hash value, which generates the entire tree from the leaf node from bottom to top.
The hash value of the leaf node is the same as that in the hash table, which stores the hash value obtained after the real data is hashed.
When computing upwards, add the two hash values ​​together, and then perform a hash operation to get the hash value of the parent, so that the layer-by-layer operation finally gets the hash value of the root node to generate the entire tree.

(2) Common applications of Merkle trees

The Merkle tree is mainly used for data verification, and its biggest role is to ensure security . The most commonly used place is Thunder to download files through BT seeds. This download method belongs to distributed download. First, the file is cut into n parts, and then each part is hashed to obtain a hash value. These hash values ​​are used as leaf nodes to generate a Merkle tree, which is stored in the BT seed. After the file download is complete, perform a hash operation on the downloaded file again, and compare the hash value obtained after this operation with the hash value in the BT seed for verification.

(3) Compared with the hash table, the advantages of the Merkle tree

Compared with the hash table, the advantage of the Merkle tree is that it can perform overall verification of local data in units of branches, while the hash table either verifies the data corresponding to a hash value alone, or verifies the entire hash table data. Not as convenient as a tree structure.

4. How is the Merkle tree root hash in the request header obtained?

First, split the data in the request body and perform hash operations one by one. The obtained hash value is used as a leaf node and is operated layer by layer until the root node is obtained. The hash value stored in the root node is the root hash of the Merkle tree. .

5. Why does the blockchain use the Merkle tree root hash to link blocks?

As mentioned above when talking about the Merkle tree, the biggest function of the Merkle tree is that it can perform data verification flexibly. The principle is also said, that is, comparing the Hash value, once the data is abnormal, the newly generated hash value is absolutely inconsistent with the Merkle tree root hash.

Speaking of this, you should think of a feature of the blockchain, which cannot be tampered with . That's right, non-tampering is achieved by the Merkle tree.

Guess you like

Origin blog.csdn.net/wangyangnuli/article/details/121927490