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.

96 lines
2.5 KiB

  1. package files_test
  2. import (
  3. "bytes"
  4. "errors"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/tendermint/tendermint/lite"
  11. liteErr "github.com/tendermint/tendermint/lite/errors"
  12. "github.com/tendermint/tendermint/lite/files"
  13. )
  14. func checkEqual(stored, loaded lite.FullCommit, chainID string) error {
  15. err := loaded.ValidateBasic(chainID)
  16. if err != nil {
  17. return err
  18. }
  19. if !bytes.Equal(stored.ValidatorsHash(), loaded.ValidatorsHash()) {
  20. return errors.New("Different block hashes")
  21. }
  22. return nil
  23. }
  24. func TestFileProvider(t *testing.T) {
  25. assert, require := assert.New(t), require.New(t)
  26. dir, err := ioutil.TempDir("", "fileprovider-test")
  27. assert.Nil(err)
  28. defer os.RemoveAll(dir)
  29. p := files.NewProvider(dir)
  30. chainID := "test-files"
  31. appHash := []byte("some-data")
  32. keys := lite.GenValKeys(5)
  33. count := 10
  34. // make a bunch of seeds...
  35. seeds := make([]lite.FullCommit, count)
  36. for i := 0; i < count; i++ {
  37. // two seeds for each validator, to check how we handle dups
  38. // (10, 0), (10, 1), (10, 1), (10, 2), (10, 2), ...
  39. vals := keys.ToValidators(10, int64(count/2))
  40. h := int64(20 + 10*i)
  41. check := keys.GenCommit(chainID, h, nil, vals, appHash, []byte("params"), []byte("results"), 0, 5)
  42. seeds[i] = lite.NewFullCommit(check, vals)
  43. }
  44. // check provider is empty
  45. seed, err := p.GetByHeight(20)
  46. require.NotNil(err)
  47. assert.True(liteErr.IsCommitNotFoundErr(err))
  48. seed, err = p.GetByHash(seeds[3].ValidatorsHash())
  49. require.NotNil(err)
  50. assert.True(liteErr.IsCommitNotFoundErr(err))
  51. // now add them all to the provider
  52. for _, s := range seeds {
  53. err = p.StoreCommit(s)
  54. require.Nil(err)
  55. // and make sure we can get it back
  56. s2, err := p.GetByHash(s.ValidatorsHash())
  57. assert.Nil(err)
  58. err = checkEqual(s, s2, chainID)
  59. assert.Nil(err)
  60. // by height as well
  61. s2, err = p.GetByHeight(s.Height())
  62. err = checkEqual(s, s2, chainID)
  63. assert.Nil(err)
  64. }
  65. // make sure we get the last hash if we overstep
  66. seed, err = p.GetByHeight(5000)
  67. if assert.Nil(err, "%+v", err) {
  68. assert.Equal(seeds[count-1].Height(), seed.Height())
  69. err = checkEqual(seeds[count-1], seed, chainID)
  70. assert.Nil(err)
  71. }
  72. // and middle ones as well
  73. seed, err = p.GetByHeight(47)
  74. if assert.Nil(err, "%+v", err) {
  75. // we only step by 10, so 40 must be the one below this
  76. assert.EqualValues(40, seed.Height())
  77. }
  78. // and proper error for too low
  79. _, err = p.GetByHeight(5)
  80. assert.NotNil(err)
  81. assert.True(liteErr.IsCommitNotFoundErr(err))
  82. }