You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
6.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package merkle
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "fmt"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/db"
  8. "runtime"
  9. "testing"
  10. )
  11. func init() {
  12. // TODO: seed rand?
  13. }
  14. func randstr(length int) string {
  15. return RandStr(length)
  16. }
  17. func TestUnit(t *testing.T) {
  18. // Convenience for a new node
  19. N := func(l, r interface{}) *IAVLNode {
  20. var left, right *IAVLNode
  21. if _, ok := l.(*IAVLNode); ok {
  22. left = l.(*IAVLNode)
  23. } else {
  24. left = NewIAVLNode([]byte{byte(l.(int))}, nil)
  25. }
  26. if _, ok := r.(*IAVLNode); ok {
  27. right = r.(*IAVLNode)
  28. } else {
  29. right = NewIAVLNode([]byte{byte(r.(int))}, nil)
  30. }
  31. n := &IAVLNode{
  32. key: right.lmd(nil).key,
  33. left: left,
  34. right: right,
  35. }
  36. n.calcHeightAndSize(nil)
  37. n.HashWithCount()
  38. return n
  39. }
  40. // Convenience for simple printing of keys & tree structure
  41. var P func(*IAVLNode) string
  42. P = func(n *IAVLNode) string {
  43. if n.height == 0 {
  44. return fmt.Sprintf("%v", n.key[0])
  45. } else {
  46. return fmt.Sprintf("(%v %v)", P(n.left), P(n.right))
  47. }
  48. }
  49. expectHash := func(n2 *IAVLNode, hashCount uint64) {
  50. // ensure number of new hash calculations is as expected.
  51. hash, count := n2.HashWithCount()
  52. if count != hashCount {
  53. t.Fatalf("Expected %v new hashes, got %v", hashCount, count)
  54. }
  55. // nuke hashes and reconstruct hash, ensure it's the same.
  56. n2.traverse(nil, func(node *IAVLNode) bool {
  57. node.hash = nil
  58. return false
  59. })
  60. // ensure that the new hash after nuking is the same as the old.
  61. newHash, _ := n2.HashWithCount()
  62. if bytes.Compare(hash, newHash) != 0 {
  63. t.Fatalf("Expected hash %v but got %v after nuking", hash, newHash)
  64. }
  65. }
  66. expectSet := func(n *IAVLNode, i int, repr string, hashCount uint64) {
  67. n2, updated := n.set(nil, []byte{byte(i)}, nil)
  68. // ensure node was added & structure is as expected.
  69. if updated == true || P(n2) != repr {
  70. t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v",
  71. i, P(n), repr, P(n2), updated)
  72. }
  73. // ensure hash calculation requirements
  74. expectHash(n2, hashCount)
  75. }
  76. expectRemove := func(n *IAVLNode, i int, repr string, hashCount uint64) {
  77. n2, _, value, err := n.remove(nil, []byte{byte(i)})
  78. // ensure node was added & structure is as expected.
  79. if value != nil || err != nil || P(n2) != repr {
  80. t.Fatalf("Removing %v from %v:\nExpected %v\nUnexpectedly got %v value:%v err:%v",
  81. i, P(n), repr, P(n2), value, err)
  82. }
  83. // ensure hash calculation requirements
  84. expectHash(n2, hashCount)
  85. }
  86. //////// Test Set cases:
  87. // Case 1:
  88. n1 := N(4, 20)
  89. expectSet(n1, 8, "((4 8) 20)", 3)
  90. expectSet(n1, 25, "(4 (20 25))", 3)
  91. n2 := N(4, N(20, 25))
  92. expectSet(n2, 8, "((4 8) (20 25))", 3)
  93. expectSet(n2, 30, "((4 20) (25 30))", 4)
  94. n3 := N(N(1, 2), 6)
  95. expectSet(n3, 4, "((1 2) (4 6))", 4)
  96. expectSet(n3, 8, "((1 2) (6 8))", 3)
  97. n4 := N(N(1, 2), N(N(5, 6), N(7, 9)))
  98. expectSet(n4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
  99. expectSet(n4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
  100. //////// Test Remove cases:
  101. n10 := N(N(1, 2), 3)
  102. expectRemove(n10, 2, "(1 3)", 1)
  103. expectRemove(n10, 3, "(1 2)", 0)
  104. n11 := N(N(N(1, 2), 3), N(4, 5))
  105. expectRemove(n11, 4, "((1 2) (3 5))", 2)
  106. expectRemove(n11, 3, "((1 2) (4 5))", 1)
  107. }
  108. func TestIntegration(t *testing.T) {
  109. type record struct {
  110. key string
  111. value string
  112. }
  113. records := make([]*record, 400)
  114. var tree *IAVLTree = NewIAVLTree(nil)
  115. var err error
  116. var val []byte
  117. var updated bool
  118. randomRecord := func() *record {
  119. return &record{randstr(20), randstr(20)}
  120. }
  121. for i := range records {
  122. r := randomRecord()
  123. records[i] = r
  124. //t.Log("New record", r)
  125. //PrintIAVLNode(tree.root)
  126. updated = tree.Set([]byte(r.key), []byte(""))
  127. if updated {
  128. t.Error("should have not been updated")
  129. }
  130. updated = tree.Set([]byte(r.key), []byte(r.value))
  131. if !updated {
  132. t.Error("should have been updated")
  133. }
  134. if tree.Size() != uint64(i+1) {
  135. t.Error("size was wrong", tree.Size(), i+1)
  136. }
  137. }
  138. for _, r := range records {
  139. if has := tree.Has([]byte(r.key)); !has {
  140. t.Error("Missing key", r.key)
  141. }
  142. if has := tree.Has([]byte(randstr(12))); has {
  143. t.Error("Table has extra key")
  144. }
  145. if val := tree.Get([]byte(r.key)); string(val) != r.value {
  146. t.Error("wrong value")
  147. }
  148. }
  149. for i, x := range records {
  150. if val, err = tree.Remove([]byte(x.key)); err != nil {
  151. t.Error(err)
  152. } else if string(val) != x.value {
  153. t.Error("wrong value")
  154. }
  155. for _, r := range records[i+1:] {
  156. if has := tree.Has([]byte(r.key)); !has {
  157. t.Error("Missing key", r.key)
  158. }
  159. if has := tree.Has([]byte(randstr(12))); has {
  160. t.Error("Table has extra key")
  161. }
  162. val := tree.Get([]byte(r.key))
  163. if string(val) != r.value {
  164. t.Error("wrong value")
  165. }
  166. }
  167. if tree.Size() != uint64(len(records)-(i+1)) {
  168. t.Error("size was wrong", tree.Size(), (len(records) - (i + 1)))
  169. }
  170. }
  171. }
  172. func TestPersistence(t *testing.T) {
  173. db := db.NewMemDB()
  174. // Create some random key value pairs
  175. records := make(map[string]string)
  176. for i := 0; i < 10000; i++ {
  177. records[randstr(20)] = randstr(20)
  178. }
  179. // Construct some tree and save it
  180. t1 := NewIAVLTree(db)
  181. for key, value := range records {
  182. t1.Set([]byte(key), []byte(value))
  183. }
  184. t1.Save()
  185. hash, _ := t1.HashWithCount()
  186. // Load a tree
  187. t2 := NewIAVLTreeFromHash(db, hash)
  188. for key, value := range records {
  189. t2value := t2.Get([]byte(key))
  190. if string(t2value) != value {
  191. t.Fatalf("Invalid value. Expected %v, got %v", value, t2value)
  192. }
  193. }
  194. }
  195. func BenchmarkHash(b *testing.B) {
  196. b.StopTimer()
  197. s := randstr(128)
  198. b.StartTimer()
  199. for i := 0; i < b.N; i++ {
  200. hasher := sha256.New()
  201. hasher.Write([]byte(s))
  202. hasher.Sum(nil)
  203. }
  204. }
  205. func BenchmarkImmutableAvlTree(b *testing.B) {
  206. b.StopTimer()
  207. type record struct {
  208. key string
  209. value string
  210. }
  211. randomRecord := func() *record {
  212. return &record{randstr(32), randstr(32)}
  213. }
  214. t := NewIAVLTree(nil)
  215. for i := 0; i < 1000000; i++ {
  216. r := randomRecord()
  217. t.Set([]byte(r.key), []byte(r.value))
  218. }
  219. fmt.Println("ok, starting")
  220. runtime.GC()
  221. b.StartTimer()
  222. for i := 0; i < b.N; i++ {
  223. r := randomRecord()
  224. t.Set([]byte(r.key), []byte(r.value))
  225. t.Remove([]byte(r.key))
  226. }
  227. }