Example #1
0
// 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
}
Example #2
0
func SimpleHashFromTwoHashes(left []byte, right []byte) []byte {
	var n int64
	var err error
	var hasher = ripemd160.New()
	binary.WriteByteSlice(left, hasher, &n, &err)
	binary.WriteByteSlice(right, hasher, &n, &err)
	if err != nil {
		panic(err)
	}
	return hasher.Sum(nil)
}
Example #3
0
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)
}
Example #4
0
// Encode object using codec, then write a (length prefixed) byteslice.
func encodeByteSlice(o interface{}, codec binary.Codec, w io.Writer, n *int64, err *error) {
	buf, n_ := new(bytes.Buffer), new(int64)
	codec.Encode(o, buf, n_, err)
	if *err != nil {
		return
	}
	binary.WriteByteSlice(buf.Bytes(), w, n, err)
}
Example #5
0
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)
}
Example #6
0
// 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
}