This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
/
iavl_proof.go
58 lines (52 loc) · 1.72 KB
/
iavl_proof.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
54
55
56
57
58
package proof
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/tendermint/iavl"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
)
// IAVLMerklePath represents a Merkle step to a leaf data node in an iAVL tree.
type IAVLMerklePath struct {
IsDataOnRight bool `json:"isDataOnRight"`
SubtreeHeight uint8 `json:"subtreeHeight"`
SubtreeSize uint64 `json:"subtreeSize"`
SubtreeVersion uint64 `json:"subtreeVersion"`
SiblingHash tmbytes.HexBytes `json:"siblingHash"`
}
// IAVLMerklePathEthereum is an Ethereum version of IAVLMerklePath for solidity ABI-encoding.
type IAVLMerklePathEthereum struct {
IsDataOnRight bool
SubtreeHeight uint8
SubtreeSize *big.Int
SubtreeVersion *big.Int
SiblingHash common.Hash
}
func (merklePath *IAVLMerklePath) encodeToEthFormat() IAVLMerklePathEthereum {
return IAVLMerklePathEthereum{
merklePath.IsDataOnRight,
merklePath.SubtreeHeight,
big.NewInt(int64(merklePath.SubtreeSize)),
big.NewInt(int64(merklePath.SubtreeVersion)),
common.BytesToHash(merklePath.SiblingHash),
}
}
// GetIAVLMerklePaths returns the list of IAVLMerklePath elements from the given iAVL proof.
func GetIAVLMerklePaths(proof *iavl.ValueOp) []IAVLMerklePath {
paths := make([]IAVLMerklePath, 0)
for i := len(proof.Proof.LeftPath) - 1; i >= 0; i-- {
path := proof.Proof.LeftPath[i]
imp := IAVLMerklePath{}
imp.SubtreeHeight = uint8(path.Height)
imp.SubtreeSize = uint64(path.Size)
imp.SubtreeVersion = uint64(path.Version)
if len(path.Right) == 0 {
imp.SiblingHash = path.Left
imp.IsDataOnRight = true
} else {
imp.SiblingHash = path.Right
imp.IsDataOnRight = false
}
paths = append(paths, imp)
}
return paths
}