func HashFromTwoHashes(left []byte, right []byte) []byte { var n int64 var err error var hasher = sha256.New() binary.WriteByteSlice(left, hasher, &n, &err) binary.WriteByteSlice(right, hasher, &n, &err) if err != nil { panic(err) } return hasher.Sum(nil) }
func (s *State) Save() { s.accounts.Save() s.validatorInfos.Save() buf, n, err := new(bytes.Buffer), new(int64), new(error) binary.WriteUvarint(s.LastBlockHeight, buf, n, err) binary.WriteByteSlice(s.LastBlockHash, buf, n, err) binary.WriteBinary(s.LastBlockParts, buf, n, err) binary.WriteTime(s.LastBlockTime, buf, n, err) binary.WriteBinary(s.BondedValidators, buf, n, err) binary.WriteBinary(s.LastBondedValidators, buf, n, err) binary.WriteBinary(s.UnbondingValidators, buf, n, err) binary.WriteByteSlice(s.accounts.Hash(), buf, n, err) binary.WriteByteSlice(s.validatorInfos.Hash(), buf, n, err) if *err != nil { panic(*err) } s.DB.Set(stateKey, buf.Bytes()) }
// NOTE: sets hashes recursively func (node *IAVLNode) writeToCountHashes(t *IAVLTree, w io.Writer) (n int64, hashCount uint64, err error) { // height & size & key binary.WriteUint8(node.height, w, &n, &err) binary.WriteUint64(node.size, w, &n, &err) t.keyCodec.Encode(node.key, w, &n, &err) if err != nil { return } if node.height == 0 { // value t.valueCodec.Encode(node.value, 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 save") } 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 save") } binary.WriteByteSlice(node.rightHash, w, &n, &err) } return }