-
Notifications
You must be signed in to change notification settings - Fork 69
/
ops_test.go
544 lines (506 loc) · 12.7 KB
/
ops_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package ics23
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"testing"
)
func TestValidateIavlOps(t *testing.T) {
var (
op opType
layerNum int
)
cases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
{
"failure: reading varint",
func() {
op.(*InnerOp).Prefix = []byte{}
},
errors.New("failed to read IAVL height varint: EOF"),
},
{
"failure: invalid height value",
func() {
op.(*InnerOp).Prefix = []byte{1}
},
errors.New("IAVL height (-1) must be non-negative and greater than or equal to the layer number (1)"),
},
{
"failure: invalid size varint",
func() {
var varintBuf [binary.MaxVarintLen64]byte
prefix := convertVarIntToBytes(int64(5), varintBuf)
op.(*InnerOp).Prefix = prefix
},
errors.New("failed to read IAVL size varint: EOF"),
},
{
"failure: invalid size value",
func() {
var varintBuf [binary.MaxVarintLen64]byte
prefix := convertVarIntToBytes(int64(5), varintBuf)
prefix = append(prefix, convertVarIntToBytes(int64(-1), varintBuf)...) // size
op.(*InnerOp).Prefix = prefix
},
errors.New("IAVL size must be non-negative"),
},
{
"failure: invalid version varint",
func() {
var varintBuf [binary.MaxVarintLen64]byte
prefix := convertVarIntToBytes(int64(5), varintBuf)
prefix = append(prefix, convertVarIntToBytes(int64(10), varintBuf)...)
op.(*InnerOp).Prefix = prefix
},
errors.New("failed to read IAVL version varint: EOF"),
},
{
"failure: invalid version value",
func() {
var varintBuf [binary.MaxVarintLen64]byte
prefix := convertVarIntToBytes(int64(5), varintBuf)
prefix = append(prefix, convertVarIntToBytes(int64(10), varintBuf)...)
prefix = append(prefix, convertVarIntToBytes(int64(-1), varintBuf)...) // version
op.(*InnerOp).Prefix = prefix
},
errors.New("IAVL version must be non-negative"),
},
{
"failure: invalid remaining length with layer number is 0",
func() {
layerNum = 0
},
fmt.Errorf("expected remaining prefix length to be 0, got: 1"),
},
{
"failure: invalid remaining length with non-zero layer number",
func() {
layerNum = 1
op.(*InnerOp).Prefix = append(op.(*InnerOp).Prefix, []byte{1}...)
},
fmt.Errorf("remainder of prefix must be of length 1 or 34, got: 2"),
},
{
"failure: invalid hash",
func() {
op.(*InnerOp).Hash = HashOp_NO_HASH
},
fmt.Errorf("IAVL hash op must be %v", HashOp_SHA256),
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
op = &InnerOp{
Hash: HashOp_SHA256,
Prefix: generateInnerOpPrefix(),
Suffix: []byte(""),
}
layerNum = 1
tc.malleate()
err := validateIavlOps(op, layerNum)
if tc.expError == nil && err != nil {
t.Fatal(err)
}
if tc.expError != nil && err.Error() != tc.expError.Error() {
t.Fatalf("expected: %v, got: %v", tc.expError, err)
}
})
}
}
func TestValidateTendermintOps(t *testing.T) {
var op *InnerOp
cases := []struct {
name string
malleate func()
expError error
}{
{
"success: valid prefix when suffix populated",
func() {},
nil,
},
{
"success: valid prefix when suffix empty",
func() {
op.Prefix = []byte{1, 2}
op.Suffix = nil
},
nil,
},
{
"failure: empty prefix and suffix",
func() {
op.Prefix = nil
op.Suffix = nil
},
errors.New("inner op prefix must not be empty"),
},
{
"failure: invalid prefix when suffix populated",
func() {
op.Prefix = []byte{0}
op.Suffix = []byte{1}
},
fmt.Errorf("expected inner op prefix: %v, got: %v", []byte{1}, []byte{0}),
},
{
"failure: invalid prefix when suffix empty",
func() {
op.Prefix = []byte{2, 1}
op.Suffix = nil
},
fmt.Errorf("expected inner op prefix to begin with: %v, got: %v", []byte{1}, []byte{2}),
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
op = &InnerOp{
Hash: HashOp_SHA256,
Prefix: []byte{1},
Suffix: []byte{1},
}
tc.malleate()
err := validateTendermintOps(op)
if tc.expError == nil && err != nil {
t.Fatal(err)
}
if tc.expError != nil && err.Error() != tc.expError.Error() {
t.Fatalf("expected: %v, got: %v", tc.expError, err)
}
})
}
}
func TestLeafOp(t *testing.T) {
cases := LeafOpTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res, err := tc.Op.Apply(tc.Key, tc.Value)
// short-circuit with error case
if tc.IsErr && err == nil {
t.Fatal("expected error, but got none")
}
if !tc.IsErr && err != nil {
t.Fatal(err)
}
if !bytes.Equal(res, tc.Expected) {
t.Errorf("bad result: %s vs %s", toHex(res), toHex(tc.Expected))
}
})
}
}
func TestInnerOp(t *testing.T) {
cases := InnerOpTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res, err := tc.Op.Apply(tc.Child)
if tc.IsErr && err == nil {
t.Fatal("Expected error, but got none")
}
if !tc.IsErr && err != nil {
t.Fatal(err)
}
if !bytes.Equal(res, tc.Expected) {
t.Errorf("Bad result: %s vs %s", toHex(res), toHex(tc.Expected))
}
})
}
}
func TestDoHash(t *testing.T) {
cases := DoHashTestData(t)
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res, err := doHash(tc.HashOp, []byte(tc.Preimage))
if err != nil {
t.Fatal(err)
}
hexRes := hex.EncodeToString(res)
if hexRes != tc.ExpectedHash {
t.Fatalf("Expected %s got %s", tc.ExpectedHash, hexRes)
}
})
}
}
func TestInnerOpCheckAgainstSpec(t *testing.T) {
var (
spec *ProofSpec
innerOp *InnerOp
)
cases := []struct {
name string
malleate func()
expError error
}{
{
"success",
func() {},
nil,
},
{
"failure: empty spec",
func() {
spec = nil
},
errors.New("op and spec must be both non-nil"),
},
{
"failure: empty inner spec",
func() {
spec.InnerSpec = nil
},
errors.New("spec.InnerSpec must be non-nil"),
},
{
"failure: empty leaf spec",
func() {
spec.LeafSpec = nil
},
errors.New("spec.LeafSpec must be non-nil"),
},
{
"failure: incorrect hash function inner op",
func() {
innerOp.Hash = HashOp_BITCOIN
},
fmt.Errorf("unexpected HashOp: %d", HashOp_BITCOIN),
},
{
"failure: spec fails validation",
func() {
innerOp.Prefix = []byte{0x01}
},
fmt.Errorf("IAVL height (-1) must be non-negative and greater than or equal to the layer number (1)"),
},
{
"failure: inner prefix starts with leaf prefix",
func() {
// change spec to be non-iavl spec to skip strict iavl validation
spec.LeafSpec.PrehashKey = HashOp_BITCOIN
innerOp.Prefix = append(spec.LeafSpec.Prefix, innerOp.Prefix...)
},
fmt.Errorf("inner Prefix starts with %X", []byte{0}),
},
{
"failure: inner prefix too short",
func() {
// change spec to be non-iavl spec to skip strict iavl validation
spec.LeafSpec.PrehashKey = HashOp_BITCOIN
innerOp.Prefix = []byte{0x01}
},
errors.New("innerOp prefix too short (1)"),
},
{
"failure: inner prefix too long",
func() {
// change spec to be non-iavl spec to skip strict iavl validation
spec.LeafSpec.PrehashKey = HashOp_BITCOIN
innerOp.Prefix = []byte("AgQIIGe3bHuC1g6+5/Qd0RoCU0waFu+nDCFzEDViMN/VrQwgIA==")
},
errors.New("innerOp prefix too long (52)"),
},
{
"failure: child size must be greater than zero",
func() {
spec.InnerSpec.ChildSize = 0
},
errors.New("spec.InnerSpec.ChildSize must be >= 1"),
},
{
"failure: MaxPrefixLength >= MinPrefixLength + ChildSize",
func() {
spec.InnerSpec.MaxPrefixLength = spec.InnerSpec.MinPrefixLength + spec.InnerSpec.ChildSize
},
errors.New("spec.InnerSpec.MaxPrefixLength must be < spec.InnerSpec.MinPrefixLength + spec.InnerSpec.ChildSize"),
},
{
"failure: inner op suffix malformed",
func() {
innerOp.Suffix = []byte{0x01}
},
fmt.Errorf("InnerOp suffix malformed"),
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
spec = &ProofSpec{ // IavlSpec
LeafSpec: &LeafOp{
Prefix: []byte{0},
PrehashKey: HashOp_NO_HASH,
Hash: HashOp_SHA256,
PrehashValue: HashOp_SHA256,
Length: LengthOp_VAR_PROTO,
},
InnerSpec: &InnerSpec{
ChildOrder: []int32{0, 1},
MinPrefixLength: 4,
MaxPrefixLength: 12,
ChildSize: 33, // (with length byte)
EmptyChild: nil,
Hash: HashOp_SHA256,
},
}
innerOp = &InnerOp{
Hash: HashOp_SHA256,
Prefix: generateInnerOpPrefix(),
Suffix: []byte(""),
}
tc.malleate()
err := innerOp.CheckAgainstSpec(spec, 1) // use a layer number of 1
if tc.expError == nil && err != nil {
t.Fatal(err)
}
if tc.expError != nil && err.Error() != tc.expError.Error() {
t.Fatalf("expected: %v, got: %v", tc.expError, err)
}
})
}
}
func TestForgeNonExistenceProofWithIncorrectMaxPrefixLength(t *testing.T) {
spec := &ProofSpec{ // TendermintSpec
LeafSpec: &LeafOp{
Prefix: []byte{0},
PrehashKey: HashOp_NO_HASH,
Hash: HashOp_SHA256,
PrehashValue: HashOp_SHA256,
Length: LengthOp_VAR_PROTO,
},
InnerSpec: &InnerSpec{
ChildOrder: []int32{0, 1},
MinPrefixLength: 1,
MaxPrefixLength: 1,
ChildSize: 32, // (no length byte)
Hash: HashOp_SHA256,
},
}
spec.InnerSpec.MaxPrefixLength = 33
leafOp := spec.LeafSpec
aLeaf, _ := leafOp.Apply([]byte("a"), []byte("a"))
bLeaf, _ := leafOp.Apply([]byte("b"), []byte("b"))
b2Leaf, _ := leafOp.Apply([]byte("b2"), []byte("b2"))
cLeaf, _ := leafOp.Apply([]byte("c"), []byte("c"))
aExist := ExistenceProof{
Key: []byte("a"),
Value: []byte("a"),
Leaf: leafOp,
Path: []*InnerOp{
{
Hash: spec.InnerSpec.Hash,
Prefix: []byte{1},
Suffix: append(bLeaf, b2Leaf...),
},
{
Hash: spec.InnerSpec.Hash,
Prefix: []byte{1},
Suffix: cLeaf,
},
},
}
bExist := ExistenceProof{
Key: []byte("b"),
Value: []byte("b"),
Leaf: leafOp,
Path: []*InnerOp{
{
Hash: spec.InnerSpec.Hash,
Prefix: append([]byte{1}, aLeaf...),
Suffix: b2Leaf,
},
{
Hash: spec.InnerSpec.Hash,
Prefix: []byte{1},
Suffix: cLeaf,
},
},
}
b2Exist := ExistenceProof{
Key: []byte("b2"),
Value: []byte("b2"),
Leaf: leafOp,
Path: []*InnerOp{
{
Hash: spec.InnerSpec.Hash,
Prefix: append(append([]byte{1}, aLeaf...), bLeaf...),
Suffix: []byte{},
},
{
Hash: spec.InnerSpec.Hash,
Prefix: []byte{1},
Suffix: cLeaf,
},
},
}
yHash, _ := aExist.Path[0].Apply(aLeaf)
cExist := ExistenceProof{
Key: []byte("c"),
Value: []byte("c"),
Leaf: leafOp,
Path: []*InnerOp{
{
Hash: spec.InnerSpec.Hash,
Prefix: append([]byte{1}, yHash...),
Suffix: []byte{},
},
},
}
aNotExist := NonExistenceProof{
Key: []byte("a"),
Left: nil,
Right: &bExist,
}
root, err := aExist.Calculate()
if err != nil {
t.Fatal("failed to calculate existence proof of leaf a")
}
expError := fmt.Errorf("inner, %w", errors.New("spec.InnerSpec.MaxPrefixLength must be < spec.InnerSpec.MinPrefixLength + spec.InnerSpec.ChildSize"))
err = aExist.Verify(spec, root, []byte("a"), []byte("a"))
if err.Error() != expError.Error() {
t.Fatal("attempting to prove existence of leaf a returned incorrect error")
}
err = bExist.Verify(spec, root, []byte("b"), []byte("b"))
if err.Error() != expError.Error() {
t.Fatal("attempting to prove existence of leaf b returned incorrect error")
}
err = b2Exist.Verify(spec, root, []byte("b2"), []byte("b2"))
if err.Error() != expError.Error() {
t.Fatal("attempting to prove existence of third leaf returned incorrect error")
}
err = cExist.Verify(spec, root, []byte("c"), []byte("c"))
if err.Error() != expError.Error() {
t.Fatal("attempting to prove existence of leaf c returned incorrect error")
}
err = aNotExist.Verify(spec, root, []byte("a"))
expError = fmt.Errorf("right proof, %w", expError)
if err.Error() != expError.Error() {
t.Fatal("attempting to prove non-existence of leaf a returned incorrect error")
}
}
// generatePrefix generates a valid iavl prefix for an inner op.
func generateInnerOpPrefix() []byte {
var (
varintBuf [binary.MaxVarintLen64]byte
lengthByte byte = 0x20
)
height, size, version := 5, 10, 20
prefix := convertVarIntToBytes(int64(height), varintBuf)
prefix = append(prefix, convertVarIntToBytes(int64(size), varintBuf)...)
prefix = append(prefix, convertVarIntToBytes(int64(version), varintBuf)...)
prefix = append(prefix, lengthByte)
return prefix
}
func convertVarIntToBytes(orig int64, buf [binary.MaxVarintLen64]byte) []byte {
n := binary.PutVarint(buf[:], orig)
return buf[:n]
}