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.

108 lines
2.4 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. makeParams(2, 5, 7),
  52. makeParams(1, 7, 6),
  53. makeParams(9, 5, 4),
  54. makeParams(7, 8, 9),
  55. makeParams(4, 6, 5),
  56. }
  57. hashes := make([][]byte, len(params))
  58. for i := range params {
  59. hashes[i] = params[i].Hash()
  60. }
  61. // make sure there are no duplicates...
  62. // sort, then check in order for matches
  63. sort.Slice(hashes, func(i, j int) bool {
  64. return bytes.Compare(hashes[i], hashes[j]) < 0
  65. })
  66. for i := 0; i < len(hashes)-1; i++ {
  67. assert.NotEqual(t, hashes[i], hashes[i+1])
  68. }
  69. }
  70. func TestConsensusParamsUpdate(t *testing.T) {
  71. testCases := []struct {
  72. params ConsensusParams
  73. updates *abci.ConsensusParams
  74. updatedParams ConsensusParams
  75. }{
  76. // empty updates
  77. {
  78. makeParams(1, 2, 3),
  79. &abci.ConsensusParams{},
  80. makeParams(1, 2, 3),
  81. },
  82. // fine updates
  83. {
  84. makeParams(1, 2, 3),
  85. &abci.ConsensusParams{
  86. BlockSize: &abci.BlockSize{
  87. MaxBytes: 100,
  88. MaxGas: 200,
  89. },
  90. EvidenceParams: &abci.EvidenceParams{
  91. MaxAge: 300,
  92. },
  93. },
  94. makeParams(100, 200, 300),
  95. },
  96. }
  97. for _, tc := range testCases {
  98. assert.Equal(t, tc.updatedParams, tc.params.Update(tc.updates))
  99. }
  100. }