forked from cosmos/cosmos-sdk
/
commit_info.go
53 lines (44 loc) · 1.41 KB
/
commit_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package types
import (
tmcrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
sdkmaps "github.com/cosmos/cosmos-sdk/store/internal/maps"
)
// GetHash returns the GetHash from the CommitID.
// This is used in CommitInfo.Hash()
//
// When we commit to this in a merkle proof, we create a map of storeInfo.Name -> storeInfo.GetHash()
// and build a merkle proof from that.
// This is then chained with the substore proof, so we prove the root hash from the substore before this
// and need to pass that (unmodified) as the leaf value of the multistore proof.
func (si StoreInfo) GetHash() []byte {
return si.CommitId.Hash
}
func (ci CommitInfo) toMap() map[string][]byte {
m := make(map[string][]byte, len(ci.StoreInfos))
for _, storeInfo := range ci.StoreInfos {
m[storeInfo.Name] = storeInfo.GetHash()
}
return m
}
// Hash returns the simple merkle root hash of the stores sorted by name.
func (ci CommitInfo) Hash() []byte {
// we need a special case for empty set, as SimpleProofsFromMap requires at least one entry
if len(ci.StoreInfos) == 0 {
return nil
}
rootHash, _, _ := sdkmaps.ProofsFromMap(ci.toMap())
return rootHash
}
func (ci CommitInfo) ProofOp(storeName string) tmcrypto.ProofOp {
ret, err := ProofOpFromMap(ci.toMap(), storeName)
if err != nil {
panic(err)
}
return ret
}
func (ci CommitInfo) CommitID() CommitID {
return CommitID{
Version: ci.Version,
Hash: ci.Hash(),
}
}