Hash Pointer of Blockchain Technology

Hello, everyone, our third phase of blockchain technology sharing is coming, so without further ado, let's start.

When it comes to blockchain, everyone may mention: it cannot be tampered with . But why is the blockchain immutable?

picture

 

Give the answer first, which is related to the blockchain data structure hash pointer and Merkle tree . So let's share the content related to the hash pointer today.

1. The linked list learned in those years

Blockchain, as the name suggests, is also a chain. Friends who have studied computer data structures know that one of the data structures is a linked list. So what is a blockchain linked list?

Structures and linked lists belong to the basic courses of computer science. But after working for a long time, everyone’s reaction may be:

picture

To understand linked lists, let's use a simple example to illustrate, assuming you have a house in Beijing, Xi'an, Shanghai, and Hangzhou.

picture

 

But you only have the address of the house in Beijing, and the addresses of the rest of the houses are stored in other rooms.

picture

You find a house in Beijing based on the address of the house in Beijing, and you know the situation of the house in Beijing, and there is a note in it with the address of the house in Shanghai; similarly, you find the house in Shanghai based on this address, and you also see the situation of the house in Shanghai , and there is also a note with the address of the room in Xi'an; and so on, until you find a house in Hangzhou, which is your last house, then the note with the address will be empty.

The above example is an example of a singly linked list in a linked list.

Linked list jumps in discontinuous memory space through pointers, so as to realize the feature of dynamic expansion.

In the above example, the note storing the address of the next house is a pointer, representing the address of the next house;

Discontinuous space is well understood by everyone, just like your house in Beijing, Shanghai, Xi'an, Hangzhou, the geographical location is discontinuous.

Linked lists cannot be accessed randomly, only sequentially.

It can also be seen from the above example that you can only start accessing from the room in Beijing, because you only have the address in Beijing.

Ordinary pointers point to the address of the structure in memory.

So what does the hash pointer used in the blockchain mean? Since it is a pointer, it must be an address. In addition to saving the location of the structure, the hash pointer also needs to save the hash value of the structure.

If you don't know about hashing, you can read this article Are your passwords safe? | Hash of blockchain technology

2. Hash pointer and hash list

So why use hash pointers?

The most important function is not only to find the structure, but also to prevent the data pointed to in the address from being tampered with .

A linked list connected by hash pointers is called a hash list.

Still use your four sets as an example. Beijing, Shanghai, Xi'an, Hangzhou.

picture

picture

For example, a normal hash linked list is like the picture above, consisting of four nodes.

The address of node 4 can be random, because there are no nodes behind it,

Node 4, which is the node of the house in Hangzhou, is combined with the data to calculate the hash value, and assign this hash value to node 3, the house in Xi’an, so that the address in the node of the house in Xi’an points to the house in node 4, Hangzhou;

Similarly, node 3 is the node of the house in Xi’an and the data is combined, the hash value is calculated, and the hash value is assigned to node 2, the house in Shanghai, so that the address in the node of the house in Shanghai points to the house in node 3 in Xi’an ;

The same is true for node 2;

Then, after node 1 is combined with the data, the hash value is calculated, and the hash value is assigned to the address of the Beijing house, and the address of the Beijing house points to node 1.

In this way, the address of the Beijing house is published as the entrance. If you want to know your billions of dollars in the future, you only need to get the address of the Beijing house.

picture

 

Because you are too rich, some bad people want to take advantage of your bad memory and tamper with the information of a certain house of yours.

picture

1. Changed the data of the Xi’an house in node 3: Xi’an was changed to Arabic, we marked the tampered data in node 3,


2. Because the address of the house in Xi’an in node 2 was calculated from the data that has not been tampered with in node 3, after the data is changed, in order to make the address of the house in Xi’an in node 2 point to node 3 again, you need to Calculate the address of the house in Xi’an in node 2 , so that the address of the house in Xi’an changes, and we mark the address of the house in Xi’an;

3. By analogy, the address of the Shanghai house in node 1 has also changed, and we will mark it out;


4.  After the address of the Shanghai house changed, because the address of the old Beijing house was calculated  from the address of the old Shanghai house and the data of node 1, the address of the Shanghai house changed. In order to make the address of the Beijing house point to Node 1 needs to calculate the address of the Beijing house again, so the address of the Beijing house has also changed, and we mark the address of the Beijing house.


So I saw that because the data in node 3 was changed, the address of the house in Xi’an, the address of the house in Shanghai , and the address of the house in Beijing all changed.

In this way, the address of the new Beijing house is published as the entrance, and you find that the new address is different from your previous address, which is a big deal, as your information has been tampered with.

This is the hash list.

3. Blockchain data structure

A blockchain is a chain composed of different blocks linked by hash pointers.

picture

Next to the second part, the picture above is a small blockchain. The first block is generated by the system, called the Genesis block. The address H(0) of the previous block in the first block chain, the address H(1) of the previous block in the second block, points to block 1, and the third block is the latest generation Block, which contains the address H(2) pointing to block 2, similarly, the address of block 3 will also be stored in the system.


According to the characteristics of the hash pointer in the second part, when the information in a certain block is tampered with, all subsequent blocks will be modified.

But in fact, the information of each block is far more complicated than the above.

picture

 

Each block consists of a block header (head) and a block body (body) .

The block header stores structured data, the size is 80 bytes;

The main body of the block uses a magical tree structure to record all transaction information during the period when the block is dug out, which requires a relatively large space.

On average, assuming that there are 400 transaction information in a block, the block body may be more than 1000 times larger than the block header.

picture

Expand each block, then the information of the block is as shown in the figure above:

Version number : used to identify the referenced rules. Software systems are constantly iterating and evolving, just like the version numbers of our Android and iOS systems.

Previous block hash value: also known as " parent block hash value ", this hash value is obtained by hashing the block header data of the previous block (SHA256 algorithm), its meaning is: every Each newly mined block is connected to the previous block in order.

Merkle root: In the block body, all transaction information is first hashed in groups of two. This structure is called a Merkle tree (Merkle Tree), and it is an upside-down tree. The leaf nodes store transactions, and the hash values ​​are stored upwards.

Timestamp: Record the time when this block was generated, accurate to the second. Every time a new block is born, it will be stamped with a corresponding time stamp, so as to ensure that the blocks on the entire chain are arranged in chronological order.

Difficulty value: The difficulty target for digging out the block. When Satoshi Nakamoto designed Bitcoin, he added a mining difficulty adjustment mechanism to make the block generation time of Bitcoin ideally constant at about 10 minutes .

The Bitcoin protocol stipulates that every  2016  blocks, it will automatically adjust the mining difficulty of the next 2016 blocks according to the total time of the last 2016 blocks in the past. Ideally, it takes two weeks (2016*10s) for 2016 blocks. If the actual time is less than two weeks, the difficulty will be increased, and if it takes more than two weeks, the difficulty will be decreased.

Random number : Mining is to find a random number (Nonce) to participate in the hash operation Hash (Block Header), so that the final hash value meets the difficulty requirements.

Bitcoin competes for the accounting rights of new blocks through mining. Whoever gets the bookkeeping right of the new block, the new block it creates is legal. The purpose of mining is to win bookkeeping rights and confirm new blocks and transactions.

picture

 

 

Today's keywords are mainly linked lists, pointers, hash pointers, hash linked lists, and blockchains . In the next issue, we will share the Merkle tree, which is the merkle tree.

picture

 If you are also interested in the blockchain, search the WeChat public account " Candy Chain Notes ", and let's move forward together.

Guess you like

Origin blog.csdn.net/koudan567/article/details/120358074