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.

67 lines
1.8 KiB

  1. package state
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. cfg "github.com/tendermint/tendermint/config"
  8. dbm "github.com/tendermint/tendermint/libs/db"
  9. "github.com/tendermint/tendermint/types"
  10. )
  11. func TestSaveValidatorsInfo(t *testing.T) {
  12. // test we persist validators every valSetCheckpointInterval blocks
  13. stateDB := dbm.NewMemDB()
  14. val, _ := types.RandValidator(true, 10)
  15. vals := types.NewValidatorSet([]*types.Validator{val})
  16. // TODO(melekes): remove in 0.33 release
  17. // https://github.com/tendermint/tendermint/issues/3543
  18. saveValidatorsInfo(stateDB, 1, 1, vals)
  19. saveValidatorsInfo(stateDB, 2, 1, vals)
  20. assert.NotPanics(t, func() {
  21. _, err := LoadValidators(stateDB, 2)
  22. if err != nil {
  23. panic(err)
  24. }
  25. })
  26. //ENDREMOVE
  27. saveValidatorsInfo(stateDB, valSetCheckpointInterval, 1, vals)
  28. loadedVals, err := LoadValidators(stateDB, valSetCheckpointInterval)
  29. assert.NoError(t, err)
  30. assert.NotZero(t, loadedVals.Size())
  31. }
  32. func BenchmarkLoadValidators(b *testing.B) {
  33. const valSetSize = 100
  34. config := cfg.ResetTestRoot("state_")
  35. defer os.RemoveAll(config.RootDir)
  36. dbType := dbm.DBBackendType(config.DBBackend)
  37. stateDB := dbm.NewDB("state", dbType, config.DBDir())
  38. state, err := LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
  39. if err != nil {
  40. b.Fatal(err)
  41. }
  42. state.Validators = genValSet(valSetSize)
  43. state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
  44. SaveState(stateDB, state)
  45. for i := 10; i < 10000000000; i *= 10 { // 10, 100, 1000, ...
  46. saveValidatorsInfo(stateDB, int64(i), state.LastHeightValidatorsChanged, state.NextValidators)
  47. b.Run(fmt.Sprintf("height=%d", i), func(b *testing.B) {
  48. for n := 0; n < b.N; n++ {
  49. _, err := LoadValidators(stateDB, int64(i))
  50. if err != nil {
  51. b.Fatal(err)
  52. }
  53. }
  54. })
  55. }
  56. }