Merkle Tree and Blockchain

Source: Internet
Author: User
Tags hash
What is Merkle tree?

Suppose you already know what a hashing algorithm is and what hash is used to do.

When the network transmits the data, a receives the transmitted file of B, and needs to confirm that the received file is undamaged. How to resolve.

One way to do this is to send the file hash results to A,a before the file is sent to the file, and then the hash is computed once and then compared to the received hash to know whether the file is corrupt.

However, when the file is very large, it is often necessary to split the file into a large number of pieces of the respective transmission, this time you need to know the hash value of each data block. How to do it.

In this case, you can download the data before downloading a list of hashes (hash lists), the list of each item corresponding to a data block hash value. This hash list can be used to calculate a hash of a root. In practical applications, we can ensure that the correct file is downloaded by ensuring that the correct root hash is obtained from a trusted source.

It seems to be perfect. But it's not good enough.

The above problem is based on the hash list scheme:

There are times when we get (traverse) The hash list of all data blocks at a higher cost, only to get the hash of some nodes.

Is there a way to verify the integrity of the entire file through a partial hash?

The answer is yes, Merkle tree can do that. It looks like this:

Features are as follows:

1, data structure is a tree, can be two fork tree, can also be multi-fork tree (this blog with two fork tree to analyze)

2. The value of the leaf node of the Merkle tree is the unit data of the data set or the hash of the unit data.

3, Merke tree non-leaf node value is the hash value of all its child nodes value.

Obviously, this structure is compared to the hash list, and Genhachy is not calculated by concatenation of all data block hashes, but by a hierarchical relationship.

in the image above, the leaf node Node7 value (V7) = hash (F1), is the hash of the F1 file, and its father node Node3 value = hash (V7, V8), that is, its child node Node7 node8 worth of hash Other Application Scenarios File Download

Let's say I have two machines, A and B, and there's a file that's transferred from A to B. b First obtain the trusted file Merkle tree, when the file is downloaded, b through the Merkle tree root node and the obtained root node comparison, if not consistent, through the structure of the binary tree can be quickly located in the log (N) complexity of the data block error. Replica Synchronization

All the machines in a cluster need to keep the data in sync, and if the data is inconsistent, you need to quickly locate the inconsistent nodes.

A Merkle tree can be constructed on each machine for data in each interval, so that when data is compared between the two machines, the comparison is performed from the root node of the Merkle Tree, and if the root node is the same, the two replicas are now consistent and do not require any processing; Traversing Merkle tree, locating inconsistent nodes is also very fast merkle tree application on the blockchain

The following is the focus of this article.

In the Bitcoin system's blockchain, each chunk has a merkle tree.

You can see that the Merkle root hash has the head of each chunk, which is connected to the block body by this root value, and the chunk body contains a large number of transactions. Each transaction is equivalent to the previously mentioned block of data, and the transaction itself has its own hash value to uniquely identify itself. What is a SPV?

For better explanation, here is a concept, SPV. That is, the "simplified payment verification" described by Nakamoto is the SPV that allows the blockchain and bitcoin to be spread more broadly.

The bitcoin wallets that most of us install on computers are lightweight (non-full-node), which means that there is no chunk data (hundreds of G) on the local computer. In theory, to validate a transaction, the wallet needs to traverse all the chunks to find all the transactions related to the transaction to be verified individually.

But the lightweight wallet does not have complete data, how to verify the legitimacy of a payment?

The Merkle tree plays a key role.

Of course, the SPV has its limitations (this is free to write a detailed article). This is the split point.

How does the bitcoin system validate the legitimacy of a deal?

First, the root hash of the Merkle tree in each chunk in the blockchain is publicly trusted. Assuming that Alice is transferring money to Bob a bitcoin, the Bitcoin wallet needs to confirm that the transaction is included in the blockchain.

As shown in the figure above,
Suppose we want to determine if the transaction exists for HK representative, just generate a Merkle path with only 4 hash lengths to prove that the transaction is stored in the chunk. The path has 4 hashes (highlighted in blue in the figure) HL, HIJ, Hmnop, and HABCDEFGH.

By the 4 of these
The authentication path generated by the hash value, and then by calculating the additional four pairs of hash values HKL, HIJKL, Hijklmnop
and Merkle roots (marked by dashed lines in the graph), any node can prove that HK (in the figure by the green
Annotations) are included in the Merkle root.

the specific authentication path is generated, mainly through traversal. There are specialized algorithms that are interested in self-search for related papers

Reference

[1] http://www.cnblogs.com/fengzhiwu/p/5524324.html

[2] << proficient in bitcoin >>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.