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.

195 lines
5.1 KiB

  1. package kvdb
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func addTestKV(t *testing.T, db *KVDB, k, v []byte) {
  12. tx, err := db.db.NewTx()
  13. require.NoError(t, err)
  14. err = tx.Put(k, v)
  15. require.NoError(t, err)
  16. err = tx.Commit()
  17. require.NoError(t, err)
  18. }
  19. func printCheckpoints(t *testing.T, path string) {
  20. files, err := ioutil.ReadDir(path)
  21. assert.NoError(t, err)
  22. fmt.Println(path)
  23. for _, f := range files {
  24. fmt.Println(" " + f.Name())
  25. }
  26. }
  27. func TestCheckpoints(t *testing.T) {
  28. dir, err := ioutil.TempDir("", "sdb")
  29. require.NoError(t, err)
  30. defer assert.NoError(t, os.RemoveAll(dir))
  31. db, err := NewKVDB(dir, 128)
  32. assert.NoError(t, err)
  33. // add test key-values
  34. for i := 0; i < 10; i++ {
  35. addTestKV(t, db, []byte{byte(i), byte(i)}, []byte{byte(i * 2), byte(i * 2)})
  36. }
  37. // do checkpoints and check that currentBatch is correct
  38. err = db.MakeCheckpoint()
  39. assert.NoError(t, err)
  40. cb, err := db.GetCurrentBatch()
  41. assert.NoError(t, err)
  42. assert.Equal(t, common.BatchNum(1), cb)
  43. for i := 1; i < 10; i++ {
  44. err = db.MakeCheckpoint()
  45. assert.NoError(t, err)
  46. cb, err = db.GetCurrentBatch()
  47. assert.NoError(t, err)
  48. assert.Equal(t, common.BatchNum(i+1), cb)
  49. }
  50. // printCheckpoints(t, sdb.path)
  51. // reset checkpoint
  52. err = db.Reset(3)
  53. assert.NoError(t, err)
  54. // check that reset can be repeated (as there exist the 'current' and
  55. // 'BatchNum3', from where the 'current' is a copy)
  56. err = db.Reset(3)
  57. require.NoError(t, err)
  58. // check that currentBatch is as expected after Reset
  59. cb, err = db.GetCurrentBatch()
  60. assert.NoError(t, err)
  61. assert.Equal(t, common.BatchNum(3), cb)
  62. // advance one checkpoint and check that currentBatch is fine
  63. err = db.MakeCheckpoint()
  64. assert.NoError(t, err)
  65. cb, err = db.GetCurrentBatch()
  66. assert.NoError(t, err)
  67. assert.Equal(t, common.BatchNum(4), cb)
  68. err = db.DeleteCheckpoint(common.BatchNum(1))
  69. assert.NoError(t, err)
  70. err = db.DeleteCheckpoint(common.BatchNum(2))
  71. assert.NoError(t, err)
  72. err = db.DeleteCheckpoint(common.BatchNum(1)) // does not exist, should return err
  73. assert.NotNil(t, err)
  74. err = db.DeleteCheckpoint(common.BatchNum(2)) // does not exist, should return err
  75. assert.NotNil(t, err)
  76. // Create a new KVDB which will get Reset from the initial KVDB
  77. dirLocal, err := ioutil.TempDir("", "ldb")
  78. require.NoError(t, err)
  79. defer assert.NoError(t, os.RemoveAll(dirLocal))
  80. ldb, err := NewKVDB(dirLocal, 128)
  81. assert.NoError(t, err)
  82. // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
  83. err = ldb.ResetFromSynchronizer(4, db)
  84. assert.NoError(t, err)
  85. // check that currentBatch is 4 after the Reset
  86. cb, err = ldb.GetCurrentBatch()
  87. assert.NoError(t, err)
  88. assert.Equal(t, common.BatchNum(4), cb)
  89. // advance one checkpoint in ldb
  90. err = ldb.MakeCheckpoint()
  91. assert.NoError(t, err)
  92. cb, err = ldb.GetCurrentBatch()
  93. assert.NoError(t, err)
  94. assert.Equal(t, common.BatchNum(5), cb)
  95. // Create a 3rd KVDB which will get Reset from the initial KVDB
  96. dirLocal2, err := ioutil.TempDir("", "ldb2")
  97. require.NoError(t, err)
  98. defer assert.NoError(t, os.RemoveAll(dirLocal2))
  99. ldb2, err := NewKVDB(dirLocal2, 128)
  100. assert.NoError(t, err)
  101. // get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
  102. err = ldb2.ResetFromSynchronizer(4, db)
  103. assert.NoError(t, err)
  104. // check that currentBatch is 4 after the Reset
  105. cb, err = ldb2.GetCurrentBatch()
  106. assert.NoError(t, err)
  107. assert.Equal(t, common.BatchNum(4), cb)
  108. // advance one checkpoint in ldb2
  109. err = ldb2.MakeCheckpoint()
  110. assert.NoError(t, err)
  111. cb, err = ldb2.GetCurrentBatch()
  112. assert.NoError(t, err)
  113. assert.Equal(t, common.BatchNum(5), cb)
  114. debug := false
  115. if debug {
  116. printCheckpoints(t, db.path)
  117. printCheckpoints(t, ldb.path)
  118. printCheckpoints(t, ldb2.path)
  119. }
  120. }
  121. func TestListCheckpoints(t *testing.T) {
  122. dir, err := ioutil.TempDir("", "tmpdb")
  123. require.NoError(t, err)
  124. defer assert.NoError(t, os.RemoveAll(dir))
  125. db, err := NewKVDB(dir, 128)
  126. require.NoError(t, err)
  127. numCheckpoints := 16
  128. // do checkpoints
  129. for i := 0; i < numCheckpoints; i++ {
  130. err = db.MakeCheckpoint()
  131. require.NoError(t, err)
  132. }
  133. list, err := db.ListCheckpoints()
  134. require.NoError(t, err)
  135. assert.Equal(t, numCheckpoints, len(list))
  136. assert.Equal(t, 1, list[0])
  137. assert.Equal(t, numCheckpoints, list[len(list)-1])
  138. numReset := 10
  139. err = db.Reset(common.BatchNum(numReset))
  140. require.NoError(t, err)
  141. list, err = db.ListCheckpoints()
  142. require.NoError(t, err)
  143. assert.Equal(t, numReset, len(list))
  144. assert.Equal(t, 1, list[0])
  145. assert.Equal(t, numReset, list[len(list)-1])
  146. }
  147. func TestDeleteOldCheckpoints(t *testing.T) {
  148. dir, err := ioutil.TempDir("", "tmpdb")
  149. require.NoError(t, err)
  150. defer assert.NoError(t, os.RemoveAll(dir))
  151. keep := 16
  152. db, err := NewKVDB(dir, keep)
  153. require.NoError(t, err)
  154. numCheckpoints := 32
  155. // do checkpoints and check that we never have more than `keep`
  156. // checkpoints
  157. for i := 0; i < numCheckpoints; i++ {
  158. err = db.MakeCheckpoint()
  159. require.NoError(t, err)
  160. checkpoints, err := db.ListCheckpoints()
  161. require.NoError(t, err)
  162. assert.LessOrEqual(t, len(checkpoints), keep)
  163. }
  164. }