github.com/phillinzzz/newBsc@v1.1.6/core/vm/lightclient/rootmultistore.go (about)

     1  package lightclient
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/tendermint/tendermint/crypto/merkle"
     7  	"github.com/tendermint/tendermint/crypto/tmhash"
     8  )
     9  
    10  //----------------------------------------
    11  // CommitID
    12  
    13  // CommitID contains the tree version number and its merkle root.
    14  type CommitID struct {
    15  	Version int64
    16  	Hash    []byte
    17  }
    18  
    19  func (cid CommitID) IsZero() bool { //nolint
    20  	return cid.Version == 0 && len(cid.Hash) == 0
    21  }
    22  
    23  func (cid CommitID) String() string {
    24  	return fmt.Sprintf("CommitID{%v:%X}", cid.Hash, cid.Version)
    25  }
    26  
    27  //----------------------------------------
    28  // CommitInfo
    29  
    30  // NOTE: Keep CommitInfo a simple immutable struct.
    31  type CommitInfo struct {
    32  
    33  	// Version
    34  	Version int64
    35  
    36  	// Store info for
    37  	StoreInfos []StoreInfo
    38  }
    39  
    40  // Hash returns the simple merkle root hash of the stores sorted by name.
    41  func (ci CommitInfo) Hash() []byte {
    42  	// TODO cache to ci.hash []byte
    43  	m := make(map[string][]byte, len(ci.StoreInfos))
    44  	for _, storeInfo := range ci.StoreInfos {
    45  		m[storeInfo.Name] = storeInfo.Hash()
    46  	}
    47  	return merkle.SimpleHashFromMap(m)
    48  }
    49  
    50  func (ci CommitInfo) CommitID() CommitID {
    51  	return CommitID{
    52  		Version: ci.Version,
    53  		Hash:    ci.Hash(),
    54  	}
    55  }
    56  
    57  //----------------------------------------
    58  // StoreInfo
    59  
    60  // StoreInfo contains the name and core reference for an
    61  // underlying store.  It is the leaf of the rootMultiStores top
    62  // level simple merkle tree.
    63  type StoreInfo struct {
    64  	Name string
    65  	Core StoreCore
    66  }
    67  
    68  type StoreCore struct {
    69  	// StoreType StoreType
    70  	CommitID CommitID
    71  	// ... maybe add more state
    72  }
    73  
    74  // Implements merkle.Hasher.
    75  func (si StoreInfo) Hash() []byte {
    76  	// Doesn't write Name, since merkle.SimpleHashFromMap() will
    77  	// include them via the keys.
    78  	bz, _ := Cdc.MarshalBinaryLengthPrefixed(si.Core) // Does not error
    79  	hasher := tmhash.New()
    80  	_, err := hasher.Write(bz)
    81  	if err != nil {
    82  		// TODO: Handle with #870
    83  		panic(err)
    84  	}
    85  	return hasher.Sum(nil)
    86  }