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.

149 lines
4.2 KiB

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