proof_test.go 4.3 KB

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