proof_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. package trie
  17. import (
  18. "bytes"
  19. crand "crypto/rand"
  20. mrand "math/rand"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. func init() {
  27. mrand.Seed(time.Now().Unix())
  28. }
  29. func TestProof(t *testing.T) {
  30. trie, vals := randomTrie(500)
  31. root := trie.Hash()
  32. for _, kv := range vals {
  33. proof := trie.Prove(kv.k)
  34. if proof == nil {
  35. t.Fatalf("missing key %x while constructing proof", kv.k)
  36. }
  37. val, err := VerifyProof(root, kv.k, proof)
  38. if err != nil {
  39. t.Fatalf("VerifyProof error for key %x: %v\nraw proof: %x", kv.k, err, proof)
  40. }
  41. if !bytes.Equal(val, kv.v) {
  42. t.Fatalf("VerifyProof returned wrong value for key %x: got %x, want %x", kv.k, val, kv.v)
  43. }
  44. }
  45. }
  46. func TestOneElementProof(t *testing.T) {
  47. trie := new(Trie)
  48. updateString(trie, "k", "v")
  49. proof := trie.Prove([]byte("k"))
  50. if proof == nil {
  51. t.Fatal("nil proof")
  52. }
  53. if len(proof) != 1 {
  54. t.Error("proof should have one element")
  55. }
  56. val, err := VerifyProof(trie.Hash(), []byte("k"), proof)
  57. if err != nil {
  58. t.Fatalf("VerifyProof error: %v\nraw proof: %x", err, proof)
  59. }
  60. if !bytes.Equal(val, []byte("v")) {
  61. t.Fatalf("VerifyProof returned wrong value: got %x, want 'k'", val)
  62. }
  63. }
  64. func TestVerifyBadProof(t *testing.T) {
  65. trie, vals := randomTrie(800)
  66. root := trie.Hash()
  67. for _, kv := range vals {
  68. proof := trie.Prove(kv.k)
  69. if proof == nil {
  70. t.Fatal("nil proof")
  71. }
  72. mutateByte(proof[mrand.Intn(len(proof))])
  73. if _, err := VerifyProof(root, kv.k, proof); err == nil {
  74. t.Fatalf("expected proof to fail for key %x", kv.k)
  75. }
  76. }
  77. }
  78. // mutateByte changes one byte in b.
  79. func mutateByte(b []byte) {
  80. for r := mrand.Intn(len(b)); ; {
  81. new := byte(mrand.Intn(255))
  82. if new != b[r] {
  83. b[r] = new
  84. break
  85. }
  86. }
  87. }
  88. func BenchmarkProve(b *testing.B) {
  89. trie, vals := randomTrie(100)
  90. var keys []string
  91. for k := range vals {
  92. keys = append(keys, k)
  93. }
  94. b.ResetTimer()
  95. for i := 0; i < b.N; i++ {
  96. kv := vals[keys[i%len(keys)]]
  97. if trie.Prove(kv.k) == nil {
  98. b.Fatalf("nil proof for %x", kv.k)
  99. }
  100. }
  101. }
  102. func BenchmarkVerifyProof(b *testing.B) {
  103. trie, vals := randomTrie(100)
  104. root := trie.Hash()
  105. var keys []string
  106. var proofs [][]rlp.RawValue
  107. for k := range vals {
  108. keys = append(keys, k)
  109. proofs = append(proofs, trie.Prove([]byte(k)))
  110. }
  111. b.ResetTimer()
  112. for i := 0; i < b.N; i++ {
  113. im := i % len(keys)
  114. if _, err := VerifyProof(root, []byte(keys[im]), proofs[im]); err != nil {
  115. b.Fatalf("key %x: %v", keys[im], err)
  116. }
  117. }
  118. }
  119. func randomTrie(n int) (*Trie, map[string]*kv) {
  120. trie := new(Trie)
  121. vals := make(map[string]*kv)
  122. for i := byte(0); i < 100; i++ {
  123. value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
  124. value2 := &kv{common.LeftPadBytes([]byte{i + 10}, 32), []byte{i}, false}
  125. trie.Update(value.k, value.v)
  126. trie.Update(value2.k, value2.v)
  127. vals[string(value.k)] = value
  128. vals[string(value2.k)] = value2
  129. }
  130. for i := 0; i < n; i++ {
  131. value := &kv{randBytes(32), randBytes(20), false}
  132. trie.Update(value.k, value.v)
  133. vals[string(value.k)] = value
  134. }
  135. return trie, vals
  136. }
  137. func randBytes(n int) []byte {
  138. r := make([]byte, n)
  139. crand.Read(r)
  140. return r
  141. }