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.

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