Blockchain technology and application 2 - BTC-data structure

Bitcoin - Data Structures

insert image description here

1. Block chain

Hash pointer:

(1) The location to save the value
(2) The hash value to save the value

Blockchain: a linked list using hash pointers
insert image description here

genesis block: genesis block (the first block created)
most recent block: the most recently created block
tamper-evident log: tamper proof record

Each current block saves the hash value and location of all the contents of the previous block, forming a hash pointer, which is stored in the current block, and all blocks use the hash pointer to form a blockchain. The hash pointer of the last block is kept inside the system .

When the content of a block in the blockchain (the red block in the figure) is tampered with, the hash pointers in all blocks behind this block (the block to the right of the red block in the figure) must be modified . Therefore, it is only necessary to detect whether the blocks on the entire blockchain have been tampered with by comparing whether the hash value of the last block has been modified.

With this property, some nodes on Bitcoin do not need to save all the blocks, and some may only save the few thousand blocks closest to themselves. If you want to use earlier blocks, you can ask other people, but how to ensure that the blocks you ask are reliable? It is only necessary to compare whether the hash value stored in the first block of the saved block is consistent with the hash value of the block given by others .

2. Merkle tree

Merkle Tree: A binary tree using hash pointers. The tree contains data blocks (transaction records) and hash pointers.
insert image description here

Block structure:

(1) block header: save the root hash
(2) block body: save the transaction record data information

The purpose of the Merkle tree: to prove to the light node that a certain record has been written into the blockchain. proof of membership or proof of inclusion

There are two types of nodes (blocks) in Bitcoin:
light nodes: only block headers are saved;
full nodes: both block headers and block bodies

Merkle proof:
insert image description here
The nodes circled in the figure are light nodes.
a. First, you need to request a Merkle proof path from a full node, and request the required hash value on the path (red H in the figure)
b. Then calculate the hash value of the record that needs to be proved, and then pass the given path one step Calculate the hash value in one step until the root hash value (root hash) is calculated.
c. Because the block header is stored in the light node, and the root hash is stored in the block header, it is only necessary to compare the calculated root hash. Just match the saved root hash.

The second use of Merkle tree: proof of non-membership, which proves that a certain transaction is not in a certain light node.

Use sorted Merkle tree: sort the hash values ​​of transaction records tx, if the calculated hash value of tx to be proved is not among these values, but between two tx hash values, then calculate according to the path of Merkle proof The hash values ​​of these two tx are calculated to the root hash to see if it has been tampered with. If it has not been tampered with, it proves that the tx to be proved is not in the light node.

The sorted Merkle tree is not used in Bitcoin because proof of non-membership is not required in Bitcoin.

3. The problem of hash pointer

As long as there is no ring in the data structure, then the hash pointer can be used. If there is a ring, there will be a problem of circular dependency.
insert image description here

Guess you like

Origin blog.csdn.net/Dajian1040556534/article/details/128980564