gitee.com/liu-zhao234568/cntest@v1.0.0/trie/proof_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package trie 18 19 import ( 20 "bytes" 21 crand "crypto/rand" 22 "encoding/binary" 23 mrand "math/rand" 24 "sort" 25 "testing" 26 "time" 27 28 "gitee.com/liu-zhao234568/cntest/common" 29 "gitee.com/liu-zhao234568/cntest/crypto" 30 "gitee.com/liu-zhao234568/cntest/ethdb/memorydb" 31 ) 32 33 func init() { 34 mrand.Seed(time.Now().Unix()) 35 } 36 37 // makeProvers creates Merkle trie provers based on different implementations to 38 // test all variations. 39 func makeProvers(trie *Trie) []func(key []byte) *memorydb.Database { 40 var provers []func(key []byte) *memorydb.Database 41 42 // Create a direct trie based Merkle prover 43 provers = append(provers, func(key []byte) *memorydb.Database { 44 proof := memorydb.New() 45 trie.Prove(key, 0, proof) 46 return proof 47 }) 48 // Create a leaf iterator based Merkle prover 49 provers = append(provers, func(key []byte) *memorydb.Database { 50 proof := memorydb.New() 51 if it := NewIterator(trie.NodeIterator(key)); it.Next() && bytes.Equal(key, it.Key) { 52 for _, p := range it.Prove() { 53 proof.Put(crypto.Keccak256(p), p) 54 } 55 } 56 return proof 57 }) 58 return provers 59 } 60 61 func TestProof(t *testing.T) { 62 trie, vals := randomTrie(500) 63 root := trie.Hash() 64 for i, prover := range makeProvers(trie) { 65 for _, kv := range vals { 66 proof := prover(kv.k) 67 if proof == nil { 68 t.Fatalf("prover %d: missing key %x while constructing proof", i, kv.k) 69 } 70 val, err := VerifyProof(root, kv.k, proof) 71 if err != nil { 72 t.Fatalf("prover %d: failed to verify proof for key %x: %v\nraw proof: %x", i, kv.k, err, proof) 73 } 74 if !bytes.Equal(val, kv.v) { 75 t.Fatalf("prover %d: verified value mismatch for key %x: have %x, want %x", i, kv.k, val, kv.v) 76 } 77 } 78 } 79 } 80 81 func TestOneElementProof(t *testing.T) { 82 trie := new(Trie) 83 updateString(trie, "k", "v") 84 for i, prover := range makeProvers(trie) { 85 proof := prover([]byte("k")) 86 if proof == nil { 87 t.Fatalf("prover %d: nil proof", i) 88 } 89 if proof.Len() != 1 { 90 t.Errorf("prover %d: proof should have one element", i) 91 } 92 val, err := VerifyProof(trie.Hash(), []byte("k"), proof) 93 if err != nil { 94 t.Fatalf("prover %d: failed to verify proof: %v\nraw proof: %x", i, err, proof) 95 } 96 if !bytes.Equal(val, []byte("v")) { 97 t.Fatalf("prover %d: verified value mismatch: have %x, want 'k'", i, val) 98 } 99 } 100 } 101 102 func TestBadProof(t *testing.T) { 103 trie, vals := randomTrie(800) 104 root := trie.Hash() 105 for i, prover := range makeProvers(trie) { 106 for _, kv := range vals { 107 proof := prover(kv.k) 108 if proof == nil { 109 t.Fatalf("prover %d: nil proof", i) 110 } 111 it := proof.NewIterator(nil, nil) 112 for i, d := 0, mrand.Intn(proof.Len()); i <= d; i++ { 113 it.Next() 114 } 115 key := it.Key() 116 val, _ := proof.Get(key) 117 proof.Delete(key) 118 it.Release() 119 120 mutateByte(val) 121 proof.Put(crypto.Keccak256(val), val) 122 123 if _, err := VerifyProof(root, kv.k, proof); err == nil { 124 t.Fatalf("prover %d: expected proof to fail for key %x", i, kv.k) 125 } 126 } 127 } 128 } 129 130 // Tests that missing keys can also be proven. The test explicitly uses a single 131 // entry trie and checks for missing keys both before and after the single entry. 132 func TestMissingKeyProof(t *testing.T) { 133 trie := new(Trie) 134 updateString(trie, "k", "v") 135 136 for i, key := range []string{"a", "j", "l", "z"} { 137 proof := memorydb.New() 138 trie.Prove([]byte(key), 0, proof) 139 140 if proof.Len() != 1 { 141 t.Errorf("test %d: proof should have one element", i) 142 } 143 val, err := VerifyProof(trie.Hash(), []byte(key), proof) 144 if err != nil { 145 t.Fatalf("test %d: failed to verify proof: %v\nraw proof: %x", i, err, proof) 146 } 147 if val != nil { 148 t.Fatalf("test %d: verified value mismatch: have %x, want nil", i, val) 149 } 150 } 151 } 152 153 type entrySlice []*kv 154 155 func (p entrySlice) Len() int { return len(p) } 156 func (p entrySlice) Less(i, j int) bool { return bytes.Compare(p[i].k, p[j].k) < 0 } 157 func (p entrySlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 158 159 // TestRangeProof tests normal range proof with both edge proofs 160 // as the existent proof. The test cases are generated randomly. 161 func TestRangeProof(t *testing.T) { 162 trie, vals := randomTrie(4096) 163 var entries entrySlice 164 for _, kv := range vals { 165 entries = append(entries, kv) 166 } 167 sort.Sort(entries) 168 for i := 0; i < 500; i++ { 169 start := mrand.Intn(len(entries)) 170 end := mrand.Intn(len(entries)-start) + start + 1 171 172 proof := memorydb.New() 173 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 174 t.Fatalf("Failed to prove the first node %v", err) 175 } 176 if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { 177 t.Fatalf("Failed to prove the last node %v", err) 178 } 179 var keys [][]byte 180 var vals [][]byte 181 for i := start; i < end; i++ { 182 keys = append(keys, entries[i].k) 183 vals = append(vals, entries[i].v) 184 } 185 _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof) 186 if err != nil { 187 t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err) 188 } 189 } 190 } 191 192 // TestRangeProof tests normal range proof with two non-existent proofs. 193 // The test cases are generated randomly. 194 func TestRangeProofWithNonExistentProof(t *testing.T) { 195 trie, vals := randomTrie(4096) 196 var entries entrySlice 197 for _, kv := range vals { 198 entries = append(entries, kv) 199 } 200 sort.Sort(entries) 201 for i := 0; i < 500; i++ { 202 start := mrand.Intn(len(entries)) 203 end := mrand.Intn(len(entries)-start) + start + 1 204 proof := memorydb.New() 205 206 // Short circuit if the decreased key is same with the previous key 207 first := decreseKey(common.CopyBytes(entries[start].k)) 208 if start != 0 && bytes.Equal(first, entries[start-1].k) { 209 continue 210 } 211 // Short circuit if the decreased key is underflow 212 if bytes.Compare(first, entries[start].k) > 0 { 213 continue 214 } 215 // Short circuit if the increased key is same with the next key 216 last := increseKey(common.CopyBytes(entries[end-1].k)) 217 if end != len(entries) && bytes.Equal(last, entries[end].k) { 218 continue 219 } 220 // Short circuit if the increased key is overflow 221 if bytes.Compare(last, entries[end-1].k) < 0 { 222 continue 223 } 224 if err := trie.Prove(first, 0, proof); err != nil { 225 t.Fatalf("Failed to prove the first node %v", err) 226 } 227 if err := trie.Prove(last, 0, proof); err != nil { 228 t.Fatalf("Failed to prove the last node %v", err) 229 } 230 var keys [][]byte 231 var vals [][]byte 232 for i := start; i < end; i++ { 233 keys = append(keys, entries[i].k) 234 vals = append(vals, entries[i].v) 235 } 236 _, err := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof) 237 if err != nil { 238 t.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err) 239 } 240 } 241 // Special case, two edge proofs for two edge key. 242 proof := memorydb.New() 243 first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() 244 last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes() 245 if err := trie.Prove(first, 0, proof); err != nil { 246 t.Fatalf("Failed to prove the first node %v", err) 247 } 248 if err := trie.Prove(last, 0, proof); err != nil { 249 t.Fatalf("Failed to prove the last node %v", err) 250 } 251 var k [][]byte 252 var v [][]byte 253 for i := 0; i < len(entries); i++ { 254 k = append(k, entries[i].k) 255 v = append(v, entries[i].v) 256 } 257 _, err := VerifyRangeProof(trie.Hash(), first, last, k, v, proof) 258 if err != nil { 259 t.Fatal("Failed to verify whole rang with non-existent edges") 260 } 261 } 262 263 // TestRangeProofWithInvalidNonExistentProof tests such scenarios: 264 // - There exists a gap between the first element and the left edge proof 265 // - There exists a gap between the last element and the right edge proof 266 func TestRangeProofWithInvalidNonExistentProof(t *testing.T) { 267 trie, vals := randomTrie(4096) 268 var entries entrySlice 269 for _, kv := range vals { 270 entries = append(entries, kv) 271 } 272 sort.Sort(entries) 273 274 // Case 1 275 start, end := 100, 200 276 first := decreseKey(common.CopyBytes(entries[start].k)) 277 278 proof := memorydb.New() 279 if err := trie.Prove(first, 0, proof); err != nil { 280 t.Fatalf("Failed to prove the first node %v", err) 281 } 282 if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { 283 t.Fatalf("Failed to prove the last node %v", err) 284 } 285 start = 105 // Gap created 286 k := make([][]byte, 0) 287 v := make([][]byte, 0) 288 for i := start; i < end; i++ { 289 k = append(k, entries[i].k) 290 v = append(v, entries[i].v) 291 } 292 _, err := VerifyRangeProof(trie.Hash(), first, k[len(k)-1], k, v, proof) 293 if err == nil { 294 t.Fatalf("Expected to detect the error, got nil") 295 } 296 297 // Case 2 298 start, end = 100, 200 299 last := increseKey(common.CopyBytes(entries[end-1].k)) 300 proof = memorydb.New() 301 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 302 t.Fatalf("Failed to prove the first node %v", err) 303 } 304 if err := trie.Prove(last, 0, proof); err != nil { 305 t.Fatalf("Failed to prove the last node %v", err) 306 } 307 end = 195 // Capped slice 308 k = make([][]byte, 0) 309 v = make([][]byte, 0) 310 for i := start; i < end; i++ { 311 k = append(k, entries[i].k) 312 v = append(v, entries[i].v) 313 } 314 _, err = VerifyRangeProof(trie.Hash(), k[0], last, k, v, proof) 315 if err == nil { 316 t.Fatalf("Expected to detect the error, got nil") 317 } 318 } 319 320 // TestOneElementRangeProof tests the proof with only one 321 // element. The first edge proof can be existent one or 322 // non-existent one. 323 func TestOneElementRangeProof(t *testing.T) { 324 trie, vals := randomTrie(4096) 325 var entries entrySlice 326 for _, kv := range vals { 327 entries = append(entries, kv) 328 } 329 sort.Sort(entries) 330 331 // One element with existent edge proof, both edge proofs 332 // point to the SAME key. 333 start := 1000 334 proof := memorydb.New() 335 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 336 t.Fatalf("Failed to prove the first node %v", err) 337 } 338 _, err := VerifyRangeProof(trie.Hash(), entries[start].k, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof) 339 if err != nil { 340 t.Fatalf("Expected no error, got %v", err) 341 } 342 343 // One element with left non-existent edge proof 344 start = 1000 345 first := decreseKey(common.CopyBytes(entries[start].k)) 346 proof = memorydb.New() 347 if err := trie.Prove(first, 0, proof); err != nil { 348 t.Fatalf("Failed to prove the first node %v", err) 349 } 350 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 351 t.Fatalf("Failed to prove the last node %v", err) 352 } 353 _, err = VerifyRangeProof(trie.Hash(), first, entries[start].k, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof) 354 if err != nil { 355 t.Fatalf("Expected no error, got %v", err) 356 } 357 358 // One element with right non-existent edge proof 359 start = 1000 360 last := increseKey(common.CopyBytes(entries[start].k)) 361 proof = memorydb.New() 362 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 363 t.Fatalf("Failed to prove the first node %v", err) 364 } 365 if err := trie.Prove(last, 0, proof); err != nil { 366 t.Fatalf("Failed to prove the last node %v", err) 367 } 368 _, err = VerifyRangeProof(trie.Hash(), entries[start].k, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof) 369 if err != nil { 370 t.Fatalf("Expected no error, got %v", err) 371 } 372 373 // One element with two non-existent edge proofs 374 start = 1000 375 first, last = decreseKey(common.CopyBytes(entries[start].k)), increseKey(common.CopyBytes(entries[start].k)) 376 proof = memorydb.New() 377 if err := trie.Prove(first, 0, proof); err != nil { 378 t.Fatalf("Failed to prove the first node %v", err) 379 } 380 if err := trie.Prove(last, 0, proof); err != nil { 381 t.Fatalf("Failed to prove the last node %v", err) 382 } 383 _, err = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[start].k}, [][]byte{entries[start].v}, proof) 384 if err != nil { 385 t.Fatalf("Expected no error, got %v", err) 386 } 387 388 // Test the mini trie with only a single element. 389 tinyTrie := new(Trie) 390 entry := &kv{randBytes(32), randBytes(20), false} 391 tinyTrie.Update(entry.k, entry.v) 392 393 first = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() 394 last = entry.k 395 proof = memorydb.New() 396 if err := tinyTrie.Prove(first, 0, proof); err != nil { 397 t.Fatalf("Failed to prove the first node %v", err) 398 } 399 if err := tinyTrie.Prove(last, 0, proof); err != nil { 400 t.Fatalf("Failed to prove the last node %v", err) 401 } 402 _, err = VerifyRangeProof(tinyTrie.Hash(), first, last, [][]byte{entry.k}, [][]byte{entry.v}, proof) 403 if err != nil { 404 t.Fatalf("Expected no error, got %v", err) 405 } 406 } 407 408 // TestAllElementsProof tests the range proof with all elements. 409 // The edge proofs can be nil. 410 func TestAllElementsProof(t *testing.T) { 411 trie, vals := randomTrie(4096) 412 var entries entrySlice 413 for _, kv := range vals { 414 entries = append(entries, kv) 415 } 416 sort.Sort(entries) 417 418 var k [][]byte 419 var v [][]byte 420 for i := 0; i < len(entries); i++ { 421 k = append(k, entries[i].k) 422 v = append(v, entries[i].v) 423 } 424 _, err := VerifyRangeProof(trie.Hash(), nil, nil, k, v, nil) 425 if err != nil { 426 t.Fatalf("Expected no error, got %v", err) 427 } 428 429 // With edge proofs, it should still work. 430 proof := memorydb.New() 431 if err := trie.Prove(entries[0].k, 0, proof); err != nil { 432 t.Fatalf("Failed to prove the first node %v", err) 433 } 434 if err := trie.Prove(entries[len(entries)-1].k, 0, proof); err != nil { 435 t.Fatalf("Failed to prove the last node %v", err) 436 } 437 _, err = VerifyRangeProof(trie.Hash(), k[0], k[len(k)-1], k, v, proof) 438 if err != nil { 439 t.Fatalf("Expected no error, got %v", err) 440 } 441 442 // Even with non-existent edge proofs, it should still work. 443 proof = memorydb.New() 444 first := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000").Bytes() 445 last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes() 446 if err := trie.Prove(first, 0, proof); err != nil { 447 t.Fatalf("Failed to prove the first node %v", err) 448 } 449 if err := trie.Prove(last, 0, proof); err != nil { 450 t.Fatalf("Failed to prove the last node %v", err) 451 } 452 _, err = VerifyRangeProof(trie.Hash(), first, last, k, v, proof) 453 if err != nil { 454 t.Fatalf("Expected no error, got %v", err) 455 } 456 } 457 458 // TestSingleSideRangeProof tests the range starts from zero. 459 func TestSingleSideRangeProof(t *testing.T) { 460 for i := 0; i < 64; i++ { 461 trie := new(Trie) 462 var entries entrySlice 463 for i := 0; i < 4096; i++ { 464 value := &kv{randBytes(32), randBytes(20), false} 465 trie.Update(value.k, value.v) 466 entries = append(entries, value) 467 } 468 sort.Sort(entries) 469 470 var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1} 471 for _, pos := range cases { 472 proof := memorydb.New() 473 if err := trie.Prove(common.Hash{}.Bytes(), 0, proof); err != nil { 474 t.Fatalf("Failed to prove the first node %v", err) 475 } 476 if err := trie.Prove(entries[pos].k, 0, proof); err != nil { 477 t.Fatalf("Failed to prove the first node %v", err) 478 } 479 k := make([][]byte, 0) 480 v := make([][]byte, 0) 481 for i := 0; i <= pos; i++ { 482 k = append(k, entries[i].k) 483 v = append(v, entries[i].v) 484 } 485 _, err := VerifyRangeProof(trie.Hash(), common.Hash{}.Bytes(), k[len(k)-1], k, v, proof) 486 if err != nil { 487 t.Fatalf("Expected no error, got %v", err) 488 } 489 } 490 } 491 } 492 493 // TestReverseSingleSideRangeProof tests the range ends with 0xffff...fff. 494 func TestReverseSingleSideRangeProof(t *testing.T) { 495 for i := 0; i < 64; i++ { 496 trie := new(Trie) 497 var entries entrySlice 498 for i := 0; i < 4096; i++ { 499 value := &kv{randBytes(32), randBytes(20), false} 500 trie.Update(value.k, value.v) 501 entries = append(entries, value) 502 } 503 sort.Sort(entries) 504 505 var cases = []int{0, 1, 50, 100, 1000, 2000, len(entries) - 1} 506 for _, pos := range cases { 507 proof := memorydb.New() 508 if err := trie.Prove(entries[pos].k, 0, proof); err != nil { 509 t.Fatalf("Failed to prove the first node %v", err) 510 } 511 last := common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") 512 if err := trie.Prove(last.Bytes(), 0, proof); err != nil { 513 t.Fatalf("Failed to prove the last node %v", err) 514 } 515 k := make([][]byte, 0) 516 v := make([][]byte, 0) 517 for i := pos; i < len(entries); i++ { 518 k = append(k, entries[i].k) 519 v = append(v, entries[i].v) 520 } 521 _, err := VerifyRangeProof(trie.Hash(), k[0], last.Bytes(), k, v, proof) 522 if err != nil { 523 t.Fatalf("Expected no error, got %v", err) 524 } 525 } 526 } 527 } 528 529 // TestBadRangeProof tests a few cases which the proof is wrong. 530 // The prover is expected to detect the error. 531 func TestBadRangeProof(t *testing.T) { 532 trie, vals := randomTrie(4096) 533 var entries entrySlice 534 for _, kv := range vals { 535 entries = append(entries, kv) 536 } 537 sort.Sort(entries) 538 539 for i := 0; i < 500; i++ { 540 start := mrand.Intn(len(entries)) 541 end := mrand.Intn(len(entries)-start) + start + 1 542 proof := memorydb.New() 543 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 544 t.Fatalf("Failed to prove the first node %v", err) 545 } 546 if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { 547 t.Fatalf("Failed to prove the last node %v", err) 548 } 549 var keys [][]byte 550 var vals [][]byte 551 for i := start; i < end; i++ { 552 keys = append(keys, entries[i].k) 553 vals = append(vals, entries[i].v) 554 } 555 var first, last = keys[0], keys[len(keys)-1] 556 testcase := mrand.Intn(6) 557 var index int 558 switch testcase { 559 case 0: 560 // Modified key 561 index = mrand.Intn(end - start) 562 keys[index] = randBytes(32) // In theory it can't be same 563 case 1: 564 // Modified val 565 index = mrand.Intn(end - start) 566 vals[index] = randBytes(20) // In theory it can't be same 567 case 2: 568 // Gapped entry slice 569 index = mrand.Intn(end - start) 570 if (index == 0 && start < 100) || (index == end-start-1 && end <= 100) { 571 continue 572 } 573 keys = append(keys[:index], keys[index+1:]...) 574 vals = append(vals[:index], vals[index+1:]...) 575 case 3: 576 // Out of order 577 index1 := mrand.Intn(end - start) 578 index2 := mrand.Intn(end - start) 579 if index1 == index2 { 580 continue 581 } 582 keys[index1], keys[index2] = keys[index2], keys[index1] 583 vals[index1], vals[index2] = vals[index2], vals[index1] 584 case 4: 585 // Set random key to nil, do nothing 586 index = mrand.Intn(end - start) 587 keys[index] = nil 588 case 5: 589 // Set random value to nil, deletion 590 index = mrand.Intn(end - start) 591 vals[index] = nil 592 } 593 _, err := VerifyRangeProof(trie.Hash(), first, last, keys, vals, proof) 594 if err == nil { 595 t.Fatalf("%d Case %d index %d range: (%d->%d) expect error, got nil", i, testcase, index, start, end-1) 596 } 597 } 598 } 599 600 // TestGappedRangeProof focuses on the small trie with embedded nodes. 601 // If the gapped node is embedded in the trie, it should be detected too. 602 func TestGappedRangeProof(t *testing.T) { 603 trie := new(Trie) 604 var entries []*kv // Sorted entries 605 for i := byte(0); i < 10; i++ { 606 value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false} 607 trie.Update(value.k, value.v) 608 entries = append(entries, value) 609 } 610 first, last := 2, 8 611 proof := memorydb.New() 612 if err := trie.Prove(entries[first].k, 0, proof); err != nil { 613 t.Fatalf("Failed to prove the first node %v", err) 614 } 615 if err := trie.Prove(entries[last-1].k, 0, proof); err != nil { 616 t.Fatalf("Failed to prove the last node %v", err) 617 } 618 var keys [][]byte 619 var vals [][]byte 620 for i := first; i < last; i++ { 621 if i == (first+last)/2 { 622 continue 623 } 624 keys = append(keys, entries[i].k) 625 vals = append(vals, entries[i].v) 626 } 627 _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof) 628 if err == nil { 629 t.Fatal("expect error, got nil") 630 } 631 } 632 633 // TestSameSideProofs tests the element is not in the range covered by proofs 634 func TestSameSideProofs(t *testing.T) { 635 trie, vals := randomTrie(4096) 636 var entries entrySlice 637 for _, kv := range vals { 638 entries = append(entries, kv) 639 } 640 sort.Sort(entries) 641 642 pos := 1000 643 first := decreseKey(common.CopyBytes(entries[pos].k)) 644 first = decreseKey(first) 645 last := decreseKey(common.CopyBytes(entries[pos].k)) 646 647 proof := memorydb.New() 648 if err := trie.Prove(first, 0, proof); err != nil { 649 t.Fatalf("Failed to prove the first node %v", err) 650 } 651 if err := trie.Prove(last, 0, proof); err != nil { 652 t.Fatalf("Failed to prove the last node %v", err) 653 } 654 _, err := VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof) 655 if err == nil { 656 t.Fatalf("Expected error, got nil") 657 } 658 659 first = increseKey(common.CopyBytes(entries[pos].k)) 660 last = increseKey(common.CopyBytes(entries[pos].k)) 661 last = increseKey(last) 662 663 proof = memorydb.New() 664 if err := trie.Prove(first, 0, proof); err != nil { 665 t.Fatalf("Failed to prove the first node %v", err) 666 } 667 if err := trie.Prove(last, 0, proof); err != nil { 668 t.Fatalf("Failed to prove the last node %v", err) 669 } 670 _, err = VerifyRangeProof(trie.Hash(), first, last, [][]byte{entries[pos].k}, [][]byte{entries[pos].v}, proof) 671 if err == nil { 672 t.Fatalf("Expected error, got nil") 673 } 674 } 675 676 func TestHasRightElement(t *testing.T) { 677 trie := new(Trie) 678 var entries entrySlice 679 for i := 0; i < 4096; i++ { 680 value := &kv{randBytes(32), randBytes(20), false} 681 trie.Update(value.k, value.v) 682 entries = append(entries, value) 683 } 684 sort.Sort(entries) 685 686 var cases = []struct { 687 start int 688 end int 689 hasMore bool 690 }{ 691 {-1, 1, true}, // single element with non-existent left proof 692 {0, 1, true}, // single element with existent left proof 693 {0, 10, true}, 694 {50, 100, true}, 695 {50, len(entries), false}, // No more element expected 696 {len(entries) - 1, len(entries), false}, // Single last element with two existent proofs(point to same key) 697 {len(entries) - 1, -1, false}, // Single last element with non-existent right proof 698 {0, len(entries), false}, // The whole set with existent left proof 699 {-1, len(entries), false}, // The whole set with non-existent left proof 700 {-1, -1, false}, // The whole set with non-existent left/right proof 701 } 702 for _, c := range cases { 703 var ( 704 firstKey []byte 705 lastKey []byte 706 start = c.start 707 end = c.end 708 proof = memorydb.New() 709 ) 710 if c.start == -1 { 711 firstKey, start = common.Hash{}.Bytes(), 0 712 if err := trie.Prove(firstKey, 0, proof); err != nil { 713 t.Fatalf("Failed to prove the first node %v", err) 714 } 715 } else { 716 firstKey = entries[c.start].k 717 if err := trie.Prove(entries[c.start].k, 0, proof); err != nil { 718 t.Fatalf("Failed to prove the first node %v", err) 719 } 720 } 721 if c.end == -1 { 722 lastKey, end = common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").Bytes(), len(entries) 723 if err := trie.Prove(lastKey, 0, proof); err != nil { 724 t.Fatalf("Failed to prove the first node %v", err) 725 } 726 } else { 727 lastKey = entries[c.end-1].k 728 if err := trie.Prove(entries[c.end-1].k, 0, proof); err != nil { 729 t.Fatalf("Failed to prove the first node %v", err) 730 } 731 } 732 k := make([][]byte, 0) 733 v := make([][]byte, 0) 734 for i := start; i < end; i++ { 735 k = append(k, entries[i].k) 736 v = append(v, entries[i].v) 737 } 738 hasMore, err := VerifyRangeProof(trie.Hash(), firstKey, lastKey, k, v, proof) 739 if err != nil { 740 t.Fatalf("Expected no error, got %v", err) 741 } 742 if hasMore != c.hasMore { 743 t.Fatalf("Wrong hasMore indicator, want %t, got %t", c.hasMore, hasMore) 744 } 745 } 746 } 747 748 // TestEmptyRangeProof tests the range proof with "no" element. 749 // The first edge proof must be a non-existent proof. 750 func TestEmptyRangeProof(t *testing.T) { 751 trie, vals := randomTrie(4096) 752 var entries entrySlice 753 for _, kv := range vals { 754 entries = append(entries, kv) 755 } 756 sort.Sort(entries) 757 758 var cases = []struct { 759 pos int 760 err bool 761 }{ 762 {len(entries) - 1, false}, 763 {500, true}, 764 } 765 for _, c := range cases { 766 proof := memorydb.New() 767 first := increseKey(common.CopyBytes(entries[c.pos].k)) 768 if err := trie.Prove(first, 0, proof); err != nil { 769 t.Fatalf("Failed to prove the first node %v", err) 770 } 771 _, err := VerifyRangeProof(trie.Hash(), first, nil, nil, nil, proof) 772 if c.err && err == nil { 773 t.Fatalf("Expected error, got nil") 774 } 775 if !c.err && err != nil { 776 t.Fatalf("Expected no error, got %v", err) 777 } 778 } 779 } 780 781 // TestBloatedProof tests a malicious proof, where the proof is more or less the 782 // whole trie. Previously we didn't accept such packets, but the new APIs do, so 783 // lets leave this test as a bit weird, but present. 784 func TestBloatedProof(t *testing.T) { 785 // Use a small trie 786 trie, kvs := nonRandomTrie(100) 787 var entries entrySlice 788 for _, kv := range kvs { 789 entries = append(entries, kv) 790 } 791 sort.Sort(entries) 792 var keys [][]byte 793 var vals [][]byte 794 795 proof := memorydb.New() 796 // In the 'malicious' case, we add proofs for every single item 797 // (but only one key/value pair used as leaf) 798 for i, entry := range entries { 799 trie.Prove(entry.k, 0, proof) 800 if i == 50 { 801 keys = append(keys, entry.k) 802 vals = append(vals, entry.v) 803 } 804 } 805 // For reference, we use the same function, but _only_ prove the first 806 // and last element 807 want := memorydb.New() 808 trie.Prove(keys[0], 0, want) 809 trie.Prove(keys[len(keys)-1], 0, want) 810 811 if _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, vals, proof); err != nil { 812 t.Fatalf("expected bloated proof to succeed, got %v", err) 813 } 814 } 815 816 // mutateByte changes one byte in b. 817 func mutateByte(b []byte) { 818 for r := mrand.Intn(len(b)); ; { 819 new := byte(mrand.Intn(255)) 820 if new != b[r] { 821 b[r] = new 822 break 823 } 824 } 825 } 826 827 func increseKey(key []byte) []byte { 828 for i := len(key) - 1; i >= 0; i-- { 829 key[i]++ 830 if key[i] != 0x0 { 831 break 832 } 833 } 834 return key 835 } 836 837 func decreseKey(key []byte) []byte { 838 for i := len(key) - 1; i >= 0; i-- { 839 key[i]-- 840 if key[i] != 0xff { 841 break 842 } 843 } 844 return key 845 } 846 847 func BenchmarkProve(b *testing.B) { 848 trie, vals := randomTrie(100) 849 var keys []string 850 for k := range vals { 851 keys = append(keys, k) 852 } 853 854 b.ResetTimer() 855 for i := 0; i < b.N; i++ { 856 kv := vals[keys[i%len(keys)]] 857 proofs := memorydb.New() 858 if trie.Prove(kv.k, 0, proofs); proofs.Len() == 0 { 859 b.Fatalf("zero length proof for %x", kv.k) 860 } 861 } 862 } 863 864 func BenchmarkVerifyProof(b *testing.B) { 865 trie, vals := randomTrie(100) 866 root := trie.Hash() 867 var keys []string 868 var proofs []*memorydb.Database 869 for k := range vals { 870 keys = append(keys, k) 871 proof := memorydb.New() 872 trie.Prove([]byte(k), 0, proof) 873 proofs = append(proofs, proof) 874 } 875 876 b.ResetTimer() 877 for i := 0; i < b.N; i++ { 878 im := i % len(keys) 879 if _, err := VerifyProof(root, []byte(keys[im]), proofs[im]); err != nil { 880 b.Fatalf("key %x: %v", keys[im], err) 881 } 882 } 883 } 884 885 func BenchmarkVerifyRangeProof10(b *testing.B) { benchmarkVerifyRangeProof(b, 10) } 886 func BenchmarkVerifyRangeProof100(b *testing.B) { benchmarkVerifyRangeProof(b, 100) } 887 func BenchmarkVerifyRangeProof1000(b *testing.B) { benchmarkVerifyRangeProof(b, 1000) } 888 func BenchmarkVerifyRangeProof5000(b *testing.B) { benchmarkVerifyRangeProof(b, 5000) } 889 890 func benchmarkVerifyRangeProof(b *testing.B, size int) { 891 trie, vals := randomTrie(8192) 892 var entries entrySlice 893 for _, kv := range vals { 894 entries = append(entries, kv) 895 } 896 sort.Sort(entries) 897 898 start := 2 899 end := start + size 900 proof := memorydb.New() 901 if err := trie.Prove(entries[start].k, 0, proof); err != nil { 902 b.Fatalf("Failed to prove the first node %v", err) 903 } 904 if err := trie.Prove(entries[end-1].k, 0, proof); err != nil { 905 b.Fatalf("Failed to prove the last node %v", err) 906 } 907 var keys [][]byte 908 var values [][]byte 909 for i := start; i < end; i++ { 910 keys = append(keys, entries[i].k) 911 values = append(values, entries[i].v) 912 } 913 914 b.ResetTimer() 915 for i := 0; i < b.N; i++ { 916 _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, values, proof) 917 if err != nil { 918 b.Fatalf("Case %d(%d->%d) expect no error, got %v", i, start, end-1, err) 919 } 920 } 921 } 922 923 func BenchmarkVerifyRangeNoProof10(b *testing.B) { benchmarkVerifyRangeNoProof(b, 100) } 924 func BenchmarkVerifyRangeNoProof500(b *testing.B) { benchmarkVerifyRangeNoProof(b, 500) } 925 func BenchmarkVerifyRangeNoProof1000(b *testing.B) { benchmarkVerifyRangeNoProof(b, 1000) } 926 927 func benchmarkVerifyRangeNoProof(b *testing.B, size int) { 928 trie, vals := randomTrie(size) 929 var entries entrySlice 930 for _, kv := range vals { 931 entries = append(entries, kv) 932 } 933 sort.Sort(entries) 934 935 var keys [][]byte 936 var values [][]byte 937 for _, entry := range entries { 938 keys = append(keys, entry.k) 939 values = append(values, entry.v) 940 } 941 b.ResetTimer() 942 for i := 0; i < b.N; i++ { 943 _, err := VerifyRangeProof(trie.Hash(), keys[0], keys[len(keys)-1], keys, values, nil) 944 if err != nil { 945 b.Fatalf("Expected no error, got %v", err) 946 } 947 } 948 } 949 950 func randomTrie(n int) (*Trie, map[string]*kv) { 951 trie := new(Trie) 952 vals := make(map[string]*kv) 953 for i := byte(0); i < 100; i++ { 954 value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false} 955 value2 := &kv{common.LeftPadBytes([]byte{i + 10}, 32), []byte{i}, false} 956 trie.Update(value.k, value.v) 957 trie.Update(value2.k, value2.v) 958 vals[string(value.k)] = value 959 vals[string(value2.k)] = value2 960 } 961 for i := 0; i < n; i++ { 962 value := &kv{randBytes(32), randBytes(20), false} 963 trie.Update(value.k, value.v) 964 vals[string(value.k)] = value 965 } 966 return trie, vals 967 } 968 969 func randBytes(n int) []byte { 970 r := make([]byte, n) 971 crand.Read(r) 972 return r 973 } 974 975 func nonRandomTrie(n int) (*Trie, map[string]*kv) { 976 trie := new(Trie) 977 vals := make(map[string]*kv) 978 max := uint64(0xffffffffffffffff) 979 for i := uint64(0); i < uint64(n); i++ { 980 value := make([]byte, 32) 981 key := make([]byte, 32) 982 binary.LittleEndian.PutUint64(key, i) 983 binary.LittleEndian.PutUint64(value, i-max) 984 //value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false} 985 elem := &kv{key, value, false} 986 trie.Update(elem.k, elem.v) 987 vals[string(elem.k)] = elem 988 } 989 return trie, vals 990 }