[Blockchain Exploration Notes 5]: Hash And Data Structure

admin 50 0

If you want to understand how blockchain works, you must understand three important data structures: hash pointers, Merkle trees, and blockchain.

Hash pointer

The value of the hash pointer is calculated based on the data and points to the location of the data, so the hash pointer can tell us where the data is stored and the hash value of the data. Through the hash pointer, we can easily determine whether the data has been tampered with.

Hash pointers are extremely important in blockchain. The structure of the blockchain starts with the genesis block, and each subsequent block is connected by a hash pointer. Each block contains the hash pointer of the previous block, so that subsequent blocks can not only find all previous blocks, but also verify whether the data of the previous block has been changed, thus ensuring that the blockchain is not easily Tampering characteristics.

The second use of hash pointers in blockchain is to build trees (trees), discussed in detail below.

Merkle tree

In the block header, the hash value of the tree root (Root) is stored. First let us understand what a Merkle tree is.

The picture above is what the tree looks like.

Merkle trees are used in blockchain to organize and record transactions stored in blocks in order to effectively verify whether transactions exist in the block. The tree is built by recursively computing the hashes of nodes until there is only one hash. When there are N pieces of data in the tree, up to 2*log2(N) calculations are needed to verify whether specific data exists, so the tree is quite efficient.

Trees are built from the bottom up. For example, suppose we now have four transactions A, B, C, and D that need to be stored and recorded in the block. Let's see how to build a Merkle tree. First, use A, B, C, and D to construct the leaf nodes of the tree, and store their secondary hash values ​​in the leaf nodes, which are HA, HB, HC, and HD in the figure above.

Then the hash values ​​of two adjacent nodes are connected together, a secondary hash calculation is performed, and their parent node is constructed.

H(AB) = ((H(A) + H(B)))

This process is repeated until only one top-level node remains, the root node of the tree stored in the block header.

Since the tree is a binary tree, it requires an even number of leaf nodes. If there happens to be an odd number of transactions, the last transaction is copied and an even number of leaf nodes are created to achieve balance.

Blockchain

A blockchain is an ordered, back-linked list of transaction blocks built based on hash pointers, meaning that each block in the blockchain is connected to the previous block through a hash pointer. The overall structure is as follows:

As the basic structural unit of the blockchain, a block consists of a block header containing metadata and a block body containing transaction data. The header size is 80 bytes and the transaction record must be at least 50 bytes. On average, each block contains more than 500 transaction records. Therefore, the size of a complete block is generally 1000 times the size of its header. The figure below is the structure of the block header.

1. Version number

This depends on the Bitcoin client and won't change for some time. Even if there are changes, there will be Bitcoin core developers to coordinate the upgrade strategy, which can be understood as a static constant.

2. Hash value of the previous block

One hash is enough. The previous block has already been packed.

3. The roots of the Merkel tree

Get it from the list of transactions included in this transaction.

4. Timestamp

Take your time when packing. It doesn't need to be precise, just a few seconds or tens of seconds before and after.

5.Difficulty target

Depends on the average generation time of blocks generated in the past two weeks. If a block is generated every 10 minutes on average for two weeks, then 2016 blocks will be generated in two weeks. The software calculates the generation time of the latest 2016 blocks, then compares them and adjusts the difficulty accordingly in order to generate the next block. The expected block time is still about 10 minutes. Because the most recent 2016 blocks have been determined, this number is also determined.

6. Random numbers

This is the goal of mining. This is a 32-bit number. The random number can vary and must be tried from 0 to a maximum value of 2^32. Until the last hash result appears, whose number is below the difficulty target value.

标签: #Blockchain #Hash table #Hash lookup

  • 评论列表

留言评论