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
5.9 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
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/binary"
  7. . "github.com/tendermint/tendermint/common"
  8. "github.com/tendermint/tendermint/db"
  9. "runtime"
  10. "testing"
  11. )
  12. func init() {
  13. // TODO: seed rand?
  14. }
  15. func randstr(length int) String {
  16. return String(RandStr(length))
  17. }
  18. func TestUnit(t *testing.T) {
  19. // Convenience for a new node
  20. N := func(l, r interface{}) *IAVLNode {
  21. var left, right *IAVLNode
  22. if _, ok := l.(*IAVLNode); ok {
  23. left = l.(*IAVLNode)
  24. } else {
  25. left = NewIAVLNode(Int32(l.(int)), nil)
  26. }
  27. if _, ok := r.(*IAVLNode); ok {
  28. right = r.(*IAVLNode)
  29. } else {
  30. right = NewIAVLNode(Int32(r.(int)), nil)
  31. }
  32. n := &IAVLNode{
  33. key: right.lmd(nil).key,
  34. left: left,
  35. right: right,
  36. }
  37. n.calcHeightAndSize(nil)
  38. n.Hash()
  39. return n
  40. }
  41. // Convenience for simple printing of keys & tree structure
  42. var P func(*IAVLNode) string
  43. P = func(n *IAVLNode) string {
  44. if n.height == 0 {
  45. return fmt.Sprintf("%v", n.key)
  46. } else {
  47. return fmt.Sprintf("(%v %v)", P(n.left), P(n.right))
  48. }
  49. }
  50. expectHash := func(n2 *IAVLNode, hashCount uint64) {
  51. // ensure number of new hash calculations is as expected.
  52. hash, count := n2.Hash()
  53. if count != hashCount {
  54. t.Fatalf("Expected %v new hashes, got %v", hashCount, count)
  55. }
  56. // nuke hashes and reconstruct hash, ensure it's the same.
  57. (&IAVLTree{root: n2}).Traverse(func(node Node) bool {
  58. node.(*IAVLNode).hash = nil
  59. return false
  60. })
  61. // ensure that the new hash after nuking is the same as the old.
  62. newHash, _ := n2.Hash()
  63. if bytes.Compare(hash, newHash) != 0 {
  64. t.Fatalf("Expected hash %v but got %v after nuking", hash, newHash)
  65. }
  66. }
  67. expectPut := func(n *IAVLNode, i int, repr string, hashCount uint64) {
  68. n2, updated := n.put(nil, Int32(i), nil)
  69. // ensure node was added & structure is as expected.
  70. if updated == true || P(n2) != repr {
  71. t.Fatalf("Adding %v to %v:\nExpected %v\nUnexpectedly got %v updated:%v",
  72. i, P(n), repr, P(n2), updated)
  73. }
  74. // ensure hash calculation requirements
  75. expectHash(n2, hashCount)
  76. }
  77. expectRemove := func(n *IAVLNode, i int, repr string, hashCount uint64) {
  78. n2, _, value, err := n.remove(nil, Int32(i))
  79. // ensure node was added & structure is as expected.
  80. if value != nil || err != nil || P(n2) != repr {
  81. t.Fatalf("Removing %v from %v:\nExpected %v\nUnexpectedly got %v value:%v err:%v",
  82. i, P(n), repr, P(n2), value, err)
  83. }
  84. // ensure hash calculation requirements
  85. expectHash(n2, hashCount)
  86. }
  87. //////// Test Put cases:
  88. // Case 1:
  89. n1 := N(4, 20)
  90. expectPut(n1, 8, "((4 8) 20)", 3)
  91. expectPut(n1, 25, "(4 (20 25))", 3)
  92. n2 := N(4, N(20, 25))
  93. expectPut(n2, 8, "((4 8) (20 25))", 3)
  94. expectPut(n2, 30, "((4 20) (25 30))", 4)
  95. n3 := N(N(1, 2), 6)
  96. expectPut(n3, 4, "((1 2) (4 6))", 4)
  97. expectPut(n3, 8, "((1 2) (6 8))", 3)
  98. n4 := N(N(1, 2), N(N(5, 6), N(7, 9)))
  99. expectPut(n4, 8, "(((1 2) (5 6)) ((7 8) 9))", 5)
  100. expectPut(n4, 10, "(((1 2) (5 6)) (7 (9 10)))", 5)
  101. //////// Test Remove cases:
  102. n10 := N(N(1, 2), 3)
  103. expectRemove(n10, 2, "(1 3)", 1)
  104. expectRemove(n10, 3, "(1 2)", 0)
  105. n11 := N(N(N(1, 2), 3), N(4, 5))
  106. expectRemove(n11, 4, "((1 2) (3 5))", 2)
  107. expectRemove(n11, 3, "((1 2) (4 5))", 1)
  108. }
  109. func TestIntegration(t *testing.T) {
  110. type record struct {
  111. key String
  112. value String
  113. }
  114. records := make([]*record, 400)
  115. var tree *IAVLTree = NewIAVLTree(nil)
  116. var err error
  117. var val Value
  118. var updated bool
  119. randomRecord := func() *record {
  120. return &record{randstr(20), randstr(20)}
  121. }
  122. for i := range records {
  123. r := randomRecord()
  124. records[i] = r
  125. //t.Log("New record", r)
  126. //PrintIAVLNode(tree.root)
  127. updated = tree.Put(r.key, String(""))
  128. if updated {
  129. t.Error("should have not been updated")
  130. }
  131. updated = tree.Put(r.key, r.value)
  132. if !updated {
  133. t.Error("should have been updated")
  134. }
  135. if tree.Size() != uint64(i+1) {
  136. t.Error("size was wrong", tree.Size(), i+1)
  137. }
  138. }
  139. for _, r := range records {
  140. if has := tree.Has(r.key); !has {
  141. t.Error("Missing key", r.key)
  142. }
  143. if has := tree.Has(randstr(12)); has {
  144. t.Error("Table has extra key")
  145. }
  146. if val := tree.Get(r.key); !(val.(String)).Equals(r.value) {
  147. t.Error("wrong value")
  148. }
  149. }
  150. for i, x := range records {
  151. if val, err = tree.Remove(x.key); err != nil {
  152. t.Error(err)
  153. } else if !(val.(String)).Equals(x.value) {
  154. t.Error("wrong value")
  155. }
  156. for _, r := range records[i+1:] {
  157. if has := tree.Has(r.key); !has {
  158. t.Error("Missing key", r.key)
  159. }
  160. if has := tree.Has(randstr(12)); has {
  161. t.Error("Table has extra key")
  162. }
  163. if val := tree.Get(r.key); !(val.(String)).Equals(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[String(randstr(20))] = String(randstr(20))
  178. }
  179. // Construct some tree and save it
  180. t1 := NewIAVLTree(db)
  181. for key, value := range records {
  182. t1.Put(key, value)
  183. }
  184. t1.Save()
  185. hash, _ := t1.Hash()
  186. // Load a tree
  187. t2 := NewIAVLTreeFromHash(db, hash)
  188. for key, value := range records {
  189. t2value := t2.Get(key)
  190. if !BinaryEqual(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.Put(r.key, 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.Put(r.key, r.value)
  225. t.Remove(r.key)
  226. }
  227. }