Package merkle 实现默克尔树相关的hash计算



    View Source
    const HashSize = 32

      HashSize hash长度

      View Source
      const MaxHashStringSize = HashSize * 2

        MaxHashStringSize 最大hash字符串长度


        View Source
        var ErrHashStrSize = fmt.Errorf("max hash string length is %v bytes", MaxHashStringSize)

          ErrHashStrSize error


          func CalcMerkleRoot

          func CalcMerkleRoot(cfg *types.Chain33Config, height int64, txs []*types.Transaction) []byte

            CalcMerkleRoot 计算merkle树根

            func CalcMerkleRootCache

            func CalcMerkleRootCache(txs []*types.TransactionCache) []byte

              CalcMerkleRootCache 计算merkle树根缓存

              func CalcMultiLayerMerkleInfo

              func CalcMultiLayerMerkleInfo(cfg *types.Chain33Config, height int64, txs []*types.Transaction) ([]byte, []types.ChildChain)

                CalcMultiLayerMerkleInfo 计算多层merkle树根hash以及子链根hash信息

                func Computation

                func Computation(leaves [][]byte, flage int, branchpos uint32) (roothash []byte, mutated bool, pbranch [][]byte)

                  Computation This implements a constant-space merkle root/path calculator, limited to 2^32 leaves. flage =1 只计算roothash flage =2 只计算branch flage =3 计算roothash 和 branch

                  func Decode

                  func Decode(dst *Hash, src string) error

                    Decode decodes the byte-reversed hexadecimal string encoding of a Hash to a destination.

                    func GetHashFromTwoHash

                    func GetHashFromTwoHash(parent []byte, left []byte, right []byte) []byte

                      GetHashFromTwoHash 计算左右节点hash的父hash

                      func GetMerkleBranch

                      func GetMerkleBranch(leaves [][]byte, position uint32) [][]byte

                        GetMerkleBranch 获取指定txindex的branch position 从0开始

                        func GetMerkleRoot

                        func GetMerkleRoot(hashes [][]byte) []byte

                          GetMerkleRoot 256构成一个组,进行计算 n * step = hashes (hashes / n)

                          func GetMerkleRootAndBranch

                          func GetMerkleRootAndBranch(leaves [][]byte, position uint32) (roothash []byte, branchs [][]byte)

                            GetMerkleRootAndBranch 获取merkle roothash 以及指定tx index的branch,注释:position从0开始

                            func GetMerkleRootFromBranch

                            func GetMerkleRootFromBranch(merkleBranch [][]byte, leaf []byte, Index uint32) []byte

                              GetMerkleRootFromBranch 通过branch 获取对应的roothash 用于指定txhash的proof证明


                              type Hash

                              type Hash [HashSize]byte

                                Hash 哈希

                                func NewHash

                                func NewHash(newHash []byte) (*Hash, error)

                                  NewHash returns a new Hash from a byte slice. An error is returned if the number of bytes passed in is not HashSize.

                                  func NewHashFromStr

                                  func NewHashFromStr(hash string) (*Hash, error)

                                    NewHashFromStr creates a Hash from a hash string. The string should be the hexadecimal string of a byte-reversed hash, but any missing characters result in zero padding at the end of the Hash.

                                    func (*Hash) CloneBytes

                                    func (hash *Hash) CloneBytes() []byte

                                      CloneBytes returns a copy of the bytes which represent the hash as a byte slice. NOTE: It is generally cheaper to just slice the hash directly thereby reusing the same bytes rather than calling this method.

                                      func (*Hash) SetBytes

                                      func (hash *Hash) SetBytes(newHash []byte) error

                                        SetBytes sets the bytes which represent the hash. An error is returned if the number of bytes passed in is not HashSize.

                                        func (Hash) String

                                        func (hash Hash) String() string

                                          String returns the Hash as the hexadecimal string of the byte-reversed hash.

                                          Source Files