Blockchain learning three - the data structure of Bitcoin

Blockchain learning three - the data structure of Bitcoin

文章内容来源于北京大学肖臻老师《区块链技术与应用》公开课


1. Hash pointers

Ordinary pointers store the starting address of the structure in memory.
The hash pointer stores the hash value of the structure in addition to the starting address.
According to the hash value, it can be detected whether the structure has been tampered with.

2. Blockchain

A linked list composed of blocks
Q: The difference between the linked list used by the blockchain and the ordinary linked list
A: The linked list used by the blockchain uses hash pointers instead of ordinary pointers
blockchain table

The first block generated in the system: genesis block The
most recent block generated in the system: most recent
Each block has a hash pointer
to get the hash value by combining the contents of the entire block together Hash (including hash pointer)
only needs to remember the last hash value to detect whether the previous block has been tampered with (dominos, one move will affect the whole body),
so the node only needs to save the nearest node, and needs the previous block The block can be asked for by others, and the previous block can be checked whether it is correct by calculating the hash value.

3. Merkle tree

The root node can also take a hash value called root hash.
Knowing the root hash can detect whether the entire tree node has been tampered with (higher efficiency). The change of each node will cause the root node to change.
The bottom child node is equivalent to
Merkle tree
这个数跟二叉树很像
最上面深颜色的方块代表区块,tx即最下面的一行代表交易,H()哈希值
every transaction. Each block contains block header (has root hash value, block header has no specific transaction data) and block body (block body contains transaction data) The root hash
value of the merkle tree composed of all transactions contained in the block block header

1. The role of Merkle tree: Merkle Proof

Full node: save the content of the entire blockchain, including block header and block body (the block body contains transaction data)
light node: only save the block header
Q (Merkle Proof): how to prove to the light node that the transaction has been written In the blockchain (the light node does not save the transaction list, only the root hash value block header) the yellow square in the above picture is included in the Merkle tree.
A: The light node sends a request to the full node. The full node sends the red H() hash value in the figure to the light node. The light node calculates the green hash value H() locally, and the green hash value of the upper node can be calculated from the green hash value and the red hash value; then the green hash value is calculated from the previous calculation value and the red hash value of this layer; calculate the green hash value of the next layer, and then calculate the hash value of the root node from the green hash value and the red hash value, and the hash value of the light node Comparing the hash value can determine whether the transaction information is included.
只验证交易数据所在的到根节点的一条分支即可,根哈希值不变,即交易都不会被篡改。人为制造哈希碰撞可以篡改 难度太大了collision resistence

Merkle Proof (Proof of membership, Proof of inclusion): Prove to the light node that the transaction has been written into the blockchain. If there are n transactions, the time complexity is log(n)

2.Proof of non-membership

Q: How to prove Proof of non-membership
A: Send a tree directly to the light node, and send the entire transaction to the light node. The time complexity is O(n)
optimization: the transaction hash values ​​are sorted in order, and other steps are similar to the merkle proof process. The time complexity is log(n). The price is to sort first. sorted merkle tree.
Bitcoin does not require sorting and does not require Proof of non-membership

Four. Summary

Both the blockchain and the Merkle tree need to be constructed using hash pointers. A linked list without a ring can use a hash pointer, but a linked list with a ring cannot use a hash pointer.

Guess you like

Origin blog.csdn.net/qq_43589852/article/details/131090892