// depth: If it is a positive number, it is the depth of the specified merkle tree. At this time,
// if the number of leaves of the complete binary tree specified by the depth is greater than
// len(hashList), at most one NilHashSlice is supplemented per layer
If it is a positive number, it is the depth of the specified merkle tree. At this time,
if the number of leaves of the complete binary tree specified by the depth is greater than
len(hashList), at most one NilHashSlice is supplemented per layer
// depth : If it is a positive number it is the depth of the specified merkle tree . At this time // if the number of leaves of the complete binary tree specified by the depth is greater than // len ( hashList ) at most one NilHashSlice is supplemented per layer
func MerkleHash(hashList [][]byte, toBeProof int, proofs *MerkleProofs) (root []byte, err error) {
	if len(hashList) == 0 {
		return CopyBytes(NilHashSlice), nil
	}
	depth := 1
	max := 2
	for max < len(hashList) {
		max <<= 1
		depth++
	}
	list := make([][]byte, len(hashList))
	copy(list, hashList)
	var hh []byte
	for d := 0; d < depth; d++ {
		length := len(list)
		next := list[0 : (length+1)/2]
		for i := 0; i < length; i += 2 {
			j := i + 1
			right := NilHashSlice
			if j < length {
				right = list[j]
			}
			hh = HashPair(list[i], right)
			if proofs != nil && toBeProof >= 0 {
				if toBeProof == i {
					proofs.Append(BytesToHash(right), false)
				} else if toBeProof == j {
					proofs.Append(BytesToHash(list[i]), true)
				}
			}
			next[i>>1] = hh
			if j >= length {
				break
			}
		}
		list = next
		if toBeProof >= 0 {
			toBeProof >>= 1
		}
	}
	return list[0], nil
}
func MerkleHash ( hashList [ ] [ ] byte , toBeProof int , proofs * MerkleProofs ) ( root [ ] byte , err error ) { if len ( hashList ) == 0 { return CopyBytes ( NilHashSlice ) , nil } depth := 1 max := 2 for max < len ( hashList ) { max <<= 1 depth ++ } list := make ( [ ] [ ] byte , len ( hashList ) ) copy ( list , hashList ) var hh [ ] byte for d := 0 ; d < depth ; d ++ { length := len ( list ) next := list [ 0 : ( length + 1 ) / 2 ] for i := 0 ; i < length ; i += 2 { j := i + 1 right := NilHashSlice if j < length { right = list [ j ] } hh = HashPair ( list [ i ] , right ) if proofs != nil && toBeProof >= 0 { if toBeProof == i { proofs . Append ( BytesToHash ( right ) , false ) } else if toBeProof == j { proofs . Append ( BytesToHash ( list [ i ] ) , true ) } } next [ i >> 1 ] = hh if j >= length { break } } list = next if toBeProof >= 0 { toBeProof >>= 1 } } return list [ 0 ] , nil }
If it is a positive number, it is the depth of the specified merkle tree.
If it is a positive number it is the depth of the specified merkle tree .
// Find the smallest power of 2 greater than the length of hashList, and get the height of // the complete binary tree (the number from top to bottom) // Calculate Merkle hash according to the height of binary tree, and fill in NilHashSlice when it is needed