[Software Engineering and Practice] (3) Merkle Tree in Data Accounts - Part 1

2021SC@SDUSC

1. Merkle Tree

1. Function

1. Merkle TreeThe function is to shorten the verification data packet time , so that the data packet verification can achieve higher efficiency.

hash list2. Sometimes we get (traverse) all the data blocks at a relatively high cost here , and we can only get the hashes of some nodes. Instead, the entire data packet Merkle Treecan be checked by partial hash .

3. Basically, Merkle Treeit is used for integrity verification. The so-called integrity verification is to check whether the data has been damaged or maliciously tampered with. The biggest application of Merkle Tree is on the peer-to-peer network

2. Features

1. The data structure is a tree, which can be a binary tree or a multi-fork tree

2. The value of the leaf node of the Merkle Tree is the unit data or unit data HASH of the data set.

3. Merke Tree non-leaf node value is the HASH value of all its child nodes value.

2. Code Analysis

1.MerkleTree

The Merkle tree is a tree composed of "hash values" as nodes. The hash values ​​of the child nodes are combined in order, and the hash calculation is performed on the obtained data to obtain the hash value of the parent node. Therefore, the "root hash" of the Merkle tree can represent the ordered state of all the leaf nodes, including the value and arrangement order of the leaf nodes.

The following line of code can get the root node of the Merkle tree. The main purpose of getting the root node is to facilitate the query of the key value

HashDigest getRootHash();

The first line of code below can return the total number of keys; the second line of code can return the latest data of the specified key; the third line of code can return the latest specified version data

long getTotalKeys();
KVEntry getData(byte[] key);
KVEntry getData(byte[] key, long version);

The following code can be used to set the key value

void setData(byte[] key, long expectedVersion, byte[] newValue);

The following code returns the Merkle proof of the latest version of the specified key ; the root hash of the Merkle proof is the root hash of the current Merkle tree; the data hash of the Merkle proof is the hash of the value of the latest version of the specified key Merkel proves that there are at least 4 hash paths, including: root node hash + (0-N) path node hash + leaf node hash + data item hash (Key, Version, Value) + data value hash;

MerkleProof getProof(byte[] key);

The following code works similarly to the above code, but the following code is used to return the Merkle proof of the version specified by the key

MerkleProof getProof(byte[] key, long version);

An iterator is used to traverse all keys and return the latest version

SkippingIterator<KVEntry> iterator();

Returns the latest version for the specified key using an iterator

SkippingIterator<KVEntry> iterator(byte[] key);

Returns all data before the specified version of the specified key (including the specified version

SkippingIterator<KVEntry> iterator(byte[] key, long version);

Returns the version of the specified key; if it does not exist, returns -1;

long getVersion(byte[] key);

2.MerkleProofException

Merkel proves an exception; it means that the data cannot pass the verification when processing the data, and the potential reason may be that the data has been modified or tampered with.

public class MerkleProofException extends RuntimeException {
    
    

	private static final long serialVersionUID = 4110511167046780109L;

	public MerkleProofException(String message) {
    
    
		super(message);
	}

	public MerkleProofException(String message, Throwable cause) {
    
    
		super(message, cause);
	}

}

3.MerkleDataProof

Use Merkle tree for data verification.

public interface MerkleDataProof {
    
    
	
	DataEntry<Bytes, byte[]> getData();
	
	MerkleProof getProof();
}

Guess you like

Origin blog.csdn.net/weixin_45932150/article/details/120812668