// Computes the root hash of the subtree indicated by the specified prefix
// If there are no nodes under the subtree, return nil
// If a node is missing in any step, NilHash is used instead
// leftIsNil, rightIsNil returns whether the left and right children are nil values when
// calculating the current node hash, and if err is not nil, the two values are meaningless.
Computes the root hash of the subtree indicated by the specified prefix
If there are no nodes under the subtree, return nil
If a node is missing in any step, NilHash is used instead
leftIsNil, rightIsNil returns whether the left and right children are nil values when
calculating the current node hash, and if err is not nil, the two values are meaningless.
// Computes the root hash of the subtree indicated by the specified prefix // If there are no nodes under the subtree return nil // If a node is missing in any step NilHash is used instead // leftIsNil rightIsNil returns whether the left and right children are nil values when // calculating the current node hash and if err is not nil the two values are meaningless .
func (n *TreeNode) HashAtPrefix(prefix []byte) (hashAtPrefix []byte, leftIsNil bool, rightIsNil bool, err error) {
	if len(prefix) > ValueKeyLength {
		return nil, false, false, common.ErrUnsupported
	}
	if len(prefix) == ValueKeyLength {
		p, err := ToByte(prefix)
		if err != nil {
			return nil, false, false, err
		}
		hashAtPrefix, err = n.LeafHash(int(p))
		return hashAtPrefix, false, false, err
	}
	r, exist := n.Branchs[string(prefix)]
	if exist && r != nil {
		return r, false, false, nil
	}
	child := make([]byte, len(prefix)+1)
	copy(child, prefix)
	left, lln, lrn, err := n.HashAtPrefix(child)
	if err != nil {
		return nil, false, false, err
	}
	if lln && !lrn {
		return nil, false, false, ErrMissingValue
	}
	child[len(child)-1] = 0x1
	right, rln, rrn, err := n.HashAtPrefix(child)
	if err != nil {
		return nil, false, false, err
	}
	if rln && !rrn {
		return nil, false, false, ErrMissingValue
	}
	if lrn && right != nil {
		if !lln {
			child[len(child)-1] = 0x0
			delete(n.Branchs, string(child))
		}
		return nil, false, false, ErrMissingValue
	}
	if left == nil {
		if right == nil {
			return nil, true, true, nil
		} else {
			 has no value, and right child has value, this is an error state
			return nil, false, false, ErrMissingValue
		}
	} else {
		if right == nil {
			rightIsNil = true
			right = common.NilHashSlice
		}
	}
	h := common.HashPair(left, right)
	n.Branchs[string(prefix)] = h
	return h, leftIsNil, rightIsNil, nil
}
func ( n * TreeNode ) HashAtPrefix ( prefix [ ] byte ) ( hashAtPrefix [ ] byte , leftIsNil bool , rightIsNil bool , err error ) { if len ( prefix ) > ValueKeyLength { return nil , false , false , common . ErrUnsupported } if len ( prefix ) == ValueKeyLength { p , err := ToByte ( prefix ) if err != nil { return nil , false , false , err } hashAtPrefix , err = n . LeafHash ( int ( p ) ) return hashAtPrefix , false , false , err } r , exist := n . Branchs [ string ( prefix ) ] if exist && r != nil { return r , false , false , nil } child := make ( [ ] byte , len ( prefix ) + 1 ) copy ( child , prefix ) left , lln , lrn , err := n . HashAtPrefix ( child ) if err != nil { return nil , false , false , err } if lln && ! lrn { return nil , false , false , ErrMissingValue } child [ len ( child ) - 1 ] = 0x1 right , rln , rrn , err := n . HashAtPrefix ( child ) if err != nil { return nil , false , false , err } if rln && ! rrn { return nil , false , false , ErrMissingValue } if lrn && right != nil { if ! lln { child [ len ( child ) - 1 ] = 0x0 delete ( n . Branchs , string ( child ) ) } return nil , false , false , ErrMissingValue } if left == nil { if right == nil { return nil , true , true , nil } else { return nil , false , false , ErrMissingValue } } else { if right == nil { rightIsNil = true right = common . NilHashSlice } } h := common . HashPair ( left , right ) n . Branchs [ string ( prefix ) ] = h return h , leftIsNil , rightIsNil , nil }
Computes the root hash of the subtree indicated by the specified prefix
If there are no nodes under the subtree, return nil
If a node is missing in any step, NilHash is used instead
leftIsNil, rightIsNil returns whether the left and right children are nil values when
calculating the current node hash, and if err is not nil, the two values are meaningless.
Computes the root hash of the subtree indicated by the specified prefix If there are no nodes under the subtree return nil If a node is missing in any step NilHash is used instead leftIsNil rightIsNil returns whether the left and right children are nil values when calculating the current node hash and if err is not nil the two values are meaningless .
// left child // location = 6 // the left child of the left child has no value, and the right child of the left child // has value, this is an error state // right child // the left child of the right child has no value, and the right child of the right child // has value, this is an error state // Since the left child of the left child has a value, hash with one-sided calculation // will succeed and cached. The cached value will hide the fact that it has a child as // nil at the next time it is accessed. So this cache needs to be cleared. // the right child of the left child has no value, and the right child has value, this is // an error state // return nil if both child nodes of current node in the binary tree are nil // it has already checked at "lrn && right!=nil", so it should not be here // left child has no value, and right child has value, this is an error state