-
Notifications
You must be signed in to change notification settings - Fork 17
/
iavl_proof.go
153 lines (139 loc) · 3.79 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
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
package iavl
import (
"bytes"
"golang.org/x/crypto/ripemd160"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/go-wire"
)
const proofLimit = 1 << 16 // 64 KB
type IAVLProof struct {
LeafHash []byte
InnerNodes []IAVLProofInnerNode
RootHash []byte
}
func (proof *IAVLProof) Verify(key []byte, value []byte, root []byte) bool {
if !bytes.Equal(proof.RootHash, root) {
return false
}
leafNode := IAVLProofLeafNode{KeyBytes: key, ValueBytes: value}
leafHash := leafNode.Hash()
if !bytes.Equal(leafHash, proof.LeafHash) {
return false
}
hash := leafHash
for _, branch := range proof.InnerNodes {
hash = branch.Hash(hash)
}
return bytes.Equal(proof.RootHash, hash)
}
// Please leave this here! I use it in light-client to fulfill an interface
func (proof *IAVLProof) Root() []byte {
return proof.RootHash
}
// ReadProof will deserialize a IAVLProof from bytes
func ReadProof(data []byte) (*IAVLProof, error) {
// TODO: make go-wire never panic
n, err := int(0), error(nil)
proof := wire.ReadBinary(&IAVLProof{}, bytes.NewBuffer(data), proofLimit, &n, &err).(*IAVLProof)
return proof, err
}
type IAVLProofInnerNode struct {
Height int8
Size int
Left []byte
Right []byte
}
func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte {
hasher := ripemd160.New()
buf := new(bytes.Buffer)
n, err := int(0), error(nil)
wire.WriteInt8(branch.Height, buf, &n, &err)
wire.WriteVarint(branch.Size, buf, &n, &err)
if len(branch.Left) == 0 {
wire.WriteByteSlice(childHash, buf, &n, &err)
wire.WriteByteSlice(branch.Right, buf, &n, &err)
} else {
wire.WriteByteSlice(branch.Left, buf, &n, &err)
wire.WriteByteSlice(childHash, buf, &n, &err)
}
if err != nil {
PanicCrisis(Fmt("Failed to hash IAVLProofInnerNode: %v", err))
}
// fmt.Printf("InnerNode hash bytes: %X\n", buf.Bytes())
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}
type IAVLProofLeafNode struct {
KeyBytes []byte
ValueBytes []byte
}
func (leaf IAVLProofLeafNode) Hash() []byte {
hasher := ripemd160.New()
buf := new(bytes.Buffer)
n, err := int(0), error(nil)
wire.WriteInt8(0, buf, &n, &err)
wire.WriteVarint(1, buf, &n, &err)
wire.WriteByteSlice(leaf.KeyBytes, buf, &n, &err)
wire.WriteByteSlice(leaf.ValueBytes, buf, &n, &err)
if err != nil {
PanicCrisis(Fmt("Failed to hash IAVLProofLeafNode: %v", err))
}
// fmt.Printf("LeafNode hash bytes: %X\n", buf.Bytes())
hasher.Write(buf.Bytes())
return hasher.Sum(nil)
}
func (node *IAVLNode) constructProof(t *IAVLTree, key []byte, valuePtr *[]byte, proof *IAVLProof) (exists bool) {
if node.height == 0 {
if bytes.Compare(node.key, key) == 0 {
*valuePtr = node.value
proof.LeafHash = node.hash
return true
} else {
return false
}
} else {
if bytes.Compare(key, node.key) < 0 {
exists := node.getLeftNode(t).constructProof(t, key, valuePtr, proof)
if !exists {
return false
}
branch := IAVLProofInnerNode{
Height: node.height,
Size: node.size,
Left: nil,
Right: node.getRightNode(t).hash,
}
proof.InnerNodes = append(proof.InnerNodes, branch)
return true
} else {
exists := node.getRightNode(t).constructProof(t, key, valuePtr, proof)
if !exists {
return false
}
branch := IAVLProofInnerNode{
Height: node.height,
Size: node.size,
Left: node.getLeftNode(t).hash,
Right: nil,
}
proof.InnerNodes = append(proof.InnerNodes, branch)
return true
}
}
}
// Returns nil, nil if key is not in tree.
func (t *IAVLTree) ConstructProof(key []byte) (value []byte, proof *IAVLProof) {
if t.root == nil {
return nil, nil
}
t.root.hashWithCount(t) // Ensure that all hashes are calculated.
proof = &IAVLProof{
RootHash: t.root.hash,
}
exists := t.root.constructProof(t, key, &value, proof)
if exists {
return value, proof
} else {
return nil, nil
}
}