This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testutils_test.go
182 lines (154 loc) · 3.74 KB
/
testutils_test.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
package iavl
import (
"fmt"
mrand "math/rand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/go-wire"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/db"
. "github.com/tendermint/tmlibs/test"
"runtime"
"testing"
)
func dummyPathToKey(t *Tree, key []byte) *PathToKey {
path, _, err := t.root.pathToKey(t, key)
if err != nil {
panic(err)
}
return path
}
func dummyLeafNode(key, val []byte) proofLeafNode {
return proofLeafNode{key, val, 1}
}
func randstr(length int) string {
return RandStr(length)
}
func i2b(i int) []byte {
bz := make([]byte, 4)
wire.PutInt32(bz, int32(i))
return bz
}
func b2i(bz []byte) int {
i := wire.GetInt32(bz)
return int(i)
}
// Convenience for a new node
func N(l, r interface{}) *Node {
var left, right *Node
if _, ok := l.(*Node); ok {
left = l.(*Node)
} else {
left = NewNode(i2b(l.(int)), nil, 0)
}
if _, ok := r.(*Node); ok {
right = r.(*Node)
} else {
right = NewNode(i2b(r.(int)), nil, 0)
}
n := &Node{
key: right.lmd(nil).key,
value: nil,
leftNode: left,
rightNode: right,
}
n.calcHeightAndSize(nil)
return n
}
// Setup a deep node
func T(n *Node) *Tree {
d := db.NewDB("test", db.MemDBBackend, "")
t := NewTree(d, 0)
n.hashWithCount()
t.root = n
return t
}
// Convenience for simple printing of keys & tree structure
func P(n *Node) string {
if n.height == 0 {
return fmt.Sprintf("%v", b2i(n.key))
} else {
return fmt.Sprintf("(%v %v)", P(n.leftNode), P(n.rightNode))
}
}
func randBytes(length int) []byte {
key := make([]byte, length)
// math.rand.Read always returns err=nil
mrand.Read(key)
return key
}
type traverser struct {
first string
last string
count int
}
func (t *traverser) view(key, value []byte) bool {
if t.first == "" {
t.first = string(key)
}
t.last = string(key)
t.count += 1
return false
}
func expectTraverse(t *testing.T, trav traverser, start, end string, count int) {
if trav.first != start {
t.Error("Bad start", start, trav.first)
}
if trav.last != end {
t.Error("Bad end", end, trav.last)
}
if trav.count != count {
t.Error("Bad count", count, trav.count)
}
}
func testProof(t *testing.T, proof *KeyExistsProof, keyBytes, valueBytes, rootHashBytes []byte) {
// Proof must verify.
require.NoError(t, proof.Verify(keyBytes, valueBytes, rootHashBytes))
// Write/Read then verify.
proofBytes := proof.Bytes()
proof2, err := ReadKeyProof(proofBytes)
require.Nil(t, err, "Failed to read KeyExistsProof from bytes: %v", err)
require.NoError(t, proof2.Verify(keyBytes, valueBytes, proof.RootHash))
// Random mutations must not verify
for i := 0; i < 10; i++ {
badProofBytes := MutateByteSlice(proofBytes)
badProof, err := ReadKeyProof(badProofBytes)
// may be invalid... errors are okay
if err == nil {
assert.Error(t, badProof.Verify(keyBytes, valueBytes, rootHashBytes),
"Proof was still valid after a random mutation:\n%X\n%X",
proofBytes, badProofBytes)
}
}
// targetted changes fails...
proof.RootHash = MutateByteSlice(proof.RootHash)
assert.Error(t, proof.Verify(keyBytes, valueBytes, rootHashBytes))
}
func BenchmarkImmutableAvlTreeMemDB(b *testing.B) {
db := db.NewDB("test", db.MemDBBackend, "")
benchmarkImmutableAvlTreeWithDB(b, db)
}
func benchmarkImmutableAvlTreeWithDB(b *testing.B, db db.DB) {
defer db.Close()
b.StopTimer()
t := NewVersionedTree(db, 100000)
for i := 0; i < 1000000; i++ {
t.Set(i2b(int(RandInt32())), nil)
if i > 990000 && i%1000 == 999 {
t.SaveVersion()
}
}
b.ReportAllocs()
t.SaveVersion()
fmt.Println("ok, starting")
runtime.GC()
b.StartTimer()
for i := 0; i < b.N; i++ {
ri := i2b(int(RandInt32()))
t.Set(ri, nil)
t.Remove(ri)
if i%100 == 99 {
t.SaveVersion()
}
}
}