// NOTE: sets hashes recursively func (node *IAVLNode) writeHashBytes(t *IAVLTree, w io.Writer) (n int64, hashCount int, err error) { // height & size binary.WriteInt8(node.height, w, &n, &err) binary.WriteVarint(node.size, w, &n, &err) // key is not written for inner nodes, unlike writePersistBytes if node.height == 0 { // key & value encodeByteSlice(node.key, t.keyCodec, w, &n, &err) encodeByteSlice(node.value, t.valueCodec, w, &n, &err) } else { // left if node.leftNode != nil { leftHash, leftCount := node.leftNode.hashWithCount(t) node.leftHash = leftHash hashCount += leftCount } if node.leftHash == nil { panic("node.leftHash was nil in writeHashBytes") } binary.WriteByteSlice(node.leftHash, w, &n, &err) // right if node.rightNode != nil { rightHash, rightCount := node.rightNode.hashWithCount(t) node.rightHash = rightHash hashCount += rightCount } if node.rightHash == nil { panic("node.rightHash was nil in writeHashBytes") } binary.WriteByteSlice(node.rightHash, w, &n, &err) } return }
func (leaf IAVLProofLeafNode) Hash() []byte { hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int64(0), error(nil) binary.WriteInt8(0, buf, &n, &err) binary.WriteVarint(1, buf, &n, &err) binary.WriteByteSlice(leaf.KeyBytes, buf, &n, &err) binary.WriteByteSlice(leaf.ValueBytes, buf, &n, &err) if err != nil { panic(Fmt("Failed to hash IAVLProofLeafNode: %v", err)) } // fmt.Printf("LeafNode hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) return hasher.Sum(nil) }
func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte { hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int64(0), error(nil) binary.WriteInt8(branch.Height, buf, &n, &err) binary.WriteVarint(branch.Size, buf, &n, &err) if len(branch.Left) == 0 { binary.WriteByteSlice(childHash, buf, &n, &err) binary.WriteByteSlice(branch.Right, buf, &n, &err) } else { binary.WriteByteSlice(branch.Left, buf, &n, &err) binary.WriteByteSlice(childHash, buf, &n, &err) } if err != nil { panic(Fmt("Failed to hash IAVLProofInnerNode: %v", err)) } // fmt.Printf("InnerNode hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) return hasher.Sum(nil) }
// NOTE: sets hashes recursively func (node *IAVLNode) writePersistBytes(t *IAVLTree, w io.Writer) (n int64, err error) { // node header binary.WriteInt8(node.height, w, &n, &err) binary.WriteVarint(node.size, w, &n, &err) // key (unlike writeHashBytes, key is written for inner nodes) encodeByteSlice(node.key, t.keyCodec, w, &n, &err) if node.height == 0 { // value encodeByteSlice(node.value, t.valueCodec, w, &n, &err) } else { // left if node.leftHash == nil { panic("node.leftHash was nil in writePersistBytes") } binary.WriteByteSlice(node.leftHash, w, &n, &err) // right if node.rightHash == nil { panic("node.rightHash was nil in writePersistBytes") } binary.WriteByteSlice(node.rightHash, w, &n, &err) } return }