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.

110 lines
2.6 KiB

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