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.

141 lines
4.2 KiB

  1. package lite
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. dbm "github.com/tendermint/tm-db"
  8. log "github.com/tendermint/tendermint/libs/log"
  9. lerr "github.com/tendermint/tendermint/lite/errors"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. // missingProvider doesn't store anything, always a miss.
  13. // Designed as a mock for testing.
  14. type missingProvider struct{}
  15. // NewMissingProvider returns a provider which does not store anything and always misses.
  16. func NewMissingProvider() PersistentProvider {
  17. return missingProvider{}
  18. }
  19. func (missingProvider) SaveFullCommit(FullCommit) error { return nil }
  20. func (missingProvider) LatestFullCommit(chainID string, minHeight, maxHeight int64) (FullCommit, error) {
  21. return FullCommit{}, lerr.ErrCommitNotFound()
  22. }
  23. func (missingProvider) ValidatorSet(chainID string, height int64) (*types.ValidatorSet, error) {
  24. return nil, errors.New("missing validator set")
  25. }
  26. func (missingProvider) SetLogger(_ log.Logger) {}
  27. func TestMemProvider(t *testing.T) {
  28. p := NewDBProvider("mem", dbm.NewMemDB())
  29. checkProvider(t, p, "test-mem", "empty")
  30. }
  31. func TestMultiProvider(t *testing.T) {
  32. p := NewMultiProvider(
  33. NewMissingProvider(),
  34. NewDBProvider("mem", dbm.NewMemDB()),
  35. NewMissingProvider(),
  36. )
  37. checkProvider(t, p, "test-cache", "kjfhekfhkewhgit")
  38. }
  39. func checkProvider(t *testing.T, p PersistentProvider, chainID, app string) {
  40. assert, require := assert.New(t), require.New(t)
  41. appHash := []byte(app)
  42. keys := genPrivKeys(5)
  43. count := 10
  44. // Make a bunch of full commits.
  45. fcz := make([]FullCommit, count)
  46. for i := 0; i < count; i++ {
  47. vals := keys.ToValidators(10, int64(count/2))
  48. h := int64(20 + 10*i)
  49. fcz[i] = keys.GenFullCommit(chainID, h, nil, vals, vals, appHash, []byte("params"), []byte("results"), 0, 5)
  50. }
  51. // Check that provider is initially empty.
  52. fc, err := p.LatestFullCommit(chainID, 1, 1<<63-1)
  53. require.NotNil(err)
  54. assert.True(lerr.IsErrCommitNotFound(err))
  55. // Save all full commits to the provider.
  56. for _, fc := range fcz {
  57. err = p.SaveFullCommit(fc)
  58. require.Nil(err)
  59. // Make sure we can get it back.
  60. fc2, err := p.LatestFullCommit(chainID, fc.Height(), fc.Height())
  61. assert.Nil(err)
  62. assert.Equal(fc.SignedHeader, fc2.SignedHeader)
  63. assert.Equal(fc.Validators, fc2.Validators)
  64. assert.Equal(fc.NextValidators, fc2.NextValidators)
  65. }
  66. // Make sure we get the last hash if we overstep.
  67. fc, err = p.LatestFullCommit(chainID, 1, 5000)
  68. if assert.Nil(err) {
  69. assert.Equal(fcz[count-1].Height(), fc.Height())
  70. assert.Equal(fcz[count-1], fc)
  71. }
  72. // ... and middle ones as well.
  73. fc, err = p.LatestFullCommit(chainID, 1, 47)
  74. if assert.Nil(err) {
  75. // we only step by 10, so 40 must be the one below this
  76. assert.EqualValues(40, fc.Height())
  77. }
  78. }
  79. // This will make a get height, and if it is good, set the data as well.
  80. func checkLatestFullCommit(t *testing.T, p PersistentProvider, chainID string, ask, expect int64) {
  81. fc, err := p.LatestFullCommit(chainID, 1, ask)
  82. require.Nil(t, err)
  83. if assert.Equal(t, expect, fc.Height()) {
  84. err = p.SaveFullCommit(fc)
  85. require.Nil(t, err)
  86. }
  87. }
  88. func TestMultiLatestFullCommit(t *testing.T) {
  89. require := require.New(t)
  90. // We will write data to the second level of the cache (p2), and see what
  91. // gets cached/stored in.
  92. p := NewDBProvider("mem1", dbm.NewMemDB())
  93. p2 := NewDBProvider("mem2", dbm.NewMemDB())
  94. cp := NewMultiProvider(p, p2)
  95. chainID := "cache-best-height"
  96. appHash := []byte("01234567")
  97. keys := genPrivKeys(5)
  98. count := 10
  99. // Set a bunch of full commits.
  100. for i := 0; i < count; i++ {
  101. vals := keys.ToValidators(10, int64(count/2))
  102. h := int64(10 * (i + 1))
  103. fc := keys.GenFullCommit(chainID, h, nil, vals, vals, appHash, []byte("params"), []byte("results"), 0, 5)
  104. err := p2.SaveFullCommit(fc)
  105. require.NoError(err)
  106. }
  107. // Get a few heights from the cache and set them proper.
  108. checkLatestFullCommit(t, cp, chainID, 57, 50)
  109. checkLatestFullCommit(t, cp, chainID, 33, 30)
  110. // make sure they are set in p as well (but nothing else)
  111. checkLatestFullCommit(t, p, chainID, 44, 30)
  112. checkLatestFullCommit(t, p, chainID, 50, 50)
  113. checkLatestFullCommit(t, p, chainID, 99, 50)
  114. // now, query the cache for a higher value
  115. checkLatestFullCommit(t, p2, chainID, 99, 90)
  116. checkLatestFullCommit(t, cp, chainID, 99, 90)
  117. }