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.

103 lines
2.3 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "sort"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. abci "github.com/tendermint/tendermint/abci/types"
  8. )
  9. func TestConsensusParamsValidation(t *testing.T) {
  10. testCases := []struct {
  11. params ConsensusParams
  12. valid bool
  13. }{
  14. // test block size
  15. 0: {makeParams(1, 0, 1), true},
  16. 1: {makeParams(0, 0, 1), false},
  17. 2: {makeParams(47*1024*1024, 0, 1), true},
  18. 3: {makeParams(10, 0, 1), true},
  19. 4: {makeParams(100*1024*1024, 0, 1), true},
  20. 5: {makeParams(101*1024*1024, 0, 1), false},
  21. 6: {makeParams(1024*1024*1024, 0, 1), false},
  22. 7: {makeParams(1024*1024*1024, 0, -1), false},
  23. // test evidence age
  24. 8: {makeParams(1, 0, 0), false},
  25. 9: {makeParams(1, 0, -1), false},
  26. }
  27. for i, tc := range testCases {
  28. if tc.valid {
  29. assert.NoErrorf(t, tc.params.Validate(), "expected no error for valid params (#%d)", i)
  30. } else {
  31. assert.Errorf(t, tc.params.Validate(), "expected error for non valid params (#%d)", i)
  32. }
  33. }
  34. }
  35. func makeParams(blockBytes, blockGas, evidenceAge int64) ConsensusParams {
  36. return ConsensusParams{
  37. BlockSize: BlockSize{
  38. MaxBytes: blockBytes,
  39. MaxGas: blockGas,
  40. },
  41. EvidenceParams: EvidenceParams{
  42. MaxAge: evidenceAge,
  43. },
  44. }
  45. }
  46. func TestConsensusParamsHash(t *testing.T) {
  47. params := []ConsensusParams{
  48. makeParams(4, 2, 3),
  49. makeParams(1, 4, 3),
  50. makeParams(1, 2, 4),
  51. }
  52. hashes := make([][]byte, len(params))
  53. for i := range params {
  54. hashes[i] = params[i].Hash()
  55. }
  56. // make sure there are no duplicates...
  57. // sort, then check in order for matches
  58. sort.Slice(hashes, func(i, j int) bool {
  59. return bytes.Compare(hashes[i], hashes[j]) < 0
  60. })
  61. for i := 0; i < len(hashes)-1; i++ {
  62. assert.NotEqual(t, hashes[i], hashes[i+1])
  63. }
  64. }
  65. func TestConsensusParamsUpdate(t *testing.T) {
  66. testCases := []struct {
  67. params ConsensusParams
  68. updates *abci.ConsensusParams
  69. updatedParams ConsensusParams
  70. }{
  71. // empty updates
  72. {
  73. makeParams(1, 2, 3),
  74. &abci.ConsensusParams{},
  75. makeParams(1, 2, 3),
  76. },
  77. // fine updates
  78. {
  79. makeParams(1, 2, 3),
  80. &abci.ConsensusParams{
  81. BlockSize: &abci.BlockSize{
  82. MaxBytes: 100,
  83. MaxGas: 200,
  84. },
  85. EvidenceParams: &abci.EvidenceParams{
  86. MaxAge: 300,
  87. },
  88. },
  89. makeParams(100, 200, 300),
  90. },
  91. }
  92. for _, tc := range testCases {
  93. assert.Equal(t, tc.updatedParams, tc.params.Update(tc.updates))
  94. }
  95. }