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.

128 lines
3.4 KiB

  1. package certifiers_test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/certifiers"
  7. "github.com/tendermint/tendermint/certifiers/errors"
  8. )
  9. func TestMemProvider(t *testing.T) {
  10. p := certifiers.NewMemStoreProvider()
  11. checkProvider(t, p, "test-mem", "empty")
  12. }
  13. func TestCacheProvider(t *testing.T) {
  14. p := certifiers.NewCacheProvider(
  15. certifiers.NewMissingProvider(),
  16. certifiers.NewMemStoreProvider(),
  17. certifiers.NewMissingProvider(),
  18. )
  19. checkProvider(t, p, "test-cache", "kjfhekfhkewhgit")
  20. }
  21. func checkProvider(t *testing.T, p certifiers.Provider, chainID, app string) {
  22. assert, require := assert.New(t), require.New(t)
  23. appHash := []byte(app)
  24. keys := certifiers.GenValKeys(5)
  25. count := 10
  26. // make a bunch of commits...
  27. commits := make([]certifiers.FullCommit, count)
  28. for i := 0; i < count; i++ {
  29. // two commits for each validator, to check how we handle dups
  30. // (10, 0), (10, 1), (10, 1), (10, 2), (10, 2), ...
  31. vals := keys.ToValidators(10, int64(count/2))
  32. h := 20 + 10*i
  33. commits[i] = keys.GenFullCommit(chainID, h, nil, vals, appHash, 0, 5)
  34. }
  35. // check provider is empty
  36. fc, err := p.GetByHeight(20)
  37. require.NotNil(err)
  38. assert.True(errors.IsCommitNotFoundErr(err))
  39. fc, err = p.GetByHash(commits[3].ValidatorsHash())
  40. require.NotNil(err)
  41. assert.True(errors.IsCommitNotFoundErr(err))
  42. // now add them all to the provider
  43. for _, s := range commits {
  44. err = p.StoreCommit(s)
  45. require.Nil(err)
  46. // and make sure we can get it back
  47. s2, err := p.GetByHash(s.ValidatorsHash())
  48. assert.Nil(err)
  49. assert.Equal(s, s2)
  50. // by height as well
  51. s2, err = p.GetByHeight(s.Height())
  52. assert.Nil(err)
  53. assert.Equal(s, s2)
  54. }
  55. // make sure we get the last hash if we overstep
  56. fc, err = p.GetByHeight(5000)
  57. if assert.Nil(err) {
  58. assert.Equal(commits[count-1].Height(), fc.Height())
  59. assert.Equal(commits[count-1], fc)
  60. }
  61. // and middle ones as well
  62. fc, err = p.GetByHeight(47)
  63. if assert.Nil(err) {
  64. // we only step by 10, so 40 must be the one below this
  65. assert.Equal(40, fc.Height())
  66. }
  67. }
  68. // this will make a get height, and if it is good, set the data as well
  69. func checkGetHeight(t *testing.T, p certifiers.Provider, ask, expect int) {
  70. fc, err := p.GetByHeight(ask)
  71. require.Nil(t, err, "%+v", err)
  72. if assert.Equal(t, expect, fc.Height()) {
  73. err = p.StoreCommit(fc)
  74. require.Nil(t, err, "%+v", err)
  75. }
  76. }
  77. func TestCacheGetsBestHeight(t *testing.T) {
  78. // assert, require := assert.New(t), require.New(t)
  79. require := require.New(t)
  80. // we will write data to the second level of the cache (p2),
  81. // and see what gets cached, stored in
  82. p := certifiers.NewMemStoreProvider()
  83. p2 := certifiers.NewMemStoreProvider()
  84. cp := certifiers.NewCacheProvider(p, p2)
  85. chainID := "cache-best-height"
  86. appHash := []byte("01234567")
  87. keys := certifiers.GenValKeys(5)
  88. count := 10
  89. // set a bunch of commits
  90. for i := 0; i < count; i++ {
  91. vals := keys.ToValidators(10, int64(count/2))
  92. h := 10 * (i + 1)
  93. fc := keys.GenFullCommit(chainID, h, nil, vals, appHash, 0, 5)
  94. err := p2.StoreCommit(fc)
  95. require.NoError(err)
  96. }
  97. // let's get a few heights from the cache and set them proper
  98. checkGetHeight(t, cp, 57, 50)
  99. checkGetHeight(t, cp, 33, 30)
  100. // make sure they are set in p as well (but nothing else)
  101. checkGetHeight(t, p, 44, 30)
  102. checkGetHeight(t, p, 50, 50)
  103. checkGetHeight(t, p, 99, 50)
  104. // now, query the cache for a higher value
  105. checkGetHeight(t, p2, 99, 90)
  106. checkGetHeight(t, cp, 99, 90)
  107. }