// generate merkle proof by specified index
// rootHash: root hash of current node
// toBeProof: hash of prooved position (specified by index)
// proof: merkle proof
generate merkle proof by specified index
rootHash: root hash of current node
toBeProof: hash of prooved position (specified by index)
proof: merkle proof
// generate merkle proof by specified index // rootHash : root hash of current node // toBeProof : hash of prooved position ( specified by index ) // proof : merkle proof
func (n *TreeNode) MakeProof(index int) (rootHash []byte, toBeProof []byte, proof *common.MerkleProofs, err error) {
	if n == nil {
		return nil, nil, nil, common.ErrNil
	}
	if index < 0 || index >= childrenLength {
		return nil, nil, nil, common.ErrIllegalParams
	}
	if (n.isLeaf && n.Leafs[index] == nil) || (n.isLeaf == false && n.Children[index] == nil) {
		return nil, nil, nil, common.ErrNil
	}
	toBeProof = n.Leafs[index]
	proof = common.NewMerkleProofs()
	var left, right []byte
	if index%2 == 0 {
		left, err = n.LeafHash(index)
		if err != nil {
			return nil, nil, nil, err
		}
		right, err = n.LeafHash(index + 1)
		if err != nil {
			return nil, nil, nil, err
		}
		if right == nil {
			right = common.NilHashSlice
		}
		proof.Append(common.BytesToHash(right), false)
	} else {
		left, err = n.LeafHash(index - 1)
		if err != nil {
			return nil, nil, nil, err
		}
		if left == nil {
			return nil, nil, nil, ErrMissingValue
		}
		right, err = n.LeafHash(index)
		if err != nil {
			return nil, nil, nil, err
		}
		proof.Append(common.BytesToHash(left), true)
	}
	rootHash = common.HashPair(left, right)
	bits, err := ToBinary(byte(index), ValueKeyLength)
	for i := 2; i >= 0; i-- {
		if bits[i] == 0x0 {
			left = rootHash
			rightPrefix := make([]byte, i+1)
			copy(rightPrefix, bits[:i])
			rightPrefix[i] = 0x1
			right, _, _, err = n.HashAtPrefix(rightPrefix)
			if err != nil {
				return nil, nil, nil, err
			}
			if right == nil {
				right = common.NilHashSlice
			}
			proof.Append(common.BytesToHash(right), false)
		} else {
			leftPrefix := make([]byte, i+1)
			copy(leftPrefix, bits[:i])
			leftPrefix[i] = 0x0
			var lln, lrn bool
			left, lln, lrn, err = n.HashAtPrefix(leftPrefix)
			if err != nil {
				return nil, nil, nil, err
			}
			if left == nil || lln || lrn {
				return nil, nil, nil, ErrMissingValue
			}
			proof.Append(common.BytesToHash(left), true)
		}
		rootHash = common.HashPair(left, right)
	}
	return
}
func ( n * TreeNode ) MakeProof ( index int ) ( rootHash [ ] byte , toBeProof [ ] byte , proof * common . MerkleProofs , err error ) { if n == nil { return nil , nil , nil , common . ErrNil } if index < 0 || index >= childrenLength { return nil , nil , nil , common . ErrIllegalParams } if ( n . isLeaf && n . Leafs [ index ] == nil ) || ( n . isLeaf == false && n . Children [ index ] == nil ) { return nil , nil , nil , common . ErrNil } toBeProof = n . Leafs [ index ] proof = common . NewMerkleProofs ( ) var left , right [ ] byte if index % 2 == 0 { left , err = n . LeafHash ( index ) if err != nil { return nil , nil , nil , err } right , err = n . LeafHash ( index + 1 ) if err != nil { return nil , nil , nil , err } if right == nil { right = common . NilHashSlice } proof . Append ( common . BytesToHash ( right ) , false ) } else { left , err = n . LeafHash ( index - 1 ) if err != nil { return nil , nil , nil , err } if left == nil { return nil , nil , nil , ErrMissingValue } right , err = n . LeafHash ( index ) if err != nil { return nil , nil , nil , err } proof . Append ( common . BytesToHash ( left ) , true ) } rootHash = common . HashPair ( left , right ) bits , err := ToBinary ( byte ( index ) , ValueKeyLength ) for i := 2 ; i >= 0 ; i -- { if bits [ i ] == 0x0 { left = rootHash rightPrefix := make ( [ ] byte , i + 1 ) copy ( rightPrefix , bits [ : i ] ) rightPrefix [ i ] = 0x1 right , _ , _ , err = n . HashAtPrefix ( rightPrefix ) if err != nil { return nil , nil , nil , err } if right == nil { right = common . NilHashSlice } proof . Append ( common . BytesToHash ( right ) , false ) } else { leftPrefix := make ( [ ] byte , i + 1 ) copy ( leftPrefix , bits [ : i ] ) leftPrefix [ i ] = 0x0 var lln , lrn bool left , lln , lrn , err = n . HashAtPrefix ( leftPrefix ) if err != nil { return nil , nil , nil , err } if left == nil || lln || lrn { return nil , nil , nil , ErrMissingValue } proof . Append ( common . BytesToHash ( left ) , true ) } rootHash = common . HashPair ( left , right ) } return }
generate merkle proof by specified index
rootHash: root hash of current node
toBeProof: hash of prooved position (specified by index)
proof: merkle proof
generate merkle proof by specified index rootHash : root hash of current node toBeProof : hash of prooved position ( specified by index ) proof : merkle proof
// The proved position must have a value, because the whole tree is filled from left to // right, and the hash is also calculated from left to right. Nilhash can be filled only // in the last hash of each layer. Therefore, the leaves of nil value are not directly // involved in the calculation of roothash // layer1 // Because it is a complete binary tree from left to right, the left side of non-nil value cannot be nil // Layer 2-4, calculate the hash layer by layer from bottom to top, and record the proof path // convert index to 4-byte binary