// NOTE: sets hashes recursively func (node *IAVLNode) writeHashBytes(t *IAVLTree, w io.Writer) (n int64, hashCount int, err error) { // height & size wire.WriteInt8(node.height, w, &n, &err) wire.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 { PanicSanity("node.leftHash was nil in writeHashBytes") } wire.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 { PanicSanity("node.rightHash was nil in writeHashBytes") } wire.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) wire.WriteInt8(0, buf, &n, &err) wire.WriteVarint(1, buf, &n, &err) wire.WriteByteSlice(leaf.KeyBytes, buf, &n, &err) wire.WriteByteSlice(leaf.ValueBytes, buf, &n, &err) if err != nil { PanicCrisis(Fmt("Failed to hash IAVLProofLeafNode: %v", err)) } // fmt.Printf("LeafNode hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) return hasher.Sum(nil) }
// Encode object using codec, then write a (length prefixed) byteslice. func encodeByteSlice(o interface{}, codec wire.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 } wire.WriteByteSlice(buf.Bytes(), w, n, err) }
func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte { hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int64(0), error(nil) wire.WriteInt8(branch.Height, buf, &n, &err) wire.WriteVarint(branch.Size, buf, &n, &err) if len(branch.Left) == 0 { wire.WriteByteSlice(childHash, buf, &n, &err) wire.WriteByteSlice(branch.Right, buf, &n, &err) } else { wire.WriteByteSlice(branch.Left, buf, &n, &err) wire.WriteByteSlice(childHash, buf, &n, &err) } if err != nil { PanicCrisis(Fmt("Failed to hash IAVLProofInnerNode: %v", err)) } // fmt.Printf("InnerNode hash bytes: %X\n", buf.Bytes()) hasher.Write(buf.Bytes()) return hasher.Sum(nil) }
func (s *State) Save() { s.accounts.Save() s.validatorInfos.Save() s.nameReg.Save() buf, n, err := new(bytes.Buffer), new(int64), new(error) wire.WriteString(s.ChainID, buf, n, err) wire.WriteVarint(s.LastBlockHeight, buf, n, err) wire.WriteByteSlice(s.LastBlockHash, buf, n, err) wire.WriteBinary(s.LastBlockParts, buf, n, err) wire.WriteTime(s.LastBlockTime, buf, n, err) wire.WriteBinary(s.BondedValidators, buf, n, err) wire.WriteBinary(s.LastBondedValidators, buf, n, err) wire.WriteBinary(s.UnbondingValidators, buf, n, err) wire.WriteByteSlice(s.accounts.Hash(), buf, n, err) wire.WriteByteSlice(s.validatorInfos.Hash(), buf, n, err) wire.WriteByteSlice(s.nameReg.Hash(), buf, n, err) if *err != nil { PanicCrisis(*err) } s.DB.Set(stateKey, buf.Bytes()) }
// NOTE: sets hashes recursively func (node *IAVLNode) writePersistBytes(t *IAVLTree, w io.Writer) (n int64, err error) { // node header wire.WriteInt8(node.height, w, &n, &err) wire.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 { PanicSanity("node.leftHash was nil in writePersistBytes") } wire.WriteByteSlice(node.leftHash, w, &n, &err) // right if node.rightHash == nil { PanicSanity("node.rightHash was nil in writePersistBytes") } wire.WriteByteSlice(node.rightHash, w, &n, &err) } return }