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.

137 lines
3.1 KiB

7 years ago
7 years ago
  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 newConsensusParams(txsBytes, partSize int) ConsensusParams {
  10. return ConsensusParams{
  11. BlockSize: BlockSize{MaxBytes: txsBytes},
  12. BlockGossip: BlockGossip{BlockPartSizeBytes: partSize},
  13. }
  14. }
  15. func TestConsensusParamsValidation(t *testing.T) {
  16. testCases := []struct {
  17. params ConsensusParams
  18. valid bool
  19. }{
  20. {newConsensusParams(1, 1), true},
  21. {newConsensusParams(1, 0), false},
  22. {newConsensusParams(0, 1), false},
  23. {newConsensusParams(0, 0), false},
  24. {newConsensusParams(0, 10), false},
  25. {newConsensusParams(10, -1), false},
  26. {newConsensusParams(47*1024*1024, 400), true},
  27. {newConsensusParams(10, 400), true},
  28. {newConsensusParams(100*1024*1024, 400), true},
  29. {newConsensusParams(101*1024*1024, 400), false},
  30. {newConsensusParams(1024*1024*1024, 400), false},
  31. }
  32. for _, tc := range testCases {
  33. if tc.valid {
  34. assert.NoError(t, tc.params.Validate(), "expected no error for valid params")
  35. } else {
  36. assert.Error(t, tc.params.Validate(), "expected error for non valid params")
  37. }
  38. }
  39. }
  40. func makeParams(txsBytes, blockGas, txBytes, txGas, partSize int) ConsensusParams {
  41. return ConsensusParams{
  42. BlockSize: BlockSize{
  43. MaxBytes: txsBytes,
  44. MaxGas: int64(blockGas),
  45. },
  46. TxSize: TxSize{
  47. MaxBytes: txBytes,
  48. MaxGas: int64(txGas),
  49. },
  50. BlockGossip: BlockGossip{
  51. BlockPartSizeBytes: partSize,
  52. },
  53. }
  54. }
  55. func TestConsensusParamsHash(t *testing.T) {
  56. params := []ConsensusParams{
  57. makeParams(6, 2, 3, 4, 5),
  58. makeParams(1, 6, 3, 4, 5),
  59. makeParams(1, 2, 6, 4, 5),
  60. makeParams(1, 2, 3, 6, 5),
  61. makeParams(1, 2, 3, 4, 6),
  62. }
  63. hashes := make([][]byte, len(params))
  64. for i := range params {
  65. hashes[i] = params[i].Hash()
  66. }
  67. // make sure there are no duplicates...
  68. // sort, then check in order for matches
  69. sort.Slice(hashes, func(i, j int) bool {
  70. return bytes.Compare(hashes[i], hashes[j]) < 0
  71. })
  72. for i := 0; i < len(hashes)-1; i++ {
  73. assert.NotEqual(t, hashes[i], hashes[i+1])
  74. }
  75. }
  76. func TestConsensusParamsUpdate(t *testing.T) {
  77. testCases := []struct {
  78. params ConsensusParams
  79. updates *abci.ConsensusParams
  80. updatedParams ConsensusParams
  81. }{
  82. // empty updates
  83. {
  84. makeParams(1, 2, 3, 4, 5),
  85. &abci.ConsensusParams{},
  86. makeParams(1, 2, 3, 4, 5),
  87. },
  88. // negative BlockPartSizeBytes
  89. {
  90. makeParams(1, 2, 3, 4, 5),
  91. &abci.ConsensusParams{
  92. BlockSize: &abci.BlockSize{
  93. MaxBytes: -100,
  94. MaxGas: -200,
  95. },
  96. TxSize: &abci.TxSize{
  97. MaxBytes: -400,
  98. MaxGas: -500,
  99. },
  100. BlockGossip: &abci.BlockGossip{
  101. BlockPartSizeBytes: -600,
  102. },
  103. },
  104. makeParams(1, 2, 3, 4, 5),
  105. },
  106. // fine updates
  107. {
  108. makeParams(1, 2, 3, 4, 5),
  109. &abci.ConsensusParams{
  110. BlockSize: &abci.BlockSize{
  111. MaxBytes: 100,
  112. MaxGas: 200,
  113. },
  114. TxSize: &abci.TxSize{
  115. MaxBytes: 300,
  116. MaxGas: 400,
  117. },
  118. BlockGossip: &abci.BlockGossip{
  119. BlockPartSizeBytes: 500,
  120. },
  121. },
  122. makeParams(100, 200, 300, 400, 500),
  123. },
  124. }
  125. for _, tc := range testCases {
  126. assert.Equal(t, tc.updatedParams, tc.params.Update(tc.updates))
  127. }
  128. }