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.

83 lines
2.4 KiB

  1. package state
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. cfg "github.com/tendermint/tendermint/config"
  9. dbm "github.com/tendermint/tendermint/libs/db"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. func TestStoreLoadValidators(t *testing.T) {
  13. stateDB := dbm.NewMemDB()
  14. val, _ := types.RandValidator(true, 10)
  15. vals := types.NewValidatorSet([]*types.Validator{val})
  16. // 1) LoadValidators loads validators using a height where they were last changed
  17. saveValidatorsInfo(stateDB, 1, 1, vals)
  18. saveValidatorsInfo(stateDB, 2, 1, vals)
  19. loadedVals, err := LoadValidators(stateDB, 2)
  20. require.NoError(t, err)
  21. assert.NotZero(t, loadedVals.Size())
  22. // 2) LoadValidators loads validators using a checkpoint height
  23. // TODO(melekes): REMOVE in 0.33 release
  24. // https://github.com/tendermint/tendermint/issues/3543
  25. // for releases prior to v0.31.4, it uses last height changed
  26. valInfo := &ValidatorsInfo{
  27. LastHeightChanged: valSetCheckpointInterval,
  28. }
  29. stateDB.Set(calcValidatorsKey(valSetCheckpointInterval), valInfo.Bytes())
  30. assert.NotPanics(t, func() {
  31. saveValidatorsInfo(stateDB, valSetCheckpointInterval+1, 1, vals)
  32. loadedVals, err := LoadValidators(stateDB, valSetCheckpointInterval+1)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if loadedVals.Size() == 0 {
  37. t.Fatal("Expected validators to be non-empty")
  38. }
  39. })
  40. // ENDREMOVE
  41. saveValidatorsInfo(stateDB, valSetCheckpointInterval, 1, vals)
  42. loadedVals, err = LoadValidators(stateDB, valSetCheckpointInterval)
  43. require.NoError(t, err)
  44. assert.NotZero(t, loadedVals.Size())
  45. }
  46. func BenchmarkLoadValidators(b *testing.B) {
  47. const valSetSize = 100
  48. config := cfg.ResetTestRoot("state_")
  49. defer os.RemoveAll(config.RootDir)
  50. dbType := dbm.DBBackendType(config.DBBackend)
  51. stateDB := dbm.NewDB("state", dbType, config.DBDir())
  52. state, err := LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
  53. if err != nil {
  54. b.Fatal(err)
  55. }
  56. state.Validators = genValSet(valSetSize)
  57. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  58. SaveState(stateDB, state)
  59. for i := 10; i < 10000000000; i *= 10 { // 10, 100, 1000, ...
  60. saveValidatorsInfo(stateDB, int64(i), state.LastHeightValidatorsChanged, state.NextValidators)
  61. b.Run(fmt.Sprintf("height=%d", i), func(b *testing.B) {
  62. for n := 0; n < b.N; n++ {
  63. _, err := LoadValidators(stateDB, int64(i))
  64. if err != nil {
  65. b.Fatal(err)
  66. }
  67. }
  68. })
  69. }
  70. }