forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proof.go
183 lines (159 loc) · 4.27 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package iavl
import (
"bytes"
"fmt"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
)
var (
// ErrInvalidProof is returned by Verify when a proof cannot be validated.
ErrInvalidProof = fmt.Errorf("invalid proof")
// ErrInvalidInputs is returned when the inputs passed to the function are invalid.
ErrInvalidInputs = fmt.Errorf("invalid inputs")
// ErrInvalidRoot is returned when the root passed in does not match the proof's.
ErrInvalidRoot = fmt.Errorf("invalid root")
)
//----------------------------------------
type proofInnerNode struct {
Height int8 `json:"height"`
Size int64 `json:"size"`
Version int64 `json:"version"`
Left []byte `json:"left"`
Right []byte `json:"right"`
}
func (pin proofInnerNode) String() string {
return pin.stringIndented("")
}
func (pin proofInnerNode) stringIndented(indent string) string {
return fmt.Sprintf(`proofInnerNode{
%s Height: %v
%s Size: %v
%s Version: %v
%s Left: %X
%s Right: %X
%s}`,
indent, pin.Height,
indent, pin.Size,
indent, pin.Version,
indent, pin.Left,
indent, pin.Right,
indent)
}
func (pin proofInnerNode) Hash(childHash []byte) []byte {
hasher := tmhash.New()
buf := new(bytes.Buffer)
err := amino.EncodeInt8(buf, pin.Height)
if err == nil {
err = amino.EncodeVarint(buf, pin.Size)
}
if err == nil {
err = amino.EncodeVarint(buf, pin.Version)
}
if len(pin.Left) == 0 {
if err == nil {
err = amino.EncodeByteSlice(buf, childHash)
}
if err == nil {
err = amino.EncodeByteSlice(buf, pin.Right)
}
} else {
if err == nil {
err = amino.EncodeByteSlice(buf, pin.Left)
}
if err == nil {
err = amino.EncodeByteSlice(buf, childHash)
}
}
if err != nil {
panic(fmt.Sprintf("Failed to hash proofInnerNode: %v", err))
}
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}
//----------------------------------------
type proofLeafNode struct {
Key cmn.HexBytes `json:"key"`
ValueHash cmn.HexBytes `json:"value"`
Version int64 `json:"version"`
}
func (pln proofLeafNode) String() string {
return pln.stringIndented("")
}
func (pln proofLeafNode) stringIndented(indent string) string {
return fmt.Sprintf(`proofLeafNode{
%s Key: %v
%s ValueHash: %X
%s Version: %v
%s}`,
indent, pln.Key,
indent, pln.ValueHash,
indent, pln.Version,
indent)
}
func (pln proofLeafNode) Hash() []byte {
hasher := tmhash.New()
buf := new(bytes.Buffer)
err := amino.EncodeInt8(buf, 0)
if err == nil {
err = amino.EncodeVarint(buf, 1)
}
if err == nil {
err = amino.EncodeVarint(buf, pln.Version)
}
if err == nil {
err = amino.EncodeByteSlice(buf, pln.Key)
}
if err == nil {
err = amino.EncodeByteSlice(buf, pln.ValueHash)
}
if err != nil {
panic(fmt.Sprintf("Failed to hash proofLeafNode: %v", err))
}
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}
//----------------------------------------
// If the key does not exist, returns the path to the next leaf left of key (w/
// path), except when key is less than the least item, in which case it returns
// a path to the least item.
func (node *Node) PathToLeaf(t *ImmutableTree, key []byte) (PathToLeaf, *Node, error) {
path := new(PathToLeaf)
val, err := node.pathToLeaf(t, key, path)
return *path, val, err
}
// pathToLeaf is a helper which recursively constructs the PathToLeaf.
// As an optimization the already constructed path is passed in as an argument
// and is shared among recursive calls.
func (node *Node) pathToLeaf(t *ImmutableTree, key []byte, path *PathToLeaf) (*Node, error) {
if node.height == 0 {
if bytes.Equal(node.key, key) {
return node, nil
}
return node, cmn.NewError("key does not exist")
}
if bytes.Compare(key, node.key) < 0 {
// left side
pin := proofInnerNode{
Height: node.height,
Size: node.size,
Version: node.version,
Left: nil,
Right: node.getRightNode(t).hash,
}
*path = append(*path, pin)
n, err := node.getLeftNode(t).pathToLeaf(t, key, path)
return n, err
}
// right side
pin := proofInnerNode{
Height: node.height,
Size: node.size,
Version: node.version,
Left: node.getLeftNode(t).hash,
Right: nil,
}
*path = append(*path, pin)
n, err := node.getRightNode(t).pathToLeaf(t, key, path)
return n, err
}